Esempio n. 1
0
        private async Task LoadSymbolViewModel()
        {
            if (symbol == null)
            {
                return;
            }

            SymbolViewModel = new SymbolViewModel(
                userAccount.Exchange, exchangeService, chartHelper,
                orderBookHelperFactory.GetOrderBookHelper(userAccount.Exchange),
                tradeHelperFactory.GetTradeHelper(userAccount.Exchange),
                userAccount.Preferences, Logger)
            {
                Dispatcher = ViewModelContext.UiDispatcher
            };

            ObserveSymbol(SymbolViewModel);

            try
            {
                await SymbolViewModel.SetSymbol(symbol);

                SymbolViewModel.IsActive = true;
            }
            catch (Exception ex)
            {
                TradingViewModelException(ex.ToString(), ex);
            }
        }
        public override async Task TradeNotificationsAsync(List <StrategyNotification> tradeNotifications)
        {
            if (tradeNotifications == null)
            {
                throw new ArgumentNullException(nameof(tradeNotifications));
            }

            if (cancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            await tradesSemaphoreSlim.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(true);

            try
            {
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }

                if (Symbols != null)
                {
                    IsLoadingTrades = false;

                    List <MovingAverageTrade> tradesUpdate = null;

                    foreach (var notification in tradeNotifications)
                    {
                        if (tradesUpdate == null)
                        {
                            tradesUpdate = JsonConvert.DeserializeObject <List <MovingAverageTrade> >(notification.Message);
                            continue;
                        }

                        var updateTrades = JsonConvert.DeserializeObject <List <MovingAverageTrade> >(notification.Message);
                        var newTrades    = updateTrades.Except(tradesUpdate).ToList();
                        tradesUpdate.AddRange(newTrades.OrderBy(t => t.Time));
                    }

                    var trade = tradesUpdate.First();

                    var symbol = Symbols.First(s => s.ExchangeSymbol.Equals(trade.Symbol, StringComparison.Ordinal));

                    var pricePrecision    = symbol.PricePrecision;
                    var quantityPrecision = symbol.QuantityPrecision;

                    var tradeHelper = tradeHelperFactory.GetTradeHelper(trade.Exchange);
Esempio n. 3
0
        private async Task LoadSymbolViewModel()
        {
            if (symbol == null)
            {
                return;
            }

            SymbolViewModel = new SymbolViewModel(
                userAccount.Exchange, exchangeService, chartHelper,
                orderBookHelperFactory.GetOrderBookHelper(userAccount.Exchange),
                tradeHelperFactory.GetTradeHelper(userAccount.Exchange),
                userAccount.Preferences, Logger)
            {
                Dispatcher = ViewModelContext.UiDispatcher
            };

            ObserveSymbol(SymbolViewModel);

            await SymbolViewModel.SetSymbol(symbol).ConfigureAwait(true);

            SymbolViewModel.IsActive = true;
        }
Esempio n. 4
0
        public override async Task TradeNotificationsAsync(List <StrategyNotification> tradeNotifications)
        {
            if (tradeNotifications == null)
            {
                throw new ArgumentNullException(nameof(tradeNotifications));
            }

            if (cancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            await tradesSemaphoreSlim.WaitAsync(cancellationTokenSource.Token).ConfigureAwait(true);

            try
            {
                if (cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }

                if (Symbols != null)
                {
                    IsLoadingTrades = false;

                    List <MovingAverageTrade> tradesUpdate = null;

                    foreach (var notification in tradeNotifications)
                    {
                        if (tradesUpdate == null)
                        {
                            tradesUpdate = JsonConvert.DeserializeObject <List <MovingAverageTrade> >(notification.Message);
                            continue;
                        }

                        var updateTrades = JsonConvert.DeserializeObject <List <MovingAverageTrade> >(notification.Message);
                        var newTrades    = updateTrades.Except(tradesUpdate).ToList();
                        tradesUpdate.AddRange(newTrades.OrderBy(t => t.Time));
                    }

                    var trade = tradesUpdate.First();

                    var symbol = Symbols.First(s => s.ExchangeSymbol.Equals(trade.Symbol, StringComparison.Ordinal));

                    var pricePrecision    = symbol.PricePrecision;
                    var quantityPrecision = symbol.QuantityPrecision;

                    var tradeHelper = tradeHelperFactory.GetTradeHelper(trade.Exchange);

                    Func <ITrade, int, int, Trade> createSmaTrade = (t, p, q) => new Trade {
                        Price = ((MovingAverageTrade)t).MovingAveragePrice.Trim(p), Time = t.Time.ToLocalTime(), Exchange = t.Exchange
                    };
                    Func <ITrade, int, int, Trade> createBuyIndicator = (t, p, q) => new Trade {
                        Price = ((MovingAverageTrade)t).BuyPrice.Trim(p), Time = t.Time.ToLocalTime(), Exchange = t.Exchange
                    };
                    Func <ITrade, int, int, Trade> createSellIndicator = (t, p, q) => new Trade {
                        Price = ((MovingAverageTrade)t).SellPrice.Trim(p), Time = t.Time.ToLocalTime(), Exchange = t.Exchange
                    };

                    var tradesDisplayCount      = Strategy.TradesDisplayCount;
                    var tradesChartDisplayCount = Strategy.TradesChartDisplayCount;

                    if (TradesChart == null)
                    {
                        var result = await tradeHelper.CreateLocalTradeList <Trade>(symbol, tradesUpdate, tradesDisplayCount, tradesChartDisplayCount, 0).ConfigureAwait(true);

                        Trades      = result.Trades;
                        TradesChart = result.TradesChart;

                        SmaTradesChart = tradeHelper.CreateLocalChartTrades(tradesUpdate, createSmaTrade, tradesChartDisplayCount, pricePrecision, quantityPrecision);

                        BuyIndicatorChart = tradeHelper.CreateLocalChartTrades(tradesUpdate, createBuyIndicator, tradesChartDisplayCount, pricePrecision, quantityPrecision);

                        SellIndicatorChart = tradeHelper.CreateLocalChartTrades(tradesUpdate, createSellIndicator, tradesChartDisplayCount, pricePrecision, quantityPrecision);
                    }
                    else
                    {
                        List <Trade> newTrades;

                        // Get the latest available trade - the first trade on the
                        // trade list (which is also the last trade in the chart).
                        var seed     = Trades.First();
                        var seedTime = seed.Time;
                        var seedId   = seed.Id;

                        tradeHelper.UpdateTrades(symbol, tradesUpdate, Trades, tradesDisplayCount, tradesChartDisplayCount, out newTrades, ref tradesChart);

                        Trades = newTrades;

                        tradeHelper.UpdateLocalChartTrades(tradesUpdate, createSmaTrade, seedTime, seedId, tradesChartDisplayCount, pricePrecision, quantityPrecision, ref smaTradesChart);

                        tradeHelper.UpdateLocalChartTrades(tradesUpdate, createBuyIndicator, seedTime, seedId, tradesChartDisplayCount, pricePrecision, quantityPrecision, ref buyIndicatorChart);

                        tradeHelper.UpdateLocalChartTrades(tradesUpdate, createSellIndicator, seedTime, seedId, tradesChartDisplayCount, pricePrecision, quantityPrecision, ref sellIndicatorChart);
                    }
                }
            }
            catch (Exception ex)
            {
                OnException($"{Strategy.Name} : TradeNotificationsAsync - {ex.Message}", ex);
            }
            finally
            {
                tradesSemaphoreSlim.Release();
            }
        }