예제 #1
0
        //TODO historical tick data! aggTrades
        private async Task <List <OHLCBar> > GetData(HistoricalDataRequest req)
        {
            string symbol = string.IsNullOrEmpty(req.Instrument.DatasourceSymbol)
                ? req.Instrument.Symbol
                : req.Instrument.DatasourceSymbol;
            string interval  = BarSizetoInterval(req.Frequency);
            long   startTime = MyUtils.ConvertToMillisecondTimestamp(DateTime.SpecifyKind(req.StartingDate, DateTimeKind.Utc));
            long   endTime   = MyUtils.ConvertToMillisecondTimestamp(DateTime.SpecifyKind(req.EndingDate, DateTimeKind.Utc));
            string url       = $"https://www.binance.com/api/v1/klines?symbol={symbol}&interval={interval}&startTime={startTime}&endTime={endTime}";

            _logger.Info("Binance filling historical req from URL: " + url);

            var result = await _httpClient.GetAsync(url);

            if (429 == (int)result.StatusCode)
            {
                //pacing violation, grab Retry-After, wait, and re-request
                var retryHeader = result.Headers.RetryAfter;
                await Task.Delay((int)retryHeader.Delta.Value.TotalSeconds * 1000 + 1000);

                return(await GetData(req));
            }
            else
            {
                result.EnsureSuccessStatusCode();
            }
            string contents = await result.Content.ReadAsStringAsync();

            return(ParseHistoricalData(JArray.Parse(contents)));
        }
예제 #2
0
        private async Task <List <OHLCBar> > GetData(HistoricalDataRequest req)
        {
            string symbol = string.IsNullOrEmpty(req.Instrument.DatasourceSymbol)
                ? req.Instrument.Symbol
                : req.Instrument.DatasourceSymbol;
            string interval  = BarSizetoInterval(req.Frequency);
            long   startTime = MyUtils.ConvertToMillisecondTimestamp(DateTime.SpecifyKind(req.StartingDate, DateTimeKind.Utc));
            long   endTime   = MyUtils.ConvertToMillisecondTimestamp(DateTime.SpecifyKind(req.EndingDate, DateTimeKind.Utc));
            string url       = $"https://www.binance.com/api/v1/klines?symbol={symbol}&interval={interval}&startTime={startTime}&endTime={endTime}";

            _logger.Info("Binance filling historical req from URL: " + url);

            var result = await _httpClient.GetAsync(url);

            if (result.IsSuccessStatusCode)
            {
                string contents = await result.Content.ReadAsStringAsync();

                return(ParseHistoricalData(JArray.Parse(contents)));
            }

            throw new Exception("Error downloading data from binance: " + result.StatusCode);
        }