/// <summary> /// When Order gets filled /// </summary> /// <param name="inboundOrder"></param> /// <param name="matchedOrder"></param> /// <param name="fillFlags"></param> /// <param name="filledPrice"></param> /// <param name="filledVolume"></param> public void OnOrderFilled(Order inboundOrder, Order matchedOrder, FillFlags fillFlags, Price filledPrice, Volume filledVolume) { /*// Publish the order that was just received from the user and got filled * OutputDisruptor.Publish(inboundOrder); * Log.Debug("Order Filled: " + inboundOrder.ToString() + " | Published to output Disruptor."); * * // Publish the order that was on the order book and got matched with the incoming order * OutputDisruptor.Publish(matchedOrder); * Log.Debug("Order Filled: " + matchedOrder.ToString() + " | Published to output Disruptor.");*/ }
/// <summary> /// When order gets filled /// </summary> /// <param name="inboundOrder"></param> /// <param name="matchedOrder"> </param> /// <param name="fillFlags"> </param> /// <param name="filledPrice"></param> /// <param name="filledVolume"></param> public void OnOrderFilled(Order inboundOrder, Order matchedOrder, FillFlags fillFlags, Price filledPrice, Volume filledVolume) { bool isFilled = false; if (inboundOrder.OrderType == OrderType.Limit) { isFilled = fillFlags == FillFlags.InboundFilled || fillFlags == FillFlags.BothFilled; _depth.FillOrder(inboundOrder.Price, filledPrice, filledVolume, isFilled, inboundOrder.OrderSide); } if (matchedOrder.OrderType == OrderType.Limit) { isFilled = fillFlags == FillFlags.MatchedFilled || fillFlags == FillFlags.BothFilled; _depth.FillOrder(matchedOrder.Price, filledPrice, filledVolume, isFilled, matchedOrder.OrderSide); } }
/// <summary> /// Update the order according to the fill /// </summary> /// <param name="matchedOrder"></param> /// <param name="inboundOrder"> </param> /// <param name="sendOrderAcceptedEvent"> </param> public void CrossOrders(Order inboundOrder, Order matchedOrder, bool sendOrderAcceptedEvent) { decimal filledQuantity = Math.Min(inboundOrder.OpenQuantity.Value, matchedOrder.OpenQuantity.Value); decimal filledPrice = matchedOrder.Price.Value; // Send Order accepted notification to subscribers if (sendOrderAcceptedEvent) { RaiseOrderAcceptedEvent(inboundOrder, filledPrice, filledQuantity); } Price inboundFilledCost = new Price(filledQuantity * filledPrice); inboundOrder.Fill(new Volume(filledQuantity), inboundFilledCost); Price matchedFilledCost = new Price(filledQuantity * filledPrice); matchedOrder.Fill(new Volume(filledQuantity), matchedFilledCost); FillFlags fillFlags = FillFlags.NetitherFilled; if (matchedOrder.OpenQuantity.Value == 0 && inboundOrder.OpenQuantity.Value == 0) { fillFlags = FillFlags.BothFilled; } else if (matchedOrder.OpenQuantity.Value == 0) { fillFlags = FillFlags.MatchedFilled; } else if (inboundOrder.OpenQuantity.Value == 0) { fillFlags = FillFlags.InboundFilled; } OrderFillEventsRaise(inboundOrder, matchedOrder, fillFlags, filledPrice, filledQuantity); // Create trade. The least amount of the two orders will be the trade's executed volume Trade trade = GenerateTrade(filledPrice, filledQuantity, matchedOrder, inboundOrder); if (TradeExecuted != null) { TradeExecuted(trade); } }
/// <summary> /// Called when the orders will cross and get filled, and will raise the events for order fill and order changed /// </summary> /// <param name="inboundOrder"></param> /// <param name="matchedOrder"></param> /// <param name="fillFlags"></param> /// <param name="filledPrice"></param> /// <param name="filledQuantity"></param> private void OrderFillEventsRaise(Order inboundOrder, Order matchedOrder, FillFlags fillFlags, decimal filledPrice, decimal filledQuantity) { // Event will be handled in the Depth Order book if (OrderFilled != null) { OrderFilled(inboundOrder, matchedOrder, fillFlags, new Price(filledPrice), new Volume(filledQuantity)); } // Event for the incoming order from the user that got filled // This event will be handled by the OrderListener, which will then dispatch the event on the ouput disruptor // to be journaled and then sent to the read side if (OrderChanged != null) { OrderChanged(inboundOrder); } // Event for the order on order book that got matched with the incoming order if (OrderChanged != null) { OrderChanged(matchedOrder); } }