gpt4 book ai didi

java - 自定义 Interactive Brokers 的 reqIds() 和 reqMktData() Java 方法

转载 作者:行者123 更新时间:2023-12-02 04:44:06 26 4
gpt4 key购买 nike

我正在尝试在Interactive Brokers 的Java API 中编写自定义代码。有很多方法可以通过 eClientSocket 对象发送到 TWS。两个示例是 reqIds() 和 reqMktData()。这些都是 void 方法,因此它们不返回任何内容。相反,它们“激活”调用它们的类(在本例中为 SampleFrame)中编写的方法。这些方法也是无效的,因为它们不返回任何数据。相反,代码是在这些方法(分别是nextValidId()和tickPrice())中编写的,以处理从TWS(交易者工作站)发回的数据。

我在创建 nextValidId() 和 tickPrice() 方法的修改版本时遇到问题,因为 reqIds() 和 reqMktData() 实际上并未在自己的代码中指定这些方法名称。因此,我无法编写一个名为“tickPriceBlackBox()”的方法,该方法是从 reqMktData() 内部调用的,或者是从名为 reqMktDataBlackBox() 的 reqMktData() 副本中调用的。同样,reqMktData() 中没有可以修改以调用特定的 tickPriceBlackBox() 方法的特定代码。就好像 TWS 本身内的代码被硬连线来调用tickPrice() 方法一样,这使得我无法创建用于返回价格信息的新方法。

谁能解释一下发生了什么,或者如何创建解决方案?

这是一些代码:


void onReqMktData() {//requests market data from TWS / Interactive Brokers
// run m_orderDlg
m_orderDlg.init("Mkt Data Options", true, "Market Data Options", m_mktDataOptions);
m_orderDlg.show();<br/>
if( !m_orderDlg.m_rc ) {
return;
}
m_mktDataOptions = m_orderDlg.getOptions();<p></p>

<pre><code> // req mkt data
m_client.reqMktData( m_orderDlg.m_id, m_orderDlg.m_contract,
m_orderDlg.m_genericTicks, m_orderDlg.m_snapshotMktData, m_mktDataOptions);
}
</code></pre>

