From 9e26a2c651e7ee47db165ddb95f68f2fc10a5685 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Sat, 19 Oct 2019 21:42:08 -0400 Subject: [PATCH] adding top --- .vscode/launch.json | 2 +- .../spark_api/Co2/NativePriceHelpers.cs | 8 +- .../sparkcmd/Cmd/Historical/Algos/AlgoBase.cs | 34 +++-- .../sparkcmd/Cmd/Historical/Algos/Algos.cs | 10 +- .../Cmd/Historical/Algos/CompositeAlgo.cs | 23 ++-- .../sparkcmd/Cmd/Historical/Algos/JoinAlgo.cs | 73 +++++++---- .../sparkcmd/Cmd/Historical/Algos/LeanAlgo.cs | 23 ++-- .../sparkcmd/Cmd/Historical/Algos/PegAlgo.cs | 124 ------------------ .../Cmd/Historical/Algos/SimpleSpreader.cs | 17 --- .../sparkcmd/Cmd/Historical/Algos/TopAlgo.cs | 104 +++++++++++++++ .../Cmd/Historical/Analyzers/AlgoAnalyzer.cs | 32 ++--- .../Cmd/Historical/Analyzers/OrderBook.cs | 36 ++--- 12 files changed, 245 insertions(+), 241 deletions(-) delete mode 100644 src/client/sparkcmd/Cmd/Historical/Algos/PegAlgo.cs delete mode 100644 src/client/sparkcmd/Cmd/Historical/Algos/SimpleSpreader.cs create mode 100644 src/client/sparkcmd/Cmd/Historical/Algos/TopAlgo.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 571322d63..4c0e80121 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -111,7 +111,7 @@ "--rsecuritytype", "Spread", "--algo_security", "ZNU9-ZNZ9", "--algo_type", "PegCoverLean", - "--algo_params", "size=50;lean_factor=3.5;join_factor=3.0;lean_quantity=30000", + "--algo_params", "size=50;lean_factor=3.5;join_factor=3.0;lean_quantity=10000;top_quantity=3000;join_quantity=3000", ], "cwd": "${workspaceRoot}", "stopAtEntry": false, diff --git a/src/client/spark_api/Co2/NativePriceHelpers.cs b/src/client/spark_api/Co2/NativePriceHelpers.cs index 8580cc7b7..a49ab893e 100644 --- a/src/client/spark_api/Co2/NativePriceHelpers.cs +++ b/src/client/spark_api/Co2/NativePriceHelpers.cs @@ -77,14 +77,14 @@ namespace mbts.co2 { } - public static IEnumerable all_bids(this bts.co2.BookMessage book) + public static IList all_bids(this bts.co2.BookMessage book) { - var lev = book.Bids.Select(x => NativePriceHelpers.to_double(x.Mantissa, (sbyte)x.Exponent)); + var lev = book.Bids.Select(x => NativePriceHelpers.to_double(x.Mantissa, (sbyte)x.Exponent)).ToList(); return lev; } - public static IEnumerable all_asks(this bts.co2.BookMessage book) + public static IList all_asks(this bts.co2.BookMessage book) { - var lev = book.Asks.Select(x => NativePriceHelpers.to_double(x.Mantissa, (sbyte)x.Exponent)); + var lev = book.Asks.Select(x => NativePriceHelpers.to_double(x.Mantissa, (sbyte)x.Exponent)).ToList(); return lev; } } diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/AlgoBase.cs b/src/client/sparkcmd/Cmd/Historical/Algos/AlgoBase.cs index 662ebcdfc..d8d6531b3 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/AlgoBase.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/AlgoBase.cs @@ -12,7 +12,8 @@ namespace bts.history { class Catalyst { - public MarketDataIncrementalRefresh packet; + public co2.BookMessage pre_book; + public MarketDataIncrementalRefresh pre_packet; public MarketDataIncrementalRefresh last_packet; } @@ -30,6 +31,7 @@ namespace bts.history public string security_desc { get; private set; } public Bucket bucket { get; private set;} public Action on_fill; + public Action on_cancel; protected OrderBook _order_book; List _catalysts = new List(); @@ -42,11 +44,16 @@ namespace bts.history public AlgoBase() { on_fill = new Action(f => {}); + on_cancel = new Action(() => {}); } readonly Dictionary _algo_params = new Dictionary(); public IReadOnlyDictionary algo_params => _algo_params; - + + [Obsolete("you can't have the order book", true)] + public virtual OrderBook access_orderbook(T friend) { return null; } + public virtual OrderBook access_orderbook(AlgoAnalyzer friend) { return this._order_book; } + public virtual void init(OrderBook shared_book, Bucket bucket, string security_desc) { _order_book = shared_book; @@ -98,31 +105,31 @@ namespace bts.history _order_book.process_md(md, filter_md, debug_fix); } - public virtual void analyze_with_latency(MarketDataIncrementalRefresh packet) - { + public virtual void analyze_with_latency(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet) + { if(disable_latency_on_md || this.bucket.LowerBound == 0) { - on_md(packet, _lastpacket, filter_md); + on_md(pre_book, pre_packet, _lastpacket); } else { List removes = new List(); foreach (var cat in _catalysts) { - var delta = (packet.timestamp() - cat.packet.timestamp()); + var delta = (pre_packet.timestamp() - cat.pre_packet.timestamp()); double nanos = delta.Seconds * 1e9 + delta.Nanos; if (nanos >= this.bucket.LowerBound) { - on_md(cat.packet, cat.last_packet, filter_md); + on_md(cat.pre_book, cat.pre_packet, cat.last_packet); removes.Add(cat); } } foreach (var rr in removes) _catalysts.Remove(rr); - queue_catalyst(packet, _lastpacket); + queue_catalyst(pre_book, pre_packet, _lastpacket); } - _lastpacket = packet; + _lastpacket = pre_packet; } public virtual void execute_with_latency(MarketDataIncrementalRefresh packet) @@ -151,15 +158,15 @@ namespace bts.history } - protected virtual bool filter_md(MDEntry x, OrderIDEntry associated_order = null) + public virtual bool filter_md(MDEntry x, OrderIDEntry associated_order = null) { bool yes = _filter.ContainsKey(x.Symbol); return yes; } - protected void queue_catalyst(MarketDataIncrementalRefresh packet, MarketDataIncrementalRefresh last_packet) + protected void queue_catalyst(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet, MarketDataIncrementalRefresh last_packet) { - _catalysts.Add(new Catalyst { packet = packet, last_packet = last_packet }); + _catalysts.Add(new Catalyst { pre_book = pre_book, pre_packet = pre_packet, last_packet = last_packet }); } protected void queue_reaction(ReactionFunction reaction_function, MarketDataIncrementalRefresh packet, @@ -172,7 +179,6 @@ namespace bts.history public abstract OrderEntry update(double price, uint size = uint.MinValue); public abstract void cancel(); public abstract bool is_completed(); - protected virtual void on_md(MarketDataIncrementalRefresh pre_book_packet, MarketDataIncrementalRefresh last_packet, - Func filter) {} + protected virtual void on_md(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet, MarketDataIncrementalRefresh last_packet) {} } } \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs b/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs index 7b70aba29..0c92ed577 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/Algos.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; +using MathNet.Numerics.Statistics; namespace bts.history { @@ -9,8 +10,13 @@ namespace bts.history public JoinCoverLean() : base() {} } - class PegCoverLean : CoverAlgo, LeanAlgo> + class PegCoverLean : CoverAlgo< TopAlgo< JoinAlgo > , LeanAlgo> { - public PegCoverLean() : base() { } + public PegCoverLean() : base() {} + public override void init(OrderBook book, Bucket bucket, string security_desc) + { + base.init(book, bucket, security_desc); + this.set_param(nameof(JoinAlgo.initial_pile), true.ToString()); + } } } \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/CompositeAlgo.cs b/src/client/sparkcmd/Cmd/Historical/Algos/CompositeAlgo.cs index 908eded97..99df8de37 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/CompositeAlgo.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/CompositeAlgo.cs @@ -21,8 +21,10 @@ namespace bts.history _child.init(book, bucket, security_desc); _child.on_fill += f => this.on_fill(f); _child.on_fill += f => on_child_fill(_child, f); + _child.on_cancel += () => this.on_cancel(); + _child.on_cancel += () => on_child_cancel(_child); } - public override void init(Bucket bucket, string security_desc) + public sealed override void init(Bucket bucket, string security_desc) { init(new OrderBook(), bucket, security_desc); } @@ -43,6 +45,7 @@ namespace bts.history } protected virtual void on_child_fill(AlgoBase child, FillEntry fill) {} + protected virtual void on_child_cancel(AlgoBase child) { } } abstract class CompositeAlgo : CompositeBase @@ -60,7 +63,7 @@ namespace bts.history { var b = base.report(); var c = _child.report(); - return $"{b} {c} Stats: {Stringizer.to_string(_order_book.stats)}"; + return $"{b} {c}"; } public override void set_param(string name, string val) @@ -69,10 +72,10 @@ namespace bts.history _child.set_param(name, val); } - public override void analyze_with_latency(MarketDataIncrementalRefresh packet) + public override void analyze_with_latency(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet) { - base.analyze_with_latency(packet); - _child.analyze_with_latency(packet); + base.analyze_with_latency(pre_book, pre_packet); + _child.analyze_with_latency(pre_book, pre_packet); } public override void execute_with_latency(MarketDataIncrementalRefresh packet) @@ -116,7 +119,7 @@ namespace bts.history if(sub.report() is int ir) ih += ir; } - return $"{b} {c} Hedge Payups {sh} {ih} Stats: {Stringizer.to_string(_order_book.stats)} "; + return $"{b} {c} Hedge Payups {sh} {ih} Incomplete {_hedges.Sum( x => x.is_completed() ? 0u : 1u) } "; } public override void set_param(string name, string val) @@ -127,12 +130,12 @@ namespace bts.history sub.set_param(name, val); } - public override void analyze_with_latency(MarketDataIncrementalRefresh packet) + public override void analyze_with_latency(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet) { - base.analyze_with_latency(packet); - _child.analyze_with_latency(packet); + base.analyze_with_latency(pre_book, pre_packet); + _child.analyze_with_latency(pre_book, pre_packet); foreach (var sub in _hedges) - sub.analyze_with_latency(packet); + sub.analyze_with_latency(pre_book, pre_packet); } public override void execute_with_latency(MarketDataIncrementalRefresh packet) diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/JoinAlgo.cs b/src/client/sparkcmd/Cmd/Historical/Algos/JoinAlgo.cs index d81c2020b..04cee2504 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/JoinAlgo.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/JoinAlgo.cs @@ -9,27 +9,32 @@ namespace bts.history { enum AlgoState { - WaitingForJoin, Joined, Done + Begin, WaitingForJoin, Joined, Done } //algo state AlgoState _state; bool _has_joined; co2.BookMessage _snapshot, _currentbook; - int _turncount; - uint _mysize; + int _turncount, _pilecount; api.Side _myside; + double _myprice; + uint _mysize; + public JoinAlgo() : base() {} //algo parameters + public bool initial_pile { get; set; } = false; public bool should_chase { get; set; } = true; public double join_factor { get; set; } = 1.0; + public uint join_quantity{ get; set; } = 0; public override OrderEntry activate(api.Side side, double price, uint size) { - _state = AlgoState.WaitingForJoin; - _mysize = size; + _state = AlgoState.Begin; _myside = side; + _myprice = price; + _mysize = size; return null; } @@ -40,7 +45,8 @@ namespace bts.history public override object report() { - return $"Turns { _turncount }"; + var b = base.report(); + return $"{b} Turns,Piles,JoinPrice={_turncount},{_pilecount},{_myprice}"; } protected override void on_child_fill(AlgoBase child, FillEntry fill) @@ -49,38 +55,52 @@ namespace bts.history _state = AlgoState.Done; } - protected override void on_md(MarketDataIncrementalRefresh pre_book_packet, MarketDataIncrementalRefresh last_packet, - Func filter) + protected override void on_child_cancel(AlgoBase child) + { + _state = AlgoState.Done; + } + + protected override void on_md(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet, + MarketDataIncrementalRefresh last_packet) { if(_snapshot == null) - { - _snapshot = _order_book.current_book?.Clone(); - OrderBook.keep_book(_snapshot, pre_book_packet, filter); //this book does not have this packet yet - } - if(_currentbook == null) - { - _currentbook = _order_book.current_book?.Clone(); - } - OrderBook.keep_book(_currentbook, pre_book_packet, filter); - + _snapshot = pre_book.Clone(); + _currentbook = pre_book; switch (_state) { - case AlgoState.WaitingForJoin: //first join + case AlgoState.Begin: + { + if(initial_pile) + { + var thresh = (this.join_factor == 0.0 ? this.join_quantity : this.join_factor * _mysize); + if(_currentbook.sidesize_at(_myside, 0) > thresh) + { + _pilecount++; + queue_reaction(deploy_join_function, pre_packet, last_packet, _currentbook.side_at(_myside, 0)); + _state = AlgoState.WaitingForJoin; + } + } + else + _state = AlgoState.WaitingForJoin; + } + break; + case AlgoState.WaitingForJoin: { bool turn = ((_myside == api.Side.Buy && _currentbook.side_at(_myside, 0) > _snapshot.side_at(_myside, 0)) || //new top level bid price greater than old, market turn (_myside == api.Side.Sell && _currentbook.side_at(_myside, 0) < _snapshot.side_at(_myside, 0))); //new top level ask price less than old, market turn if(!turn) - turn = check_orders_turn(pre_book_packet); + turn = check_orders_turn(pre_packet); else { - _order_book.validate_new_queue(this, pre_book_packet, filter); - } - if(turn && _currentbook.sidesize_at(_myside, 0) > (_mysize * join_factor)) + _order_book.validate_new_queue(this, pre_packet, filter_md); + } + var thresh = (this.join_factor == 0.0 ? this.join_quantity : this.join_factor * _mysize); + if(turn && _currentbook.sidesize_at(_myside, 0) > thresh) { _turncount++; - queue_reaction(deploy_join_function, pre_book_packet, last_packet, + queue_reaction(deploy_join_function, pre_packet, last_packet, _currentbook.side_at(_myside, 0)); if(this.should_chase) { @@ -124,7 +144,10 @@ namespace bts.history bool turn = ((_myside == api.Side.Buy && mdval.MdEntryPx > _snapshot.side_at(_myside, 0)) || (_myside == api.Side.Sell && mdval.MdEntryPx < _snapshot.side_at(_myside, 0))); if(turn) + { + Log.Testing.error("MBO ORDER TURN"); return true; + } } } return false; @@ -143,6 +166,7 @@ namespace bts.history if(neworder.size == 0) //this would imply an immediate fill Log.Testing.error("Join actually crossed the market"); _has_joined = true; + _myprice = joinprice; } else { @@ -151,6 +175,7 @@ namespace bts.history neworder = _child.activate(_myside, joinprice, _mysize); if(neworder.size == 0) //this would imply an immediate fill Log.Testing.error("Join actually crossed the market"); + _myprice = joinprice; } } } diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/LeanAlgo.cs b/src/client/sparkcmd/Cmd/Historical/Algos/LeanAlgo.cs index 06419520f..569a7582a 100644 --- a/src/client/sparkcmd/Cmd/Historical/Algos/LeanAlgo.cs +++ b/src/client/sparkcmd/Cmd/Historical/Algos/LeanAlgo.cs @@ -52,19 +52,18 @@ namespace bts.history _state = AlgoState.Done; } - protected override void on_md(MarketDataIncrementalRefresh pre_book_packet, MarketDataIncrementalRefresh last_packet, - Func filter) + protected override void on_child_cancel(AlgoBase child) + { + _state = AlgoState.Done; + } + + protected override void on_md(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet, + MarketDataIncrementalRefresh last_packet) { if (_snapshot == null) - { - _snapshot = _order_book.current_book?.Clone(); - OrderBook.keep_book(_snapshot, pre_book_packet, filter); //this book does not have this packet yet - } - if (_currentbook == null) - { - _currentbook = _order_book.current_book?.Clone(); - } - OrderBook.keep_book(_currentbook, pre_book_packet, filter); + _snapshot = pre_book.Clone(); + _currentbook = pre_book; + switch (_state) { case AlgoState.LeanWatch: @@ -89,7 +88,7 @@ namespace bts.history { _payupcount++; _state = AlgoState.Payup; - queue_reaction(payup_function, pre_book_packet, last_packet); + queue_reaction(payup_function, pre_packet, last_packet); } } break; diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/PegAlgo.cs b/src/client/sparkcmd/Cmd/Historical/Algos/PegAlgo.cs deleted file mode 100644 index 8dbd464e9..000000000 --- a/src/client/sparkcmd/Cmd/Historical/Algos/PegAlgo.cs +++ /dev/null @@ -1,124 +0,0 @@ - -using System; -using mbts.co2; - -namespace bts.history -{ - class PegAlgo : CompositeAlgo - where Child : AlgoBase, new() - { - enum AlgoState - { - WaitingForJoin, Joined, Done - } - - //algo state - AlgoState _state; - bool _has_joined; - co2.BookMessage _snapshot, _currentbook; - int _turncount, _pilecount; - uint _mysize; - double _myprice; - api.Side _myside; - - public PegAlgo() : base() {} - //algo parameters - - public override OrderEntry activate(api.Side side, double price, uint size) - { - _state = AlgoState.WaitingForJoin; - _mysize = size; - _myside = side; - return null; - } - - public override bool is_completed() - { - return _state == AlgoState.Done; - } - - public override object report() - { - return $"Piles,Turns={_pilecount},{ _turncount }"; - } - - protected override void on_child_fill(AlgoBase child, FillEntry fill) - { - if(fill.fully_filled) - _state = AlgoState.Done; - } - - protected override void on_md(MarketDataIncrementalRefresh pre_book_packet, MarketDataIncrementalRefresh last_packet, - Func filter) - { - if(_snapshot == null) - { - _snapshot = _order_book.current_book?.Clone(); - OrderBook.keep_book(_snapshot, pre_book_packet, filter); //this book does not have this packet yet - } - if(_currentbook == null) - { - _currentbook = _order_book.current_book?.Clone(); - } - OrderBook.keep_book(_currentbook, pre_book_packet, filter); - - switch (_state) - { - case AlgoState.WaitingForJoin: - { - bool turn = _currentbook.side_at(_myside, 0) != _snapshot.side_at(_myside, 0); - if(turn) - { - _pilecount++; - queue_reaction(deploy_join_function, pre_book_packet, last_packet, - _currentbook.side_at(_myside, 0)); - - bool realturn = ((_myside == api.Side.Buy && - _currentbook.side_at(_myside, 0) > _snapshot.side_at(_myside, 0)) || - (_myside == api.Side.Sell && - _currentbook.side_at(_myside, 0) < _snapshot.side_at(_myside, 0))); - if(realturn) - { - _order_book.validate_new_queue(this, pre_book_packet, filter); - _turncount++; - } - } - _snapshot = _currentbook.Clone(); - } - break; - case AlgoState.Done: - break; - default: - Log.assert(false, "invalid algo state"); - break; - } - } - - void deploy_join_function(MarketDataIncrementalRefresh packet, MarketDataIncrementalRefresh last_packet, object param) - { - if(_state == AlgoState.Joined || _state == AlgoState.WaitingForJoin) - { - Log.Testing.info("deploy_join_function " + this.bucket.UpperBound); - - double joinprice = (double) param; - if(!_has_joined) - { - var neworder = _child.activate(_myside, joinprice, _mysize); - _myprice = joinprice; - if(neworder.size == 0) //this would imply an immediate fill - Log.Testing.error("PegAlgo actually crossed the market"); - _has_joined = true; - } - else - { - var neworder = _child.update(joinprice); - if(neworder == null) - neworder = _child.activate(_myside, joinprice, _mysize); - _myprice = joinprice; - if(neworder.size == 0) //this would imply an immediate fill - Log.Testing.error("PegAlgo actually crossed the market"); - } - } - } - } -} \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/SimpleSpreader.cs b/src/client/sparkcmd/Cmd/Historical/Algos/SimpleSpreader.cs deleted file mode 100644 index 35dca5ce4..000000000 --- a/src/client/sparkcmd/Cmd/Historical/Algos/SimpleSpreader.cs +++ /dev/null @@ -1,17 +0,0 @@ - -using System; -using System.Collections.Generic; - -namespace bts.history -{ - class SimpleSpreader : CoverAlgo< JoinAlgo, LeanAlgo > - { - public SimpleSpreader() : base() {} - - // public override bool is_completed() - // { - // bool comp = _child.is_completed(); - // return comp; - // } - } -} \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/Algos/TopAlgo.cs b/src/client/sparkcmd/Cmd/Historical/Algos/TopAlgo.cs new file mode 100644 index 000000000..79fa6f343 --- /dev/null +++ b/src/client/sparkcmd/Cmd/Historical/Algos/TopAlgo.cs @@ -0,0 +1,104 @@ + +using System; +using mbts.co2; + +namespace bts.history +{ + class TopAlgo : CompositeAlgo + where Child : AlgoBase, new() + { + enum AlgoState + { + WaitingToCancel, CancelRequested, Done + } + + //algo state + AlgoState _state; + co2.BookMessage _currentbook; + int _topcount; + uint _mysize; + double _myprice; + api.Side _myside; + + public TopAlgo() : base() {} + //algo parameters + public double top_factor { get; set; } = 1.0; + public uint top_quantity { get; set; } = 0; + + public override OrderEntry activate(api.Side side, double price, uint size) + { + _state = AlgoState.WaitingToCancel; + _mysize = size; + _myside = side; + return base.activate(side, price, size); + } + + public override OrderEntry update(double price, uint size = uint.MinValue) + { + _myprice = price; + if(size > uint.MaxValue) + _mysize = size; + return base.update(price, size); + } + + public override bool is_completed() + { + return _state == AlgoState.Done; + } + + public override object report() + { + var b = base.report(); + return $"{b} Cancels { _topcount }"; + } + + protected override void on_child_fill(AlgoBase child, FillEntry fill) + { + if(fill.fully_filled) + _state = AlgoState.Done; + } + + protected override void on_child_cancel(AlgoBase child) + { + _state = AlgoState.Done; + } + + protected override void on_md(co2.BookMessage pre_book, MarketDataIncrementalRefresh pre_packet, + MarketDataIncrementalRefresh last_packet) + { + _currentbook = pre_book; + switch (_state) + { + case AlgoState.WaitingToCancel: + { + bool top = _currentbook.side_at(_myside, 0) == _myprice; + var thresh = (this.top_factor == 0.0 ? this.top_quantity : this.top_factor * _mysize); + if(top && _currentbook.sidesize_at(_myside, 0) < thresh) + { + queue_reaction(cancel_top_function , pre_packet, last_packet, + _currentbook.side_at(_myside, 0)); + _state = AlgoState.CancelRequested; + _topcount++; + } + } + break; + case AlgoState.CancelRequested: + break; + case AlgoState.Done: + break; + default: + Log.assert(false, "invalid algo state"); + break; + } + } + + void cancel_top_function(MarketDataIncrementalRefresh packet, MarketDataIncrementalRefresh last_packet, object param) + { + Log.assert(_state == AlgoState.WaitingToCancel); + Log.Testing.info("cancel_top_function " + this.bucket.UpperBound); + + cancel(); + _state = AlgoState.Done; + } + } +} \ No newline at end of file diff --git a/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs b/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs index ae07623b9..7b294eae7 100644 --- a/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs +++ b/src/client/sparkcmd/Cmd/Historical/Analyzers/AlgoAnalyzer.cs @@ -5,7 +5,7 @@ using MathNet.Numerics.Statistics; namespace bts.history { - class AlgoAnalyzer : IAnalyzer + sealed class AlgoAnalyzer : IAnalyzer { const double _max_nanos = 500_000.0; readonly DistributionEstimator _dist = new DistributionEstimator(); @@ -68,25 +68,27 @@ namespace bts.history { //check the market data for a 'catalyst' with latency foreach (var algo in _algos) - { - algo.Value.analyze_with_latency(md); - } - //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 order book with my order - } - foreach(var algo in _algos) - { - algo.Value.post_process(md, debug_fix); //maybe do the action if enough time has passed, update my books - } + { + var ob = algo.Value.access_orderbook(this); + co2.BookMessage pre_book = ob.current_book?.Clone(); + OrderBook.keep_book(pre_book, md, algo.Value.filter_md); + algo.Value.analyze_with_latency(pre_book, md); + //maybe do the action if enough time has passed, update order book with my order + algo.Value.execute_with_latency(md); + //update algo's order books + algo.Value.post_process(md, debug_fix); + } + if (_samples++ % 1000 == 0) { var dt = md.TradeDate.ToDateTime(); Console.WriteLine($"{_security_desc} {_samples } Trade date {dt.ToShortDateString()} {dt.ToShortTimeString()}"); - Console.WriteLine($" Best Stats: {_algos.First().Key.UpperBound} {_algos.First().Value.report() } "); - Console.WriteLine($" Worst Stats: {_algos.Last().Key.UpperBound} {_algos.Last().Value.report() } "); + var first = _algos.First().Value; var firstob = first.access_orderbook(this); + var last = _algos.Last().Value; var lastob = first.access_orderbook(this); + var lf = Environment.NewLine; + Console.WriteLine($"{first.GetType().Name} Best Stats: {_algos.First().Key.UpperBound} {first.report()}{lf}{Stringizer.to_string(firstob.stats)} "); + Console.WriteLine($"{last.GetType().Name} Worst Stats: {_algos.Last().Key.UpperBound} {last.report()}{lf}{Stringizer.to_string(lastob.stats)}"); Console.WriteLine($"{ _hist.ToString() }"); Console.WriteLine($"-----------------------------------------------------------------------------"); } diff --git a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs index 32cc1310e..4558431c4 100644 --- a/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs +++ b/src/client/sparkcmd/Cmd/Historical/Analyzers/OrderBook.cs @@ -104,11 +104,11 @@ namespace bts.history if(entry.MdPriceLevel == 1 && entry.MdEntryType == MDEntryType.Bid) { Log.assert(_mbo.TryGetValue(entry.MdEntryPx, out QueueType q)); - if(q.Where(x => x.Value.side == api.Side.Sell).Count() > pre_book_packet.OrderIdEntries.Count) + //at times the crossed orders take a few lines to get deleted from queue. + if(q.Where(x => x.Value.side == api.Side.Sell).Count() > (pre_book_packet.OrderIdEntries.Count + pre_book_packet.MdEntries.Count)) Log.Testing.warn("q should be empty"); - if (algo.bucket.LowerBound == 0 && q.Count > entry.NumberOfOrders) + if (algo.bucket.LowerBound == 0 && q.Count > 1) Log.Testing.warn("q should be empty"); - } } } @@ -133,24 +133,24 @@ 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.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_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_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_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_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); @@ -212,7 +212,7 @@ namespace bts.history List fills = new List(); if (myorder.side == Side.Buy) { - int level = 0; int nasks = NativePriceHelpers.all_asks(this.current_book).Count(); + int level = 0; int nasks = NativePriceHelpers.all_asks(this.current_book).Count; while (myorder.size > 0 && level < nasks) { var currask = this.current_book.ask_at(level); @@ -228,7 +228,7 @@ namespace bts.history } else { - int level = 0; int nbids= NativePriceHelpers.all_bids(this.current_book).Count(); + int level = 0; int nbids= NativePriceHelpers.all_bids(this.current_book).Count; while (myorder.size > 0 && level < nbids) { var currbid = this.current_book.bid_at(level); @@ -618,7 +618,7 @@ namespace bts.history if (!desc) { bts.Log.Testing.info("bad bid book " + debug_fix ?? ""); - //Log.assert(false, "bad bid book"); + //Log.assert(false, "bad bid book"); //can't really assert as book can be invalide in the opening / beginning of file states return false; } return desc;