public static BinanceCandle Create(string[] data, bool isRevert, int digits) { if (data.Length < 5) { throw new Exception($"Cannot parse data:{JsonConvert.SerializeObject(data)} "); } var ts = long.Parse(data[0]); var candle = new BinanceCandle() { DateTime = ts.UnixTimeToDateTime(), Open = double.Parse(data[1]), High = double.Parse(data[2]), Low = double.Parse(data[3]), Close = double.Parse(data[4]) }; if (isRevert) { candle.Open = Math.Round(1 / candle.Open, digits); candle.Close = Math.Round(1 / candle.Close, digits); var low = Math.Round(1 / candle.High, digits); var high = Math.Round(1 / candle.Low, digits); candle.High = high; candle.Low = low; } return(candle); }
public static async Task <List <BinanceCandle> > GetCandles(string symbol, int limit, string interval, long endtime, bool isRevert, int digit) { var url = "https://api.binance.com/api/v3/klines"; if (endtime > 0) { url += $"?symbol={symbol}&limit={limit}&interval={interval}&endTime={endtime}"; } else { url += $"?symbol={symbol}&limit={limit}&interval={interval}"; } //Console.WriteLine(url); var json = await _http.GetStringAsync(url); var data = JsonConvert.DeserializeObject <string[][]>(json); return(data.Select(e => BinanceCandle.Create(e, isRevert, digit)).ToList()); //https://api.binance.com/api/v3/klines?symbol=BTCUSD&limit=10&interval=1m //https://api.binance.com/api/v3/klines?symbol=BTCBUSD&limit=2&interval=1m&endTime=1629381839999 }