java TWS盈透证券API -如何修复“未找到请求的安全定义”?

3hvapo4f  于 2022-12-02  发布在  Java
关注(0)|答案(7)|浏览(294)

使用Java API(我猜这适用于任何其他TWS Interactive Brokers客户端API),我得到一个错误“No security definition has been found for the request”常见问题解答和其他资源完全没有帮助。

Contract contract = new Contract();

    int id = incId;           

    System.out.println("Oder Id " + id );

    // use UTC seconds as transaction id

    // This is the problem you need to have a blank contractId
    contract.m_conId = 12345;
    contract.m_symbol = signal.symbol;
    contract.m_secType = "STK";
    contract.m_expiry = "";
    contract.m_strike = 0;
    contract.m_exchange = "SMART";
    contract.m_primaryExch = "ISLAND";
    contract.m_currency = "USD";

    //etc

    Order order = new Order();

    // set order fields
    order.m_account = "XXXXXX";
    order.m_orderId = id;
    //etc

    GetInstance().wrapper.m_client.placeOrder(id, contract, order);
j7dteeu8

j7dteeu81#

这里的关键是contractId字段应该保留为空。使用contractId提交会导致安全错误。

laik7k3q

laik7k3q2#

通过将交换设置为“智能”,为我解决了这个问题。
我的用例是获取我当前持有的所有合同并发送MOC订单。我使用reqPositions方法获取了合同,但这些返回值中的合同仍然出现此错误。
在这些合同上将Exchange设置为SMART为我解决了这个问题。

slhcrj9b

slhcrj9b3#

在某些情况下,交换需要留空。我在使用此查找时遇到了一些运气:
https://pennies.interactivebrokers.com/cstools/contract_info/v3.9/index.php
例如,对于CL:

con.connect()

contract = Contract()
contract.m_symbol = "CL"
contract.m_exchange = ""
contract.m_currency = "USD"
contract.m_secType = "FUT"

con.reqContractDetails(1, contract)

time.sleep(2)

con.disconnect()
qmelpv7a

qmelpv7a4#

此错误的其他可能原因包括:

  • ConId应设置为0。
  • TradingClass应保留为空。
  • LocalSymbol或GlobalSymbol的问题。
  • 其他合同变量设置不正确。
  • 请求的特定合同目前在市场上不存在。
nc1teljy

nc1teljy5#

我遇到过同样的问题,但这是因为我没有填充SecIdTypeSecId的值。
下面是一个成功的订单和请求的示例

IBApi.Order order = new IBApi.Order()
{
    Account = OrderCreationConfig.IndividualAccount
    , ClientId = OrderCreationConfig.OrderSlaveClientId //1
    , Action = orderNodeEntity.OrderAction //"BUY"
    , TotalQuantity = orderNodeEntity.NrOfStocks
    , OrderType = OrderCreationConfig.OrderTypeLMT //"LMT"
    , Tif = OrderCreationConfig.OrderTifGTC //"GTC"
    , OcaType = OrderCreationConfig.OcaTypeId //3
    , LmtPrice = price
    , AuxPrice = 0
    , TrailStopPrice = double.MaxValue
    , VolatilityType = 0
    , DeltaNeutralOrderType = "None"
};

IBApi.Contract contract = new IBApi.Contract()
{
      Symbol = orderNodeEntity.Symbol
     , SecType = OrderCreationConfig.ContractSecTypeSTK //"STK"
     , Strike = 0
     , Right = OrderCreationConfig.ContractRightQuestionMark //"?"
     , Exchange = OrderCreationConfig.ContractExchangeIsland //"ISLAND"
     , Currency = OrderCreationConfig.ContractCurrencyUSD //"USD"
     , LocalSymbol = orderNodeEntity.Symbol
     , TradingClass = null        
     , SecIdType = OrderCreationConfig.ContractSecIdTypeISIN //"ISIN"
     , SecId = this.GetISINCode(orderNodeEntity.Symbol) //"US0378331005" 
};
ego6inou

ego6inou6#

也要确保为你的合同选择正确的lastTradeDateOrContractMonth。我在试图在不合法的到期日出售期权时也遇到了同样的错误。

qvsjd97n

qvsjd97n7#

使用ISLAND代替SMART进入交换。

Contract contract = new Contract();

int id = incId;           

System.out.println("Oder Id " + id );

// use UTC seconds as transaction id

// This is the problem you need to have a blank contractId
contract.m_conId = 12345;
contract.m_symbol = signal.symbol;
contract.m_secType = "STK";
contract.m_expiry = "";
contract.m_strike = 0;
contract.m_exchange = "ISLAND";
contract.m_currency = "USD";

//etc

Order order = new Order();

// set order fields
order.m_account = "XXXXXX";
order.m_orderId = id;
//etc

GetInstance().wrapper.m_client.placeOrder(id, contract, order);

相关问题