public void cancelOrder(FlexTrade.Order o) { log.Info("Cancelling order #" + o.internalId); ibClient.CancelOrder(Convert.ToInt32(o.exchangeId)); if(ordersOrgFormat.ContainsKey(o.internalId)) { Order ord = ordersOrgFormat[o.internalId]; ord.status = Order.OrderStatus.CANCELLED; if(openOrders.ContainsKey(ord.internalId)) { orderInputQueue.Remove(openOrders[ord.internalId]); openOrders.Remove(ord.internalId); } ordersOrgFormat.Remove(ord.internalId); openOrderContracts.Remove(ord.internalId); } }
private Contract createContractFromProduct(FlexTrade.Product p) { Contract c = null; if (p is FlexTrade.Equity) { c = new Krs.Ats.IBNet.Contracts.Equity(p.symbol); } else { //throw exception!! We don't support other products } return c; }
//Take in an order in the canonical format, translate it, and add to the internal queue public int submitOrder(FlexTrade.Order o) { int currentID = orderIdCounter; //Must pass all orders through the risk filter to ensure that it is compliant try { log.Debug("Running order #" + o.internalId + " through risk filter."); riskFilter.isAcceptable(o); } catch(UnacceptableRiskException e) { log.Error("Order " + o.internalId + " rejected by risk filter"); //Alert any interested in hearing about orders caught by the risk filter if(RiskFilterFailure != null) RiskFilterFailure(e.riskFilterMessages); //Rethrow the messages so that the sender of the order knows it failed throw; } //If the order ID is still set to -1, then we haven't received the intial ID value from IB //We must wait until this is received. if (!isReadyToTakeOrders()) { log.Error("Manager not fully initialized."); return -1; } else { Contract contract = null; //Translate to the internal representation of an order Krs.Ats.IBNet.Order internalOrder = new Krs.Ats.IBNet.Order(); internalOrder.Action = (o.side.Equals(FlexTrade.Order.Side.BUY) ? ActionSide.Buy : ActionSide.Sell); internalOrder.OutsideRth = false; internalOrder.TotalQuantity = o.orderQuantity; internalOrder.OrderId = currentID; o.internalId = currentID; contract = createContractFromProduct(o.product); if (o is FlexTrade.LimitOrder) { internalOrder.OrderType = OrderType.Limit; internalOrder.LimitPrice = (decimal)((FlexTrade.LimitOrder)o).limitPrice; internalOrder.AuxPrice = 0; } else if (o is FlexTrade.MarketOrder) { internalOrder.OrderType = OrderType.Market; internalOrder.AuxPrice = 0; } else { //throw exception!! We don't support other order types } //Keeping track of all the objects involved orderInputQueue.Add(internalOrder); openOrderContracts.Add(currentID, contract); ordersOrgFormat.Add(currentID, o); //TODO For now this will be done on the same thread. It was created as a separate method to remind me that it would be //better to execute this asynchronously for improved performance in the future. log.Info("Sending order #" + internalOrder.OrderId + " for " + internalOrder.TotalQuantity + " of " + contract.Symbol); placeOrder(internalOrder, contract); requestMarketDataForProduct(o.product); o.status = Order.OrderStatus.SENT; //increment the order and ticker IDs for the next order and contract orderIdCounter++; return currentID; } }