Exemple #1
0
    protected void OnOpenOrder(int orderId, IBOrder order, IBContract contract, IBOrderState orderState)
    {
      if (OpenOrder != null)
        OpenOrder(this, new TWSOpenOrderEventArgs(this) {
          OrderId = orderId,
          Order = order,
          Contract = contract
        });

      if (OrderChanged != null) {
        OrderRecord or;
        if (_orderRecords.TryGetValue(orderId, out or))
        {
          OrderChanged(this, new TWSOrderChangedEventArgs(this, or) {
            ChangeType = IBOrderChangeType.OpenOrder,
            ReportedContract = contract,
            OpenOrder = order,
            OpenOrderState = orderState
          });
        }
      }
    }
Exemple #2
0
    private void ProcessOpenOrder()
    {
      // read version
      var version = _enc.DecodeInt();
      var order = new IBOrder { OrderId = _enc.DecodeInt() };

      // read contract fields
      var contract = new IBContract {
        ContractId = version >= 17 ? _enc.DecodeInt() : 0,
        Symbol = _enc.DecodeString(),
        SecurityType = _enc.DecodeEnum<IBSecurityType>(),
        Expiry = DecodeIBExpiry(_enc),
        Strike = _enc.DecodeDouble(),
        Right = _enc.DecodeString(),
        Exchange = _enc.DecodeString(),
        Currency = _enc.DecodeString(),
        LocalSymbol = (version >= 2) ? _enc.DecodeString() : null
      };

      // read other order fields
      order.Action = _enc.DecodeEnum<IBAction>();
      order.TotalQuantity = _enc.DecodeInt();
      order.OrderType = _enc.DecodeEnum<IBOrderType>();
      order.LimitPrice = version < 29 ? _enc.DecodeDouble() : _enc.DecodeDoubleMax();
      order.AuxPrice = version < 30 ? _enc.DecodeDouble() : _enc.DecodeDoubleMax();

      order.Tif = _enc.DecodeEnum<IBTimeInForce>();
      order.OcaGroup = _enc.DecodeString();
      order.Account = _enc.DecodeString();
      order.OpenClose = _enc.DecodeString();
      order.Origin = _enc.DecodeEnum<IBOrderOrigin>();
      order.OrderRef = _enc.DecodeString();

      if (version >= 3)
        order.ClientId = _enc.DecodeInt();

      if (version >= 4) {
        order.PermId = _enc.DecodeInt();
        if (version < 18)
          order.IgnoreRth = _enc.DecodeInt() == 1;
        else
          order.OutsideRth = _enc.DecodeInt() == 1;

        order.Hidden = _enc.DecodeBool();
        order.DiscretionaryAmt = _enc.DecodeDouble();
      }

      if (version >= 5)
        order.GoodAfterTime = _enc.DecodeString();

      if (version >= 6)
        order.SharesAllocation = _enc.DecodeString();

      if (version >= 7) {
        order.FaGroup = _enc.DecodeString();
        order.FaMethod = _enc.DecodeEnum<IBFinancialAdvisorAllocationMethod>();
        order.FaPercentage = _enc.DecodeString();
        order.FaProfile = _enc.DecodeString();
      }

      if (version >= 8)
        order.GoodTillDate = _enc.DecodeString();

      if (version >= 9) {
        order.Rule80A = _enc.DecodeEnum<IBAgentDescription>();
        order.PercentOffset = _enc.DecodeDouble();
        order.SettlingFirm = _enc.DecodeString();
        order.ShortSaleSlot = _enc.DecodeInt();
        order.DesignatedLocation = _enc.DecodeString();
        if (ServerInfo.Version == 51)
          _enc.DecodeInt(); // exemptCode
        else if (version >= 23)
          order.ExemptCode = _enc.DecodeInt();

        order.AuctionStrategy = _enc.DecodeInt();
        order.StartingPrice = _enc.DecodeDouble();
        order.StockRefPrice = _enc.DecodeDouble();
        order.Delta = _enc.DecodeDouble();
        order.StockRangeLower = _enc.DecodeDouble();
        order.StockRangeUpper = _enc.DecodeDouble();
        order.DisplaySize = _enc.DecodeInt();
        if (version < 18)
          order.RthOnly = _enc.DecodeBool();
        order.BlockOrder = _enc.DecodeBool();
        order.SweepToFill = _enc.DecodeBool();
        order.AllOrNone = _enc.DecodeBool();
        order.MinQty = _enc.DecodeInt();
        order.OcaType = _enc.DecodeEnum<IBOcaType>();
        order.ETradeOnly = _enc.DecodeBool();
        order.FirmQuoteOnly = _enc.DecodeBool();
        order.NbboPriceCap = _enc.DecodeDouble();
      }

      if (version >= 10) {
        order.ParentId = _enc.DecodeInt();
        order.TriggerMethod = _enc.DecodeInt();
      }

      if (version >= 11) {
        order.Volatility = _enc.DecodeDouble();
        order.VolatilityType = _enc.DecodeEnum<IBVolatilityType>();
        if (version == 11) {
          var receivedInt = _enc.DecodeInt();
          order.DeltaNeutralOrderType = ((receivedInt == 0) ? IBOrderType.None : IBOrderType.Market);
        }
        else {
          // version 12 and up

          order.DeltaNeutralOrderType = _enc.DecodeEnum<IBOrderType>();
          order.DeltaNeutralAuxPrice = _enc.DecodeDoubleMax();

          if (version >= 27 && order.DeltaNeutralOrderType != IBOrderType.Empty) {
            order.DeltaNeutralContractId = _enc.DecodeInt();
            order.DeltaNeutralSettlingFirm = _enc.DecodeString();
            order.DeltaNeutralClearingAccount = _enc.DecodeString();
            order.DeltaNeutralClearingIntent = _enc.DecodeString();
          }

          if (version >= 31 && order.DeltaNeutralOrderType != IBOrderType.Empty) {
            order.DeltaNeutralOpenClose = _enc.DecodeString();
            order.DeltaNeutralShortSale = _enc.DecodeBool();
            order.DeltaNeutralShortSaleSlot = _enc.DecodeInt();
            order.DeltaNeutralDesignatedLocation = _enc.DecodeString();
          }
        }
        order.ContinuousUpdate = _enc.DecodeInt();
        if (ServerInfo.Version == 26) {
          order.StockRangeLower = _enc.DecodeDouble();
          order.StockRangeUpper = _enc.DecodeDouble();
        }
        order.ReferencePriceType = _enc.DecodeInt();
      }

      if (version >= 13)
        order.TrailStopPrice = _enc.DecodeDoubleMax();

      if (version >= 30)
        order.TrailingPercent = _enc.DecodeDoubleMax();

      if (version >= 14) {
        order.BasisPoints = _enc.DecodeDouble();
        order.BasisPointsType = _enc.DecodeInt();
        contract.ComboLegsDescription = _enc.DecodeString();
      }

      if (version >= 29) {
        var comboLegsCount = _enc.DecodeInt();
        if (comboLegsCount > 0) {
          contract.ComboLegs = new List<IBComboLeg>(comboLegsCount);
          for (var i = 0; i < comboLegsCount; ++i)
            contract.ComboLegs.Add(new IBComboLeg {
              ContractId = _enc.DecodeInt(),
              Ratio = _enc.DecodeInt(),
              Action = _enc.DecodeEnum<IBAction>(),
              Exchange = _enc.DecodeString(),
              OpenClose = _enc.DecodeEnum<IBComboOpenClose>(),
              ShortSaleSlot = _enc.DecodeEnum<IBShortSaleSlot>(),
              DesignatedLocation = _enc.DecodeString(),
              ExemptCode = _enc.DecodeInt(),
            });
        }

        var orderComboLegsCount = _enc.DecodeInt();
        if (orderComboLegsCount > 0) {
          order.OrderComboLegs = new List<IBOrderComboLeg>(orderComboLegsCount);
          for (var i = 0; i < orderComboLegsCount; ++i)
            order.OrderComboLegs.Add(new IBOrderComboLeg { Price = _enc.DecodeDoubleMax() });
        }
      }

      if (version >= 26) {
        var smartComboRoutingParamsCount = _enc.DecodeInt();
        if (smartComboRoutingParamsCount > 0) {
          order.SmartComboRoutingParams = new List<IBTagValue>(smartComboRoutingParamsCount);
          for (var i = 0; i < smartComboRoutingParamsCount; ++i)
            order.SmartComboRoutingParams.Add(new IBTagValue {
                Tag = _enc.DecodeString(),
                Value = _enc.DecodeString()
              });
        }
      }

      if (version >= 15)
      {
        if (version >= 20) {
          order.ScaleInitLevelSize = _enc.DecodeIntMax();
          order.ScaleSubsLevelSize = _enc.DecodeIntMax();
        }
        else {
          /* int notSuppScaleNumComponents = */
          _enc.DecodeIntMax();
          order.ScaleInitLevelSize = _enc.DecodeIntMax();
        }
        order.ScalePriceIncrement = _enc.DecodeDoubleMax();
      }

      if (version >= 28 && order.ScalePriceIncrement > 0.0 && order.ScalePriceIncrement != Double.MaxValue) {
        order.ScalePriceAdjustValue = _enc.DecodeDoubleMax();
        order.ScalePriceAdjustInterval = _enc.DecodeIntMax();
        order.ScaleProfitOffset = _enc.DecodeDoubleMax();
        order.ScaleAutoReset = _enc.DecodeBool();
        order.ScaleInitPosition = _enc.DecodeIntMax();
        order.ScaleInitFillQty = _enc.DecodeIntMax();
        order.ScaleRandomPercent = _enc.DecodeBool();
      }

      if (version >= 24)
      {
        order.HedgeType = _enc.DecodeString();
        if (!String.IsNullOrEmpty(order.HedgeType))
          order.HedgeParam = _enc.DecodeString();
      }

      if (version >= 25)
        order.OptOutSmartRouting = _enc.DecodeBool();

      if (version >= 19) {
        order.ClearingAccount = _enc.DecodeString();
        order.ClearingIntent = _enc.DecodeString();
      }

      if (version >= 22)
        order.NotHeld = _enc.DecodeBool();

      if (version >= 20)
      {
        if (_enc.DecodeBool()) {
          contract.UnderlyingComponent = new IBUnderlyinhComponent {
            ContractId = _enc.DecodeInt(),
            Delta = _enc.DecodeDouble(),
            Price = _enc.DecodeDouble()
          };
        }
      }

      if (version >= 21)
      {
        order.AlgoStrategy = _enc.DecodeString();
        if (!String.IsNullOrEmpty(order.AlgoStrategy))
        {
          var algoParamsCount = _enc.DecodeInt();
          if (algoParamsCount > 0) {
            order.AlgoParams = new List<IBTagValue>(algoParamsCount);
            for (var i = 0; i < algoParamsCount; ++i)
              order.AlgoParams.Add(new IBTagValue {
                Tag = _enc.DecodeString(),
                Value = _enc.DecodeString(),
              });
          }
        }
      }

      var orderState = new IBOrderState();

      if (version >= 16) {
        order.WhatIf = _enc.DecodeBool();

        orderState.Status = _enc.DecodeEnum<IBOrderStatus>();
        orderState.InitMargin = _enc.DecodeString();
        orderState.MaintMargin = _enc.DecodeString();
        orderState.EquityWithLoan = _enc.DecodeString();
        orderState.Commission = _enc.DecodeDoubleMax();
        orderState.MinCommission = _enc.DecodeDoubleMax();
        orderState.MaxCommission = _enc.DecodeDoubleMax();
        orderState.CommissionCurrency = _enc.DecodeString();
        orderState.WarningText = _enc.DecodeString();
      }

      OnOpenOrder(order.OrderId, order, contract, orderState);
    }