Beispiel #1
0
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            obj    = await MakeJsonRequestAsync <JToken>("/public/orderbook/" + symbol + "?limit=" + maxCount.ToStringInvariant());

            if (obj != null && obj.HasValues)
            {
                foreach (JToken order in obj["ask"])
                {
                    if (orders.Asks.Count < maxCount)
                    {
                        var depth = new ExchangeOrderPrice {
                            Price = order["price"].ConvertInvariant <decimal>(), Amount = order["size"].ConvertInvariant <decimal>()
                        };
                        orders.Asks[depth.Price] = depth;
                    }
                }

                foreach (JToken order in obj["bid"])
                {
                    if (orders.Bids.Count < maxCount)
                    {
                        var depth = new ExchangeOrderPrice {
                            Price = order["price"].ConvertInvariant <decimal>(), Amount = order["size"].ConvertInvariant <decimal>()
                        };
                        orders.Bids[depth.Price] = depth;
                    }
                }
            }
            return(orders);
        }
Beispiel #2
0
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            JObject obj = await MakeJsonRequestAsync <Newtonsoft.Json.Linq.JObject>("public/getorderbook?market=" + symbol + "&type=both&limit_bids=" + maxCount + "&limit_asks=" + maxCount);

            JToken            book   = CheckError(obj);
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            bids   = book["buy"];

            foreach (JToken token in bids)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token["Quantity"].ConvertInvariant <decimal>(), Price = token["Rate"].ConvertInvariant <decimal>()
                };
                orders.Bids.Add(order);
            }
            JToken asks = book["sell"];

            foreach (JToken token in asks)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token["Quantity"].ConvertInvariant <decimal>(), Price = token["Rate"].ConvertInvariant <decimal>()
                };
                orders.Asks.Add(order);
            }
            return(orders);
        }
Beispiel #3
0
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string marketSymbol, int maxCount = 100)
        {
            var book = await MakeCoinmateRequest <CoinmateOrderBook>("/orderBook?&groupByPriceLimit=False&currencyPair=" + marketSymbol);

            var result = new ExchangeOrderBook
            {
                MarketSymbol = marketSymbol,
            };

            book.Asks
            .GroupBy(x => x.Price)
            .ToList()
            .ForEach(x => result.Asks.Add(x.Key, new ExchangeOrderPrice {
                Amount = x.Sum(x => x.Amount), Price = x.Key
            }));

            book.Bids
            .GroupBy(x => x.Price)
            .ToList()
            .ForEach(x => result.Bids.Add(x.Key, new ExchangeOrderPrice {
                Amount = x.Sum(x => x.Amount), Price = x.Key
            }));

            return(result);
        }
Beispiel #4
0
        private static ExchangeOrderBook ConvertToExchangeOrderBook(int maxCount, BL3POrderBook bl3POrderBook)
        {
            var exchangeOrderBook = new ExchangeOrderBook
            {
                MarketSymbol   = bl3POrderBook.MarketSymbol,
                LastUpdatedUtc = CryptoUtility.UtcNow
            };

            var asks = bl3POrderBook.Asks
                       .OrderBy(b => b.Price, exchangeOrderBook.Asks.Comparer)
                       .Take(maxCount);

            foreach (var ask in asks)
            {
                exchangeOrderBook.Asks.Add(ask.Price, ask.ToExchangeOrder());
            }

            var bids = bl3POrderBook.Bids
                       .OrderBy(b => b.Price, exchangeOrderBook.Bids.Comparer)
                       .Take(maxCount);

            foreach (var bid in bids)
            {
                exchangeOrderBook.Bids.Add(bid.Price, bid.ToExchangeOrder());
            }

            return(exchangeOrderBook);
        }
