private void ReadOrderStatus(IBSocket socket, ServerVersions version) { var id = socket.ReadInt(); var status = socket.ReadOrderStatus(); /* filled */ socket.ReadInt(); var balance = socket.ReadDecimal(); var avgPrice = socket.ReadDecimal(); var permId = version >= ServerVersions.V2 ? socket.ReadInt() : (int?)null; var parentId = version >= ServerVersions.V3 ? socket.ReadInt() : (int?)null; var lastTradePrice = version >= ServerVersions.V4 ? socket.ReadDecimal() : (decimal?)null; var clientId = version >= ServerVersions.V5 ? socket.ReadInt() : (int?)null; var whyHeld = version >= ServerVersions.V6 ? socket.ReadStr() : null; var execMsg = new ExecutionMessage { ExecutionType = ExecutionTypes.Transaction, OriginalTransactionId = id, Balance = balance, OrderStatus = status, OrderState = status.ToOrderState(), HasOrderInfo = true, }; execMsg.SetAveragePrice(avgPrice); if (permId != null) execMsg.SetPermId(permId.Value); if (parentId != null) execMsg.Condition = new IBOrderCondition { ParentId = parentId.Value }; if (lastTradePrice != null) execMsg.SetLastTradePrice(lastTradePrice.Value); if (clientId != null) execMsg.SetClientId(clientId.Value); if (whyHeld != null) execMsg.SetWhyHeld(whyHeld); SendOutMessage(execMsg); }
private void ReadOpenOrder(IBSocket socket, ServerVersions version) { var transactionId = socket.ReadInt(); var contractId = version >= ServerVersions.V17 ? socket.ReadInt() : -1; var secCode = socket.ReadStr(); var type = socket.ReadSecurityType(); var expiryDate = socket.ReadExpiry(); var strike = socket.ReadDecimal(); var optionType = socket.ReadOptionType(); var multiplier = version >= ServerVersions.V32 ? socket.ReadMultiplier() : null; var boardCode = socket.ReadBoardCode(); var currency = socket.ReadCurrency(); secCode = version >= ServerVersions.V2 ? socket.ReadLocalCode(secCode) : null; var secClass = (version >= ServerVersions.V32) ? socket.ReadStr() : null; var ibCon = new IBOrderCondition(); // read order fields var direction = socket.ReadOrderSide(); var volume = socket.ReadDecimal(); OrderTypes orderType; IBOrderCondition.ExtendedOrderTypes? extendedType; socket.ReadOrderType(out orderType, out extendedType); ibCon.ExtendedType = extendedType; var price = socket.ReadDecimal(); ibCon.StopPrice = socket.ReadDecimal(); var expiration = socket.ReadStr(); ibCon.Oca.Group = socket.ReadStr(); var portfolio = socket.ReadStr(); ibCon.IsOpenOrClose = socket.ReadStr() == "O"; ibCon.Origin = (IBOrderCondition.OrderOrigins)socket.ReadInt(); var comment = socket.ReadStr(); var clientId = version >= ServerVersions.V3 ? socket.ReadInt() : (int?)null; int? permId = null; if (version >= ServerVersions.V4) { permId = socket.ReadInt(); if (version < ServerVersions.V18) { // will never happen /* order.m_ignoreRth = */ socket.ReadBool(); } else ibCon.OutsideRth = socket.ReadBool(); ibCon.Hidden = socket.ReadBool(); ibCon.SmartRouting.DiscretionaryAmount = socket.ReadDecimal(); } if (version >= ServerVersions.V5) ibCon.GoodAfterTime = socket.ReadNullDateTime(IBSocketHelper.TimeFormat); if (version >= ServerVersions.V6) { // skip deprecated sharesAllocation field socket.ReadStr(); } if (version >= ServerVersions.V7) { ibCon.FinancialAdvisor.Group = socket.ReadStr(); ibCon.FinancialAdvisor.Allocation = socket.ReadFinancialAdvisor(); ibCon.FinancialAdvisor.Percentage = socket.ReadStr(); ibCon.FinancialAdvisor.Profile = socket.ReadStr(); } var orderExpiryDate = version >= ServerVersions.V8 ? socket.ReadNullDateTime(IBSocketHelper.TimeFormat) : null; var visibleVolume = volume; if (version >= ServerVersions.V9) { ibCon.Agent = socket.ReadAgent(); ibCon.PercentOffset = socket.ReadDecimal(); ibCon.Clearing.SettlingFirm = socket.ReadStr(); ibCon.ShortSale.Slot = (IBOrderCondition.ShortSaleSlots)socket.ReadInt(); ibCon.ShortSale.Location = socket.ReadStr(); if (socket.ServerVersion == ServerVersions.V51) socket.ReadInt(); //exempt code else if (version >= ServerVersions.V23) ibCon.ShortSale.ExemptCode = socket.ReadInt(); ibCon.AuctionStrategy = (IBOrderCondition.AuctionStrategies)socket.ReadInt(); ibCon.StartingPrice = socket.ReadDecimal(); ibCon.StockRefPrice = socket.ReadDecimal(); ibCon.Delta = socket.ReadDecimal(); ibCon.StockRangeLower = socket.ReadDecimal(); ibCon.StockRangeUpper = socket.ReadDecimal(); visibleVolume = socket.ReadInt(); if (version < ServerVersions.V18) { // will never happen /* order.m_rthOnly = */ socket.ReadBool(); } ibCon.BlockOrder = socket.ReadBool(); ibCon.SweepToFill = socket.ReadBool(); ibCon.AllOrNone = socket.ReadBool(); ibCon.MinVolume = socket.ReadInt(); ibCon.Oca.Type = (IBOrderCondition.OcaTypes)socket.ReadInt(); ibCon.SmartRouting.ETradeOnly = socket.ReadBool(); ibCon.SmartRouting.FirmQuoteOnly = socket.ReadBool(); ibCon.SmartRouting.NbboPriceCap = socket.ReadDecimal(); } if (version >= ServerVersions.V10) { ibCon.ParentId = socket.ReadInt(); ibCon.TriggerMethod = (IBOrderCondition.TriggerMethods)socket.ReadInt(); } if (version >= ServerVersions.V11) { ibCon.Volatility.Volatility = socket.ReadDecimal(); ibCon.Volatility.VolatilityTimeFrame = socket.ReadVolatilityType(); if (version == ServerVersions.V11) { if (!socket.ReadBool()) ibCon.Volatility.ExtendedOrderType = IBOrderCondition.ExtendedOrderTypes.Empty; else ibCon.Volatility.OrderType = OrderTypes.Market; } else { OrderTypes volOrdertype; IBOrderCondition.ExtendedOrderTypes? volExtendedType; socket.ReadOrderType(out volOrdertype, out volExtendedType); ibCon.Volatility.OrderType = volOrdertype; ibCon.Volatility.ExtendedOrderType = volExtendedType; ibCon.Volatility.StopPrice = socket.ReadDecimal(); if (volExtendedType != IBOrderCondition.ExtendedOrderTypes.Empty) { if (version >= ServerVersions.V27) { ibCon.Volatility.ConId = socket.ReadInt(); ibCon.Volatility.SettlingFirm = socket.ReadStr(); var portfolioName = socket.ReadStr(); if (!portfolioName.IsEmpty()) ibCon.Volatility.ClearingPortfolio = portfolioName; ibCon.Volatility.ClearingIntent = socket.ReadStr(); } if (version >= ServerVersions.V31) { var isOpenOrCloseStr = socket.ReadStr(); ibCon.Volatility.ShortSale.IsOpenOrClose = isOpenOrCloseStr == "?" ? (bool?)null : isOpenOrCloseStr.To<int>() == 1; ibCon.Volatility.IsShortSale = socket.ReadBool(); ibCon.Volatility.ShortSale.Slot = (IBOrderCondition.ShortSaleSlots)socket.ReadInt(); ibCon.Volatility.ShortSale.Location = socket.ReadStr(); } } } ibCon.Volatility.ContinuousUpdate = socket.ReadBool(); if (socket.ServerVersion == ServerVersions.V26) { ibCon.StockRangeLower = socket.ReadDecimal(); ibCon.StockRangeUpper = socket.ReadDecimal(); } ibCon.Volatility.IsAverageBestPrice = socket.ReadBool(); } if (version >= ServerVersions.V13) ibCon.TrailStopPrice = socket.ReadDecimal(); if (version >= ServerVersions.V30) ibCon.TrailStopVolumePercentage = socket.ReadNullDecimal(); if (version >= ServerVersions.V14) { ibCon.Combo.BasisPoints = socket.ReadDecimal(); ibCon.Combo.BasisPointsType = socket.ReadInt(); ibCon.Combo.LegsDescription = socket.ReadStr(); } if (version >= ServerVersions.V29) { var comboLegsCount = socket.ReadInt(); if (comboLegsCount > 0) { //contract.m_comboLegs = new Vector(comboLegsCount); for (var i = 0; i < comboLegsCount; ++i) { //int conId = socket.ReadInt(); //int ratio = socket.ReadInt(); //String action = socket.ReadStr(); //String exchange = socket.ReadStr(); //int openClose = socket.ReadInt(); //int shortSaleSlot = socket.ReadInt(); //String designatedLocation = socket.ReadStr(); //int exemptCode = socket.ReadInt(); //ComboLeg comboLeg = new ComboLeg(conId, ratio, action, exchange, openClose, // shortSaleSlot, designatedLocation, exemptCode); //contract.m_comboLegs.add(comboLeg); } } var orderComboLegsCount = socket.ReadInt(); if (orderComboLegsCount > 0) { //order.m_orderComboLegs = new Vector(orderComboLegsCount); for (var i = 0; i < orderComboLegsCount; ++i) { //var comboPrice = socket.ReadNullDecimal(); //OrderComboLeg orderComboLeg = new OrderComboLeg(comboPrice); //order.m_orderComboLegs.add(orderComboLeg); } } } if (version >= ServerVersions.V26) { var smartComboRoutingParamsCount = socket.ReadInt(); if (smartComboRoutingParamsCount > 0) { var @params = new List<Tuple<string, string>>(); for (var i = 0; i < smartComboRoutingParamsCount; ++i) @params.Add(Tuple.Create(socket.ReadStr(), socket.ReadStr())); ibCon.SmartRouting.ComboParams = @params; } } if (version >= ServerVersions.V15) { if (version >= ServerVersions.V20) { ibCon.Scale.InitLevelSize = socket.ReadNullInt(); ibCon.Scale.SubsLevelSize = socket.ReadNullInt(); } else { /* int notSuppScaleNumComponents = */ socket.ReadNullInt(); ibCon.Scale.InitLevelSize = socket.ReadNullInt(); } ibCon.Scale.PriceIncrement = socket.ReadNullDecimal(); } if (version >= ServerVersions.V28 && ibCon.Scale.PriceIncrement > 0) { ibCon.Scale.PriceAdjustValue = socket.ReadNullDecimal(); ibCon.Scale.PriceAdjustInterval = socket.ReadInt(); ibCon.Scale.ProfitOffset = socket.ReadNullDecimal(); ibCon.Scale.AutoReset = socket.ReadBool(); ibCon.Scale.InitPosition = socket.ReadNullInt(); ibCon.Scale.InitFillQty = socket.ReadNullInt(); ibCon.Scale.RandomPercent = socket.ReadBool(); } if (version >= ServerVersions.V24) socket.ReadHedge(ibCon); if (version >= ServerVersions.V25) ibCon.SmartRouting.OptOutSmartRouting = socket.ReadBool(); if (version >= ServerVersions.V19) { var portfolioName = socket.ReadStr(); if (!portfolioName.IsEmpty()) ibCon.Clearing.ClearingPortfolio = portfolioName; ibCon.Clearing.Intent = socket.ReadIntent(); } if (version >= ServerVersions.V22) ibCon.SmartRouting.NotHeld = socket.ReadBool(); if (version >= ServerVersions.V20) { if (socket.ReadBool()) { //UnderlyingComponent underComp = new UnderlyingComponent(); //underComp.ContractId = socket.ReadInt(); //underComp.Delta = socket.ReadDecimal(); //underComp.Price = socket.ReadDecimal(); //contract.UnderlyingComponent = underComp; } } if (version >= ServerVersions.V21) { ibCon.Algo.Strategy = socket.ReadStr(); if (!ibCon.Algo.Strategy.IsEmpty()) { var algoParamsCount = socket.ReadInt(); if (algoParamsCount > 0) { var algoParams = new List<Tuple<string, string>>(); for (var i = 0; i < algoParamsCount; i++) algoParams.Add(Tuple.Create(socket.ReadStr(), socket.ReadStr())); ibCon.Algo.Params = algoParams; } } } //OrderState orderState = new OrderState(); OrderStatus? status = null; if (version >= ServerVersions.V16) { socket.ReadStr(); //order.WhatIf = !(string.IsNullOrEmpty(rstr) || rstr == "0"); status = socket.ReadOrderStatus(); //orderState.InitMargin = socket.ReadStr(); //orderState.MaintMargin = socket.ReadStr(); //orderState.EquityWithLoan = socket.ReadStr(); //orderState.IbCommission = socket.ReadNullDecimal(); //orderState.MinCommission = socket.ReadNullDecimal(); //orderState.MaxCommission = socket.ReadNullDecimal(); //orderState.CommissionCurrency = socket.ReadStr(); //orderState.WarningText = socket.ReadStr(); } var secId = new SecurityId { SecurityCode = secCode, BoardCode = GetBoardCode(boardCode), InteractiveBrokers = contractId, }; SendOutMessage(new SecurityMessage { SecurityId = secId, ExpiryDate = expiryDate, Strike = strike, OptionType = optionType, Class = secClass, SecurityType = type, Currency = currency, Multiplier = multiplier ?? 0, }); var orderMsg = new ExecutionMessage { ExecutionType = ExecutionTypes.Transaction, SecurityId = secId, OriginalTransactionId = transactionId, OrderType = orderType, Side = direction, OrderVolume = volume, OrderPrice = price, Condition = ibCon, ExpiryDate = orderExpiryDate, VisibleVolume = visibleVolume, PortfolioName = portfolio, Comment = comment, OrderStatus = status, OrderState = status?.ToOrderState(), HasOrderInfo = true, }; if (orderMsg.OrderState == OrderStates.Active || orderMsg.OrderState == OrderStates.Done) orderMsg.OrderId = transactionId; switch (expiration) { case "DAY": orderMsg.TimeInForce = TimeInForce.PutInQueue; break; case "GTC": //orderMsg.ExpiryDate = DateTimeOffset.MaxValue; break; case "IOC": orderMsg.TimeInForce = TimeInForce.CancelBalance; break; case "FOK": orderMsg.TimeInForce = TimeInForce.MatchOrCancel; break; case "GTD": break; case "OPG": ibCon.IsMarketOnOpen = true; break; default: throw new InvalidOperationException(LocalizedStrings.Str2515Params.Put(expiration)); } if (clientId != null) orderMsg.SetClientId(clientId.Value); if (permId != null) orderMsg.SetPermId(permId.Value); SendOutMessage(orderMsg); }
private void ReadMyTrade(IBSocket socket, ServerVersions version) { /* requestId */ if (version >= ServerVersions.V7) socket.ReadInt(); // http://www.interactivebrokers.com/en/software/api/apiguide/java/execution.htm var transactionId = socket.ReadInt(); //Handle the 2^31-1 == 0 bug if (transactionId == int.MaxValue) transactionId = 0; //Read Contract Fields var contractId = version >= ServerVersions.V5 ? socket.ReadInt() : -1; var secName = socket.ReadStr(); var type = socket.ReadSecurityType(); var expiryDate = socket.ReadExpiry(); var strike = socket.ReadDecimal(); var optionType = socket.ReadOptionType(); var multiplier = version >= ServerVersions.V9 ? socket.ReadMultiplier() : null; var boardCode = socket.ReadBoardCode(); var currency = socket.ReadCurrency(); var secCode = socket.ReadLocalCode(secName); var secClass = (version >= ServerVersions.V10) ? socket.ReadStr() : null; var tradeId = socket.ReadStr(); var time = socket.ReadDateTime("yyyyMMdd HH:mm:ss"); var portfolio = socket.ReadStr(); /* exchange */ socket.ReadStr(); var side = socket.ReadTradeSide(); var volume = socket.ReadDecimal(); var price = socket.ReadDecimal(); var permId = version >= ServerVersions.V2 ? socket.ReadInt() : (int?)null; var clientId = version >= ServerVersions.V3 ? socket.ReadInt() : (int?)null; var liquidation = version >= ServerVersions.V4 ? socket.ReadInt() : (int?)null; var cumulativeQuantity = version >= ServerVersions.V6 ? socket.ReadInt() : (int?)null; var averagePrice = version >= ServerVersions.V6 ? socket.ReadDecimal() : (decimal?)null; var orderRef = version >= ServerVersions.V8 ? socket.ReadStr() : null; var evRule = version >= ServerVersions.V9 ? socket.ReadStr() : null; var evMultiplier = version >= ServerVersions.V9 ? socket.ReadDecimal() : (decimal?)null; var secId = new SecurityId { SecurityCode = secCode, BoardCode = GetBoardCode(boardCode), InteractiveBrokers = contractId, }; SendOutMessage(new SecurityMessage { SecurityId = secId, Name = secName, SecurityType = type, ExpiryDate = expiryDate, Strike = strike, OptionType = optionType, Currency = currency, Multiplier = multiplier ?? 0, Class = secClass }); // заявка была создана руками if (transactionId == 0) return; _secIdByTradeIds[tradeId] = secId; var execMsg = new ExecutionMessage { ExecutionType = ExecutionTypes.Transaction, OriginalTransactionId = transactionId, TradeStringId = tradeId, OriginSide = side, TradePrice = price, TradeVolume = volume, PortfolioName = portfolio, ServerTime = time, SecurityId = secId, HasTradeInfo = true, }; if (permId != null) execMsg.SetPermId(permId.Value); if (clientId != null) execMsg.SetClientId(clientId.Value); if (liquidation != null) execMsg.SetLiquidation(liquidation.Value); if (cumulativeQuantity != null) execMsg.SetCumulativeQuantity(cumulativeQuantity.Value); if (averagePrice != null) execMsg.SetAveragePrice(averagePrice.Value); if (orderRef != null) execMsg.SetOrderRef(orderRef); if (evRule != null) execMsg.SetEvRule(evRule); if (evMultiplier != null) execMsg.SetEvMultiplier(evMultiplier.Value); SendOutMessage(execMsg); }