private async void TimerCandleSubscribe(object obj)
 {
     var candleSubscribe = (CandleSubscribe)obj;
     var contextApi      = candleSubscribe.contextApi;
     var figi            = candleSubscribe.figi;
     var candleInterval  = candleSubscribe.candleInterval;
     await contextApi.SendStreamingRequestAsync(StreamingRequest.SubscribeCandle(figi, candleInterval));
 }
Beispiel #2
0
        public override async Task <ExecutionResult> RunAsync(IStepExecutionContext context)
        {
            var req = StreamingRequest.SubscribeCandle(
                "BBG006L8G4H1" /* YNDX */,
                CandleInterval.Minute()
                );
            await Proxy.SendStreamingRequestAsync(req);

            Console.WriteLine($"{nameof(RunWatchers)} - OK.");

            return(ExecutionResult.Next());
        }
        //TODO переделать
        public void CandleSubscribe(CandleSubscribe candleSubscribe)
        {
            var contextApi = candleSubscribe.contextApi;

            contextApi.UseStreaming();
            contextApi.StreamingEventReceived += ContextApi_StreamingEventReceived;

            //int period = 7500; //ms
            //var timer = new Timer(TimerCandleSubscribe, candleSubscribe, 0, period);

            // _timers.Add(timer);

            Task.Run(async() => await contextApi.SendStreamingRequestAsync(StreamingRequest.SubscribeCandle(candleSubscribe.figi, candleSubscribe.candleInterval)));
        }
Beispiel #4
0
        public async Task SubscribeOnCandles(string currencyPairId, CandlePeriod period, Action <IList <Candle> > callback, int limit = 30)
        {
            Connect();

            var innerPeriod = period.ToInnerFormat();

            var dateFrom = DateTime.UtcNow;

            switch (period)
            {
            case CandlePeriod.Minute1:
                dateFrom = dateFrom.AddMinutes(-(limit * 1));
                break;

            case CandlePeriod.Minute3:
                dateFrom = dateFrom.AddMinutes(-(limit * 3));
                break;

            case CandlePeriod.Minute5:
                dateFrom = dateFrom.AddMinutes(-(limit * 5));
                break;

            case CandlePeriod.Minute15:
                dateFrom = dateFrom.AddMinutes(-(limit * 15));
                break;

            case CandlePeriod.Minute30:
                dateFrom = dateFrom.AddMinutes(-(limit * 30));
                break;

            case CandlePeriod.Hour1:
                dateFrom = dateFrom.AddHours(-(limit * 1));
                break;

            case CandlePeriod.Hour4:
                dateFrom = dateFrom.AddHours(-(limit * 4));
                break;

            case CandlePeriod.Day1:
                dateFrom = dateFrom.AddDays(-(limit * 1));
                break;

            case CandlePeriod.Day7:
                dateFrom = dateFrom.AddDays(-(limit * 7));
                break;

            case CandlePeriod.Month1:
                dateFrom = dateFrom.AddMonths(-(limit * 1));
                break;
            }
            var dateTo = DateTime.UtcNow;

            var innerExistingCandles = await _connection.Context.MarketCandlesAsync(currencyPairId, dateFrom, dateTo, innerPeriod);

            var existingCandles = innerExistingCandles.Candles.Select(candle => candle.ToOuterModel()).ToList();

            callback(existingCandles);

            var subscription = new Subscription
            {
                SubscribeRequest   = StreamingRequest.SubscribeCandle(currencyPairId, innerPeriod),
                UnsubscribeRequest = StreamingRequest.UnsubscribeCandle(currencyPairId, innerPeriod),
                EventHandler       = (o, e) =>
                {
                    if (!(e.Response is CandleResponse candleResponse) ||
                        candleResponse.Payload.Figi != currencyPairId ||
                        candleResponse.Payload.Interval != innerPeriod)
                    {
                        return;
                    }

                    var candle = candleResponse.Payload.ToOuterModel();
                    callback(new List <Candle> {
                        candle
                    });
                }
            };

            _connection.StreamingEventReceived += (o, e) => subscription.EventHandler(o, e);
            await _connection.Context.SendStreamingRequestAsync(subscription.SubscribeRequest);
        }