public void ApplyIncrementalUpdate(OrderBookEntryType type, OrderBookUpdateType updateType, long id, string rateString, string amountString) { if (type == OrderBookEntryType.Ask) { lock (Asks) { ApplyIncrementalUpdate(id, updateType, rateString, amountString, Asks, true); } if (AllowInvertedAsks) { lock (AsksInverted) { ApplyIncrementalUpdate(id, updateType, rateString, amountString, AsksInverted, false); } } } else { lock (Bids) { ApplyIncrementalUpdate(id, updateType, rateString, amountString, Bids, false); } } LastUpdateTime = DateTime.Now; if (UpdateCount > 0) { return; } UpdateEntries(); RaiseOnChanged(new IncrementalUpdateInfo()); }
protected override void OnOrderBookSocketMessageReceived(object sender, MessageReceivedEventArgs e) { base.OnOrderBookSocketMessageReceived(sender, e); LastWebSocketRecvTime = DateTime.Now; SocketConnectionInfo info = OrderBookSockets[(WebSocket)sender]; JObject obj = JsonConvert.DeserializeObject <JObject>(e.Message); JArray items = obj.Value <JArray>("data"); if (items == null) { return; } OrderBookUpdateType type = String2UpdateType(obj.Value <string>("action")); foreach (JObject item in items) { Ticker t = info.Ticker; OrderBookEntryType entryType = item.Value <string>("side")[0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; string rate = type == OrderBookUpdateType.Add ? item.Value <string>("price") : null; string size = type != OrderBookUpdateType.Remove ? item.Value <string>("size") : null; t.OrderBook.ApplyIncrementalUpdate(entryType, type, item.Value <long>("id"), rate, size); } }
public void ApplyIncrementalUpdate(OrderBookEntryType type, OrderBookUpdateType updateType, long id, string rateString, string amountString) { if (type == OrderBookEntryType.Ask) { ApplyIncrementalUpdate(id, updateType, rateString, amountString, Asks, true); ApplyIncrementalUpdate(id, updateType, rateString, amountString, AsksInverted, false); } else { ApplyIncrementalUpdate(id, updateType, rateString, amountString, Bids, false); } UpdateEntries(); RaiseOnChanged(new IncrementalUpdateInfo()); }
OrderBookUpdateType String2UpdateType(string action) { OrderBookUpdateType type = OrderBookUpdateType.Modify; if (action == "insert") { type = OrderBookUpdateType.Add; } if (action == "delete") { type = OrderBookUpdateType.Remove; } return(type); }
protected internal override void OnOrderBookSocketMessageReceived(object sender, MessageReceivedEventArgs e) { base.OnOrderBookSocketMessageReceived(sender, e); LastWebSocketRecvTime = DateTime.Now; SocketConnectionInfo info = OrderBookSockets.FirstOrDefault(c => c.Key == sender); if (info == null) { return; } Ticker t = info.Ticker; if (t.CaptureData) { t.CaptureDataCore(CaptureStreamType.OrderBook, CaptureMessageType.Incremental, e.Message); } const string incrementalUpdateStartString = "{\"table\":\"orderBookL2\",\"action\":\""; if (e.Message.StartsWith(incrementalUpdateStartString)) { int index = incrementalUpdateStartString.Length; if (e.Message[index] == 'u') // update { index = e.Message.IndexOf("[{"); List <string[]> jsItems = JSonHelper.Default.DeserializeArrayOfObjects(Encoding.ASCII.GetBytes(e.Message), ref index, UpdateItems); foreach (string[] item in jsItems) { OrderBookEntryType entryType = item[2][0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; t.OrderBook.ApplyIncrementalUpdate(entryType, OrderBookUpdateType.Modify, FastValueConverter.ConvertPositiveLong(item[1]), null, item[3]); } return; } //else if(e.Message[index] == 'i') { // insert // index = e.Message.IndexOf("[{"); // List<string[]> jsItems = JSonHelper.Default.DeserializeArrayOfObjects(Encoding.ASCII.GetBytes(e.Message), ref index, InsertItems); // foreach(string[] item in jsItems) { // OrderBookEntryType entryType = item[2][0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; // t.OrderBook.ApplyIncrementalUpdate(entryType, OrderBookUpdateType.Add, FastValueConverter.ConvertPositiveLong(item[1]), item[4], item[3]); // } // return; //} else if (e.Message[index] == 'd') // delete { index = e.Message.IndexOf("[{"); List <string[]> jsItems = JSonHelper.Default.DeserializeArrayOfObjects(Encoding.ASCII.GetBytes(e.Message), ref index, DeleteItems); foreach (string[] item in jsItems) { OrderBookEntryType entryType = item[2][0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; t.OrderBook.ApplyIncrementalUpdate(entryType, OrderBookUpdateType.Remove, FastValueConverter.ConvertPositiveLong(item[1]), null, null); } return; } } JObject obj = JsonConvert.DeserializeObject <JObject>(e.Message); JArray items = obj.Value <JArray>("data"); if (items == null) { return; } OrderBookUpdateType type = String2UpdateType(obj.Value <string>("action")); lock (t.OrderBook.Bids) { lock (t.OrderBook.Asks) { if (type == OrderBookUpdateType.RefreshAll) { t.OrderBook.Clear(); t.OrderBook.BeginUpdate(); } for (int i = 0; i < items.Count; i++) { JObject item = (JObject)items[i]; OrderBookEntryType entryType = item.Value <string>("side")[0] == 'S' ? OrderBookEntryType.Ask : OrderBookEntryType.Bid; string rate = null; if (type == OrderBookUpdateType.Add || type == OrderBookUpdateType.RefreshAll) { rate = item.Value <string>("price"); } string size = null; if (type != OrderBookUpdateType.Remove) { size = item.Value <string>("size"); } t.OrderBook.ApplyIncrementalUpdate(entryType, type, item.Value <long>("id"), rate, size); } } } if (type == OrderBookUpdateType.RefreshAll) { t.OrderBook.EndUpdate(); } }
protected void ApplyIncrementalUpdate(long id, OrderBookUpdateType type, string rateString, string amountString, List <OrderBookEntry> list, bool ascending) { int index = 0; if (type == OrderBookUpdateType.Remove) { for (int i = 0; i < list.Count; i++) { OrderBookEntry e = list[i]; if (e.Id == id) { list.Remove(e); return; } } if (EnableValidationOnRemove) { IsDirty = true; } return; } double amount = FastValueConverter.Convert(amountString); if (type == OrderBookUpdateType.Modify) { for (int i = 0; i < list.Count; i++) { OrderBookEntry e = list[i]; if (e.Id == id) { e.Amount = amount; return; } } if (EnableValidationOnRemove) { IsDirty = true; } return; } double value = FastValueConverter.Convert(rateString); if (ascending) { for (int i = 0; i < list.Count; i++) { OrderBookEntry e = list[i]; if (e.Value > value) { OrderBookEntry ee = new OrderBookEntry() { ValueString = rateString, AmountString = amountString, Id = id }; list.Insert(index, ee); return; } index++; } } else { for (int i = 0; i < list.Count; i++) { OrderBookEntry e = list[i]; if (e.Value < value) { OrderBookEntry ee = new OrderBookEntry() { ValueString = rateString, AmountString = amountString, Id = id }; list.Insert(index, ee); return; } index++; } } OrderBookEntry addE = new OrderBookEntry() { ValueString = rateString, AmountString = amountString, Id = id }; list.Add(addE); }
protected void ApplyIncrementalUpdate(long id, OrderBookUpdateType type, string rateString, string amountString, List <OrderBookEntry> list, bool ascending) { int index = 0; if (type == OrderBookUpdateType.Remove) { foreach (OrderBookEntry e in list) { if (e.Id == id) { list.Remove(e); return; } } //throw new DllNotFoundException("entry not found -> with value " + value + " and amount " + amount); return; } double amount = FastValueConverter.Convert(amountString); if (type == OrderBookUpdateType.Modify) { foreach (OrderBookEntry e in list) { if (e.Id == id) { e.Amount = amount; return; } } //throw new DllNotFoundException("entry not found -> with value " + value + " and amount " + amount); return; } double value = FastValueConverter.Convert(rateString); if (ascending) { foreach (OrderBookEntry e in list) { if (e.Value > value) { OrderBookEntry ee = new OrderBookEntry() { ValueString = rateString, AmountString = amountString, Id = id }; list.Insert(index, ee); return; } index++; } } else { foreach (OrderBookEntry e in list) { if (e.Value < value) { OrderBookEntry ee = new OrderBookEntry() { ValueString = rateString, AmountString = amountString, Id = id }; list.Insert(index, ee); return; } index++; } } OrderBookEntry addE = new OrderBookEntry() { ValueString = rateString, AmountString = amountString, Id = id }; list.Add(addE); }