public async virtual Task <Strategy> RunAsync(Strategy strategy, CancellationToken cancellationToken)
        {
            this.strategy          = strategy;
            this.cancellationToken = cancellationToken;

            await TryUpdateStrategy(strategy.Parameters);

            while (run)
            {
                if (this.cancellationToken.IsCancellationRequested)
                {
                    run = false;
                }
                else
                {
                    await Task.Delay(500);
                }
            }

            var strategyNotification = new StrategyNotification {
                Name = strategy.Name, Message = $"Stopping {strategy.Name}", NotificationLevel = NotificationLevel.DisconnectClient
            };

            StrategyNotification(new StrategyNotificationEventArgs {
                StrategyNotification = strategyNotification
            });

            return(this.strategy);
        }
        public virtual void SubscribeStatisticsException(Exception exception)
        {
            var message = JsonConvert.SerializeObject(exception);

            var strategyNotification = new StrategyNotification {
                Name = strategy.Name, Message = message, NotificationLevel = NotificationLevel.StatisticsError
            };

            StrategyStatisticsNotification(new StrategyNotificationEventArgs {
                StrategyNotification = strategyNotification
            });
        }
        public virtual void SubscribeCandlesticks(CandlestickEventArgs candlestickEventArgs)
        {
            var message = JsonConvert.SerializeObject(candlestickEventArgs.Candlesticks);

            var strategyNotification = new StrategyNotification {
                Name = strategy.Name, Message = message, NotificationLevel = NotificationLevel.Candlesticks
            };

            StrategyCandlesticksNotification(new StrategyNotificationEventArgs {
                StrategyNotification = strategyNotification
            });
        }
        public void SubscribeOrderBookException(Exception exception)
        {
            var message = JsonConvert.SerializeObject(exception);

            var strategyNotification = new StrategyNotification {
                Name = strategy.Name, Message = message, NotificationLevel = NotificationLevel.OrderBookError
            };

            StrategyOrderBookNotification(new StrategyNotificationEventArgs {
                StrategyNotification = strategyNotification
            });
        }
        public void SubscribeOrderBook(OrderBookEventArgs orderBookEventArgs)
        {
            var message = JsonConvert.SerializeObject(orderBookEventArgs.OrderBook);

            var strategyNotification = new StrategyNotification {
                Name = strategy.Name, Message = message, NotificationLevel = NotificationLevel.OrderBook
            };

            StrategyOrderBookNotification(new StrategyNotificationEventArgs {
                StrategyNotification = strategyNotification
            });
        }
        public virtual void SubscribeTrades(TradeEventArgs tradeEventArgs)
        {
            var message = JsonConvert.SerializeObject(tradeEventArgs.Trades);

            var strategyNotification = new StrategyNotification {
                Name = strategy.Name, Message = message, NotificationLevel = NotificationLevel.Trade
            };

            StrategyTradeNotification(new StrategyNotificationEventArgs {
                StrategyNotification = strategyNotification
            });
        }
        public virtual void SubscribeAccountInfo(AccountInfoEventArgs accountInfoEventArgs)
        {
            lock (accountLock)
            {
                accountInfo  = accountInfoEventArgs.AccountInfo.Clone();
                placingOrder = false;

                if (strategy == null)
                {
                    return;
                }

                var strategyNotification = new StrategyNotification {
                    Name = strategy.Name, Message = "Update strategy performance.", NotificationLevel = NotificationLevel.Account
                };

                StrategyAccountInfoNotification(new StrategyNotificationEventArgs {
                    StrategyNotification = strategyNotification
                });
            }
        }