debugging KFillModel, realizing I don't have all the info here

This commit is contained in:
Kevin Darby committed 2019-12-24 00:17:40 -05:00
1 parent 0fd45ba92a
commit f5e2a3c28f
5 files changed
+119 -252

No files matched your search

+7 -11
View File
@@ -135,18 +135,14 @@ namespace bts.history
void deploy_join_function(MarketDataIncrementalRefresh packet, string security_desc, double joinprice)
{
if(_state == AlgoState.Joined || _state == AlgoState.WaitingForJoin)
Log.Testing.info("deploy_join_function ");
if(_child == null)
{
Log.Testing.info("deploy_join_function ");
if(_child == null)
{
_child = this.activate<Child>(new[]{security_desc}, _myside, joinprice, _mysize, packet.timestamp());
}
else
{
_child.update(packet.timestamp(), joinprice);
}
_child = this.activate<Child>(new[]{security_desc}, _myside, joinprice, _mysize, packet.timestamp());
}
else
{
_child.update(packet.timestamp(), joinprice);
}
}
}
@@ -67,6 +67,9 @@ namespace bts.history
public void record_order_status(OrderStatus stat)
{
if (check_cross(stat))
return;
Log.assert(stat.order.order_id != 0);
QueueType queue = get_mbo_side(stat.side).GetOrAdd(stat.state.Price, x =>
{
@@ -84,14 +87,16 @@ namespace bts.history
if (q.Key > max)
max = q.Key;
}
if (prior == ulong.MaxValue)
if (prior == ulong.MinValue)
prior = max;
prior++;
if (_mystatus.TryGetValue(stat.order.order_id, out OrderStatus existing_stat))
{
if (stat.is_terminal()) //remove
{
Log.assert(queue.Remove(stat.order_priority));
if(!queue.Remove(existing_stat.order_priority))
Log.Testing.warn($"removing removed order {existing_stat.order_priority}");
stat.order_priority = ulong.MaxValue;
_mystatus[stat.order.order_id] = stat;
_mbokey.Remove(stat.order.order_id);
@@ -100,7 +105,8 @@ namespace bts.history
{
if (stat.state.Quantity > existing_stat.state.Quantity || stat.state.Price != existing_stat.state.Price)
{
Log.assert(queue.Remove(stat.order_priority));
if (!queue.Remove(existing_stat.order_priority))
Log.Testing.warn($"removing removed order {existing_stat.order_priority}");
stat.order_priority = prior;
stat.order.order_priority = prior;
stat.order.tag = "our order";
@@ -122,13 +128,14 @@ namespace bts.history
Log.Testing.info($"insert myorder {stat.order.order_id} at price { stat.state.Price } prior {stat.order_priority} piq { queue.Count } ");
}
uint _bidmismatch, _askmismatch;
public override void process_book(Timestamp transact_time, string security_desc, BookMessage book)
{
base.process_book(transact_time, security_desc, book);
if(book.bid_at(0) != bid_at(0))
Log.Testing.error("bid mismatch");
Log.Testing.error($"bid mismatch {_bidmismatch++}");
if(book.ask_at(0) != ask_at(0))
Log.Testing.error("ask mismatch");
Log.Testing.error($"ask mismatch {_askmismatch++}");
}
public void process_order(Timestamp transact_time, MBOMessage mbo)
@@ -145,13 +152,14 @@ namespace bts.history
var resting_side = Utils.opposide_side(trade_summary.AggressorSide);
var mbo = get_mbo_side(resting_side);
var queue = mbo[trade_summary.price];
List<ITradeDetail> matches = new List<ITradeDetail>();
Dictionary<ulong, ITradeDetail> matches = new Dictionary<ulong, ITradeDetail>();
uint aggressor_qty = trade_summary.Size; //this could be greater than the aggressor size in some cases where implieds trade
if(aggressor_qty != orders.First().LastQty)
Log.Testing.warn("trade had some implied");
uint topq = 0, fifoq = 0, rataq = 0, refifoq = 0;
if (_use_top)
match_top(queue, trade_summary, ref aggressor_qty, matches);
topq = match_top(transact_time, queue, trade_summary, ref aggressor_qty, matches);
uint fifo_lots = (uint)Math.Truncate(_fifo_percent * aggressor_qty);
if(fifo_lots > 0)
{
@@ -160,45 +168,66 @@ namespace bts.history
else if(aggressor_qty == 3)
fifo_lots = 2;
aggressor_qty -= fifo_lots;
match_fifo(queue, trade_summary, ref fifo_lots, matches);
fifoq = match_fifo(transact_time, queue, trade_summary, ref fifo_lots, matches);
}
if(aggressor_qty > 0)
match_prorata(queue, trade_summary, ref aggressor_qty, matches);
rataq = match_prorata(transact_time, queue, trade_summary, ref aggressor_qty, matches);
if(aggressor_qty > 0)
match_fifo(queue, trade_summary, ref aggressor_qty, matches);
validate_matches(orders, matches);
refifoq = match_fifo(transact_time, queue, trade_summary, ref aggressor_qty, matches);
validate_matches(transact_time, trade_summary, orders, matches);
}
//https://www.cmegroup.com/confluence/display/EPICSANDBOX/CME+Globex+Matching+Algorithm+Steps#CMEGlobexMatchingAlgorithmSteps-Split
uint make_match(uint qty, ulong oid, List<ITradeDetail> matches)
uint make_match(Timestamp transact_time, TradeMessage trade_summary,
uint qty, ulong oid, Dictionary<ulong, ITradeDetail> matches)
{
var match = new OrderIDEntry { LastQty = qty, OrderId = oid };
matches.Add(match);
if (_mystatus.TryGetValue(match.OrderId, out OrderStatus existing_stat))
if (_mystatus.TryGetValue(oid, out OrderStatus ourmatch))
{
existing_stat.state.Quantity -= match.LastQty;
existing_stat.order.size -= match.LastQty;
// existing_stat.state.Quantity -= qty;
// existing_stat.order.size -= qty;
if(ourmatch.state.Quantity >= qty)
{
Log.assert(!ourmatch.is_terminal(), "terminal order got fill");
generate_fill(transact_time, ourmatch, trade_summary.price, qty);
return qty;
}
return 0;
}
else
{
if(matches.TryGetValue(oid, out ITradeDetail td))
td.LastQty += qty;
else
{
td = new OrderIDEntry { LastQty = qty, OrderId = oid };
matches.Add(oid, td);
}
return qty;
}
return match.LastQty;
}
void match_top(QueueType queue, co2.TradeMessage trade_summary, ref uint aggressor_qty, List<ITradeDetail> matches)
uint match_top(Timestamp transact_time, QueueType queue, co2.TradeMessage trade_summary, ref uint aggressor_qty, Dictionary<ulong, ITradeDetail> matches)
{
var first = queue.FirstOrDefault();
if (first.Value != null && first.Value.is_top_eligible)
{
var qty = Math.Min(aggressor_qty, (uint)first.Value.size);
aggressor_qty -= make_match(qty, first.Value.order_id, matches);
aggressor_qty -= make_match(transact_time, trade_summary, qty, first.Value.order_id, matches);
return qty;
}
return 0;
}
void match_fifo(QueueType queue, TradeMessage trade_summary, ref uint fifo_lots, List<ITradeDetail> matches)
uint match_fifo(Timestamp transact_time, QueueType queue, TradeMessage trade_summary, ref uint fifo_lots, Dictionary<ulong, ITradeDetail> matches)
{
uint fifoq = 0;
foreach (var lot in queue)
{
var td = Math.Min(fifo_lots, (uint)lot.Value.size);
if (td > 0)
{
fifo_lots -= make_match(td, lot.Value.order_id, matches);
var q = make_match(transact_time, trade_summary, td, lot.Value.order_id, matches);
fifoq += q;
fifo_lots -= q;
if(fifo_lots <= 0)
{
Log.assert(fifo_lots == 0);
@@ -206,10 +235,12 @@ namespace bts.history
}
}
}
return fifoq;
}
void match_prorata(QueueType queue, TradeMessage trade_summary, ref uint aggressor_qty, List<ITradeDetail> matches)
uint match_prorata(Timestamp transact_time, QueueType queue, TradeMessage trade_summary, ref uint aggressor_qty, Dictionary<ulong, ITradeDetail> matches)
{
uint rataq = 0;
uint orig_aggressor_qty = aggressor_qty;
uint total = queue.Sum(x => (uint)x.Value.size);
var noqueue = new QueueType(); //this is the list of orders that received no prorata, and are therefore eligible for leveling
@@ -218,8 +249,9 @@ namespace bts.history
uint alloc = (uint)Math.Truncate((double)lot.Value.size / total * aggressor_qty);
if(alloc >= _min_prorata)
{
var q = make_match(alloc, lot.Value.order_id, matches);
q = Math.Min(q, aggressor_qty);
alloc = Math.Min(alloc, aggressor_qty);
var q = make_match(transact_time, trade_summary, alloc, lot.Value.order_id, matches);
rataq += q;
aggressor_qty -= q;
if (aggressor_qty <= 0)
{
@@ -235,23 +267,30 @@ namespace bts.history
var l = noqueue.Select( x => x.Value ).OrderBy( xx => xx.size ).ThenBy( yy => yy.last_change.to_datetime() ).ToArray();
for(int i = 0; i < l.Length && aggressor_qty > 0; i++)
{
var q = make_match(1, l[i].order_id, matches);
var q = make_match(transact_time, trade_summary, 1, l[i].order_id, matches);
aggressor_qty--;
rataq++;
}
}
return rataq;
}
void validate_matches(IEnumerable<ITradeDetail> orders, List<ITradeDetail> matches)
void validate_matches(Timestamp transact_time, TradeMessage trade_summary,
IEnumerable<ITradeDetail> orders, Dictionary<ulong, ITradeDetail> matches)
{
if(!matches.Any( x => _mystatus.ContainsKey( x.OrderId ) ))
if(!matches.Values.Any( x => _mystatus.ContainsKey( x.OrderId ) ))
{
var dict = matches.ToDictionary( x => x.OrderId, y => y.LastQty);
var dict = matches.Values.ToDictionary( x => x.OrderId, y => y.LastQty);
foreach(var order in orders)
{
var oid = order.OrderId;
var qty = dict[oid];
dict.TryGetValue(oid, out uint qty);
if(qty != order.LastQty)
throw new InvalidOperationException($"invalid match, {qty} vs {order.LastQty} oid = {oid}");
{
var x = find_order(oid);
Log.Testing.error($"invalid match, {qty} vs {order.LastQty} oid = {oid} { (x == null ? "NOT found" : "") } ");
}
//throw new InvalidOperationException($"invalid match, {qty} vs {order.LastQty} oid = {oid}");
}
}
}
@@ -361,7 +400,7 @@ namespace bts.history
if (currq != null && currq.TryGetValue(order.MdOrderPriority, out OrderEntry removed_order))
{
removed_order.last_change = ts;
currq.Remove(order.MdOrderPriority);
//currq.Remove(order.MdOrderPriority);
}
}
}
@@ -181,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {
"scrolled": true
},
@@ -202,38 +202,16 @@
"var algo_params = \"size=10;tick_size=0.015625,0.0078125,0.015625;hedge_factors=1,-1,-1;profit_ticks=0.0078125\";\n",
"var algo_orders = algo_results<HistoryRow>(sink, fill_model, \n",
" algo_type, algo_params, \n",
" bts.api.Side.Undefined, 0.0, 10, \n",
" bts.api.Side.Buy, 0.0, 10, \n",
" security_list, //must match below\n",
" new[] {spdf}, latency_iceberg);"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"ename": "Unhandled Exception",
"evalue": "An item with the same key has already been added. Key: 846782689978",
"output_type": "error",
"traceback": [
"Unhandled Exception",
"An item with the same key has already been added. Key: 846782689978",
" at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)",
" at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](List`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)",
" at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)",
" at bts.history.KFillModel.validate_matches(IEnumerable`1 orders, List`1 matches) in c:\\code\\bts\\spark\\src\\client\\spark_historical\\FillModel\\KFillModel.cs:line 244",
" at bts.history.KFillModel.process_trade(Timestamp transact_time, String security_desc, TradeMessage trade_summary, IEnumerable`1 orders) in c:\\code\\bts\\spark\\src\\client\\spark_historical\\FillModel\\KFillModel.cs:line 169",
" at bts.history.Query.<>c__DisplayClass16_0`1.<algo_results>b__3(T[] valcache, Int32 min) in c:\\code\\bts\\spark\\src\\client\\spark_historical\\Query\\Query.cs:line 280",
" at bts.history.Query.stream_data[T,U](IEnumerable`1[] datastream, Func`3 function)+MoveNext() in c:\\code\\bts\\spark\\src\\client\\spark_historical\\Query\\Query.cs:line 133",
" at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)",
" at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)",
" at Submission#10.<<Initialize>>d__0.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
" at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
]
}
],
"outputs": [],
"source": [
"var algo_orders_cached = algo_orders.ToArray().OrderBy(x => x.order?.TransactTime ?? x.fill.TransactTime, \n",
" HistoricalExtensions.timestamp_comparer);\n",
@@ -242,7 +220,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 9,
"metadata": {
"scrolled": true
},
@@ -250,7 +228,7 @@
{
"data": {
"text/html": [
"100"
"0"
]
},
"metadata": {},
@@ -259,7 +237,7 @@
{
"data": {
"text/html": [
"35"
"0"
]
},
"metadata": {},
@@ -268,7 +246,7 @@
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>order</th><th>fill</th></tr></thead><tbody><tr><td>0</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.777871200Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.777821200Z&quot;, order_side: Sell, order_status: Working, order_price: 129.46875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>1</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.777871200Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.777821200Z&quot;, order_side: Buy, order_status: Working, order_price: 129.421875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>2</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.787706300Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.787656300Z&quot;, order_side: Sell, order_status: Working, order_price: 129.484375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>3</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.788590500Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.788540500Z&quot;, order_side: Buy, order_status: Working, order_price: 129.4375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>4</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789026900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.788976900Z&quot;, order_side: Sell, order_status: Working, order_price: 129.46875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>5</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789026900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.788976900Z&quot;, order_side: Buy, order_status: Working, order_price: 129.421875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>6</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789093800Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.789043800Z&quot;, order_side: Sell, order_status: Working, order_price: 129.484375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>7</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789321200Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.789271200Z&quot;, order_side: Sell, order_status: Working, order_price: 129.46875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>8</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.790255800Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.790205800Z&quot;, order_side: Sell, order_status: Working, order_price: 129.484375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>9</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.790255800Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.790205800Z&quot;, order_side: Buy, order_status: Working, order_price: 129.4375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>10</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773164900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773114900Z&quot;, order_side: Sell, order_status: Working, order_price: 129.5, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>11</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773164900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773114900Z&quot;, order_side: Buy, order_status: Working, order_price: 129.453125, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>12</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773510Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773460Z&quot;, order_side: Buy, order_status: Working, order_price: 129.4375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>13</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773715700Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773665700Z&quot;, order_side: Buy, order_status: Working, order_price: 129.453125, order_size: 10, order_filled: 0 }</td><td>&lt;nullLine truncated
"<table><thead><tr><th><i>index</i></th><th>order</th><th>fill</th></tr></thead><tbody><tr><td>0</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000003, security_desc: ZNZ9-ZNH0, order_sending_time: &quot;2019-11-25T14:00:00.010532900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T14:00:00.010482900Z&quot;, order_side: Buy, order_status: Working, order_price: -0.0625, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>1</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000003, security_desc: ZNZ9-ZNH0, order_sending_time: &quot;2019-11-25T19:39:40.718202400Z&quot;, order_TRANSACT_time: &quot;2019-11-25T19:39:40.718152400Z&quot;, order_side: Buy, order_status: Working, order_price: -0.0546875, order_size: 4294533118, order_filled: 0 }</td><td>&lt;null&gt;</td></tr></tbody></table>"
]
},
"metadata": {},
@@ -291,7 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -300,19 +278,9 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>OrderUpdateAction</th><th>OrderId</th><th>SecurityDesc</th><th>MdOrderPriority</th><th>MdEntryPx</th><th>MdPriceLevel</th><th>MdDisplayQty</th><th>LastQty</th><th>MdEntryType</th><th>TradeDate</th><th>MsgSeqNum</th><th>SendingTime</th><th>TransactTime</th><th>MdEntry</th><th>OrderEntry</th></tr></thead><tbody><tr><td>0</td><td>New</td><td>846784119686</td><td>ZNH0</td><td>12365190692</td><td>129.359375</td><td>2</td><td>8</td><td>0</td><td>Bid</td><td>&quot;2019-11-25T00:00:00Z&quot;</td><td>4361037</td><td>&quot;2019-11-25T13:30:20.470246800Z&quot;</td><td>&quot;2019-11-25T13:30:20.470147Z&quot;</td><td>{ bts.history.MDEntry: MdUpdateAction: { bts.history.MDUpdateAction: value__: 49 }, MdEntryType: { bts.history.MDEntryType: value__: 48 }, SecurityId: 771691, Symbol: ZNH0, Rptseq: 463670, MdEntryPx: 129.359375, MdEntrySize: 1590, NumberOfOrders: 105, MdPriceLevel: 2, OpenCloseSettleFlag: 0, SettlePriceType: , AggressorSide: 0, MdTradeEntryId: , ReferenceId: 1, LastQty: 0, OrderId: 846784119686, MdOrderPriority: 12365190692, MdDisplayQty: 8 }</td><td>{ bts.history.OrderIDEntry: OrderId: 846784119686, MdDisplayQty: 8, MdOrderPriority: 12365190692, OrderUpdateAction: { bts.history.MDUpdateAction: value__: 48 }, ReferenceId: 1, LastQty: 0 }</td></tr><tr><td>1</td><td>Delete</td><td>846784119686</td><td>ZNH0</td><td>12365190692</td><td>129.359375</td><td>2</td><td>8</td><td>0</td><td>Bid</td><td>&quot;2019-11-25T00:00:00Z&quot;</td><td>4374305</td><td>&quot;2019-11-25T13:31:08.565352Z&quot;</td><td>&quot;2019-11-25T13:31:08.553691900Z&quot;</td><td>{ bts.history.MDEntry: MdUpdateAction: { bts.history.MDUpdateAction: value__: 49 }, MdEntryType: { bts.history.MDEntryType: value__: 48 }, SecurityId: 771691, Symbol: ZNH0, Rptseq: 464774, MdEntryPx: 129.359375, MdEntrySize: 1816, NumberOfOrders: 105, MdPriceLevel: 2, OpenCloseSettleFlag: 0, SettlePriceType: , AggressorSide: 0, MdTradeEntryId: , ReferenceId: 1, LastQty: 0, OrderId: 846784119686, MdOrderPriority: 12365190692, MdDisplayQty: 8 }</td><td>{ bts.history.OrderIDEntry: OrderId: 846784119686, MdDisplayQty: 8, MdOrderPriority: 12365190692, OrderUpdateAction: { bts.history.MDUpdateAction: value__: 50 }, ReferenceId: 1, LastQty: 0 }</td></tr></tbody></table>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"\n",
"//display(exchange_orders.Skip(20000).Take(100));\n",
@@ -321,7 +289,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {
"scrolled": true
},
@@ -340,7 +308,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -350,7 +318,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -359,7 +327,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -370,7 +338,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -380,7 +348,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {
"scrolled": true
},
@@ -394,37 +362,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"808923"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"544367"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"display(buy_spread);\n",
"display(sell_spread);\n",
@@ -433,19 +373,9 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"544367"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": []
},
{
@@ -181,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {
"scrolled": true
},
@@ -202,38 +202,16 @@
"var algo_params = \"size=10;tick_size=0.015625,0.0078125,0.015625;hedge_factors=1,-1,-1;profit_ticks=0.0078125\";\n",
"var algo_orders = algo_results<HistoryRow>(sink, fill_model, \n",
" algo_type, algo_params, \n",
" bts.api.Side.Undefined, 0.0, 10, \n",
" bts.api.Side.Buy, 0.0, 10, \n",
" security_list, //must match below\n",
" new[] {spdf}, latency_iceberg);"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [
{
"ename": "Unhandled Exception",
"evalue": "An item with the same key has already been added. Key: 846782689978",
"output_type": "error",
"traceback": [
"Unhandled Exception",
"An item with the same key has already been added. Key: 846782689978",
" at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)",
" at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](List`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)",
" at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)",
" at bts.history.KFillModel.validate_matches(IEnumerable`1 orders, List`1 matches) in c:\\code\\bts\\spark\\src\\client\\spark_historical\\FillModel\\KFillModel.cs:line 244",
" at bts.history.KFillModel.process_trade(Timestamp transact_time, String security_desc, TradeMessage trade_summary, IEnumerable`1 orders) in c:\\code\\bts\\spark\\src\\client\\spark_historical\\FillModel\\KFillModel.cs:line 169",
" at bts.history.Query.<>c__DisplayClass16_0`1.<algo_results>b__3(T[] valcache, Int32 min) in c:\\code\\bts\\spark\\src\\client\\spark_historical\\Query\\Query.cs:line 280",
" at bts.history.Query.stream_data[T,U](IEnumerable`1[] datastream, Func`3 function)+MoveNext() in c:\\code\\bts\\spark\\src\\client\\spark_historical\\Query\\Query.cs:line 133",
" at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)",
" at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)",
" at Submission#10.<<Initialize>>d__0.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
" at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)"
]
}
],
"outputs": [],
"source": [
"var algo_orders_cached = algo_orders.ToArray().OrderBy(x => x.order?.TransactTime ?? x.fill.TransactTime, \n",
" HistoricalExtensions.timestamp_comparer);\n",
@@ -242,39 +220,11 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"100"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"35"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>order</th><th>fill</th></tr></thead><tbody><tr><td>0</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.777871200Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.777821200Z&quot;, order_side: Sell, order_status: Working, order_price: 129.46875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>1</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.777871200Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.777821200Z&quot;, order_side: Buy, order_status: Working, order_price: 129.421875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>2</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.787706300Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.787656300Z&quot;, order_side: Sell, order_status: Working, order_price: 129.484375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>3</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.788590500Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.788540500Z&quot;, order_side: Buy, order_status: Working, order_price: 129.4375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>4</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789026900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.788976900Z&quot;, order_side: Sell, order_status: Working, order_price: 129.46875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>5</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789026900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.788976900Z&quot;, order_side: Buy, order_status: Working, order_price: 129.421875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>6</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789093800Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.789043800Z&quot;, order_side: Sell, order_status: Working, order_price: 129.484375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>7</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.789321200Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.789271200Z&quot;, order_side: Sell, order_status: Working, order_price: 129.46875, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>8</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.790255800Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.790205800Z&quot;, order_side: Sell, order_status: Working, order_price: 129.484375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>9</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:01:10.790255800Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:01:10.790205800Z&quot;, order_side: Buy, order_status: Working, order_price: 129.4375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>10</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000012, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773164900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773114900Z&quot;, order_side: Sell, order_status: Working, order_price: 129.5, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>11</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773164900Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773114900Z&quot;, order_side: Buy, order_status: Working, order_price: 129.453125, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>12</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773510Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773460Z&quot;, order_side: Buy, order_status: Working, order_price: 129.4375, order_size: 10, order_filled: 0 }</td><td>&lt;null&gt;</td></tr><tr><td>13</td><td>{ bts.history.Display+OrderDisplay: order_id: 1000013, security_desc: ZNH0, order_sending_time: &quot;2019-11-25T00:03:09.773715700Z&quot;, order_TRANSACT_time: &quot;2019-11-25T00:03:09.773665700Z&quot;, order_side: Buy, order_status: Working, order_price: 129.453125, order_size: 10, order_filled: 0 }</td><td>&lt;nullLine truncated
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"\n",
"display(algo_orders_cached.Where(x => x.fill != null).Sum(x => (int)x.fill.size) / 3.0);\n",
@@ -291,7 +241,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -300,19 +250,9 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table><thead><tr><th><i>index</i></th><th>OrderUpdateAction</th><th>OrderId</th><th>SecurityDesc</th><th>MdOrderPriority</th><th>MdEntryPx</th><th>MdPriceLevel</th><th>MdDisplayQty</th><th>LastQty</th><th>MdEntryType</th><th>TradeDate</th><th>MsgSeqNum</th><th>SendingTime</th><th>TransactTime</th><th>MdEntry</th><th>OrderEntry</th></tr></thead><tbody><tr><td>0</td><td>New</td><td>846784119686</td><td>ZNH0</td><td>12365190692</td><td>129.359375</td><td>2</td><td>8</td><td>0</td><td>Bid</td><td>&quot;2019-11-25T00:00:00Z&quot;</td><td>4361037</td><td>&quot;2019-11-25T13:30:20.470246800Z&quot;</td><td>&quot;2019-11-25T13:30:20.470147Z&quot;</td><td>{ bts.history.MDEntry: MdUpdateAction: { bts.history.MDUpdateAction: value__: 49 }, MdEntryType: { bts.history.MDEntryType: value__: 48 }, SecurityId: 771691, Symbol: ZNH0, Rptseq: 463670, MdEntryPx: 129.359375, MdEntrySize: 1590, NumberOfOrders: 105, MdPriceLevel: 2, OpenCloseSettleFlag: 0, SettlePriceType: , AggressorSide: 0, MdTradeEntryId: , ReferenceId: 1, LastQty: 0, OrderId: 846784119686, MdOrderPriority: 12365190692, MdDisplayQty: 8 }</td><td>{ bts.history.OrderIDEntry: OrderId: 846784119686, MdDisplayQty: 8, MdOrderPriority: 12365190692, OrderUpdateAction: { bts.history.MDUpdateAction: value__: 48 }, ReferenceId: 1, LastQty: 0 }</td></tr><tr><td>1</td><td>Delete</td><td>846784119686</td><td>ZNH0</td><td>12365190692</td><td>129.359375</td><td>2</td><td>8</td><td>0</td><td>Bid</td><td>&quot;2019-11-25T00:00:00Z&quot;</td><td>4374305</td><td>&quot;2019-11-25T13:31:08.565352Z&quot;</td><td>&quot;2019-11-25T13:31:08.553691900Z&quot;</td><td>{ bts.history.MDEntry: MdUpdateAction: { bts.history.MDUpdateAction: value__: 49 }, MdEntryType: { bts.history.MDEntryType: value__: 48 }, SecurityId: 771691, Symbol: ZNH0, Rptseq: 464774, MdEntryPx: 129.359375, MdEntrySize: 1816, NumberOfOrders: 105, MdPriceLevel: 2, OpenCloseSettleFlag: 0, SettlePriceType: , AggressorSide: 0, MdTradeEntryId: , ReferenceId: 1, LastQty: 0, OrderId: 846784119686, MdOrderPriority: 12365190692, MdDisplayQty: 8 }</td><td>{ bts.history.OrderIDEntry: OrderId: 846784119686, MdDisplayQty: 8, MdOrderPriority: 12365190692, OrderUpdateAction: { bts.history.MDUpdateAction: value__: 50 }, ReferenceId: 1, LastQty: 0 }</td></tr></tbody></table>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"\n",
"//display(exchange_orders.Skip(20000).Take(100));\n",
@@ -321,7 +261,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {
"scrolled": true
},
@@ -340,7 +280,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -350,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -359,7 +299,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -370,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -380,7 +320,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {
"scrolled": true
},
@@ -394,37 +334,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"808923"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"544367"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"display(buy_spread);\n",
"display(sell_spread);\n",
@@ -433,19 +345,9 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"544367"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": []
},
{
+1 -1
View File
@@ -272,7 +272,7 @@ namespace bts.history
if (curr.Trade != null)
{
var orders = mbo.Select(x => x.OrderEntry).ToList();
if(mbo.Count > 0) //If it's just one order CME won't send the repeating group
if(orders.Count == 0) //If it's just one order CME won't send the repeating group
{
var order = new OrderIDEntry { LastQty = curr.Trade.Size, OrderId = curr.Trade.OrderId };
orders.Add(order);