diff --git a/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs b/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs index caefeaa23..ffe4c36b0 100644 --- a/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs +++ b/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs @@ -86,7 +86,7 @@ namespace bts.history //if this event (whatever it is) came after the lower bound in this bucket, do the action foreach (var algo in _algos) { - algo.Value.execute_with_latency(md); //maybe do the action if enough time has passed, update my books + algo.Value.execute_with_latency(md); //maybe do the action if enough time has passed, update order book with my order } foreach(var algo in _algos) { diff --git a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs index 149c52bf7..a353fecfe 100644 --- a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs +++ b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs @@ -46,13 +46,14 @@ namespace bts.history class FillEntry { - public FillEntry(OrderEntry order, api.Side side, double price, ulong size, int piq) + public FillEntry(OrderEntry order, api.Side side, double price, ulong size, int piq, int queue_len) { this.order = order; this.side = side; this.price = price; this.size = size; this.piq = piq; + this.queue_len = queue_len; } public OrderEntry order { get; } public api.Side side { get; } @@ -60,6 +61,7 @@ namespace bts.history public ulong size { get; } public bool fully_filled { get; set;} public int piq { get; set; } + public int queue_len { get; set;} } @@ -83,10 +85,12 @@ namespace bts.history internal class TradeStats { public double our_average_buy_piq { get; set; } + public double our_average_buy_q_len { get; set; } public double our_average_buy_price { get; set; } public int our_total_buys { get; set; } - public double our_average_sell_piq { get; set; } + public double our_average_sell_piq { get; set; } + public double our_average_sell_q_len { get; set; } public double our_average_sell_price { get; set; } public int our_total_sells { get; set; } @@ -118,25 +122,29 @@ namespace bts.history var sellsize = sells.Sum(x => (int)x.size); var sellprice = sells.Sum(x => x.price); - stats.our_average_buy_piq = stats.our_average_buy_piq * stats.our_total_buys + buys.Sum( x => (double) x.piq); + stats.our_average_buy_piq = stats.our_average_buy_piq * stats.our_total_buys + buys.Where(x => x.piq > 0).Sum( x => (double) x.piq); + stats.our_average_buy_q_len = stats.our_average_buy_q_len * stats.our_total_buys + buys.Where(x => x.queue_len> 0).Sum(x => (double)x.queue_len); stats.our_average_buy_price = stats.our_average_buy_price * stats.our_total_buys + buys.Sum(x => x.price); stats.our_total_buys += nbuys; stats.our_average_buy_piq /= stats.our_total_buys; + stats.our_average_buy_q_len /= stats.our_total_buys; stats.our_average_buy_price /= stats.our_total_buys; - stats.our_average_sell_piq = stats.our_average_sell_piq * stats.our_total_sells + sells.Sum(x => (double)x.piq); + stats.our_average_sell_piq = stats.our_average_sell_piq * stats.our_total_sells + sells.Where(x => x.piq > 0).Sum(x => (double)x.piq); + stats.our_average_sell_q_len = stats.our_average_sell_q_len * stats.our_total_sells + sells.Where(x => x.queue_len > 0).Sum(x => (double)x.queue_len); stats.our_average_sell_price = stats.our_average_sell_price * stats.our_total_sells + sells.Sum(x => x.price); stats.our_total_sells += nsells; stats.our_average_sell_piq /= stats.our_total_sells; + stats.our_average_sell_q_len /= stats.our_total_sells; stats.our_average_sell_price /= stats.our_total_sells; this.tradelog.Clear(); return stats; } - FillEntry generate_fill(OrderEntry myorder, double price, uint qty, int piq, string dbg = null) + FillEntry generate_fill(OrderEntry myorder, double price, uint qty, int piq, int qlen, string dbg = null) { - var f = new FillEntry(myorder, myorder.side, price, qty, piq); + var f = new FillEntry(myorder, myorder.side, price, qty, piq, qlen); _tradelog.Enqueue(f); Log.assert(qty <= myorder.size); myorder.size -= qty; @@ -193,7 +201,7 @@ namespace bts.history if(myorder.price >= currask) { var qty = Math.Min(myorder.size, this.current_book.asksize_at(level)); - fills.Add(generate_fill(myorder, currask, (uint)qty, -1, "CROSS")); + fills.Add(generate_fill(myorder, currask, (uint)qty, 0, 0, "CROSS")); } else break; @@ -209,7 +217,7 @@ namespace bts.history if (myorder.price <= currbid) { var qty = Math.Min(myorder.size, this.current_book.bidsize_at(level)); - fills.Add(generate_fill(myorder, currbid, (uint)qty, -1, "CROSS")); + fills.Add(generate_fill(myorder, currbid, (uint)qty, 0, 0, "CROSS")); } else break; @@ -233,13 +241,13 @@ namespace bts.history var ts = new Timestamp(); var key = queue.Keys.LastOrDefault(999ul); _myid++; - OrderEntry myorder = new OrderEntry(ts, _myid, key, 0, side, price, size, on_fill, tag); - Log.Testing.info($"insert myorder {_myid} at price { price } piq { queue.Count } "); + OrderEntry myorder = new OrderEntry(ts, key, key, 0, side, price, size, on_fill, tag); + Log.Testing.info($"insert myorder {key} at price { price } piq { queue.Count } "); post_process_fill( check_my_cross(myorder) ); if(myorder.size > 0) - _myorders.Add(_myid, myorder); - return _myid; + _myorders.Add(key, myorder); + return key; } public OrderEntry change_my_order(ulong order_id, double price, uint size = uint.MinValue) @@ -329,7 +337,8 @@ namespace bts.history count++; } //int piq = q.IndexOfKey(myorder.order_priority); int count = q.Count; - var f = generate_fill(myorder, order.price, (uint)Math.Min(trade.LastQty, myorder.size), piq, $" {piq} of {count} "); + var f = generate_fill(myorder, order.price, (uint)Math.Min(trade.LastQty, myorder.size), + piq, count, $" {piq} of {count} "); fills.Add(f); } else @@ -384,6 +393,15 @@ namespace bts.history { Log.assert(x.order_id == order.OrderId, "orderid invariant"); x.timestamp = md.timestamp(); + if(x.order_priority != order.MdOrderPriority) + { + if(_myorders.TryGetValue(x.order_priority, out OrderEntry myorder)) + { + _myorders.Remove(x.order_priority); + myorder.order_priority = order.MdOrderPriority; + _myorders[order.MdOrderPriority] = myorder; + } + } x.order_priority = order.MdOrderPriority; x.price = mdval.MdEntryPx; x.size = order.MdDisplayQty;