/// <summary> /// Get order book for a particular symbol. /// </summary> /// <param name="symbol">Ticker symbol.</param> /// <param name="limit">Limit of records to retrieve.</param> /// <returns></returns> public async Task <OrderBook> GetOrderBook(string symbol, int limit = 100) { if (string.IsNullOrWhiteSpace(symbol)) { throw new ArgumentException("symbol cannot be empty. ", "symbol"); } var result = await _apiClient.CallAsync <dynamic>(ApiMethod.GET, EndPoints.OrderBook, false, $"symbol={symbol.ToUpper()}&limit={limit}"); var parser = new CustomParser(); var parsedResult = parser.GetParsedOrderBook(result); return(parsedResult); }
/// <summary> /// Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time. /// </summary> /// <param name="symbol">Ticker symbol.</param> /// <param name="interval">Time interval to retreive.</param> /// <param name="limit">Limit of records to retrieve.</param> /// <returns></returns> public async Task <IEnumerable <Candlestick> > GetCandleSticks(string symbol, TimeInterval interval, DateTime?startTime = null, DateTime?endTime = null, int limit = 500) { if (string.IsNullOrWhiteSpace(symbol)) { throw new ArgumentException("symbol cannot be empty. ", "symbol"); } var args = $"symbol={symbol.ToUpper()}&interval={interval.GetDescription()}" + (startTime.HasValue ? $"&startTime={startTime.Value.GetUnixTimeStamp()}" : "") + (endTime.HasValue ? $"&endTime={endTime.Value.GetUnixTimeStamp()}" : "") + $"&limit={limit}"; var result = await _apiClient.CallAsync <dynamic>(ApiMethod.GET, EndPoints.Candlesticks, false, args); var parser = new CustomParser(); var parsedResult = parser.GetParsedCandlestick(result); return(parsedResult); }
/// <summary> /// Connects to a Websocket endpoint. /// </summary> /// <typeparam name="T">Type used to parsed the response message.</typeparam> /// <param name="parameters">Paremeters to send to the Websocket.</param> /// <param name="messageDelegate">Deletage to callback after receive a message.</param> /// <param name="useCustomParser">Specifies if needs to use a custom parser for the response message.</param> public void ConnectToWebSocket <T>(string parameters, MessageHandler <T> messageHandler, bool useCustomParser = false) { var finalEndpoint = _webSocketEndpoint + parameters; var ws = new WebSocket(finalEndpoint); ws.OnMessage += (sender, e) => { dynamic eventData; if (useCustomParser) { var customParser = new CustomParser(); eventData = customParser.GetParsedDepthMessage(JsonConvert.DeserializeObject <dynamic>(e.Data)); } else { eventData = JsonConvert.DeserializeObject <T>(e.Data); } messageHandler(eventData); }; ws.OnClose += (sender, e) => { _openSockets.Remove(ws); }; ws.OnError += (sender, e) => { _openSockets.Remove(ws); }; ws.Connect(); _openSockets.Add(ws); }