I am using the Java API and I've made a simple Swing GUI with a push button. When I push the button it executes:
anInstance.connection.reqRealTimeBars(id, contract, 5,"TRADES",false); // note that connection = new EClientSocket(Wrapper) which is assigned in constructor
This causes my overrided Wrapper function realtimeBar to execute. I use Joda Time and `getSecondOfMinute` to convert the Time in the wrapper function into what second it is. If it is 55 seconds, I want to place a trade. (I.e. I want my program to place a trade every minute at the 55 second mark).
However whenever I place the trade, there is something which disconnects my connection with the API. The trade goes through, but subsequently I am no longer connected and get a nullPointerException.
I think it might have to do with synchronization and perhaps some deadlock forcing a disconnect. Anyways, the error closes my connection to the API which is not what I want since the point of my program is to trade constantly.
Does anyone know why this issue is coming up? More importantly, how do I get around this? I noticed it occurs when I use placeOrder in any wrapper function so I'm hoping someone has come across this before and can clarify for me.
This seemed like the correct forum to put it in but let me know if I should put it somewhere else.
Sample Code
anInstance.connection.reqRealTimeBars(id, contract, 5,"TRADES",false); // note that connection = new EClientSocket(Wrapper) which is assigned in constructor
This causes my overrided Wrapper function realtimeBar to execute. I use Joda Time and `getSecondOfMinute` to convert the Time in the wrapper function into what second it is. If it is 55 seconds, I want to place a trade. (I.e. I want my program to place a trade every minute at the 55 second mark).
However whenever I place the trade, there is something which disconnects my connection with the API. The trade goes through, but subsequently I am no longer connected and get a nullPointerException.
I think it might have to do with synchronization and perhaps some deadlock forcing a disconnect. Anyways, the error closes my connection to the API which is not what I want since the point of my program is to trade constantly.
Does anyone know why this issue is coming up? More importantly, how do I get around this? I noticed it occurs when I use placeOrder in any wrapper function so I'm hoping someone has come across this before and can clarify for me.
This seemed like the correct forum to put it in but let me know if I should put it somewhere else.
Sample Code
Code:
someWrapper.java which overrides 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);
}
}
GUI
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);
}