protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null)
        {
            List <ExchangeTrade> trades = new List <ExchangeTrade>();
            decimal?lastTradeID         = null;
            // TODO: Can't get Hitbtc to return other than the last 50 trades even though their API says it should (by orderid or timestamp). When passing either of these parms, it still returns the last 50
            // So until there is an update, that's what we'll go with
            JToken obj = await MakeJsonRequestAsync <JToken>("/public/trades/" + symbol);

            if (obj.HasValues)
            {
                foreach (JToken token in obj)
                {
                    ExchangeTrade trade = ParseExchangeTrade(token);
                    lastTradeID = trade.Id;
                    if (startDate == null || trade.Timestamp >= startDate)
                    {
                        trades.Add(trade);
                    }
                }
                if (trades.Count != 0)
                {
                    callback(trades.OrderBy(t => t.Timestamp));
                }
            }
        }
        protected override IWebSocket OnGetTradesWebSocket(Action <KeyValuePair <string, ExchangeTrade> > callback, params string[] symbols)
        {
            if (callback == null)
            {
                return(null);
            }

            /*
             * {
             * "e": "trade",     // Event type
             * "E": 123456789,   // Event time
             * "s": "BNBBTC",    // Symbol
             * "t": 12345,       // Trade ID
             * "p": "0.001",     // Price
             * "q": "100",       // Quantity
             * "b": 88,          // Buyer order Id
             * "a": 50,          // Seller order Id
             * "T": 123456785,   // Trade time
             * "m": true,        // Is the buyer the market maker?
             * "M": true         // Ignore.
             * }
             *
             */

            string url = GetWebSocketStreamUrlForSymbols("@trade", symbols);

            return(ConnectWebSocket(url, (msg, _socket) =>
            {
                try
                {
                    JToken token = JToken.Parse(msg.UTF8String());
                    string name = token["stream"].ToStringInvariant();
                    token = token["data"];
                    string symbol = NormalizeSymbol(name.Substring(0, name.IndexOf('@')));

                    // buy=0 -> m = true (The buyer is maker, while the seller is taker).
                    // buy=1 -> m = false(The seller is maker, while the buyer is taker).
                    ExchangeTrade trade = new ExchangeTrade
                    {
                        Timestamp = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(token["E"].ConvertInvariant <long>()),
                        Price = token["p"].ConvertInvariant <decimal>(),
                        Amount = token["q"].ConvertInvariant <decimal>(),
                        Id = token["t"].ConvertInvariant <long>(),
                        BuyOrderId = token["b"].ConvertInvariant <long>(),
                        SellOrderId = token["a"].ConvertInvariant <long>(),
                        IsBuy = token["m"].ConvertInvariant <bool>(),
                        //IsBestPriceMatch = token.ConvertInvariant<bool>(),
                    };
                    callback(new KeyValuePair <string, ExchangeTrade>(symbol, trade));
                }
                catch
                {
                }
            }));
        }
Beispiel #3
0
        private ExchangeTrade ParseTrade(JToken token)
        {
            // [{ "TradePairId":100,"Label":"LTC/BTC","Type":"Sell","Price":0.00006000, "Amount":499.99640000,"Total":0.02999978,"Timestamp": 1418297368}, ...]
            ExchangeTrade trade = new ExchangeTrade()
            {
                Timestamp = DateTimeOffset.FromUnixTimeSeconds(token["Timestamp"].ConvertInvariant <long>()).DateTime,
                Amount    = token["Amount"].ConvertInvariant <decimal>(),
                Price     = token["Price"].ConvertInvariant <decimal>(),
                IsBuy     = token["Type"].ToStringInvariant().Equals("Buy")
            };

            return(trade);
        }
Beispiel #4
0
        protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null)
        {
            List <ExchangeTrade> trades = new List <ExchangeTrade>();
            // TODO: Not directly supported so the best we can do is get their Max 200 and check the timestamp if necessary
            JToken result = await MakeJsonRequestAsync <JToken>("/public/getmarkethistory?market=" + symbol + "&count=200");

            foreach (JToken token in result)
            {
                ExchangeTrade trade = ParseTrade(token);
                if (startDate == null || trade.Timestamp >= startDate)
                {
                    trades.Add(trade);
                }
            }
            if (trades.Count != 0)
            {
                callback(trades);
            }
        }
