public async Task Handle(RoundtripTargetPriceHitIntegrationEvent @event)
        {
            try
            {
                var trace = await this._traceRepository.GetByInvestmentId(@event.InvestmentId);

                if (trace == null)
                {
                    return;
                }

                var idealPeriod = trace.IdealCandlePeriod;

                int minAmounts = 0;
                foreach (var strategy in trace.TradeStrategies)
                {
                    if (strategy.GetIdealPeriod().Name == idealPeriod)
                    {
                        if (strategy.Strategy.MinimumAmountOfCandles > minAmounts)
                        {
                            minAmounts = strategy.Strategy.MinimumAmountOfCandles;
                        }
                    }
                }


                var      period = CandlePeriod.FromName(idealPeriod);
                DateTime fromWithWarmingPeriod = (DateTime)trace.DateStarted;
                var      oneCandleMinutes      = CandlePeriodService.GetOneCandleMinutesByPeriod(period);
                var      currentTime           = new RealTimeService().GetCurrentDateTime();
                var      to = currentTime.AddMinutes(-oneCandleMinutes);
                fromWithWarmingPeriod = fromWithWarmingPeriod.AddMinutes(-oneCandleMinutes * (minAmounts + 1));


                await this._trendAnalysisIntegrationEventService
                .PublishThroughEventBusAsync(new TargetPriceCandleDataRequestedIntegrationEvent(
                                                 trace.TraceId,
                                                 @event.RoundtripId,
                                                 trace.Market.ExchangeId,
                                                 trace.Market.BaseCurrency,
                                                 trace.Market.QuoteCurrency,
                                                 idealPeriod,
                                                 @event.HitPrice,
                                                 fromWithWarmingPeriod,
                                                 to
                                                 ));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Handle Integration Event: RoundtripTargetPriceHitIntegrationEvent.");
                Console.WriteLine("Result: Failure.");
                Console.WriteLine("Error Message: " + ex.Message);
            }
        }
