softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

Options Combo Orders #2

Closed manishranjan99 closed 8 years ago

manishranjan99 commented 8 years ago

I have been very impressed with IB4m and thanks for your great work!!! Would you please provide example for Options combo orders. I couldn't find it anywhere in the documentation. I want to understand how to add multiple legs and create an options combo order.

Cheers.... Manish

softwarespartan commented 8 years ago

Hi Manish

Thanks for using IB4m!

It has been very busy few days for me but I have not forgotten about your email! Will get back to you with solution this weekend.

It is going to look similar to the following link but with a few slight changes since IB4m uses the Java API.

http://www.mathworks.com/help/trading/create-interactive-brokers-combination-order.html

Cheers

Sent from my iPhone

On Jan 20, 2016, at 1:51 PM, manishranjan99 notifications@github.com wrote:

I have been very impressed with IB4m and thanks for your great work!!! Would you please provide example for Options combo orders I couldn't find it anywhere in the documentation I want to understand how to add multiple legs and create an options combo order

Cheers Manish

— Reply to this email directly or view it on GitHub.

manishranjan99 commented 8 years ago

thanks for taking your time out to reply to my email. I followed up on your hint and digged deeper into the classes available in the TWS.jar file to add the new combo legs. I tried the following:

After I get conIDs, I do the following.

newLeg1 = com.ib.client.ComboLeg; newLeg1.m_conId = 214849049; newLeg1.m_ratio = 1; newLeg1.m_action = 'SELL'; newLeg1.m_exchange = 'SMART'; newLeg1.m_openClose = 0; newLeg1.m_shortSaleSlot = 0; newLeg1.m_designatedLocation = '';

newLeg2 = com.ib.client.ComboLeg; newLeg2.m_conId = 219152825; newLeg2.m_ratio = 1; newLeg2.m_action = 'BUY'; newLeg2.m_exchange = 'SMART'; newLeg2.m_openClose = 0; newLeg2.m_shortSaleSlot = 0; newLeg2.m_designatedLocation = '';

ComboLeg(1) = com.ib.controller.NewComboLeg(newLeg1) ComboLeg(2) = com.ib.controller.NewComboLeg(newLeg2)

contract.m_symbol = 'USD'; %// For combo order use “USD” as the symbol value all the time contract.m_secType = 'BAG'; %// BAG is the security type for COMBO order contract.m_exchange = 'SMART'; contract.m_currency = 'USD'; contract.m_comboLegs = ComboLeg;

order.m_action = 'BUY'; order.m_totalQuantity = 1; order.m_orderType = 'MKT'; OrderID = ceil((now-736333)*1E6) session.eClientSocket.placeOrder(OrderID,contract,order1); pause(5);

It seem to be giving an error. I think I am on the right track.

-1 501 Already connected. OrderID = 21446113 com.ib.controller.NewComboLeg[] value cannot be converted to type of field "m_comboLegs" in Java class com.ib.client.Contract Error in Place_Order_Example_Options (line 83) contract.m_comboLegs = ComboLeg; %//including combo order in contract object

It seems that the m_comboLegs data type in java is vector and I am not sure what function to use in matlab to combine the two legs to get m_comboLegs.

I appreciate your time and effort and would appreciate any suggestions on how to proceed from here.

Cheers,

softwarespartan commented 8 years ago

Manish

My apologies for not getting back to you this weekend!

I live in northern Virginia and we got a huge snow storm over the weekend. It was epic. I spent the whole weekend shoveling snow.

OK, so the IB API does indeed have the m_comboLegs field as a “java.util.Vector” type (!! http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) More specifically “java.util.Vector” which means that it’s a vector type that holds ComboLeg types.

First (the long way), simply create a java vector in MATLAB,

comboLegsVector = java.util.Vector();

Add legs to the vector:

comboLegsVector.add(newLeg1);
comboLegsVector.add(newLeg2);

Then simply assign

contract.m_comboLegs = comboLegsVector;

But also notice that when the contract is initialized the vector for field m_comboLegs is also initialized. Therefore (the short way) you can skip everything above and just add the legs to the field directly if you want.

contract.m_comboLegs.add(newLeg1);
contract.m_comboLegs.add(newLeg2);

If you need to know what methods a available for the java vector class just enter on the command window:

methods(java.util.Vector)

Let me know if this solves your issue. If not, I have some time tomorrow night and can dig in some more, no problem.

Also, let me know what you think of the API process for creating combo legs. Would be happy to work with you to create additional support in IB4m if needed.

Cheers -abel

On Jan 25, 2016, at 2:01 PM, manishranjan99 notifications@github.com wrote:

thanks for taking your time out to reply to my email. I followed up on your hint and digged deeper into the classes available in the TWS.jar file to add the new combo legs. I tried the following:

After I get conIDs, I do the following.

newLeg1 = com.ib.client.ComboLeg; newLeg1.m_conId = 214849049; newLeg1.m_ratio = 1; newLeg1.m_action = 'SELL'; newLeg1.m_exchange = 'SMART'; newLeg1.m_openClose = 0; newLeg1.m_shortSaleSlot = 0; newLeg1.m_designatedLocation = '';

newLeg2 = com.ib.client.ComboLeg; newLeg2.m_conId = 219152825; newLeg2.m_ratio = 1; newLeg2.m_action = 'BUY'; newLeg2.m_exchange = 'SMART'; newLeg2.m_openClose = 0; newLeg2.m_shortSaleSlot = 0; newLeg2.m_designatedLocation = '';

ComboLeg(1) = com.ib.controller.NewComboLeg(newLeg1) ComboLeg(2) = com.ib.controller.NewComboLeg(newLeg2)

contract.m_symbol = 'USD'; %// For combo order use “USD” as the symbol value all the time contract.m_secType = 'BAG'; %// BAG is the security type for COMBO order contract.m_exchange = 'SMART'; contract.m_currency = 'USD'; contract.m_comboLegs = ComboLeg;

order.m_action = 'BUY'; order.m_totalQuantity = 1; order.m_orderType = 'MKT'; OrderID = ceil((now-736333)*1E6) session.eClientSocket.placeOrder(OrderID,contract,order1); pause(5);

It seem to be giving an error. I think I am on the right track.

-1 501 Already connected. OrderID = 21446113 com.ib.controller.NewComboLeg[] value cannot be converted to type of field "m_comboLegs" in Java class com.ib.client.Contract Error in Place_Order_Example_Options (line 83) contract.m_comboLegs = ComboLeg; %//including combo order in contract object

It seems that the m_comboLegs data type in java is vector and I am not sure what function to use in matlab to combine the two legs to get m_comboLegs.

I appreciate your time and effort and would appreciate any suggestions on how to proceed from here.

Cheers,

— Reply to this email directly or view it on GitHub https://github.com/softwarespartan/IB4m/issues/2#issuecomment-174622277.

manishranjan99 commented 8 years ago

Hi Abel,

thank you so much for your help! your suggestion worked. I tried the long way and it worked. Somehow, the short way gives error. Short way, contract.m_comboLegs.add(newLeg1); contract.m_comboLegs.add(newLeg2);

error message: Attempt to reference field of non-structure array. Error in Place_Order_Example_Options (line 102) contract.m_comboLegs.add(newLeg1);

Regardless, it is working fine now and I appreciate your help. I heard about the snow storm in the news. I hope things are better now.

Kind Regards, Manish