Beispiel #5
0
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            JToken obj = await MakeJsonRequestAsync <JToken>("/book/" + symbol + "?limit_bids=" + maxCount + "&limit_asks=" + maxCount);

            if (obj == null || obj.Count() == 0)
            {
                return(null);
            }

            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            bids   = obj["bids"];

            foreach (JToken token in bids)
            {
                ExchangeOrderPrice depth = new ExchangeOrderPrice {
                    Amount = token["amount"].ConvertInvariant <decimal>(), Price = token["price"].ConvertInvariant <decimal>()
                };
                orders.Bids[depth.Price] = depth;
            }
            JToken asks = obj["asks"];

            foreach (JToken token in asks)
            {
                ExchangeOrderPrice depth = new ExchangeOrderPrice {
                    Amount = token["amount"].ConvertInvariant <decimal>(), Price = token["price"].ConvertInvariant <decimal>()
                };
                orders.Asks[depth.Price] = depth;
            }
            return(orders);
        }
        public ExchangeOrderBook GetOrderBook(string exchange, string symbol, int maxCount = 100)
        {
            ExchangeOrderBook book   = new ExchangeOrderBook();
            JObject           obj    = MakeJsonRequest <JObject>("/markets/" + exchange.ToLowerInvariant() + "/" + symbol + "/orderbook");
            JObject           result = (JObject)obj["result"];
            int count = 0;

            foreach (JArray array in result["asks"])
            {
                if (++count > maxCount)
                {
                    break;
                }
                book.Asks.Add(new ExchangeOrderPrice {
                    Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                });
            }
            count = 0;
            foreach (JArray array in result["bids"])
            {
                if (++count > maxCount)
                {
                    break;
                }
                book.Bids.Add(new ExchangeOrderPrice {
                    Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                });
            }
            return(book);
        }
Beispiel #7
0
        /// <summary>Common order book parsing method, most exchanges use "asks" and "bids" with
        /// arrays of length 2 for price and amount (or amount and price)</summary>
        /// <param name="token">Token</param>
        /// <param name="asks">Asks key</param>
        /// <param name="bids">Bids key</param>
        /// <param name="maxCount">Max count</param>
        /// <returns>Order book</returns>
        public static ExchangeOrderBook ParseOrderBookFromJTokenArrays(JToken token, string asks = "asks", string bids = "bids", string sequence = "ts", int maxCount = 100)
        {
            var book = new ExchangeOrderBook {
                SequenceId = token[sequence].ConvertInvariant <long>()
            };

            foreach (JArray array in token[asks])
            {
                var depth = new ExchangeOrderPrice {
                    Price = array[0].ConvertInvariant <decimal>(), Amount = array[1].ConvertInvariant <decimal>()
                };
                book.Asks[depth.Price] = depth;
                if (book.Asks.Count == maxCount)
                {
                    break;
                }
            }

            foreach (JArray array in token[bids])
            {
                var depth = new ExchangeOrderPrice {
                    Price = array[0].ConvertInvariant <decimal>(), Amount = array[1].ConvertInvariant <decimal>()
                };
                book.Bids[depth.Price] = depth;
                if (book.Bids.Count == maxCount)
                {
                    break;
                }
            }

            return(book);
        }
        public async Task <ExchangeOrderBook> GetOrderBookAsync(string exchange, string symbol, int maxCount = 100)
        {
            await new SynchronizationContextRemover();

            ExchangeOrderBook book   = new ExchangeOrderBook();
            JToken            result = await MakeJsonRequestAsync <JToken>("/markets/" + exchange.ToLowerInvariant() + "/" + symbol + "/orderbook");

            int count = 0;

            foreach (JArray array in result["asks"])
            {
                if (++count > maxCount)
                {
                    break;
                }
                var depth = new ExchangeOrderPrice {
                    Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                };
                book.Asks[depth.Price] = depth;
            }
            count = 0;
            foreach (JArray array in result["bids"])
            {
                if (++count > maxCount)
                {
                    break;
                }
                var depth = new ExchangeOrderPrice {
                    Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                };
                book.Bids[depth.Price] = depth;
            }
            return(book);
        }
