protected override async Task DoWork(PairConfig config, CancellationToken stoppingToken)
        {
            _logger.LogTrace($"{Exchange.Description} Candles monitor is started");

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    using (BinanceClient client = new BinanceClient())
                    {
                        DateTime    now          = DateTime.UtcNow;
                        IPeriodCode candlePeriod = config.Timeframe.HasValue ? PeriodCode.Create(config.Timeframe.Value) : DefaultCandleInterval;

                        WebCallResult <IEnumerable <BinanceKline> > klines = client.GetKlines(config.Symbol, candlePeriod.ToPeriodCode(), now.AddDays(-30), now, 500);
                        _logger.LogTrace($"Received candles from {Exchange.Description}");

                        List <Candle> candles = klines.Data.Select(x => x.ToCandle(config.Symbol, candlePeriod)).ToList();
                        foreach (Candle candle in candles)
                        {
                            await _candleProcessor.Create(candle);
                        }
                    }

                    await Task.Delay(TimeSpan.FromHours(1));
                }
                catch (Exception ex)
                {
                    _logger.LogError($"{Exchange.Description} Candles service failed with message: {ex.Message}", ex);
                }
            }
        }
Exemplo n.º 2
0
 public GetCandles(string streamSessionId, PeriodCode period, string symbol, bool onlyComplete)
     : base("getCandles", streamSessionId)
 {
     this.AddField("period", period.Code);
     this.AddField("symbol", symbol);
     this.AddField("onlyComplete", onlyComplete);
 }
Exemplo n.º 3
0
 public GetNCandlesCommand(PeriodCode period, string symbol, long end, long number)
     : base("getNCandles")
 {
     base.AddField("period", period.Code);
     base.AddField("symbol", symbol);
     base.AddField("end", end);
     base.AddField("number", number);
 }
Exemplo n.º 4
0
        public GetCandlesCommand(PeriodCode period, string symbol, long start, long end = 0)
            : base("getCandles")
        {
            base.AddField("period", period.Code);
            base.AddField("symbol", symbol);
            base.AddField("start", start);

            if (end != 0)
            {
                base.AddField("end", end);
            }
        }
Exemplo n.º 5
0
        public static StrategyModel ToModel(this Strategy entity)
        {
            StrategyModel model = new StrategyModel();

            model.Id   = entity.Id;
            model.Name = entity.Name;
            model.OptimalPeriodCode = PeriodCode.Create(entity.OptimalTimeframe).Code;
            model.OptimalPeriodName = PeriodCode.Create(entity.OptimalTimeframe).Description;
            model.NumberOfCandles   = entity.NumberOfCandles;
            model.IsEnabled         = entity.IsEnabled;

            return(model);
        }
