public static CommonObjects.Order ToCommonOrder(Com.Lmax.Api.Order.Order order, string symbol, string brokerName) { var orderPrice = order.StopReferencePrice.HasValue ? order.StopReferencePrice.Value : 0m; if (order.OrderType == Com.Lmax.Api.Order.OrderType.LIMIT && order.LimitPrice.HasValue) { orderPrice = order.LimitPrice.Value; } else if (order.OrderType == Com.Lmax.Api.Order.OrderType.STOP_ORDER && order.StopPrice.HasValue) { orderPrice = order.StopPrice.Value; } return(new CommonObjects.Order(order.InstructionId, symbol) { AccountId = order.AccountId.ToString(), AvgFillPrice = order.FilledQuantity != 0 ? orderPrice : 0, BrokerName = brokerName, BrokerID = order.OrderId, CancelledQuantity = order.CancelledQuantity, Commission = order.Commission, CurrentPrice = orderPrice, FilledQuantity = order.FilledQuantity, OpenDate = GetDateFromTicksString(order.InstructionId), OpenQuantity = order.FilledQuantity, OrderSide = order.Quantity > 0 ? Side.Buy : Side.Sell, OrderType = ToCommonOrderType(order.OrderType), Price = order.OrderType != Com.Lmax.Api.Order.OrderType.MARKET ? orderPrice : 0, Quantity = order.Quantity, SLOffset = order.StopLossOffset, TimeInForce = ToCommonTIF(order.TimeInForce), TPOffset = order.StopProfitOffset, PlacedDate = DateTime.UtcNow }); }
private static OrderState GetStateForOrder(Order.Order order) { if (order.CancelledQuantity == 0 && order.FilledQuantity == 0 && order.Quantity != 0) { return(OrderState.Working); } if (order.FilledQuantity != 0) { return(OrderState.Filled); } return(OrderState.Unknown); }
private void NotifyExecutions(Com.Lmax.Api.Order.Order order) { foreach (ExecutionBuilder executionBuilder in (IEnumerable <ExecutionBuilder>) this._executionEventHandler.GetExecutionBuilders()) { executionBuilder.Order(order); Execution execution = executionBuilder.NewInstance(); if (OrderStateEventHandler.IsExecutionForOrder(order, execution) && this.ExecutionEvent != null) { this.ExecutionEvent(execution); } } this._executionEventHandler.Clear(); }
private void OrderEventListener(Order.Order order) { OrderState stateForOrder = GetStateForOrder(order); Console.WriteLine("State for order: {0:d}, state: {1}", order.InstructionId, stateForOrder); if (order.InstructionId == _buyOrderTracker.InstructionId) { _buyOrderTracker.OrderState = stateForOrder; } else if (order.InstructionId == _sellOrderTracker.InstructionId) { _sellOrderTracker.OrderState = stateForOrder; } }
private static bool IsExecutionForOrder(Com.Lmax.Api.Order.Order order, Execution execution) { Decimal quantity = order.Quantity; int num1 = quantity.CompareTo(new Decimal(0)); quantity = execution.Quantity; int num2 = quantity.CompareTo(new Decimal(0)); int num3; if (num1 != num2) { Decimal num4 = order.Quantity; int num5 = num4.CompareTo(new Decimal(0)); num4 = execution.CancelledQuantity; int num6 = num4.CompareTo(new Decimal(0)); num3 = num5 == num6 ? 1 : 0; } else { num3 = 1; } return(num3 != 0); }
private void OnSessionOrderChanged(LmaxOrder lmaxOrder) { var transactionId = TryParseTransactionId(lmaxOrder.InstructionId); if (transactionId == null) { return; } LmaxOrderCondition condition = null; decimal price = 0; OrderTypes orderType; switch (lmaxOrder.OrderType) { case OrderType.MARKET: orderType = OrderTypes.Market; break; case OrderType.LIMIT: orderType = OrderTypes.Limit; if (lmaxOrder.LimitPrice == null) { throw new ArgumentException(LocalizedStrings.Str3394Params.Put(transactionId), nameof(lmaxOrder)); } price = (decimal)lmaxOrder.LimitPrice; break; case OrderType.STOP_ORDER: case OrderType.STOP_LOSS_MARKET_ORDER: case OrderType.STOP_PROFIT_LIMIT_ORDER: orderType = OrderTypes.Conditional; if (lmaxOrder.StopPrice == null) { throw new ArgumentException(LocalizedStrings.Str3395Params.Put(transactionId), nameof(lmaxOrder)); } price = (decimal)lmaxOrder.StopPrice; condition = new LmaxOrderCondition { StopLossOffset = lmaxOrder.StopLossOffset, TakeProfitOffset = lmaxOrder.StopProfitOffset, }; break; case OrderType.CLOSE_OUT_ORDER_POSITION: case OrderType.CLOSE_OUT_POSITION: case OrderType.SETTLEMENT_ORDER: case OrderType.OFF_ORDERBOOK: case OrderType.REVERSAL: case OrderType.UNKNOWN: orderType = OrderTypes.Execute; break; default: throw new ArgumentOutOfRangeException(); } DateTimeOffset?expiryDate = null; var tif = StockSharpTimeInForce.PutInQueue; switch (lmaxOrder.TimeInForce) { case LmaxTimeInForce.FillOrKill: tif = StockSharpTimeInForce.MatchOrCancel; break; case LmaxTimeInForce.ImmediateOrCancel: tif = StockSharpTimeInForce.CancelBalance; break; case LmaxTimeInForce.GoodForDay: expiryDate = DateTime.Today.ApplyTimeZone(TimeZoneInfo.Utc); break; case LmaxTimeInForce.GoodTilCancelled: break; case LmaxTimeInForce.Unknown: throw new NotSupportedException(LocalizedStrings.Str3396Params.Put(lmaxOrder.TimeInForce, transactionId.Value, lmaxOrder.OrderId)); default: throw new InvalidOperationException(LocalizedStrings.Str3397Params.Put(lmaxOrder.TimeInForce, transactionId.Value, lmaxOrder.OrderId)); } var msg = new ExecutionMessage { SecurityId = new SecurityId { Native = lmaxOrder.InstrumentId }, OriginalTransactionId = transactionId.Value, OrderType = orderType, OrderPrice = price, Condition = condition, OrderVolume = lmaxOrder.Quantity.Abs(), Side = lmaxOrder.Quantity > 0 ? Sides.Buy : Sides.Sell, Balance = lmaxOrder.Quantity - lmaxOrder.FilledQuantity, PortfolioName = lmaxOrder.AccountId.To <string>(), TimeInForce = tif, ExpiryDate = expiryDate, OrderStringId = lmaxOrder.OrderId, ExecutionType = ExecutionTypes.Transaction, Commission = lmaxOrder.Commission, ServerTime = CurrentTime.Convert(TimeZoneInfo.Utc), HasOrderInfo = true, }; msg.OrderState = lmaxOrder.CancelledQuantity > 0 ? OrderStates.Done : (msg.Balance == 0 ? OrderStates.Done : OrderStates.Active); //msg.Action = lmaxOrder.CancelledQuantity > 0 // ? ExecutionActions.Canceled // : (lmaxOrder.FilledQuantity == 0 ? ExecutionActions.Registered : ExecutionActions.Matched); SendOutMessage(msg); }
private void OnSessionOrderChanged(LmaxOrder lmaxOrder) { var transactionId = TryParseTransactionId(lmaxOrder.InstructionId); if (transactionId == null) return; LmaxOrderCondition condition = null; decimal price = 0; OrderTypes orderType; switch (lmaxOrder.OrderType) { case OrderType.MARKET: orderType = OrderTypes.Market; break; case OrderType.LIMIT: orderType = OrderTypes.Limit; if (lmaxOrder.LimitPrice == null) throw new ArgumentException(LocalizedStrings.Str3394Params.Put(transactionId), nameof(lmaxOrder)); price = (decimal)lmaxOrder.LimitPrice; break; case OrderType.STOP_ORDER: case OrderType.STOP_LOSS_MARKET_ORDER: case OrderType.STOP_PROFIT_LIMIT_ORDER: orderType = OrderTypes.Conditional; if (lmaxOrder.StopPrice == null) throw new ArgumentException(LocalizedStrings.Str3395Params.Put(transactionId), nameof(lmaxOrder)); price = (decimal)lmaxOrder.StopPrice; condition = new LmaxOrderCondition { StopLossOffset = lmaxOrder.StopLossOffset, TakeProfitOffset = lmaxOrder.StopProfitOffset, }; break; case OrderType.CLOSE_OUT_ORDER_POSITION: case OrderType.CLOSE_OUT_POSITION: case OrderType.SETTLEMENT_ORDER: case OrderType.OFF_ORDERBOOK: case OrderType.REVERSAL: case OrderType.UNKNOWN: orderType = OrderTypes.Execute; break; default: throw new ArgumentOutOfRangeException(); } DateTimeOffset? expiryDate = null; var tif = StockSharpTimeInForce.PutInQueue; switch (lmaxOrder.TimeInForce) { case LmaxTimeInForce.FillOrKill: tif = StockSharpTimeInForce.MatchOrCancel; break; case LmaxTimeInForce.ImmediateOrCancel: tif = StockSharpTimeInForce.CancelBalance; break; case LmaxTimeInForce.GoodForDay: expiryDate = DateTime.Today.ApplyTimeZone(TimeZoneInfo.Utc); break; case LmaxTimeInForce.GoodTilCancelled: break; case LmaxTimeInForce.Unknown: throw new NotSupportedException(LocalizedStrings.Str3396Params.Put(lmaxOrder.TimeInForce, transactionId.Value, lmaxOrder.OrderId)); default: throw new InvalidOperationException(LocalizedStrings.Str3397Params.Put(lmaxOrder.TimeInForce, transactionId.Value, lmaxOrder.OrderId)); } var msg = new ExecutionMessage { SecurityId = new SecurityId { Native = lmaxOrder.InstrumentId }, OriginalTransactionId = transactionId.Value, OrderType = orderType, OrderPrice = price, Condition = condition, OrderVolume = lmaxOrder.Quantity.Abs(), Side = lmaxOrder.Quantity > 0 ? Sides.Buy : Sides.Sell, Balance = lmaxOrder.Quantity - lmaxOrder.FilledQuantity, PortfolioName = lmaxOrder.AccountId.To<string>(), TimeInForce = tif, ExpiryDate = expiryDate, OrderStringId = lmaxOrder.OrderId, ExecutionType = ExecutionTypes.Transaction, Commission = lmaxOrder.Commission, ServerTime = CurrentTime.Convert(TimeZoneInfo.Utc) }; msg.OrderState = lmaxOrder.CancelledQuantity > 0 ? OrderStates.Done : (msg.Balance == 0 ? OrderStates.Done : OrderStates.Active); //msg.Action = lmaxOrder.CancelledQuantity > 0 // ? ExecutionActions.Canceled // : (lmaxOrder.FilledQuantity == 0 ? ExecutionActions.Registered : ExecutionActions.Matched); SendOutMessage(msg); }
public ExecutionBuilder Order(Com.Lmax.Api.Order.Order order) { this._order = order; return(this); }
public override void EndElement(string endElement) { if (!"order".Equals(endElement)) { return; } OrderBuilder orderBuilder = new OrderBuilder(); string stringValue; this.TryGetValue("instructionId", out stringValue); long longValue1; this.TryGetValue("instrumentId", out longValue1); long longValue2; this.TryGetValue("accountId", out longValue2); Decimal dec1; this.TryGetValue("quantity", out dec1); Decimal dec2; this.TryGetValue("matchedQuantity", out dec2); Decimal dec3; this.TryGetValue("cancelledQuantity", out dec3); Decimal dec4; this.TryGetValue("commission", out dec4); orderBuilder.InstructionId(stringValue).OrderId(this.GetStringValue("orderId")).InstrumentId(longValue1).AccountId(longValue2).Quantity(dec1).FilledQuantity(dec2).CancelledQuantity(dec3).OrderType(this.GetStringValue("orderType")).Commission(dec4).TimeInForce(this.GetStringValue("timeInForce")); Decimal dec5; if (this.TryGetValue("price", out dec5)) { orderBuilder.Price(dec5); } Decimal dec6; if (this.TryGetValue("stopReferencePrice", out dec6)) { orderBuilder.StopReferencePrice(dec6); } Decimal dec7; if (this.TryGetValue("stopProfitOffset", out dec7)) { orderBuilder.StopProfitOffset(dec7); } Decimal dec8; if (this.TryGetValue("stopLossOffset", out dec8)) { orderBuilder.StopLossOffset(dec8); } Com.Lmax.Api.Order.Order order = orderBuilder.NewInstance(); if (this.ShouldEmitOrder(order) && this.OrderEvent != null) { this.OrderEvent(order); } this.NotifyExecutions(order); this.ResetAll(); }
private bool ShouldEmitOrder(Com.Lmax.Api.Order.Order order) { return(this._executionEventHandler.GetExecutionBuilders().Count == 0 || OrderStateEventHandler.IsExecutionForOrder(order, this._executionEventHandler.GetExecutionBuilders()[0].NewInstance())); }