Beispiel #9
0
        /// <summary>
        /// Common order book parsing method, checks for "amount" or "quantity" and "price" elements
        /// </summary>
        /// <param name="token">Token</param>
        /// <param name="asks">Asks key</param>
        /// <param name="bids">Bids key</param>
        /// <param name="price">Price key</param>
        /// <param name="amount">Quantity key</param>
        /// <param name="sequence">Sequence key</param>
        /// <param name="maxCount">Max count</param>
        /// <returns>Order book</returns>
        public static ExchangeOrderBook ParseOrderBookFromJTokenDictionaries(JToken token, string asks = "asks", string bids = "bids", string price = "price", string amount = "amount", string sequence = "ts", int maxCount = 100)
        {
            ExchangeOrderBook book = new ExchangeOrderBook
            {
                SequenceId = token[sequence].ConvertInvariant <long>()
            };

            foreach (JToken ask in token[asks])
            {
                var depth = new ExchangeOrderPrice {
                    Price = ask[price].ConvertInvariant <decimal>(), Amount = ask[amount].ConvertInvariant <decimal>()
                };
                book.Asks[depth.Price] = depth;
                if (book.Asks.Count == maxCount)
                {
                    break;
                }
            }
            foreach (JToken bid in token[bids])
            {
                var depth = new ExchangeOrderPrice {
                    Price = bid[price].ConvertInvariant <decimal>(), Amount = bid[amount].ConvertInvariant <decimal>()
                };
                book.Bids[depth.Price] = depth;
                if (book.Bids.Count == maxCount)
                {
                    break;
                }
            }
            return(book);
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 50)
        {
            string                      url    = "/products/" + symbol.ToUpperInvariant() + "/book?level=2";
            ExchangeOrderBook           orders = new ExchangeOrderBook();
            Dictionary <string, object> books  = await MakeJsonRequestAsync <Dictionary <string, object> >(url);

            JArray asks = books["asks"] as JArray;
            JArray bids = books["bids"] as JArray;

            foreach (JArray ask in asks)
            {
                var depth = new ExchangeOrderPrice {
                    Amount = ask[1].ConvertInvariant <decimal>(), Price = ask[0].ConvertInvariant <decimal>()
                };
                orders.Asks[depth.Price] = depth;
            }
            foreach (JArray bid in bids)
            {
                var depth = new ExchangeOrderPrice {
                    Amount = bid[1].ConvertInvariant <decimal>(), Price = bid[0].ConvertInvariant <decimal>()
                };
                orders.Bids[depth.Price] = depth;
            }
            return(orders);
        }
        public override IReadOnlyCollection <KeyValuePair <string, ExchangeOrderBook> > GetOrderBooks(int maxCount = 100)
        {
            List <KeyValuePair <string, ExchangeOrderBook> > books = new List <KeyValuePair <string, ExchangeOrderBook> >();
            JObject obj = MakeJsonRequest <JObject>("/public?command=returnOrderBook&currencyPair=all&depth=" + maxCount);

            CheckError(obj);
            foreach (JProperty token in obj.Children())
            {
                ExchangeOrderBook book = new ExchangeOrderBook();
                foreach (JArray array in token.First["asks"])
                {
                    book.Asks.Add(new ExchangeOrderPrice {
                        Amount = (decimal)array[1], Price = (decimal)array[0]
                    });
                }
                foreach (JArray array in token.First["bids"])
                {
                    book.Bids.Add(new ExchangeOrderPrice {
                        Amount = (decimal)array[1], Price = (decimal)array[0]
                    });
                }
                books.Add(new KeyValuePair <string, ExchangeOrderBook>(token.Name, book));
            }
            return(books);
        }
