void OnRemove(OrderBookUpdateInfo info) { if (info.Type == OrderBookEntryType.Bid) { OrderBookEntry entry = Bids.FirstOrDefault((e) => e.Value == info.Entry.Value); if (entry == null) { Debug.WriteLine("Error removing: '" + info.Type + "' with rate " + info.Entry.Value + " not found"); return; } Bids.Remove(entry); } else { OrderBookEntry entry = Asks.FirstOrDefault((e) => e.Value == info.Entry.Value); if (entry == null) { Debug.WriteLine("Error removing: '" + info.Type + "' with rate " + info.Entry.Value + " not found"); return; } Asks.Remove(entry); } RaiseOnChanged(info); OnChangedCore(info); }
void AddUpdateToQueue(OrderBookUpdateInfo info) { if (Updates.Count == 0) { GetSnapshot(); } Updates.Add(info); }
protected internal void OnAddAsk(OrderBookUpdateInfo info) { OrderBookEntry before = Asks.FirstOrDefault((e) => e.Value < info.Entry.Value); if (before == null) { Asks.Insert(0, info.Entry); return; } Asks.Insert(Asks.IndexOf(before), info.Entry); }
void OnAddBid(OrderBookUpdateInfo info) { OrderBookEntry before = Bids.FirstOrDefault((e) => e.Value < info.Entry.Value); if (before == null) { Bids.Add(info.Entry); return; } Bids.Insert(Bids.IndexOf(before), info.Entry); }
protected internal void ApplyInfo(OrderBookUpdateInfo info) { if (info.Action == OrderBookUpdateType.Remove) { OnRemove(info); } else if (info.Action == OrderBookUpdateType.Modify) { OnModify(info); } }
protected internal void RaiseOnChanged(OrderBookUpdateInfo info) { OrderBookEventArgs e = new OrderBookEventArgs() { Update = info }; if (OnChanged != null) { OnChanged(this, e); } }
public void GetOrderBook(PoloniexTicker ticker, int depth) { string address = string.Format("https://poloniex.com/public?command=returnOrderBook¤cyPair={0}&depth={1}", Uri.EscapeDataString(ticker.CurrencyPair), depth); string text; using (WebClient client = new WebClient()) { text = client.DownloadString(address); } ticker.OrderBook.Bids.Clear(); ticker.OrderBook.Asks.Clear(); JObject res = (JObject)JsonConvert.DeserializeObject(text); foreach (JProperty prop in res.Children()) { if (prop.Name == "asks" || prop.Name == "bids") { OrderBookEntryType type = prop.Name == "asks" ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; JArray items = prop.Value.ToObject <JArray>(); foreach (JArray item in items.Children()) { OrderBookUpdateInfo info = new OrderBookUpdateInfo(); info.Type = prop.Name == "asks" ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; info.Entry = new OrderBookEntry(); info.Action = OrderBookUpdateType.Modify; JEnumerable <JToken> values = (JEnumerable <JToken>)item.Children(); JValue rateValue = (JValue)values.First(); info.Entry.Value = rateValue.ToObject <double>(); info.Entry.Amount = rateValue.Next.ToObject <double>(); if (type == OrderBookEntryType.Ask) { ticker.OrderBook.ForceAddAsk(info); } else { ticker.OrderBook.ForceAddBid(info); } } } else if (prop.Name == "seq") { ticker.OrderBook.SeqNumber = prop.Value.ToObject <int>(); Console.WriteLine("Snapshot seq no = " + ticker.OrderBook.SeqNumber); } else if (prop.Name == "isFrozen") { ticker.IsFrozen = prop.Value.ToObject <int>() == 0; } } ticker.OrderBook.ApplyQueueUpdates(); }
protected internal void OnAdd(OrderBookUpdateInfo info) { info.Action = OrderBookUpdateType.Add; if (info.Type == OrderBookEntryType.Bid) { OnAddBid(info); } else { OnAddAsk(info); } RaiseOnChanged(info); OnChangedCore(info); }
protected internal void OnModify(OrderBookUpdateInfo info) { OrderBookEntry entry = info.Type == OrderBookEntryType.Ask ? Asks.FirstOrDefault((e) => e.Value == info.Entry.Value) : Bids.FirstOrDefault((e) => e.Value == info.Entry.Value); if (entry == null) { OnAdd(info); return; } entry.Amount = info.Entry.Amount; info.Entry = entry; RaiseOnChanged(info); OnChangedCore(info); }
protected internal void ApplyQueueUpdates() { if (Updates.Count == 0) { return; } while (Updates.Count > 0) { OrderBookUpdateInfo info = Updates.First(); if (info.SeqNo <= SeqNumber) { Updates.Remove(info); continue; } if (info.SeqNo == SeqNumber + 1) { ApplyInfo(info); Updates.RemoveAt(0); } } }
public void OnRecvUpdate(OrderBookUpdateInfo info) { bool prevActualState = IsActualState; if (IsExpected(info.SeqNo)) { ApplyInfo(info); ApplyQueueUpdates(); return; } if (IsOutdated(info.SeqNo)) { return; } if (Updates.Count == 0 || Updates.Last().SeqNo == info.SeqNo - 1) { AddUpdateToQueue(info); } else { Debug.WriteLine("Error: update with seq no = " + info.SeqNo + " is skipped. Last seq no in que = " + Updates.Last().SeqNo); return; } }
void ITicker.OnChanged(OrderBookUpdateInfo info) { RaiseChanged(); }
void OnChangedCore(OrderBookUpdateInfo info) { Owner.OnChanged(info); }
protected internal void ForceAddBid(OrderBookUpdateInfo info) { Bids.Add(info.Entry); }
protected internal void ForceAddAsk(OrderBookUpdateInfo info) { Asks.Insert(0, info.Entry); }