diff --git a/src/client/spark_historical/Algos/StackerAlgo.cs b/src/client/spark_historical/Algos/StackerAlgo.cs new file mode 100644 index 000000000..f066ba249 --- /dev/null +++ b/src/client/spark_historical/Algos/StackerAlgo.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using Google.Protobuf.WellKnownTypes; + +namespace bts.history +{ + public class StackerAlgo : CompositeAlgo + where Child : AlgoBase + { + Handle[] _handles; + Dictionary _status = new Dictionary(); + api.Side _myside; + double _myprice; + + public StackerAlgo(IOrderSink sink, string[] security_list) : base(sink, security_list) { } + public double profit_ticks { get; set; } = 1.0; + public uint stack_length { get; set; } = 2; + + protected override void do_launch(string[] security_list, api.Side side, double price, uint size, Timestamp asof, ulong order_key) + { + _myside = side; + _myprice = price; + _handles = new Handle[this.stack_length]; + for(int i = 0; i < this.stack_length; i++) + { + Handle h = null; + if(side == api.Side.Buy) + h = this.activate(security_list, side, price - i * this.profit_ticks, size, asof); + else + h = this.activate(security_list, side, price + i * this.profit_ticks, size, asof); + _handles[i] = h; + } + } + + protected override void do_update(Timestamp asof, double price, uint size = uint.MinValue) + { + bool better = (_myside == api.Side.Buy ? price > _myprice : price < _myprice); + if(price == _myprice) + return; + double px = _myprice; + if(better) + { + double tick = (_myside == api.Side.Buy ? this.profit_ticks : -this.profit_ticks); + for(int i = _handles.Length-1; i >= 0; i--) + { + px += tick; + var mover = _handles[i]; + mover.update(asof, px, size); + for(int j = 1; j < _handles.Length; j++) + _handles[j] = _handles[j-1]; + _handles[0] = mover; + + if(Math.Abs(price - px) < this.profit_ticks/10.0) + break; + } + } + else + { + double tick = (_myside == api.Side.Buy ? -this.profit_ticks : this.profit_ticks); + for (int i = 0; i < _handles.Length; i++) + { + px += tick; + var mover = _handles[i]; + mover.update(asof, _myprice + tick * (_handles.Length - 1), size); + for (int j = 0; j < _handles.Length-1; j++) + _handles[j] = _handles[j+1]; + _handles[_handles.Length-1] = mover; + + if (Math.Abs(price - px) < this.profit_ticks / 10.0) + break; + } + } + _myprice = px; + } + + protected override void on_child_status(Handle child, OrderStatus stat) + { + _status[child] = stat; + //TODO fire off a status + } + } +} \ No newline at end of file diff --git a/src/client/spark_historical/Notebooks/roll_query.ipynb b/src/client/spark_historical/Notebooks/roll_query.ipynb index eaed22f6f..0ab64eaa6 100644 --- a/src/client/spark_historical/Notebooks/roll_query.ipynb +++ b/src/client/spark_historical/Notebooks/roll_query.ipynb @@ -189,7 +189,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "scrolled": true },