Beispiel #12
0
        protected override IWebSocket OnGetOrderBookWebSocket(Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] symbols)
        {
            if (symbols == null || symbols.Length == 0)
            {
                symbols = GetSymbolsAsync().Sync().ToArray();
            }
            string combined = string.Join("/", symbols.Select(s => this.NormalizeSymbol(s).ToLowerInvariant() + "@depth"));

            return(ConnectWebSocket($"/stream?streams={combined}", (_socket, msg) =>
            {
                string json = msg.ToStringFromUTF8();
                var update = JsonConvert.DeserializeObject <MultiDepthStream>(json);
                string symbol = update.Data.Symbol;
                ExchangeOrderBook book = new ExchangeOrderBook {
                    SequenceId = update.Data.FinalUpdate, Symbol = symbol
                };
                foreach (List <object> ask in update.Data.Asks)
                {
                    var depth = new ExchangeOrderPrice {
                        Price = ask[0].ConvertInvariant <decimal>(), Amount = ask[1].ConvertInvariant <decimal>()
                    };
                    book.Asks[depth.Price] = depth;
                }
                foreach (List <object> bid in update.Data.Bids)
                {
                    var depth = new ExchangeOrderPrice {
                        Price = bid[0].ConvertInvariant <decimal>(), Amount = bid[1].ConvertInvariant <decimal>()
                    };
                    book.Bids[depth.Price] = depth;
                }
                callback(book);
                return Task.CompletedTask;
            }));
        }
        private ExchangeOrderBook ParseOrderBook(JToken orderBook)
        {
            try
            {
                BittrexStreamUpdateOrderBook bittrexOrderBook = orderBook.ToObject <BittrexStreamUpdateOrderBook>();

                ExchangeOrderBook book = new ExchangeOrderBook()
                {
                    SequenceId     = bittrexOrderBook.Sequence,
                    MarketSymbol   = bittrexOrderBook.MarketSymbol,
                    LastUpdatedUtc = DateTime.UtcNow
                };

                foreach (AskDelta ask in bittrexOrderBook.AskDeltas)
                {
                    book.Asks.Add(ask.Rate, new ExchangeOrderPrice {
                        Amount = ask.Quantity, Price = ask.Rate
                    });
                }

                foreach (BidDelta bid in bittrexOrderBook.BidDeltas)
                {
                    book.Bids.Add(bid.Rate, new ExchangeOrderPrice {
                        Amount = bid.Quantity, Price = bid.Rate
                    });
                }

                return(book);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            ExchangeOrderBook orders = new ExchangeOrderBook();
            // {"TradePairId":100,"Label":"DOT/BTC","Price":0.00000317,"Volume":333389.57231468,"Total":1.05684494}
            JToken token = await MakeJsonRequestAsync <JToken>("/GetMarketOrders/" + NormalizeSymbol(symbol) + "/" + maxCount.ToStringInvariant());

            token = CheckError(token);
            if (token.HasValues)
            {
                foreach (JToken order in token["Buy"])
                {
                    orders.Bids.Add(new ExchangeOrderPrice()
                    {
                        Price = order.Value <decimal>("Price"), Amount = order.Value <decimal>("Volume")
                    });
                }
                foreach (JToken order in token["Sell"])
                {
                    orders.Asks.Add(new ExchangeOrderPrice()
                    {
                        Price = order.Value <decimal>("Price"), Amount = order.Value <decimal>("Volume")
                    });
                }
            }
            return(orders);
        }
        public override ExchangeOrderBook GetOrderBook(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            JObject json = MakeJsonRequest <JObject>("/0/public/Depth?pair=" + symbol + "&count=" + maxCount);

            CheckError(json);
            JToken obj = json["result"][symbol] as JToken;

            if (obj == null)
            {
                return(null);
            }
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            bids   = obj["bids"];

            foreach (JToken token in bids)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token[1].Value <decimal>(), Price = token[0].Value <decimal>()
                };
                orders.Bids.Add(order);
            }
            JToken asks = obj["asks"];

            foreach (JToken token in asks)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token[1].Value <decimal>(), Price = token[0].Value <decimal>()
                };
                orders.Asks.Add(order);
            }
            return(orders);
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            JObject json = await MakeJsonRequestAsync <JObject>("/0/public/Depth?pair=" + symbol + "&count=" + maxCount);

            JToken obj = CheckError(json);

            obj = obj[symbol];
            if (obj == null)
            {
                return(null);
            }
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            bids   = obj["bids"];

            foreach (JToken token in bids)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token[1].ConvertInvariant <decimal>(), Price = token[0].ConvertInvariant <decimal>()
                };
                orders.Bids.Add(order);
            }
            JToken asks = obj["asks"];

            foreach (JToken token in asks)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token[1].ConvertInvariant <decimal>(), Price = token[0].ConvertInvariant <decimal>()
                };
                orders.Asks.Add(order);
            }
            return(orders);
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            ExchangeOrderBook orders = new ExchangeOrderBook();
            var    split             = symbol.Split('_');
            JToken token             = await MakeJsonRequestAsync <JToken>("/api?method=getorders&coin=" + split[1] + "&market=" + split[0]);

            if (token != null && token.HasValues)
            {
                foreach (JArray order in token["asks"])
                {
                    var depth = new ExchangeOrderPrice {
                        Price = order[0].ConvertInvariant <decimal>(), Amount = order[1].ConvertInvariant <decimal>()
                    };
                    orders.Asks[depth.Price] = depth;
                }

                foreach (JArray order in token["bids"])
                {
                    var depth = new ExchangeOrderPrice {
                        Price = order[0].ConvertInvariant <decimal>(), Amount = order[1].ConvertInvariant <decimal>()
                    };
                    orders.Bids[depth.Price] = depth;
                }
            }
            return(orders);
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            token  = await MakeJsonRequestAsync <JToken>("/products/" + symbol + "/book?level=" + (maxCount > 50 ? "0" : "2"));

            if (token.HasValues)
            {
                foreach (JArray array in token["bids"])
                {
                    if (orders.Bids.Count < maxCount)
                    {
                        orders.Bids.Add(new ExchangeOrderPrice()
                        {
                            Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                        });
                    }
                }
                foreach (JArray array in token["asks"])
                {
                    if (orders.Asks.Count < maxCount)
                    {
                        orders.Asks.Add(new ExchangeOrderPrice()
                        {
                            Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                        });
                    }
                }
            }
            return(orders);
        }
        protected override IWebSocket OnGetDeltaOrderBookWebSocket(Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] marketSymbols)
        {
            if (marketSymbols == null || marketSymbols.Length == 0)
            {
                marketSymbols = GetMarketSymbolsAsync().Sync().ToArray();
            }
            string combined = string.Join("/", marketSymbols.Select(s => this.NormalizeMarketSymbol(s).ToLowerInvariant() + "@depth@100ms"));

            return(ConnectWebSocket($"/stream?streams={combined}", (_socket, msg) =>
            {
                string json = msg.ToStringFromUTF8();
                var update = JsonConvert.DeserializeObject <MultiDepthStream>(json);
                string marketSymbol = update.Data.MarketSymbol;
                ExchangeOrderBook book = new ExchangeOrderBook {
                    SequenceId = update.Data.FinalUpdate, MarketSymbol = marketSymbol, LastUpdatedUtc = CryptoUtility.UnixTimeStampToDateTimeMilliseconds(update.Data.EventTime)
                };
                foreach (List <object> ask in update.Data.Asks)
                {
                    var depth = new ExchangeOrderPrice {
                        Price = ask[0].ConvertInvariant <decimal>(), Amount = ask[1].ConvertInvariant <decimal>()
                    };
                    book.Asks[depth.Price] = depth;
                }
                foreach (List <object> bid in update.Data.Bids)
                {
                    var depth = new ExchangeOrderPrice {
                        Price = bid[0].ConvertInvariant <decimal>(), Amount = bid[1].ConvertInvariant <decimal>()
                    };
                    book.Bids[depth.Price] = depth;
                }
                callback(book);
                return Task.CompletedTask;
            }));
        }
