diff --git a/.vscode/launch.json b/.vscode/launch.json index 7f883d93c..e67c0bf81 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -105,13 +105,13 @@ "program": "${workspaceRoot}/build/Debug/bin/net471/sparkcmd.exe", "args": [ "historical", - "--rstart", "08/28/2019", - "--rend", "08/28/2019", - "--rsymbol", "ZN", + "--rstart", "09/03/2019", + "--rend", "09/03/2019", + "--rsymbol", "ZC", "--rsecuritytype", "Spread", - "--algo_security", "ZNU9-ZNZ9", + "--algo_security", "ZCU9-ZCZ9", "--algo_type", "PegCoverLean", - "--algo_params", "size=50;lean_factor=3.5;join_factor=3.0;lean_quantity=2000;top_quantity=2000;join_quantity=3000", + "--algo_params", "size=2;lean_factor=3.5;join_factor=3.0;top_factor=4.0" ], "cwd": "${workspaceRoot}", "stopAtEntry": false, diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs b/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs index 19907205f..1f6680ec0 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs @@ -5,7 +5,7 @@ using MathNet.Numerics.Statistics; namespace bts.history { - class JoinCoverLean : CoverAlgo< JoinAlgo, LeanAlgo > + class JoinCoverLean : CoverAlgo< JoinAlgo< TopAlgo >, LeanAlgo > { public JoinCoverLean() : base() {} } diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/CoverAlgo.cs b/src/client/sparkcmd/Cmd/Historical/Algos/CoverAlgo.cs index a8a18f674..acc3fcdad 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/CoverAlgo.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/CoverAlgo.cs @@ -1,6 +1,7 @@ +using System; +using System.Collections.Generic; using mbts.co2; using bts.api; -using System; namespace bts.history { @@ -9,6 +10,9 @@ namespace bts.history where Hedge : AlgoBase, new() { public CoverAlgo() : base() { } + + //algo state + int _payupcount; //algo parameters public uint hedge_ticks { get; set; } = 1; @@ -40,8 +44,23 @@ namespace bts.history if(!_hedgeout) return base.is_completed(); else - return false; - + return false; + } + + public override object report() + { + var c = _child.report(); + + int ih = _payupcount; + string sh = string.Empty; + foreach (var sub in _hedges.Values) + { + if (sub.report() is string sr) + sh += sr; + if (sub.report() is int ir) + ih += ir; + } + return $" {c} Hedge Payups {sh} {ih} Incomplete {_hedges.Values.Sum(x => x.is_completed() ? 0u : 1u) } "; } protected override void on_child_fill(AlgoBase child, FillEntry fill) @@ -62,7 +81,14 @@ namespace bts.history hedge.on_fill += (f) => { if (f.fully_filled) - _hedges.Remove(f.order.order_id); + { + if(_hedges.TryGetValue(f.order.order_id, out Hedge h)) + { + if(h.report() is int pay) + _payupcount += pay; + _hedges.Remove(f.order.order_id); + } + } }; hedge.on_cancel += id => _hedges.Remove(id); queue_reaction(send_hedge, _packet, _packet, (hedge, side, size, price)); diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/LimitOrder.cs b/src/client/sparkcmd/Cmd/Historical/Algos/LimitOrder.cs index 76379fec9..c03c1a87d 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/LimitOrder.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/LimitOrder.cs @@ -46,6 +46,8 @@ namespace bts.history } public override void cancel() { + if(_myorder == null) + return; this._order_book.cancel_my_order(_myorder.order_id); if(!_done) { diff --git a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs index 49745c21d..961c295ea 100644 --- a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs +++ b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs @@ -87,7 +87,6 @@ namespace bts.history public uint total_exchange_bought { get; set; } public uint total_exchange_sold { get; set; } public uint total_exchange_volume { get; set;} - public int better_order_filled, better_volume_fillled; public double last_trade_price { get; set; } public uint last_trade_size { get; set; } @@ -155,26 +154,31 @@ namespace bts.history 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_piq = buys.Where(x => x.queue_len > 0).LastOrDefault()?.piq ?? 0.0; - // stats.our_average_buy_q_len = buys.Where(x => x.queue_len > 0).LastOrDefault()?.queue_len ?? 0.0;; 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; + if(stats.our_total_buys > 0) + { + 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.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_piq = sells.Where(x => x.queue_len > 0).LastOrDefault()?.piq ?? 0.0; ; - // stats.our_average_sell_q_len = sells.Where(x => x.queue_len > 0).LastOrDefault()?.queue_len ?? 0.0; ; 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; + if (stats.our_total_sells > 0) + { + 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; + } stats.our_total_volume += this.tradelog.Sum( x => (int) x.size); + Log.assert(this.tradelog.Where(x => x.side == Side.Buy).Sum(x => (int)x.size) == + this.tradelog.Where(x => x.side == Side.Sell).Sum(x => (int)x.size)); + this.tradelog.Clear(); return stats; } @@ -388,11 +392,6 @@ namespace bts.history piq+1, count+1, $" {piq} of {count} "); fills.Add(f); } - else - { - stats.better_order_filled++; - stats.better_volume_fillled += (int)trade.LastQty; - } } } post_process_fill(fills); diff --git a/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs b/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs index 57b03fc8c..3c3e19d18 100644 --- a/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs +++ b/src/client/sparkcmd/Cmd/HistoricalDataCommand.cs @@ -107,6 +107,7 @@ namespace bts.history st = (SecurityType)Enum.Parse(typeof(SecurityType), o.replaysecurity, true); var rep = new HistoricalDataReplayer(); rep.replay(start, end, o.replaysymbol.ToUpper(), st, o).GetAwaiter().GetResult(); + Console.ReadKey(); return 0; }