static void Main(string[] args) { if (!BreakermindForex.CreateDatabase()) { return; } string urlAccounts = "https://api.spotware.com/connect/tradingaccounts?access_token=" + accessToken; string cAccounts = ""; // string urlData = @"{'data':[{'positionId':7980418,'entryTimestamp':1487151223210,'utcLastUpdateTimestamp':1487151223210,'symbolName':'GBPUSD','tradeSide':'BUY','entryPrice':1.2428,'volume':100000,'stopLoss':null,'takeProfit':null,'profit':-338,'profitInPips':-35.7,'commission':-3,'marginRate':1.17765,'swap':0,'currentPrice':1.23923,'comment':null,'channel':'cAlgo','label':null}]}"; try { try { using (var wc = new WebClient()) { // Get account from accessToken cAccounts = wc.DownloadString(urlAccounts); } } catch (Exception ee) { Console.WriteLine("Coś nie tak z pobieraniem kont"); // log error to file err.SaveError(ee.ToString()); } while (true) { // Deserialize to object class cTraderAccounts cTraderAccounts result = JsonConvert.DeserializeObject <cTraderAccounts>(cAccounts); // var aId = result.data[0].accountId; // show all accounts data foreach (cTraderAccount acc in result.data) { Thread thread = new Thread(() => { try { string txt = "\r\n" + "Account " + acc.accountId + " Broker " + acc.brokerName + " Account Number " + acc.accountNumber; WebClient pos = new WebClient(); string cAccountPositions = pos.DownloadString("https://api.spotware.com/connect/tradingaccounts/" + acc.accountId + "/positions?access_token=" + accessToken); Console.WriteLine(txt + "\nPositions from account " + acc.accountId + " >>> " + cAccountPositions); // Deserialize to object class cTraderPositions cTraderPositions positions = JsonConvert.DeserializeObject <cTraderPositions>(cAccountPositions); foreach (cTraderPosition p in positions.data) { // Add position to database Int32 Timestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; BreakermindForex.PositionAdd(acc.accountId, acc.accountNumber, p.positionId, p.entryTimestamp, p.utcLastUpdateTimestamp, p.symbolName, p.tradeSide, p.entryPrice, p.volume, p.stopLoss, p.takeProfit, p.profit, p.profitInPips, p.commision, p.marginRate, p.swap, p.currentPrice, p.comment, p.chanel, p.label, BreakermindForex.GetIPAddress(), Timestamp.ToString()); } // log to file err.SavePositions(DateTime.UtcNow.ToString() + " " + txt + "\nPositions from account " + acc.accountId + " >>> " + cAccountPositions); } catch (Exception ee) { Console.WriteLine(DateTime.UtcNow.ToString() + "Coś nie tak z pobieraniem pozycji \r\n" + ee); // log to file err.SaveError(ee.ToString()); } }); thread.Start(); Thread.Sleep(2002); } } } catch (Exception e) { Console.WriteLine("Spotwer error : " + e.ToString()); // log to file err.SaveError(e.ToString()); } }
// Get deals positions public static void GetDeals(string accessToken) { // string urlData = @"{'data':[{'positionId':7980418,'entryTimestamp':1487151223210,'utcLastUpdateTimestamp':1487151223210,'symbolName':'GBPUSD','tradeSide':'BUY','entryPrice':1.2428,'volume':100000,'stopLoss':null,'takeProfit':null,'profit':-338,'profitInPips':-35.7,'commission':-3,'marginRate':1.17765,'swap':0,'currentPrice':1.23923,'comment':null,'channel':'cAlgo','label':null}]}"; string urlAccounts = "https://api.spotware.com/connect/tradingaccounts?access_token=" + accessToken; string cAccounts = ""; try { try { using (var wc = new WebClient()) { // Get account from accessToken cAccounts = wc.DownloadString(urlAccounts); } } catch (Exception ee) { Console.WriteLine("Coś nie tak z pobieraniem kont"); // log error to file err.SaveError(ee.ToString()); } // Deserialize to object class cTraderAccounts cTraderAccounts result = JsonConvert.DeserializeObject <cTraderAccounts>(cAccounts); // var aId = result.data[0].accountId; // show all accounts Use: result.data !!!!!!!! foreach (cTraderAccount acc in result.data) { Thread thread = new Thread(() => { try { string txt = "\r\n" + "Account " + acc.accountId + " Broker " + acc.brokerName + " Account Number " + acc.accountNumber; WebClient pos = new WebClient(); string cAccountDeals = pos.DownloadString("https://api.spotware.com/connect/tradingaccounts/" + acc.accountId + "/deals?access_token=" + accessToken); // Console.WriteLine(txt + "\nDeals from account " + acc.accountId + " >>> " + cAccountDeals); Console.WriteLine(txt + "\nDeals from account " + acc.accountId + " >>> \r\n"); // Deserialize to object class cTraderPositions cTraderDeals deals = JsonConvert.DeserializeObject <cTraderDeals>(cAccountDeals); foreach (cTraderDeal p in deals.data) { // Add position to database Int32 Timestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; if (p.positionCloseDetails != null) { /* * if (p.orderId == null) p.orderId = "0"; * if (p.tradeSide == null)p.tradeSide = "0"; * if (p.volume == null) p.volume = "0"; * if (p.filledVolume == null)p.filledVolume = "0"; * if (p.symbolName == null)p.symbolName = "0"; * if (p.commision == null)p.commision = "0"; * if (p.executionPrice == null)p.executionPrice = "0"; * if (p.baseToUsdConversionRate == null)p.baseToUsdConversionRate = "0"; * if (p.marginRate == null)p.marginRate = "0"; * if (p.chanel == null)p.chanel = "0"; * if (p.label == null)p.label = "0"; * if (p.comment == null)p.comment = "0"; * if (p.createTimestamp == null)p.createTimestamp = "0"; * if (p.executionTimestamp == null)p.executionTimestamp = "0"; * // Details * if (p.positionCloseDetails.entryPrice == null)p.positionCloseDetails.entryPrice = "0"; * if (p.positionCloseDetails.profit == null) p.positionCloseDetails.profit = "0"; * if (p.positionCloseDetails.swap == null) p.positionCloseDetails.swap = "0"; * if (p.positionCloseDetails.commision == null) p.positionCloseDetails.commision = "0"; * if (p.positionCloseDetails.balance == null) p.positionCloseDetails.balance = "0"; * if (p.positionCloseDetails.balanceVersion == null) p.positionCloseDetails.balanceVersion = "0"; * if (p.positionCloseDetails.comment == null) p.positionCloseDetails.comment = "0"; * if (p.positionCloseDetails.stopLossPrice == null) p.positionCloseDetails.stopLossPrice = "0"; * if (p.positionCloseDetails.takeProfitPrice == null) p.positionCloseDetails.takeProfitPrice = "0"; * if (p.positionCloseDetails.quoteToDepositConversionRate == null) p.positionCloseDetails.quoteToDepositConversionRate = "0"; * if (p.positionCloseDetails.closedVolume == null) p.positionCloseDetails.closedVolume = "0"; * if (p.positionCloseDetails.profitInPips == null) p.positionCloseDetails.profitInPips = "0"; * if (p.positionCloseDetails.roi == null) p.positionCloseDetails.roi = "0"; * if (p.positionCloseDetails.equityBasedRoi == null) p.positionCloseDetails.equityBasedRoi = "0"; * if (p.positionCloseDetails.equity == null) p.positionCloseDetails.equity = "0"; */ BreakermindForex.DealAdd(acc.accountId, acc.accountNumber, p.dealId, p.positionId, p.orderId, p.tradeSide, p.volume, p.filledVolume, p.symbolName, p.commision, p.executionPrice, p.baseToUsdConversionRate, p.marginRate, p.chanel, p.label, p.comment, p.createTimestamp, p.executionTimestamp, "1", p.positionCloseDetails.entryPrice, p.positionCloseDetails.profit, p.positionCloseDetails.swap, p.positionCloseDetails.commision, p.positionCloseDetails.balance, p.positionCloseDetails.balanceVersion, p.positionCloseDetails.comment, p.positionCloseDetails.stopLossPrice, p.positionCloseDetails.takeProfitPrice, p.positionCloseDetails.quoteToDepositConversionRate, p.positionCloseDetails.closedVolume, p.positionCloseDetails.profitInPips, p.positionCloseDetails.roi, p.positionCloseDetails.equityBasedRoi, p.positionCloseDetails.equity, BreakermindForex.GetIPAddress().ToString(), Timestamp.ToString()); } else { BreakermindForex.DealAdd(acc.accountId, acc.accountNumber, p.dealId, p.positionId, p.orderId, p.tradeSide, p.volume, p.filledVolume, p.symbolName, p.commision, p.executionPrice, p.baseToUsdConversionRate, p.marginRate, p.chanel, p.label, p.comment, p.createTimestamp, p.executionTimestamp, "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", BreakermindForex.GetIPAddress(), Timestamp.ToString()); } } // log to file err.SavePositions(DateTime.UtcNow.ToString() + " " + txt + "\nDeals from account " + acc.accountId + " >>> " + cAccountDeals); } catch (Exception ee) { Console.WriteLine(DateTime.UtcNow.ToString() + "Coś nie tak z pobieraniem pozycji \r\n" + ee); // log to file err.SaveError(ee.ToString()); } }); thread.Start(); Thread.Sleep(2002); } } catch (Exception e) { Console.WriteLine("Spotware error : " + e.ToString()); // log to file err.SaveError(e.ToString()); } }