protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null) { HistoricalTradeHelperState state = new HistoricalTradeHelperState(this) { Callback = callback, DirectionIsBackwards = false, EndDate = endDate, ParseFunction = (JToken token) => { return(new ExchangeTrade { Amount = token["amount"].ConvertInvariant <decimal>(), Price = token["price"].ConvertInvariant <decimal>(), Timestamp = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(token["timestampms"].ConvertInvariant <long>()), Id = token["tid"].ConvertInvariant <long>(), IsBuy = token["type"].ToStringInvariant() == "buy" }); }, StartDate = startDate, Symbol = symbol, TimestampFunction = (DateTime dt) => ((long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(dt)).ToStringInvariant(), Url = "/trades/[symbol]?limit_trades=100×tamp={0}" }; await state.ProcessHistoricalTrades(); }
protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null) { symbol = NormalizeSymbol(symbol); string baseUrl = "/0/public/Trades?pair=" + symbol; string url; DateTime timestamp; List <ExchangeTrade> trades = new List <ExchangeTrade>(); while (true) { url = baseUrl; if (startDate != null) { url += "&since=" + (long)(CryptoUtility.UnixTimestampFromDateTimeMilliseconds(startDate.Value) * 1000000.0); } JToken result = await MakeJsonRequestAsync <JToken>(url); if (result == null) { break; } JArray outerArray = result[symbol] as JArray; if (outerArray == null || outerArray.Count == 0) { break; } if (startDate != null) { startDate = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(result["last"].ConvertInvariant <double>() / 1000000.0d); } foreach (JArray array in outerArray.Children <JArray>()) { timestamp = CryptoUtility.UnixTimeStampToDateTimeSeconds(array[2].ConvertInvariant <double>()); trades.Add(new ExchangeTrade { Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>(), Timestamp = timestamp, Id = timestamp.Ticks, IsBuy = array[3].ConvertInvariant <char>() == 'b' }); } trades.Sort((t1, t2) => t1.Timestamp.CompareTo(t2.Timestamp)); if (!callback(trades)) { break; } trades.Clear(); if (startDate == null) { break; } Task.Delay(1000).Wait(); } }
protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null) { const int maxCount = 100; symbol = NormalizeSymbol(symbol); string baseUrl = "/trades/t" + symbol + "/hist?sort=" + (startDate == null ? "-1" : "1") + "&limit=" + maxCount; string url; List <ExchangeTrade> trades = new List <ExchangeTrade>(); decimal[][] tradeChunk; while (true) { url = baseUrl; if (startDate != null) { url += "&start=" + (long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(startDate.Value); } tradeChunk = await MakeJsonRequestAsync <decimal[][]>(url); if (tradeChunk == null || tradeChunk.Length == 0) { break; } if (startDate != null) { startDate = CryptoUtility.UnixTimeStampToDateTimeMilliseconds((double)tradeChunk[tradeChunk.Length - 1][1]); } foreach (decimal[] tradeChunkPiece in tradeChunk) { trades.Add(new ExchangeTrade { Amount = Math.Abs(tradeChunkPiece[2]), IsBuy = tradeChunkPiece[2] > 0m, Price = tradeChunkPiece[3], Timestamp = CryptoUtility.UnixTimeStampToDateTimeMilliseconds((double)tradeChunkPiece[1]), Id = (long)tradeChunkPiece[0] }); } trades.Sort((t1, t2) => t1.Timestamp.CompareTo(t2.Timestamp)); if (!callback(trades)) { break; } trades.Clear(); if (tradeChunk.Length < maxCount || startDate == null) { break; } await Task.Delay(5000); } }
public static void CreateBinFileFromCSVFiles(string outputFile, params string[] inputFiles) { unsafe { Trade trade = new Trade(); byte[] bytes = new byte[16]; fixed(byte *ptr = bytes) { foreach (string file in inputFiles) { using (StreamReader reader = new StreamReader(file, CryptoUtility.UTF8EncodingNoPrefix)) using (Stream writer = File.Create(outputFile)) { string line; string[] lines; DateTime dt; while ((line = reader.ReadLine()) != null) { lines = line.Split(','); if (lines.Length == 3) { dt = CryptoUtility.UnixTimeStampToDateTimeSeconds(double.Parse(lines[0])); trade.Ticks = (long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(dt); trade.Price = float.Parse(lines[1]); trade.Amount = float.Parse(lines[2]); if (trade.Amount > 0.01f && trade.Price > 0.5f) { *(Trade *)ptr = trade; writer.Write(bytes, 0, bytes.Length); } } } } } } } }
protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null) { /* [ { * "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? * } ] */ HistoricalTradeHelperState state = new HistoricalTradeHelperState(this) { Callback = callback, EndDate = endDate, ParseFunction = (JToken token) => { DateTime timestamp = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(token["T"].ConvertInvariant <long>()); return(new ExchangeTrade { Amount = token["q"].ConvertInvariant <decimal>(), Price = token["p"].ConvertInvariant <decimal>(), Timestamp = timestamp, Id = token["a"].ConvertInvariant <long>(), IsBuy = token["m"].ConvertInvariant <bool>() }); }, StartDate = startDate, Symbol = symbol, TimestampFunction = (DateTime dt) => ((long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(dt)).ToStringInvariant(), Url = "/aggTrades?symbol=[symbol]&startTime={0}&endTime={1}", }; await state.ProcessHistoricalTrades(); }
/// <summary> /// Update the trade info via API /// </summary> public void Update() { Ticker = ExchangeInfo.API.GetTicker(Symbol); RecentTrades = ExchangeInfo.API.GetRecentTrades(Symbol).ToArray(); if (RecentTrades.Length == 0) { Trade = new Trade(); } else { Trade = new Trade { Amount = (float)RecentTrades[RecentTrades.Length - 1].Amount, Price = (float)RecentTrades[RecentTrades.Length - 1].Price, Ticks = (long)CryptoUtility.UnixTimestampFromDateTimeMilliseconds(RecentTrades[RecentTrades.Length - 1].Timestamp) }; } Orders = ExchangeInfo.API.GetOrderBook(Symbol); }