Ejemplo n.º 2
0
        public async Task Handle(InvestmentCandleDataRequestedIntegrationEvent @event)
        {
            try
            {
                var runningTraces = await _traceRepository.GetByStatus(TraceStatus.Started);

                foreach (var trace in runningTraces)
                {
                    var market = new Market(@event.ExchangeId, @event.BaseCurrency.ToUpper(), @event.QuoteCurrency.ToUpper());
                    if (trace.Market.ExchangeId == @event.ExchangeId &&
                        trace.Market.BaseCurrency == @event.BaseCurrency &&
                        trace.Market.QuoteCurrency == @event.QuoteCurrency)
                    {
                        foreach (var strategy in trace.TradeStrategies)
                        {
                            if (strategy.GetIdealPeriod().Name == @event.CandlePeriod)
                            {
                                var      minAmounts            = strategy.Strategy.MinimumAmountOfCandles;
                                var      period                = strategy.GetIdealPeriod();
                                DateTime fromWithWarmingPeriod = (DateTime)trace.DateStarted;
                                var      oneCandleMinutes      = CandlePeriodService.GetOneCandleMinutesByPeriod(period);
                                var      currentTime           = new RealTimeService().GetCurrentDateTime();
                                var      to = currentTime.AddMinutes(-oneCandleMinutes);
                                fromWithWarmingPeriod = fromWithWarmingPeriod.AddMinutes(-oneCandleMinutes * (minAmounts + 1));

                                await this._trendAnalysisIntegrationEventService
                                .PublishThroughEventBusAsync(new TraceDataRequestedIntegrationEvent(
                                                                 trace.TraceId,
                                                                 strategy.StrategyId,
                                                                 trace.Market.ExchangeId,
                                                                 trace.Market.BaseCurrency,
                                                                 trace.Market.QuoteCurrency,
                                                                 strategy.GetIdealPeriod().Name,
                                                                 fromWithWarmingPeriod,
                                                                 to
                                                                 ));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Handle Integration Event: CandleChartUpdatedIntegrationEvent.");
                Console.WriteLine("Result: Failure.");
                Console.WriteLine("Error Message: " + ex.Message);
            }
        }
Ejemplo n.º 3
0
        public bool IsUpToDate(ITimeService timeService)
        {
            var now        = timeService.GetCurrentDateTime();
            var lastUpdate = this.TradeSignal.SignalCandleDateTime;

            var oneCandleMins = CandlePeriodService.GetOneCandleMinutesByPeriod(this.GetIdealPeriod());

            if ((now - lastUpdate).TotalSeconds > oneCandleMins * 60 * 2)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 4
0
        public async Task Handle(BacktestingTraceStartedIntegrationEvent @event)
        {
            try
            {
                var exchangeId    = @event.ExchangeId;
                var baseCurrency  = @event.BaseCurrency;
                var quoteCurrency = @event.QuoteCurrency;


                //DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                var from = @event.From;
                var to   = @event.To;

                var result = new Dictionary <string, List <CryptoTrading.Services.ExchangeAccess.API.Application.Models.Candle> >();

                foreach (var period in CandlePeriod.List())
                {
                    var candleChart = await _candleChartRepository.GetByCurrencyPairAsync(baseCurrency, quoteCurrency, exchangeId, period);

                    if (candleChart != null)
                    {
                        var      updatedChart          = candleChart;
                        DateTime fromWithWarmingPeriod = from;
                        var      oneCandleMinutes      = CandlePeriodService.GetOneCandleMinutesByPeriod(period);
                        fromWithWarmingPeriod = fromWithWarmingPeriod.AddMinutes(-oneCandleMinutes);

                        if (@event.MinAmountOfCandles.TryGetValue(period.Name, out int warmingPeriod))
                        {
                            fromWithWarmingPeriod = fromWithWarmingPeriod.AddMinutes(-(oneCandleMinutes * warmingPeriod));
                        }


                        if (!candleChart.HasCompleteCandlesBetween(fromWithWarmingPeriod, to))
                        {
                            var candles = await this._exchangeAccessService.GetCandlesData(
                                exchangeId,
                                baseCurrency,
                                quoteCurrency,
                                period,
                                fromWithWarmingPeriod,
                                to);

                            foreach (var candle in candles)
                            {
                                candleChart.UpdateCandle(candle.Timestamp, candle.High, candle.Low, candle.Open, candle.Close, candle.Volume);
                            }

                            updatedChart = this._candleChartRepository.Update(candleChart);
                            await this._candleChartRepository.UnitOfWork.SaveEntitiesAsync();
                        }

                        var candlesData = updatedChart.GetCandles(fromWithWarmingPeriod, to);


                        var data = new List <CryptoTrading.Services.ExchangeAccess.API.Application.Models.Candle>();

                        foreach (var candle in candlesData)
                        {
                            data.Add(new CryptoTrading.Services.ExchangeAccess.API.Application.Models.Candle
                            {
                                Timestamp = candle.Timestamp,
                                Open      = candle.Open,
                                Close     = candle.Close,
                                High      = candle.High,
                                Low       = candle.Low,
                                Volume    = candle.Volume
                            });
                        }

                        result.Add(period.Name, data);
                    }
                }

                await this._exchangeAccessIntegrationEventService.PublishThroughEventBusAsync(
                    new BacktestingDataCreatedIntegrationEvent(
                        @event.TraceId,
                        @event.InvestmentId,
                        @event.ExchangeId,
                        @event.BaseCurrency,
                        @event.QuoteCurrency,
                        @event.From.ToTimestamp(),
                        @event.To.ToTimestamp(),
                        result));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Handle Integration Event: BacktestingTraceStartedIntegrationEvent.");
                Console.WriteLine("Result: Failure.");
                Console.WriteLine("Error Message: " + ex.Message);
            }
        }