使用Java API(我想这适用于任何其他TWS Interactive Brokers客户端API)时,我得到一个错误"找不到请求的安全定义"常见问题解答和其他资源完全没有帮助。
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);
这里的关键是contractId字段应该留空。使用contractId提交会导致安全错误。
通过将交换机设置为"SMART",我解决了这个问题。
我的用例是获得我目前持有的所有合同,并发送MOC订单。我使用reqPositions方法得到了contract,但那些返回值中的Contracts仍然给出了这个错误。
将这些合同的交换设置为SMART为我解决了这个问题。
在某些情况下,交换需要留空。我有一些运气使用这个查找:
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()
此错误的其他可能原因可能包括:
-ConId应设置为0。
-TradingClass应留空。
-LocalSymbol或GlobalSymbol的问题。
-其他合同变量设置不正确。
-所要求的特定合同目前市场上不存在。
我也遇到过同样的问题,但这是因为我没有填充SecIdType和SecId的值。
以下是有效的订单和请求的示例:
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"
};
还要确保为您的合同选择正确的lastTradeDateOrContractMonth。当我试图在不合法的到期日出售期权时,我也犯了同样的错误。。。
在交换中使用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);