public async Task <HistoryResponseDto> History(string symbol, Int32 from, Int32 to, string resolution) { _exchangeApi = ExchangeFactory.GetExchangeApi(Enumerations.ExchangesEnum.Gdax, null); IEnumerable <Candle> candles = _exchangeApi.GetCandles(new Coin { Code = "EUR" }, new Coin { Code = "BTC" }, 60 * 60 * 24, UnixTimeStampToDateTime(from), UnixTimeStampToDateTime(to)); candles = candles.OrderBy(x => x.Timestamp); //var asTable = ToDataTable(candles.ToList()); var response = new HistoryResponseDto { TimeStamps = candles.Select(x => (long)CryptoUtility.UnixTimestampFromDateTimeSeconds(x.Timestamp)).ToArray(), Opens = candles.Select(x => x.OpenPrice).ToArray(), Highs = candles.Select(x => x.HighPrice).ToArray(), Lows = candles.Select(x => x.LowPrice).ToArray(), Closes = candles.Select(x => x.ClosePrice).ToArray(), Volumes = candles.Select(x => Convert.ToDecimal(x.VolumeQuantity)).ToArray(), Status = "ok" }; return(response); //var json = System.IO.File.ReadAllText("D:\\Development\\CryptoBot\\CryptoBot.Api\\Data\\Sample\\history.json"); //var result = JsonConvert.DeserializeObject<HistoryResponseDto>(json); //var fromDates = result.TimeStamps.Where(x => x > from); //var toDates = result.TimeStamps.Where(x => x < to); //if(!fromDates.Any() || !toDates.Any()) // return new HistoryResponseDto{ Status = "no_data"}; //return JsonConvert.DeserializeObject<HistoryResponseDto>(json); }
public override IEnumerable <Candle> GetCandles(Coin baseCoin, Coin coin, int periodSeconds, DateTime?startDate = null, DateTime?endDate = null) { // /products/<product-id>/candles // https://api.gdax.com/products/LTC-BTC/candles?granularity=86400&start=2017-12-04T18:15:33&end=2017-12-11T18:15:33 List <Candle> candles = new List <Candle>(); string symbol = NormalizeSymbol($"{coin.Code}-{baseCoin.Code}"); if (startDate == null) { startDate = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1.0)); } if (endDate == null) { endDate = DateTime.UtcNow; } StringBuilder sb = new StringBuilder(); sb.Append("/products/"); sb.Append(symbol); sb.Append("/candles?granularity=" + periodSeconds); sb.Append("&start=" + HttpUtility.UrlEncode(startDate.Value.ToString("s", System.Globalization.CultureInfo.InvariantCulture)).ToUpper()); sb.Append("&end=" + HttpUtility.UrlEncode(endDate.Value.ToString("s", System.Globalization.CultureInfo.InvariantCulture)).ToUpper()); // nightmare here with GDAX, they ignore the date in the url and return 350+ records // howvwer, if you hover over the sb.ToString() it will acknowledge the dates, wtf? I kid you not // time, low, high, open, close, volume try { JToken token = MakeJsonRequest <JToken>(sb.ToString()); foreach (JArray candle in token) { candles.Add(new Candle { ClosePrice = (decimal)candle[4], ExchangeName = Name, HighPrice = (decimal)candle[2], LowPrice = (decimal)candle[1], Name = symbol, OpenPrice = (decimal)candle[3], PeriodSeconds = periodSeconds, Timestamp = CryptoUtility.UnixTimeStampToDateTimeSeconds((long)candle[0]), VolumePrice = (double)candle[5], VolumeQuantity = (double)candle[5] * (double)candle[4] }); } // re-sort in ascending order candles.Sort((c1, c2) => c1.Timestamp.CompareTo(c2.Timestamp)); } catch (Exception e) { throw e; } return(candles); }
public override IEnumerable <Trade> GetHistoricalTrades(Coin baseCoin, Coin coin, DateTime?sinceDateTime = null) { string symbol = $"{baseCoin.Code}-{coin.Code}"; /* [ { * "a": 26129, // Aggregate tradeId * "p": "0.01633102", // Price * "q": "4.70443515", // Quantity * "f": 27781, // First tradeId * "l": 27781, // Last tradeId * "T": 1498793709153, // Timestamp * "m": true, // Was the buyer the maker? * "M": true // Was the trade the best price match? * } ] */ symbol = NormalizeSymbol(symbol); string baseUrl = "/aggTrades?symbol=" + symbol; string url; List <Trade> trades = new List <Trade>(); DateTime cutoff = DateTime.UtcNow; while (true) { url = baseUrl; if (sinceDateTime != null) { url += "&startTime=" + CryptoUtility.UnixTimestampFromDateTimeMilliseconds(sinceDateTime.Value) + "&endTime=" + CryptoUtility.UnixTimestampFromDateTimeMilliseconds(sinceDateTime.Value + TimeSpan.FromDays(1.0)); } JArray obj = MakeJsonRequest <Newtonsoft.Json.Linq.JArray>(url); if (obj == null || obj.Count == 0) { break; } if (sinceDateTime != null) { sinceDateTime = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(obj.Last["T"].Value <long>()); if (sinceDateTime.Value > cutoff) { sinceDateTime = null; } } foreach (JToken token in obj) { // TODO: Binance doesn't provide a buy or sell type, I've put in a request for them to add this trades.Add(new CryptoBot.Model.Domain.Trading.Trade { Amount = token["q"].Value <decimal>(), Price = token["p"].Value <decimal>(), Timestamp = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(token["T"].Value <long>()), Id = token["a"].Value <long>(), IsBuy = token["m"].Value <bool>() }); } trades.Sort((t1, t2) => t1.Timestamp.CompareTo(t2.Timestamp)); foreach (Trade t in trades) { yield return(t); } trades.Clear(); if (sinceDateTime == null) { break; } System.Threading.Thread.Sleep(1000); } }