Beispiel #5
0
        private IEnumerable <ExchangeTrade> ParseTradesWebSocket(JToken token)
        {
            var trades = new List <ExchangeTrade>();

            foreach (var t in token)
            {
                var trade = new ExchangeTrade()
                {
                    Amount = t["amount"].ConvertInvariant <decimal>(),
                    // System.OverflowException: Value was either too large or too small for an Int64.
                    // Id = x["id"].ConvertInvariant<long>(),
                    IsBuy     = t["direction"].ToStringLowerInvariant().EqualsWithOption("buy"),
                    Price     = t["price"].ConvertInvariant <decimal>(),
                    Timestamp = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(t["ts"].ConvertInvariant <long>())
                };
                trades.Add(trade);
            }

            return(trades);
        }
        private IEnumerable <ExchangeTrade> ParseTradesWebSocket(JToken token)
        {
            var trades = new List <ExchangeTrade>();

            foreach (var t in token)
            {
                var ts    = TimeSpan.Parse(t[3].ToStringInvariant());
                var dt    = DateTime.Today.Add(ts).ToUniversalTime();
                var trade = new ExchangeTrade()
                {
                    Id        = t[0].ConvertInvariant <long>(),
                    Price     = t[1].ConvertInvariant <decimal>(),
                    Amount    = t[2].ConvertInvariant <decimal>(),
                    Timestamp = dt,
                    IsBuy     = t[4].ToStringInvariant().EqualsWithOption("bid"),
                };
                trades.Add(trade);
            }

            return(trades);
        }
Beispiel #7
0
        protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null)
        {
            List <ExchangeTrade> trades = new List <ExchangeTrade>();
            long?  lastTradeId          = null;
            JToken obj;
            bool   running = true;

            // Abucoins uses a page curser based on trade_id to iterate history. Keep paginating until startDate is reached or we run out of data
            while (running)
            {
                obj = await MakeJsonRequestAsync <JToken>("/products/" + symbol + "/trades" + (lastTradeId == null ? string.Empty : "?before=" + lastTradeId));

                if ((running = obj.HasValues))
                {
                    lastTradeId = obj.First()["trade_id"].ConvertInvariant <long>();
                    foreach (JToken token in obj)
                    {
                        ExchangeTrade trade = ParseExchangeTrade(token);
                        if (startDate == null || trade.Timestamp >= startDate)
                        {
                            trades.Add(trade);
                        }
                        else
                        {
                            // sinceDateTime has been passed, no more paging
                            running = false;
                            break;
                        }
                    }
                }
                if (trades.Count != 0 && !callback(trades.OrderBy(t => t.Timestamp)))
                {
                    return;
                }
                trades.Clear();
                await Task.Delay(1000);
            }
        }
        /// <summary>
        /// Max returns is trades from the last hour only
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="symbol"></param>
        /// <param name="startDate"></param>
        /// <returns></returns>
        protected override async Task OnGetHistoricalTradesAsync(Func <IEnumerable <ExchangeTrade>, bool> callback, string symbol, DateTime?startDate = null, DateTime?endDate = null)
        {
            symbol = NormalizeSymbol(symbol);
            List <ExchangeTrade> trades = new List <ExchangeTrade>();
            // Not directly supported so we'll return what they have and filter if necessary
            JToken token = await MakeJsonRequestAsync <JToken>("/exchange/last_trades?currencyPair=" + symbol + "&minutesOrHour=false");

            foreach (JToken trade in token)
            {
                ExchangeTrade rc = ParseTrade(trade);
                if (startDate != null)
                {
                    if (rc.Timestamp > startDate)
                    {
                        trades.Add(rc);
                    }
                }
                else
                {
                    trades.Add(rc);
                }
            }
            callback?.Invoke(trades);
        }