private void AddOrUpdateOrders(IBrokerAccount account, List <Trade> addedOrUpdatedTrades, WebCallResult <IEnumerable <BinanceOrder> > orders) { foreach (var o in orders.Data) { var existing = account.Trades.FirstOrDefault(t => t.Id == o.OrderId.ToString()); if (existing == null) { var trade = CreateTradeFromOrder(o); addedOrUpdatedTrades.Add(trade); account.Trades.Add(trade); } else { existing.EntryQuantity = o.QuantityFilled; existing.EntryPrice = o.AverageFillPrice; if (existing.EntryQuantity == existing.OrderAmount) { existing.CloseDateTime = o.UpdateTime; } addedOrUpdatedTrades.Add(existing); } } }
public QueryTradesViewModel() { DependencyContainer.ComposeParts(this); _broker = _brokersService.Brokers.First(b => b.Name == "FXCM"); _account = _brokersService.AccountsLookup[_broker]; SetInitialValue(); }
public bool UpdateAccount( IBrokerAccount account, IBrokersCandlesService candlesService, IMarketDetailsService marketsService, Action <string> updateProgressAction, out List <Trade> addedOrUpdatedTrades) { addedOrUpdatedTrades = new List <Trade>(); var limit = 500; foreach (var symbol in GetSymbols()) { Log.Debug($"Updating account for {symbol}"); // Get highest order ID var maxId = account.Trades.Count(t => t.CloseDateTime != null && t.Market == symbol) == 0 ? 1 : account.Trades .Where(t => t.CloseDateTime != null && t.Market == symbol) .Max(t => Convert.ToInt64(t.Id)); WebCallResult <IEnumerable <BinanceOrder> > orders = null; // Get orders while (orders == null || orders.Data.Count() == limit) { orders = _client.Spot.Order.GetAllOrders(symbol, orderId: maxId); if (orders.Success == false && orders.Error.Code == -1003) { Log.Info("Too many Binance requests - pausing requests"); // -1003 = Too many requests Thread.Sleep(60 * 1000); orders = null; continue; } if (orders.Success) { AddOrUpdateOrders(account, addedOrUpdatedTrades, orders); } else { Log.Error($"Unable to get orders for symbol {symbol} - {orders.Error.Message}"); break; } maxId = account.Trades.Count(t => t.CloseDateTime != null && t.Market == symbol) == 0 ? 1 : account.Trades .Where(t => t.CloseDateTime != null && t.Market == symbol) .Max(t => Convert.ToInt64(t.Id)); } } Log.Info($"Binance account updated - {addedOrUpdatedTrades} trades added or updated"); return(true); }
public RunStrategyLiveViewModel() { DependencyContainer.ComposeParts(this); RunLiveCommand = new DelegateCommand(o => RunLive()); _fxcm = (FxcmBroker)_brokersService.Brokers.First(x => x.Name == "FXCM"); _strategiesDirectory = _dataDirectoryService.MainDirectoryWithApplicationName; _logDirectory = DataDirectoryService.GetMainDirectoryWithApplicationName("FXCMTradeLog"); _brokersService.LoadBrokerAccounts(_tradeDetailsAutoCalculatorService, _logDirectory); _brokerAccount = _brokersService.AccountsLookup[_fxcm]; }
public TradeDetailsViewModel(Trade trade, Action closeWindow) { _closeWindow = closeWindow; DependencyContainer.ComposeParts(this); Trade = trade; Date = Trade.StartDateTimeLocal != null ? Trade.StartDateTimeLocal.Value.ToString("dd/MM/yy HH:mm") : DateTime.Now.ToString("dd/MM/yy HH:mm"); RefreshDetails(); _broker = _brokersService.GetBroker(trade.Broker); _brokerAccount = _brokersService.AccountsLookup[_broker]; AddLimitCommand = new DelegateCommand(AddLimit); AddStopCommand = new DelegateCommand(AddStop); RemoveLimitCommand = new DelegateCommand(RemoveLimit); RemoveStopCommand = new DelegateCommand(RemoveStop); SetOrderDateTimePriceCommand = new DelegateCommand(SetOrderDateTimePrice); DoneCommand = new DelegateCommand(o => Done()); }
public bool UpdateAccount(IBrokerAccount account, IBrokersCandlesService candlesService, IMarketDetailsService marketsService, Action <string> updateProgressAction, out List <Trade> addedOrUpdatedTrades) { throw new NotImplementedException(); }
public void RecalculateTrade(Trade trade) { // TODO Remove this altogether var startTime = trade.OrderDateTime ?? trade.EntryDateTime; var broker = _brokersService.Brokers.FirstOrDefault(x => x.Name == trade.Broker); var options = trade.CalculateOptions; IBrokerAccount brokerAccount = null; if (broker != null) { _brokersService.AccountsLookup.TryGetValue(broker, out brokerAccount); } if (startTime == null) { trade.StopInPips = null; trade.InitialStopInPips = null; trade.InitialStop = null; trade.LimitInPips = null; trade.InitialLimitInPips = null; trade.InitialLimit = null; trade.RiskPercentOfBalance = null; trade.RiskAmount = null; trade.RMultiple = null; return; } UpdateOrderPrice(trade); //UpdateStop(trade); //UpdateLimit(trade); // Update price per pip if (!options.HasFlag(CalculateOptions.ExcludePipsCalculations)) { UpdateTradePricePerPip(trade, broker); } // Update risk if (trade.InitialStopInPips == null || trade.PricePerPip == null) { trade.RiskPercentOfBalance = null; trade.RiskAmount = null; trade.RiskPercentOfBalance = null; } else { trade.RiskAmount = trade.PricePerPip.Value * trade.InitialStopInPips.Value; if (brokerAccount != null) { var balance = brokerAccount.GetBalance(trade.StartDateTime); if (balance != 0.0M) { trade.RiskPercentOfBalance = (trade.RiskAmount * 100M) / brokerAccount.GetBalance(startTime); } else { trade.RiskPercentOfBalance = null; } } } }