shaping up
This commit is contained in:
1 parent
161545b864
commit
23995e5cc6
6 files changed
+84
-42
No files matched your search
Vendored
+3
-3
@@ -108,10 +108,10 @@
|
||||
"--rstart", "09/30/2019",
|
||||
"--rend", "09/30/2019",
|
||||
"--rsymbol", "ZC",
|
||||
"--rsecuritytype", "Future",
|
||||
"--algo_security", "ZCZ9",
|
||||
"--rsecuritytype", "Spread",
|
||||
"--algo_security", "ZCZ9-ZCH0",
|
||||
"--algo_type", "JoinCoverLean",
|
||||
"--algo_params", "size=2;lean_factor=3.5;join_factor=3.0",
|
||||
"--algo_params", "size=5;lean_factor=3.5;join_factor=3.0",
|
||||
],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"stopAtEntry": false,
|
||||
|
||||
@@ -111,7 +111,14 @@ namespace bts.history
|
||||
rx = _reactions.Dequeue();
|
||||
rx.function(rx.packet, rx.catalyst, rx.param);
|
||||
}
|
||||
}
|
||||
// _count++;
|
||||
// if(nanos > 50_000)
|
||||
// _count_over_max++;
|
||||
// if(_count % 10 == 1)
|
||||
// {
|
||||
// Console.WriteLine("count, over 50us count" + _count + ", " + _count_over_max);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void post_process(MarketDataIncrementalRefresh packet, string debug_fix = null)
|
||||
|
||||
@@ -128,7 +128,7 @@ namespace bts.history
|
||||
if(sub.report() is int ir)
|
||||
ih += ir;
|
||||
}
|
||||
return $"{b} {c} Hedge Report {sh} {ih} Stats: {Stringizer.to_string(_order_book.stats)} ";
|
||||
return $"{b} {c} Hedge Payups {sh} {ih} Stats: {Stringizer.to_string(_order_book.stats)} ";
|
||||
}
|
||||
|
||||
public override void set_param(string name, string val)
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace bts.history
|
||||
//algo state
|
||||
AlgoState _state;
|
||||
MDEntry _joined_md;
|
||||
co2.BookMessage _book_cache;
|
||||
co2.BookMessage _book_cache, _current_book;
|
||||
int _turncount;
|
||||
uint _mysize;
|
||||
api.Side _myside;
|
||||
@@ -64,41 +64,48 @@ namespace bts.history
|
||||
case AlgoState.Begin:
|
||||
{
|
||||
_book_cache = _order_book.current_book.Clone();
|
||||
_current_book = _order_book.current_book.Clone();
|
||||
if(_book_cache != null)
|
||||
_state = AlgoState.WaitingForJoin;
|
||||
}
|
||||
break;
|
||||
case AlgoState.WaitingForJoin: //first join
|
||||
{
|
||||
bool turn = ((_myside == api.Side.Buy &&
|
||||
_order_book.current_book.side_at(_myside, 0) > _book_cache.side_at(_myside, 0)) || //new top level bid price greater than old, market turn
|
||||
(_myside == api.Side.Sell &&
|
||||
_order_book.current_book.side_at(_myside, 0) < _book_cache.side_at(_myside, 0))); //new top level ask price less than old, market turn
|
||||
if(turn && _order_book.current_book.sidesize_at(_myside, 0) > (_mysize * join_factor))
|
||||
OrderBook.keep_book(_current_book, curr);
|
||||
bool turn = ((_myside == api.Side.Buy &&
|
||||
_current_book.side_at(_myside, 0) > _book_cache.side_at(_myside, 0)) || //new top level bid price greater than old, market turn
|
||||
(_myside == api.Side.Sell &&
|
||||
_current_book.side_at(_myside, 0) < _book_cache.side_at(_myside, 0))); //new top level ask price less than old, market turn
|
||||
if(!turn)
|
||||
turn = check_orders_turn(packet);
|
||||
if(turn && _current_book.sidesize_at(_myside, 0) > (_mysize * join_factor))
|
||||
{
|
||||
_turncount++;
|
||||
_state = AlgoState.Joined;
|
||||
_joined_md = curr;
|
||||
queue_reaction(deploy_join_function, packet, curr);
|
||||
queue_reaction(deploy_join_function, packet, curr,
|
||||
_current_book.side_at(_myside, 0));
|
||||
}
|
||||
else
|
||||
_book_cache = _order_book.current_book.Clone();
|
||||
_book_cache = _current_book.Clone();
|
||||
}
|
||||
break;
|
||||
case AlgoState.Joined: //market moved away, chase
|
||||
{
|
||||
bool turnagain = ((_myside == api.Side.Buy &&
|
||||
_order_book.current_book.side_at(_myside, 0) > _joined_md.MdEntryPx) ||
|
||||
OrderBook.keep_book(_current_book, curr);
|
||||
bool turnagain = ((_myside == api.Side.Buy &&
|
||||
_current_book.side_at(_myside, 0) > _joined_md.MdEntryPx) ||
|
||||
(_myside == api.Side.Sell &&
|
||||
_order_book.current_book.side_at(_myside, 0) < _joined_md.MdEntryPx));
|
||||
_current_book.side_at(_myside, 0) < _joined_md.MdEntryPx));
|
||||
|
||||
if (turnagain && _order_book.current_book.sidesize_at(_myside, 0) > (_mysize * join_factor))
|
||||
if (turnagain && _current_book.sidesize_at(_myside, 0) > (_mysize * join_factor))
|
||||
{
|
||||
if(this.should_chase)
|
||||
{
|
||||
_turncount++;
|
||||
_joined_md = curr;
|
||||
queue_reaction(chase_join_function, packet, curr);
|
||||
queue_reaction(chase_join_function, packet, curr,
|
||||
_current_book.side_at(_myside, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -114,25 +121,50 @@ namespace bts.history
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void deploy_join_function(MarketDataIncrementalRefresh packet, MDEntry catalyst, object param)
|
||||
private bool check_orders_turn(MarketDataIncrementalRefresh packet)
|
||||
{
|
||||
foreach (var order in packet.OrderIdEntries)
|
||||
{
|
||||
// entries is indexed by 0 but ref is indexed by 1 :facepalm:
|
||||
if(order.ReferenceId == 0)
|
||||
continue;
|
||||
int mdidx = ((int)order.ReferenceId) - 1;
|
||||
Log.assert((mdidx <= packet.MdEntries.Count));
|
||||
|
||||
MDEntry mdval = packet.MdEntries[mdidx];
|
||||
if (!filter_md(mdval, order))
|
||||
continue;
|
||||
if(mdval.MdPriceLevel == 1 && order.OrderUpdateAction == MDUpdateAction.New || order.OrderUpdateAction == MDUpdateAction.Change)
|
||||
{
|
||||
bool turn = ((_myside == api.Side.Buy && mdval.MdEntryPx > _book_cache.side_at(_myside, 0)) ||
|
||||
(_myside == api.Side.Sell && mdval.MdEntryPx < _book_cache.side_at(_myside, 0)));
|
||||
if(turn)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void deploy_join_function(MarketDataIncrementalRefresh packet, MDEntry _, object param)
|
||||
{
|
||||
Log.assert(_state == AlgoState.Joined);
|
||||
Log.Testing.info("deploy_join_function " + this.bucket.UpperBound);
|
||||
|
||||
_child.activate(_myside, catalyst.MdEntryPx, _mysize);
|
||||
double joinprice = (double) param;
|
||||
_child.activate(_myside, joinprice, _mysize);
|
||||
}
|
||||
|
||||
void chase_join_function(MarketDataIncrementalRefresh packet, MDEntry catalyst, object param)
|
||||
void chase_join_function(MarketDataIncrementalRefresh packet, MDEntry _, object param)
|
||||
{
|
||||
Log.assert(_state == AlgoState.Joined);
|
||||
Log.Testing.info("chase_join_function " + this.bucket.UpperBound);
|
||||
|
||||
update(catalyst.MdEntryPx, uint.MinValue);
|
||||
double joinprice = (double)param;
|
||||
update(joinprice, uint.MinValue);
|
||||
}
|
||||
|
||||
void cancel_join_function(MarketDataIncrementalRefresh packet, MDEntry catalyst, object param)
|
||||
void cancel_join_function(MarketDataIncrementalRefresh packet, MDEntry _, object param)
|
||||
{
|
||||
Log.assert(_state == AlgoState.Joined);
|
||||
Log.Testing.info("cancel_join_function " + this.bucket.UpperBound);
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace bts.history
|
||||
}
|
||||
}
|
||||
|
||||
void payup_function(MarketDataIncrementalRefresh packet, MDEntry catalyst, object param)
|
||||
void payup_function(MarketDataIncrementalRefresh packet, MDEntry _, object param)
|
||||
{
|
||||
Log.assert(_state == AlgoState.Payup);
|
||||
double newprice = 0.0;
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace bts.history
|
||||
//orderid to { price, priority }
|
||||
readonly Dictionary< ulong, Tuple<double, ulong> > _mbokey = new Dictionary<ulong, Tuple<double, ulong>>();
|
||||
|
||||
ulong _myid = 0;
|
||||
long _myid = 0;
|
||||
readonly SortedList<ulong, OrderEntry> _myorders = new SortedList<ulong, OrderEntry>();
|
||||
|
||||
readonly BookMessage _mbp;
|
||||
@@ -130,20 +130,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 = 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 = 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);
|
||||
@@ -251,14 +255,13 @@ namespace bts.history
|
||||
|
||||
var ts = new Timestamp();
|
||||
var key = queue.Keys.LastOrDefault(999ul);
|
||||
_myid++;
|
||||
OrderEntry myorder = new OrderEntry(ts, _myid, key, 0, side, price, size, on_fill, tag);
|
||||
++_myid;
|
||||
OrderEntry myorder = new OrderEntry(ts, (ulong)_myid, key, 0, side, price, size, on_fill, tag);
|
||||
Log.Testing.info($"insert myorder {key} at price { price } piq { queue.Count } ");
|
||||
|
||||
post_process_fill( check_my_cross(myorder) );
|
||||
if(myorder.size > 0)
|
||||
_myorders.Add(_myid, myorder);
|
||||
return _myid;
|
||||
|
||||
_myorders.Add((ulong)_myid, myorder);
|
||||
post_process_fill(check_my_cross(myorder)); //reentrant -- hedge can come in here
|
||||
return (ulong)_myid;
|
||||
}
|
||||
|
||||
public OrderEntry change_my_order(ulong order_id, double price, uint size = uint.MinValue)
|
||||
@@ -351,7 +354,7 @@ namespace bts.history
|
||||
// }
|
||||
int piq = q.IndexOfKey(myorder.order_priority); int count = q.Count;
|
||||
var f = generate_fill(myorder, order.price, (uint)Math.Min(trade.LastQty, myorder.size),
|
||||
piq, count, $" {piq} of {count} ");
|
||||
piq+1, count+1, $" {piq} of {count} ");
|
||||
fills.Add(f);
|
||||
}
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user