Beispiel #20
0
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            token  = await MakeJsonRequestAsync <JToken>("/exchange/order_book?currencyPair=" + symbol + "&depth=" + maxCount.ToStringInvariant());

            token = CheckError(token);
            if (token != null)
            {
                foreach (JToken order in token["asks"])
                {
                    orders.Asks.Add(new ExchangeOrderPrice()
                    {
                        Price  = order[0].ConvertInvariant <decimal>(),
                        Amount = order[1].ConvertInvariant <decimal>()
                    });
                }
                foreach (JToken order in token["bids"])
                {
                    orders.Bids.Add(new ExchangeOrderPrice()
                    {
                        Price  = order[0].ConvertInvariant <decimal>(),
                        Amount = order[1].ConvertInvariant <decimal>()
                    });
                }
            }
            return(orders);
        }
Beispiel #21
0
        public override ExchangeOrderBook GetOrderBook(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            JObject obj = MakeJsonRequest <Newtonsoft.Json.Linq.JObject>("public/getorderbook?market=" + symbol + "&type=both&limit_bids=" + maxCount + "&limit_asks=" + maxCount);

            CheckError(obj);
            JToken book = obj["result"];

            if (book == null)
            {
                return(null);
            }
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            bids   = book["buy"];

            foreach (JToken token in bids)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token["Quantity"].Value <decimal>(), Price = token["Rate"].Value <decimal>()
                };
                orders.Bids.Add(order);
            }
            JToken asks = book["sell"];

            foreach (JToken token in asks)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token["Quantity"].Value <decimal>(), Price = token["Rate"].Value <decimal>()
                };
                orders.Asks.Add(order);
            }
            return(orders);
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            ExchangeOrderBook orders = new ExchangeOrderBook();
            //"result" : { "buy" : [{"Quantity" : 4.99400000,"Rate" : 3.00650900}, {"Quantity" : 50.00000000, "Rate" : 3.50000000 }  ] ...
            JToken result = await MakeJsonRequestAsync <JObject>("/public/getorderbook?market=" + symbol + "&type=ALL&depth=" + maxCount);

            result = CheckError(result);
            foreach (JToken token in result["buy"])
            {
                if (orders.Bids.Count < maxCount)
                {
                    orders.Bids.Add(new ExchangeOrderPrice()
                    {
                        Amount = token["Quantity"].ConvertInvariant <decimal>(), Price = token["Rate"].ConvertInvariant <decimal>()
                    });
                }
            }
            foreach (JToken token in result["sell"])
            {
                if (orders.Asks.Count < maxCount)
                {
                    orders.Asks.Add(new ExchangeOrderPrice()
                    {
                        Amount = token["Quantity"].ConvertInvariant <decimal>(), Price = token["Rate"].ConvertInvariant <decimal>()
                    });
                }
            }
            return(orders);
        }