<p>//Here is the reqMktData() method
public synchronized void reqMktData(int tickerId, Contract contract,
String genericTickList, boolean snapshot, List mktDataOptions) {
if (!m_connected) {
error(EClientErrors.NO_VALID_ID, EClientErrors.NOT_CONNECTED, "");
return;
}</p>

<pre><code> if (m_serverVersion < MIN_SERVER_VER_SNAPSHOT_MKT_DATA && snapshot) {
error(tickerId, EClientErrors.UPDATE_TWS,
" It does not support snapshot market data requests.");
return;
}

if (m_serverVersion < MIN_SERVER_VER_UNDER_COMP) {
if (contract.m_underComp != null) {
error(tickerId, EClientErrors.UPDATE_TWS,
" It does not support delta-neutral orders.");
return;
}
}

if (m_serverVersion < MIN_SERVER_VER_REQ_MKT_DATA_CONID) {
if (contract.m_conId > 0) {
error(tickerId, EClientErrors.UPDATE_TWS,
" It does not support conId parameter.");
return;
}
}

if (m_serverVersion < MIN_SERVER_VER_TRADING_CLASS) {
if (!IsEmpty(contract.m_tradingClass)) {
error(tickerId, EClientErrors.UPDATE_TWS,
" It does not support tradingClass parameter in reqMarketData.");
return;
}
}

final int VERSION = 11;

try {
// send req mkt data msg
send(REQ_MKT_DATA);
send(VERSION);
send(tickerId);

// send contract fields
if (m_serverVersion >= MIN_SERVER_VER_REQ_MKT_DATA_CONID) {
send(contract.m_conId);
}
send(contract.m_symbol);
send(contract.m_secType);
send(contract.m_expiry);
send(contract.m_strike);
send(contract.m_right);
if (m_serverVersion >= 15) {
send(contract.m_multiplier);
}
send(contract.m_exchange);
if (m_serverVersion >= 14) {
send(contract.m_primaryExch);
}
send(contract.m_currency);
if(m_serverVersion >= 2) {
send( contract.m_localSymbol);
}
if(m_serverVersion >= MIN_SERVER_VER_TRADING_CLASS) {
send( contract.m_tradingClass);
}
if(m_serverVersion >= 8 && BAG_SEC_TYPE.equalsIgnoreCase(contract.m_secType)) {
if ( contract.m_comboLegs == null ) {
send( 0);
}
else {
send( contract.m_comboLegs.size());

ComboLeg comboLeg;
for (int i=0; i < contract.m_comboLegs.size(); i ++) {
comboLeg = contract.m_comboLegs.get(i);
send( comboLeg.m_conId);
send( comboLeg.m_ratio);
send( comboLeg.m_action);
send( comboLeg.m_exchange);
}
}
}

if (m_serverVersion >= MIN_SERVER_VER_UNDER_COMP) {
if (contract.m_underComp != null) {
UnderComp underComp = contract.m_underComp;
send( true);
send( underComp.m_conId);
send( underComp.m_delta);
send( underComp.m_price);
}
else {
send( false);
}
}

if (m_serverVersion >= 31) {
/*
* Note: Even though SHORTABLE tick type supported only
* starting server version 33 it would be relatively
* expensive to expose this restriction here.
*
* Therefore we are relying on TWS doing validation.
*/
send( genericTickList);
}
if (m_serverVersion >= MIN_SERVER_VER_SNAPSHOT_MKT_DATA) {
send (snapshot);
}

// send mktDataOptions parameter
if(m_serverVersion >= MIN_SERVER_VER_LINKING) {
StringBuilder mktDataOptionsStr = new StringBuilder();
int mktDataOptionsCount = mktDataOptions == null ? 0 : mktDataOptions.size();
if( mktDataOptionsCount > 0) {
for( int i = 0; i < mktDataOptionsCount; ++i) {
TagValue tagValue = (TagValue)mktDataOptions.get(i);
mktDataOptionsStr.append( tagValue.m_tag);
mktDataOptionsStr.append( "=");
mktDataOptionsStr.append( tagValue.m_value);
mktDataOptionsStr.append( ";");
}
}
send( mktDataOptionsStr.toString());
}

}
catch( Exception e) {
error( tickerId, EClientErrors.FAIL_SEND_REQMKT, "" + e);
close();
}
}
</code></pre>

<p>//The key piece of this code, REQ_MKT_DATA, leads to a final int variable within the EClientSocket.java object, equal to 1. tickPrice() is not mentioned anywhere.</p>

<p>//This method provides stock price, but doesn't return a value. You have to put executable code within this one method. I cannot duplicate and change the name of this method (tickprice();) because none of my accessible code calls it, to my knowledge. It feels as if TWS is calling tickPrice from its end.</p>

<p>
public void tickPrice( int tickerId, int field, double price, int canAutoExecute) {
// received price tick
String msg = EWrapperMsgGenerator.tickPrice( tickerId, field, price, canAutoExecute);
m_tickers.add( msg );
}
</p>

最佳答案

tickPrice 方法是从 EReader 中调用的,EReader 是在 EClientSocket 中创建的,EClientSocket 知道 EWrapper 实现。

基本上,您调用套接字 reqMktData 方法,它会将其发送到 TWS。 EReader 会将套接字上的响应视为 tickPrice 消息,并将其发送到 Wrapper 实现。

如果您想自己处理它,那么您可以在tickPrice 方法中进行处理。它可能就像将数据传递给您定义的方法一样简单。

public void tickPrice( int tickerId, int field, double price, int canAutoExecute) {
handleTick(tickerId,field,price);
}

然后编写自己的handleTick方法

关于java - 自定义 Interactive Brokers 的 reqIds() 和 reqMktData() Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com