This commit is contained in:
Kevin Darby committed 2020-01-01 18:13:57 -05:00
1 parent 9fd3563ef8
commit cbec2476be
2 files changed
+83 -1

No files matched your search

@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using Google.Protobuf.WellKnownTypes;
namespace bts.history
{
public class StackerAlgo<Child> : CompositeAlgo<Child>
where Child : AlgoBase
{
Handle[] _handles;
Dictionary<Handle, OrderStatus> _status = new Dictionary<Handle, OrderStatus>();
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<Child>(security_list, side, price - i * this.profit_ticks, size, asof);
else
h = this.activate<Child>(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
}
}
}
@@ -189,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {
"scrolled": true
},