Untitled

mail@pastecode.io avatar
unknown
csharp
2 months ago
2.9 kB
5
Indexable
Never
        public void LogXTBPortfolioComposition()
        {
            TradesResponse openedTrades = APICommandFactory.ExecuteTradesCommand(connector, true);

            Dictionary<string, double> allTradesSum = new Dictionary<string, double>();

            foreach (TradeRecord trade in openedTrades.TradeRecords)
            {
                SymbolResponse symbolDetails = APICommandFactory.ExecuteSymbolCommand(connector, trade.Symbol);
                string companyName = symbolDetails.Symbol.Description;
                string currency = symbolDetails.Symbol.Currency;
                double? value = trade.Volume* trade.Close_price;

                if (currency != "CZK")
                {
                    switch (currency)
                    {
                        case "USD":
                            {
                                SymbolResponse usdDetails = APICommandFactory.ExecuteSymbolCommand(connector, "USDCZK");
                                value *= usdDetails.Symbol.Bid;
                                value *= 0.995;
                                break;
                            }

                        case "EUR":
                            {
                                SymbolResponse eurDetails = APICommandFactory.ExecuteSymbolCommand(connector, "EURCZK");
                                value *= eurDetails.Symbol.Bid;
                                value *= 0.995;
                                break;
                            }

                        case "GBP":
                            {
                                SymbolResponse gbpDetails = APICommandFactory.ExecuteSymbolCommand(connector, "GBPUSD");
                                SymbolResponse usdDetails = APICommandFactory.ExecuteSymbolCommand(connector, "USDCZK");
                                value *= gbpDetails.Symbol.Bid;
                                value *= 0.995;
                                value *= usdDetails.Symbol.Bid;
                                value *= 0.995;
                                break;
                            }

                        default:
                            {
                                throw new NotImplementedException();
                            }
                    }
                }

                allTradesSum.AddRange(new Dictionary<string, double> { { companyName, value.GetValueOrDefault() } });
            }

            MarginLevelResponse marginLevelResponse = APICommandFactory.ExecuteMarginLevelCommand(connector);
            allTradesSum.AddRange(new Dictionary<string, double> { { "Cash", marginLevelResponse.Balance.GetValueOrDefault() } });

            string json = JsonConvert.SerializeObject(allTradesSum, Formatting.Indented);
            File.WriteAllText("test.json", json);
        }
Leave a Comment