looks like sim spreader works now

This commit is contained in:
Kevin Darby committed 2017-10-12 23:39:59 -04:00
1 parent 15e0472ae0
commit 6fb084ae8f
3 files changed
+40 -27

No files matched your search

@@ -14,17 +14,15 @@ namespace mbts.spark.orders
class CrossingHedgeOrderActor : OrderActorBase, ICancelable, IModifiable
{
readonly IMarketDataSource _md;
readonly IExchangeGateway _exchange;
readonly IDropCopy _dc;
OrderStatus _status;
IOrderHandle _handle;
public CrossingHedgeOrderActor(IActorFabric fabric, Order order) : base(fabric, order)
{
//TODO Multi exchange
fabric.find_wellknown_service<IMarketDataSource>(order.Security.Exchange, out _md);
fabric.find_wellknown_service<IExchangeGateway>(order.Security.Exchange, out _exchange);
fabric.find_wellknown_service<IDropCopy>(order.Security.Exchange, out _dc);
}
public override void on_spawn()
{
_disposables.Add(_md.subscribe_bbo(_order.Security).Subscribe(x =>
@@ -32,29 +30,27 @@ namespace mbts.spark.orders
if(x != null)
watch_hedges(x);
}));
_disposables.Add(_dc.subscribe_fills(new Predicate<Fill>(x => _order.Id.Equals(x.Order.Id))).Subscribe(x =>
{
_fills_sink.OnNext(x);
}));
_disposables.Add(_dc.subscribe_status(new Predicate<OrderStatus>(x => _order.Id.Equals(x.Order.Id))).Subscribe(x =>
{
_status = x;
_status_sink.OnNext(x);
if(is_done_state(x.Status))
exit();
}));
var clone = _order.Clone();
clone.LimitOrder = new LimitOrder();
_exchange.send_order(clone);
//_handle = spawn_limit_order(_order.Security, _order.Size, _order.Price, _order.Side, _order.TimeInForce);
_handle = create_order<LimitOrder>(_order.Security, _order.Size, _order.Price, _order.Side,
_order.TimeInForce, new LimitOrder(), _order.Id);
track_child(_handle);
_handle.spawn();
}
override protected void on_child_status(OrderStatus child_status, ref OrderStatus merged_status)
{
base.on_child_status(child_status, ref merged_status);
_status = child_status;
}
void watch_hedges(BBOType bbo)
{
if(_status != null)
if(_status != null && !is_final_state(_status.Status))
{
PriceSize bid_or_ask;
if(_status.Order.Side == Side.Buy)
if(_order.Side == Side.Buy)
bid_or_ask = bbo.Item2;
else
bid_or_ask = bbo.Item1;
@@ -62,25 +58,25 @@ namespace mbts.spark.orders
uint distance = Convert.ToUInt32(Math.Abs(bid_or_ask.price - _status.Order.Price) / compute_tick(_order.Security));
double tick = (_order.Side == Side.Buy ? 1.0 : -1.0) * compute_tick(_order.Security);
if(distance > _order.CrossingHedgeOrder.TicksThreshold)
_exchange.modify_price(_order.Id, _status.Order.Price + _order.CrossingHedgeOrder.MarketCrossTicks * tick);
_handle.modify_price(_status.Order.Price + _order.CrossingHedgeOrder.MarketCrossTicks * tick);
}
}
bool ICancelable.on_cancel()
{
_exchange.cancel_order(_order.Id);
_handle.cancel();
return true;
}
bool IModifiable.on_modify_price(double new_price)
{
_exchange.modify_price(_order.Id, new_price);
_handle.modify_price( new_price);
return true;
}
bool IModifiable.on_modify_size(uint new_size)
{
_exchange.modify_size(_order.Id, new_size);
_handle.modify_size(new_size);
return true;
}
}
@@ -118,6 +118,7 @@ namespace mbts.spark.orders
}
readonly ConcurrentBag<IDisposable> _hedge_disposables = new ConcurrentBag<IDisposable>();
async Task hedge_working_order(Fill fill)
{
if(_order.Security.Equals(fill.Order.Security))
@@ -162,7 +163,23 @@ namespace mbts.spark.orders
var hedge = create_order<CrossingHedgeOrder>(kv.Value.leg, hedge_size, hedge_price,
hedge_side, Order.Types.TimeInForce.Day, cross);
_hedges.Add(hedge.order_id);
track_child(hedge);
//there is a race here. the working leg comes back as filled, and the other working leg was cancelled before the hedge was sent
//this puts the parent order in a terminal state, no longer listening to fills, etc. This has the unintended side effect of
//'eating' the hedge leg fills
//track_child(hedge);
_children[hedge.order_id] = hedge;
_hedge_disposables.Add(hedge.order_status.Subscribe(x =>
{
_child_status[x.Order.Id] = x;
compute_status_internal(x);
}));
_hedge_disposables.Add(hedge.fills.Subscribe(x =>
{
_child_fills.Add(x);
compute_fill_internal(x);
}));
//reduce any working quantity by the amount of the hedge
//initially I meant to reduce working on hedge fills, but now that I see it in action, there's too many orders out if we do it that way
await reduce_working(kv.Value.leg, hedge_size, hedge_side);
@@ -82,10 +82,10 @@ namespace mbts.spark.orders
protected IOrderHandle create_order<T>(Security sec, uint size, double price, Side side,
Order.Types.TimeInForce tif = Order.Types.TimeInForce.Day,
T algo_order_params = default(T)) where T : new()
T algo_order_params = default(T), OrderID id = null) where T : new()
{
Order ord = ActorMapping.create_order_of_type<T>(algo_order_params);
ord.Id = generate_order_id();
ord.Id = id ?? generate_order_id();
ord.Security = sec;
ord.Price = price;
ord.Size = size;