static void CoinbaseRest() { var keys = new ExchangeApi.Coinbase.Keys() { Key = "MY_KEY", Secret = "MY_SECRET", Passphrase = "MY_PASSPHRASE", }; using (var client = new ExchangeApi.Coinbase.REST.RestClient(ExchangeApi.Coinbase.Instance.Prod.REST, keys)) { client.SendRequest(new ExchangeApi.Coinbase.REST.CancelAllRequest()).Wait(); string orderId = client.SendRequest(new ExchangeApi.Coinbase.REST.NewOrderRequest() { ClientOrderId = Guid.NewGuid().ToString(), Side = ExchangeApi.Coinbase.Side.Sell, ProductId = "BTC-EUR", Price = 1000m, Size = 0.01m, TimeInForce = ExchangeApi.Coinbase.REST.TimeInForce.GTT, CancelAfter = ExchangeApi.Coinbase.REST.CancelAfter.Min, PostOnly = true, }).Result.OrderId; client.SendRequest(new ExchangeApi.Coinbase.REST.CancelOrderRequest() { OrderId = orderId, }).Wait(); client.SendRequest(new ExchangeApi.Coinbase.REST.CancelOrderRequest() { OrderId = orderId, }).Wait(); } }
static void ListenToMarketData() { var cfg = new Config() { Endpoint = Instance.Prod, Products = new List <string>() { "BTC-USD" }, }; using (var restClient = new ExchangeApi.Coinbase.REST.RestClient(cfg.Endpoint.REST, null)) using (var client = new Client(cfg)) { var book = new OrderBook(); var expectedBook = new OrderBook(); Action poll = () => { if (book.Sequence < 0) { _log.Info("Haven't received any messages yet"); return; } // Sleep on the scheduler thread in order to fall behind on the order book. Thread.Sleep(20000); ExchangeApi.Coinbase.REST.OrderBookResponse full = restClient.SendRequest(new ExchangeApi.Coinbase.REST.OrderBookRequest() { Product = "BTC-USD" }).Result; if (full.Sequence <= book.Sequence) { _log.Info("Order book isn't behind"); return; } _log.Info("Expecting sequence {0} in the near future", full.Sequence); Convert(full, expectedBook); }; client.OnOrderBook += (string product, TimestampedMsg <OrderBookDelta> msg) => { if (msg.Value.Bids.Count + msg.Value.Asks.Count > 10) { _log.Info("OnOrderBook({0}, IsLast={1}): {2} bid(s), {3} ask(s)", product, !client.Scheduler.HasReady(), msg.Value.Bids.Count, msg.Value.Asks.Count); } else { _log.Info("OnOrderBook({0}, IsLast={1}): {2}", product, !client.Scheduler.HasReady(), msg.Value); } ApplyDelta(book, msg.Value); Compare(expectedBook, book); }; client.Connect(); using (var timer = new PeriodicAction(cfg.Scheduler, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30), poll)) while (true) { Thread.Sleep(1000); } } }