private async Task StartMonitoringInner(CancellationToken cancellationToken)
        {
            _stockSocketConnector.Connect();

            var tradingSettings      = _configurationService.GetTradingSettings();
            var tradingCurrencyPairs = await GetActiveCurrencyPairs();

            var periodsForAnalysis = new[]
            {
                tradingSettings.Period.GetLowerFramePeriod(),
                tradingSettings.Period,
                tradingSettings.Period.GetHigherFramePeriod()
            };

            foreach (var currencyPair in tradingCurrencyPairs)
            {
                await _candleLoadingService.InitSubscription(currencyPair.Id, periodsForAnalysis);

                _candleLoadingService.CandlesUpdated += (o, e) =>
                {
                    if (e.Period != tradingSettings.Period)
                    {
                        return;
                    }

                    CheckOutForNewPosition(currencyPair);
                };
            }

            WaitHandle.WaitAny(new[] { cancellationToken.WaitHandle });

            await _stockSocketConnector.Disconnect();
        }
Exemple #2
0
        private async Task SubscribeOnTradingEvents()
        {
            _workingCandlePeriod = _configurationService.GetTradingSettings().Period;
            var tradingSettings    = _configurationService.GetTradingSettings();
            var periodsForAnalysis = new[]
            {
                tradingSettings.Period.GetLowerFramePeriod(),
                tradingSettings.Period,
                tradingSettings.Period.GetHigherFramePeriod()
            };
            await _candleLoadingService.InitSubscription(Position.CurrencyPairId, periodsForAnalysis);

            _candleLoadingService.CandlesUpdated += OnCandlesUpdated;

            await _orderBookLoadingService.InitSubscription(Position.CurrencyPair.Id);

            _orderBookLoadingService.OrderBookUpdated += OnOrderBookUpdated;

            await _tradingReportsService.InitSubscription(Position.CurrencyPairId);

            _tradingReportsService.OrdersUpdated += OnOrdersUpdated;
        }
Exemple #3
0
        private async Task CheckOutNewTickersForRequiredVolume()
        {
            var tradingSettings  = _configurationService.GetTradingSettings();
            var allCurrencyPairs = await _stockRestConnector.GetCurrencyPairs();

            var baseCurrencyPairs = allCurrencyPairs.Where(item => !_activePositionWorkers.ContainsKey(item.Id) &&
                                                           tradingSettings.QuoteCurrencies.Any(currencyId => String.Equals(item.QuoteCurrencyId, currencyId, StringComparison.OrdinalIgnoreCase)));

            var allTickers = await _stockRestConnector.GetTickers();

            var tradingTickers = allTickers
                                 .Where(tickerItem =>
                                        baseCurrencyPairs.Any(currencyPairItem => String.Equals(tickerItem.CurrencyPairId, currencyPairItem.Id)))
                                 .Where(tickerItem =>
            {
                var tickerCurrencyPair = baseCurrencyPairs.First(currencyPairItem =>
                                                                 String.Equals(tickerItem.CurrencyPairId, currencyPairItem.Id));
                decimal volumeToCompare;
                if (String.Equals(tickerCurrencyPair.QuoteCurrencyId, Constants.BTC, StringComparison.OrdinalIgnoreCase))
                {
                    volumeToCompare = tickerItem.VolumeInQuoteCurrency;
                }
                else
                {
                    var btcConvertFactor    = 0m;
                    var requestCurrencyPair = allCurrencyPairs.FirstOrDefault(item =>
                                                                              String.Equals(tickerCurrencyPair.QuoteCurrencyId, item.QuoteCurrencyId, StringComparison.OrdinalIgnoreCase) &&
                                                                              String.Equals(item.BaseCurrencyId, Constants.BTC, StringComparison.OrdinalIgnoreCase));
                    if (requestCurrencyPair != null)
                    {
                        btcConvertFactor = allTickers.First(item =>
                                                            String.Equals(item.CurrencyPairId, requestCurrencyPair.Id, StringComparison.OrdinalIgnoreCase)).LastPrice;
                    }
                    else
                    {
                        requestCurrencyPair = allCurrencyPairs.FirstOrDefault(item =>
                                                                              String.Equals(tickerCurrencyPair.QuoteCurrencyId, item.BaseCurrencyId, StringComparison.OrdinalIgnoreCase) &&
                                                                              String.Equals(item.QuoteCurrencyId, Constants.BTC, StringComparison.OrdinalIgnoreCase));
                        if (requestCurrencyPair != null)
                        {
                            btcConvertFactor = 1 / allTickers.First(item =>
                                                                    String.Equals(item.CurrencyPairId, requestCurrencyPair.Id,
                                                                                  StringComparison.OrdinalIgnoreCase)).LastPrice;
                        }
                    }

                    volumeToCompare = btcConvertFactor > 0 ? tickerItem.VolumeInQuoteCurrency / btcConvertFactor : 0;
                }
                return(volumeToCompare > tradingSettings.MinCurrencyPairTradingVolumeInBTC);
            })
                                 .ToList();

            var tradingCurrencyPairs = baseCurrencyPairs.Where(currencyPairItem => tradingTickers.Any(tickerItem =>
                                                                                                      String.Equals(tickerItem.CurrencyPairId, currencyPairItem.Id, StringComparison.OrdinalIgnoreCase))).ToList();

            var periodsForAnalysis = new[]
            {
                tradingSettings.Period.GetLowerFramePeriod(),
                tradingSettings.Period,
                tradingSettings.Period.GetHigherFramePeriod()
            };

            foreach (var currencyPair in tradingCurrencyPairs)
            {
                foreach (var candlePeriod in periodsForAnalysis)
                {
                    var candles = await _stockRestConnector.GetCandles(currencyPair.Id, candlePeriod, 30);

                    _candleLoadingService.UpdateCandles(currencyPair.Id, candlePeriod, candles);
                }

                await _candleLoadingService.InitSubscription(currencyPair.Id, periodsForAnalysis);

                _candleLoadingService.CandlesUpdated += (o, e) =>
                {
                    if (_activePositionWorkers == null)
                    {
                        return;
                    }

                    if (_activePositionWorkers.ContainsKey(e.CurrencyPairId))
                    {
                        return;
                    }

                    if (e.Period != tradingSettings.Period)
                    {
                        return;
                    }

                    CheckOutForNewPosition(currencyPair).Wait();
                };
            }
        }