Beispiel #23
0
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            token  = await MakeJsonRequestAsync <JToken>("/open/orders?symbol=" + symbol + "&limit=" + maxCount);

            token = CheckError(token);
            if (token.HasValues)
            {
                foreach (JToken order in token["BUY"])
                {
                    orders.Bids.Add(new ExchangeOrderPrice()
                    {
                        Price = order[0].ConvertInvariant <decimal>(), Amount = order[1].ConvertInvariant <decimal>()
                    });
                }
                foreach (JToken order in token["SELL"])
                {
                    orders.Asks.Add(new ExchangeOrderPrice()
                    {
                        Price = order[0].ConvertInvariant <decimal>(), Amount = order[1].ConvertInvariant <decimal>()
                    });
                }
            }
            return(orders);
        }
        public override ExchangeOrderBook GetOrderBook(string symbol, int maxCount = 100)
        {
            symbol = NormalizeSymbol(symbol);
            JObject obj = MakeJsonRequest <Newtonsoft.Json.Linq.JObject>("/book/" + symbol + "?limit_bids=" + maxCount + "&limit_asks=" + maxCount);

            if (obj == null || obj.Count == 0)
            {
                return(null);
            }

            ExchangeOrderBook orders = new ExchangeOrderBook();
            JToken            bids   = obj["bids"];

            foreach (JToken token in bids)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token["amount"].ConvertInvariant <decimal>(), Price = token["price"].ConvertInvariant <decimal>()
                };
                orders.Bids.Add(order);
            }
            JToken asks = obj["asks"];

            foreach (JToken token in asks)
            {
                ExchangeOrderPrice order = new ExchangeOrderPrice {
                    Amount = token["amount"].ConvertInvariant <decimal>(), Price = token["price"].ConvertInvariant <decimal>()
                };
                orders.Asks.Add(order);
            }
            return(orders);
        }
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string marketSymbol, int maxCount = 100)
        {
            JToken token = await MakeJsonRequestAsync <JToken>($"/{marketSymbol}/transactions");

            ExchangeOrderBook result = new ExchangeOrderBook();

            // we can not use `APIExtensions.ParseOrderBookFromJToken ...` here, because bid/ask is denoted by "side" property.
            foreach (JToken tx in token["transactions"])
            {
                var     isBuy  = (string)tx["side"] == "buy";
                decimal price  = tx["price"].ConvertInvariant <decimal>();
                decimal amount = tx["amount"].ConvertInvariant <decimal>();
                if (isBuy)
                {
                    result.Bids[price] = new ExchangeOrderPrice {
                        Amount = amount, Price = price
                    };
                }
                else
                {
                    result.Asks[price] = new ExchangeOrderPrice {
                        Amount = amount, Price = price
                    };
                }
                result.MarketSymbol = NormalizeMarketSymbol(marketSymbol);
            }
            return(result);
        }
