我正在使用 Java API,并且我用一个按钮制作了一个简单的 Swing GUI。当我按下按钮时,它执行:
anInstance.connection.reqRealTimeBars(id, contract, 5,"TRADES",false);
// note that connection = new EClientSocket(Wrapper) which is assigned in constructor`
这会导致我重写的 Wrapper 函数 realtimeBar 执行。
我使用 Joda Time 并将getSecondOfMinute
包装函数中的时间转换为秒。如果是 55 秒,我想进行交易。(即我希望我的程序每分钟在 55 秒时进行一次交易)。
但是,每当我进行交易时,都会有一些东西断开我与 API 的连接。
我认为这可能与同步和一些死锁有关?无论如何,该错误关闭了我与 API 的连接,这不是我想要的,因为我的程序的重点是不断交易。
有谁知道为什么会出现这个问题?更重要的是,我该如何解决这个问题?
示例代码
someWrapper.java 覆盖 EWrapper:
public class someWrapper implements EWrapper {
private IBProgram anInstance;
public void setReference(IBProgram anInstance){
this.anInstance = anInstance
}
// overide and implement various EWrapper methods.
// The Ewrapper method below is a sample of placing an Order
// causing problems in an EWrapper method
public void updatePortfolio(Contract contract, int position, double marketPrice, double marketValue,
double averageCost, double unrealizedPNL, double realizedPNL, String accountName){
Order myFakeOrder = new Order();
Contract myFakeContract = new Contract();
myFakeContract.m_symbol = "GOOG";
myFakeContract.m_exchange = "SMART";
myFakeContract.m_secType = "STK";
myFakeContract.m_currency = "USD";
myFakeOrder.m_action = "BUY";
myFakeOrder.m_totalQuantity = 1;
myFakeOrder.m_orderType = "LMT";
myFakeOrder.m_lmtPrice = 1;
myFakeOrder.m_tif = "DAY";
anInstance.connection.placeOrder(2124124,myFakeContract,myFakeOrder);
}
}
IBProgram.java
public class IBProgram {
someWrapper wrapper;
public EClientSocket connection;
public IBProgram() {
this.wrapper = new someWrapper();
this.connection = new EClientSocket(wrapper);
}
}
图形用户界面
public class GUI extends javax.swing.JFrame{
private IBProgram IBProgramInstance;
public GUI(){
initComponents();
}
private void initComponents() {
myButton = new javax.swing.JButton();
myButton.setText("Start Trading");
myButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
myButtonActionPerformed(evt);
}
});
}
public void setReference(IBProgram Instance){
this.IBProgramInstance = Instance;
}
private void myButtonActionPerformed (java.awt.event.ActionEvent evt) {
IBProgramInstance.connection.reqAccountUpdates(true,"myaccountid");
}
public static void main(String args[]){
IBProgram IBPInstance = new IBProgram();
IBProgramInstance.wrapper.setReference(IBPInstance);
IBProgram.connection.eConnect("127.0.0.1",7496,12);
GUI guiInstance = new GUI();
guiInstance.setReference(IBPInstance);
guiInstance.setVisible(true);
}
添加错误信息
Exception in thread "EReader" java.lang.Error: Interrupted attempt to aquire write lock
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1349)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:659)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718)
at IBconnect.IBTradeGui.setConnectionText(IBTradeGui.java:698)
at IBconnect.someWrapper.connectionClosed(someWrapper.java:63)
at com.ib.client.EClientSocket.close(EClientSocket.java:2004)
at com.ib.client.EReader.run(EReader.java:78)