Exemplo n.º 6
0
        protected override async Task DoWork(PairConfig pair, CancellationToken stoppingToken)
        {
            _logger.LogInformation($"{Exchange.Description} Candles worker is started");

            IPeriodCode candlePeriod = pair.Timeframe.HasValue ? PeriodCode.Create(pair.Timeframe.Value) : DefaultCandleInterval;

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    using (BinanceSocketClient client = new BinanceSocketClient())
                    {
                        ConnectionInfo cnInfo = new ConnectionInfo(_settings.Value.BusConnectionString);
                        using (NatsClient natsClient = new NatsClient(cnInfo))
                        {
                            if (!natsClient.IsConnected)
                            {
                                natsClient.Connect();
                            }

                            CallResult <UpdateSubscription> successKline = client.SubscribeToKlineUpdates(pair.Symbol, candlePeriod.ToPeriodCode(), async(data) =>
                            {
                                await SaveCandle(data.Data, natsClient);
                            });

                            successKline.Data.ConnectionLost     += () => { _logger.LogError($"Connection to {Exchange} is lost"); };
                            successKline.Data.ConnectionRestored += (data) => { _logger.LogError($"Connection to {Exchange} is Restored"); };

                            while (!stoppingToken.IsCancellationRequested)
                            {
                            }

                            natsClient.Disconnect();
                            await client.Unsubscribe(successKline.Data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError($"{Exchange.Description} Candles service failed with message: {ex.Message}", ex);
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Returns candles data between given start and end dates.
 /// If the chosen period is greater than 1 minute and the end date is not set, the last candle returned by the API can change
 /// until the end of the period (the candle is being automatically updated every minute).
 /// </summary>
 /// <param name="period">Requested candles interval in minutes</param>
 /// <param name="symbol">Name of candle symbol</param>
 /// <param name="end">Defines the end timestamp of candles block (rounded down to the nearest interval)</param>
 /// <param name="number">Number of requested candles</param>
 /// <returns>GetCandles response task</returns>
 new public GetCandlesResponse GetNCandles(PeriodCode period, string symbol, long end, long number)
 {
     return(SyncCommandExecuteWrapper <GetCandlesResponse>(base.GetNCandles(period, symbol, end, number)));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Returns candles data between given start and end dates.
 /// If the chosen period is greater than 1 minute and the end date is not set, the last candle returned by the API can change
 /// until the end of the period (the candle is being automatically updated every minute).
 /// </summary>
 /// <param name="period">Requested candles interval in minutes</param>
 /// <param name="symbol">Name of candle symbol</param>
 /// <param name="start">Start of chart block (rounded down to the nearest interval and excluding)</param>
 /// <param name="end">This field is optional and defines the end of candles block (rounded down to the nearest interval and excluding). If it is not set (or set to 0) then the current date is used</param>
 /// <returns>GetCandles response task</returns>
 new public GetCandlesResponse GetCandles(PeriodCode period, string symbol, long start, long end = 0)
 {
     return(SyncCommandExecuteWrapper <GetCandlesResponse>(base.GetCandles(period, symbol, start, end)));
 }
Exemplo n.º 9
0
 private static PeriodCode MapToPeriodCode(DurationEnum dataPeriod)
 {
     return(dataPeriod == DurationEnum.Day1
         ? PeriodCode.Minutes(5)
         : PeriodCode.Days(1));
 }
Exemplo n.º 10
0
 public StopCandles(PeriodCode period, string symbol)
     : base("stopCandles")
 {
     this.AddField("period", period.Code);
     this.AddField("symbol", symbol);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Unsubscribes from API chart candles.
 /// </summary>
 /// <param name="symbol">Symbol</param>
 /// <param name="period">Candles interval in minutes</param>
 public void StopCandles(PeriodCode period, string symbol)
 {
     SendStreamingCommand(new StopCandles(period, symbol));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Subscribes for API chart candles. The interval of every candle is 1 minute. A new candle arrives every minute.
 /// </summary>
 /// <param name="symbol">Symbol</param>
 /// <param name="period">Requested candles interval in minutes</param>
 /// <param name="onlyComplete">If true, only fully completed candles are transmitted.</param>
 public void SubscribeCandles(PeriodCode period, string symbol, bool onlyComplete)
 {
     SendStreamingCommand(new GetCandles(StreamSessionId, period, symbol, onlyComplete));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Returns the given number of candles data before the end date.
 /// </summary>
 /// <param name="period">Requested candles interval in minutes</param>
 /// <param name="symbol">Name of candle symbol</param>
 /// <param name="end">Defines the end timestamp of candles block (rounded down to the nearest interval)</param>
 /// <param name="number">Number of requested candles</param>
 /// <returns>GetCandles response task</returns>
 public Task <GetCandlesResponse> GetNCandles(PeriodCode period, string symbol, long end, long number)
 {
     return(connector.Execute <GetCandlesResponse>(new GetNCandlesCommand(period, symbol, end, number), (packet) => new GetCandlesResponse(packet)));
 }
Exemplo n.º 14
0
 public Period(PeriodCode code)
 {
     this.Code = code;
 }