Beispiel #26
0
        protected override IDisposable OnGetOrderBookWebSocket(Action <ExchangeSequencedWebsocketMessage <KeyValuePair <string, ExchangeOrderBook> > > callback, int maxCount = 20, params string[] symbols)
        {
            if (callback == null)
            {
                return(null);
            }

            string suffix = "@depth" + maxCount.ToStringInvariant();
            string url    = GetWebSocketStreamUrlForSymbols(suffix, symbols);

            return(ConnectWebSocket(url, (msg, _socket) =>
            {
                try
                {
                    JToken token = JToken.Parse(msg.UTF8String());
                    string name = token["stream"].ToStringInvariant();
                    token = token["data"];
                    ExchangeOrderBook orderBook = ParseOrderBook(token);
                    string symbol = NormalizeSymbol(name.Substring(0, name.IndexOf('@')));
                    int sequenceNumber = token["lastUpdateId"].ConvertInvariant <int>();
                    callback(new ExchangeSequencedWebsocketMessage <KeyValuePair <string, ExchangeOrderBook> >(sequenceNumber, new KeyValuePair <string, ExchangeOrderBook>(symbol, orderBook)));
                }
                catch
                {
                }
            }));
        }
        protected override async Task <IEnumerable <KeyValuePair <string, ExchangeOrderBook> > > OnGetOrderBooksAsync(int maxCount = 100)
        {
            List <KeyValuePair <string, ExchangeOrderBook> > books = new List <KeyValuePair <string, ExchangeOrderBook> >();
            JToken obj = await MakeJsonRequestAsync <JToken>("/public?command=returnOrderBook&currencyPair=all&depth=" + maxCount);

            foreach (JProperty token in obj.Children())
            {
                ExchangeOrderBook book = new ExchangeOrderBook();
                foreach (JArray array in token.First["asks"])
                {
                    var depth = new ExchangeOrderPrice {
                        Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                    };
                    book.Asks[depth.Price] = depth;
                }
                foreach (JArray array in token.First["bids"])
                {
                    var depth = new ExchangeOrderPrice {
                        Amount = array[1].ConvertInvariant <decimal>(), Price = array[0].ConvertInvariant <decimal>()
                    };
                    book.Bids[depth.Price] = depth;
                }
                books.Add(new KeyValuePair <string, ExchangeOrderBook>(token.Name, book));
            }
            return(books);
        }
        protected override IWebSocket OnGetOrderBookWebSocket(Action <ExchangeOrderBook> callback, int maxCount = 20, params string[] marketSymbols)
        {
            /*
             * {[
             * {
             * "binary": 0,
             * "channel": "addChannel",
             * "data": {
             * "result": true,
             * "channel": "ok_sub_spot_bch_btc_depth_5"
             * }
             * }
             * ]}
             *
             *
             *
             * {[
             * {
             * "data": {
             * "asks": [
             * [
             * "8364.1163",
             * "0.005"
             * ],
             *
             * ],
             * "bids": [
             * [
             * "8335.99",
             * "0.01837999"
             * ],
             * [
             * "8335.9899",
             * "0.06"
             * ],
             * ],
             * "timestamp": 1526734386064
             * },
             * "binary": 0,
             * "channel": "ok_sub_spot_btc_usdt_depth_20"
             * }
             * ]}
             *
             */

            return(ConnectWebSocketOkex(async(_socket) =>
            {
                marketSymbols = await AddMarketSymbolsToChannel(_socket, $"ok_sub_spot_{{0}}_depth_{maxCount}", marketSymbols);
            }, (_socket, symbol, sArray, token) =>
            {
                ExchangeOrderBook book = ExchangeAPIExtensions.ParseOrderBookFromJTokenArrays(token, sequence: "timestamp", maxCount: maxCount);
                book.MarketSymbol = symbol;
                callback(book);
                return Task.CompletedTask;
            }));
        }
Beispiel #29
0
 static void updateOrderBook(ExchangeOrderBook fullOrderBook, ExchangeOrderBook freshBook)
 {
     lock (fullOrderBook)
     {
         // update deltas as long as the full book is at or before the delta timestamp
         if (fullOrderBook.SequenceId <= freshBook.SequenceId)
         {
             fullOrderBook.ApplyUpdates(freshBook);
         }
     }
 }
        //GetOrderBook 5
        protected override async Task <ExchangeOrderBook> OnGetOrderBookAsync(string symbol, int maxCount = 100)
        {
            //https://api.lbank.info/v1/depth.do?symbol=eth_btc&size=60&merge=1

            maxCount = Math.Min(maxCount, ORDER_BOOK_MAX_SIZE);
            JToken resp = await this.MakeJsonRequestAsync <JToken>($"/depth.do?symbol={symbol}&size={maxCount}&merge=0");

            CheckResponseToken(resp);
            ExchangeOrderBook book = ExchangeAPIExtensions.ParseOrderBookFromJTokenArrays(resp, maxCount: maxCount);

            book.SequenceId = resp["timestamp"].ConvertInvariant <long>();
            return(book);
        }