Esempio n. 1
0
        public async Task <IEnumerable <CandleModel> > ImportCandlesAsync(string tradingPair, int intervalInHour, CandlePeriod candlePeriod)
        {
            var candles       = new List <CandleModel>();
            var remainedHours = intervalInHour % 24;
            var initDateTime  = DateTimeOffset.UtcNow.AddHours(-1 * intervalInHour);

            for (int i = 0; i < intervalInHour / 24; i++)
            {
                var startDateTime = initDateTime.AddDays(i);
                var endDateTime   = initDateTime.AddDays(i + 1);
                candles.AddRange(await _exchangeProvider.GetCandlesAsync(tradingPair, candlePeriod, startDateTime.ToUnixTimeSeconds(), endDateTime.ToUnixTimeSeconds()));
                Console.WriteLine($"Iteration: {i}; StartDt: {startDateTime}; EndDt: {endDateTime};  Total count: {candles.Count}");
            }

            if (remainedHours > 0)
            {
                candles.AddRange(await _exchangeProvider.GetCandlesAsync(tradingPair, candlePeriod, DateTimeOffset.UtcNow.AddHours(-1 * remainedHours).ToUnixTimeSeconds(), null));
            }

            await _candleDbRepository.SaveCandleAsync(tradingPair, Mapper.Map <List <CandleDto> >(candles));

            return(candles);
        }
        public async Task StartTradingAsync(string tradingPair, CandlePeriod candlePeriod, CancellationToken cancellationToken)
        {
            _cancellationToken   = cancellationToken;
            _tradingPair         = tradingPair;
            _delayInMilliseconds = (int)candlePeriod * _delayInMilliseconds;
            var         lastSince     = GetSinceUnixTime(candlePeriod);
            var         lastScanId    = _candleRepository.GetLatestScanId();
            CandleModel currentCandle = null;

            if (_strategy.DelayInCandlePeriod > 1)
            {
                await ExecutePastCandleCheck(tradingPair, candlePeriod, lastScanId);
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                var startWatcher = new Stopwatch();
                startWatcher.Start();
                var candles = await _exchangeProvider.GetCandlesAsync(tradingPair, candlePeriod, lastSince, lastSince + (int)candlePeriod * 60);

                var candlesList = candles.ToList();
                if (candlesList.Count == 1)
                {
                    currentCandle = candlesList.Last();
                    if (!_isSetFirstPrice)
                    {
                        _userBalanceService.FirstPrice = currentCandle;
                        _isSetFirstPrice = true;
                    }
                    var trendDirection = await _strategy.CheckTrendAsync(tradingPair, currentCandle);

                    await _candleRepository.SaveCandleAsync(tradingPair, Mapper.Map <List <CandleDto> >(new List <CandleModel> {
                        currentCandle
                    }), lastScanId);

                    _lastTrendDirection = trendDirection;
                    if (trendDirection == TrendDirection.Long)
                    {
                        await BuyAsync(currentCandle);
                    }
                    else if (trendDirection == TrendDirection.Short)
                    {
                        await SellAsync(currentCandle);
                    }
                }
                else
                {
                    Console.WriteLine($"DateTs: {DateTimeOffset.FromUnixTimeSeconds(lastSince):s}; Trend: [NO TRADES]; Close price: [NO TRADES]; Volumen: [NO TRADES]; Elapsed time: {startWatcher.ElapsedMilliseconds} ms");
                }

                var utcNow = DateTime.UtcNow;
                var delayStartInSeconds = (int)candlePeriod * 60 - utcNow.Minute % (int)candlePeriod * 60 - utcNow.Second;
                // ReSharper disable once MethodSupportsCancellation
                await Task.Delay(delayStartInSeconds * 1000);

                lastSince += (int)candlePeriod * 60;
                startWatcher.Stop();
            }

            if (cancellationToken.IsCancellationRequested)
            {
                if (currentCandle != null)
                {
                    _userBalanceService.LastPrice = currentCandle;
                    if (_lastTrendDirection == TrendDirection.Long)
                    {
                        // ReSharper disable once MethodSupportsCancellation
                        SellAsync(currentCandle).Wait();
                    }
                }
            }
        }