softwarespartan / IB4m

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

Implementing order conditioning like price, percentage, time conditions #26

Open StTrader opened 6 years ago

StTrader commented 6 years ago

Hi Abel,

First of all, thank you very much for providing this very good matlab IB API!! I tested it and it runs smoothly for me. Now, I came across the order conditioning options which IB supports.

I would like to know how I may define or implement order conditions in your matlab IB API as described in the IB API manual (see below).

http://interactivebrokers.github.io/tws-api/order_conditions.html

"
Order mkt = OrderSamples.MarketOrder("BUY", 100); //Order will become active if conditioning criteria is met mkt.conditionsCancelOrder(true); mkt.conditions().add(OrderSamples.PriceCondition(208813720, "SMART", 600, false, false)); mkt.conditions().add(OrderSamples.ExecutionCondition("EUR.USD", "CASH", "IDEALPRO", true)); mkt.conditions().add(OrderSamples.MarginCondition(30, true, false)); mkt.conditions().add(OrderSamples.PercentageChangeCondition(15.0, 208813720, "SMART", true, true)); mkt.conditions().add(OrderSamples.TimeCondition("20160118 23:59:59", true, false)); mkt.conditions().add(OrderSamples.VolumeCondition(208813720, "SMART", false, 100, true)); client.placeOrder(nextOrderId++, ContractSamples.EuropeanStock(), mkt); "

Where may I find this feature in the TWS.jar? Or any tips on this?

Thanks, Stephan

softwarespartan commented 6 years ago

Hi Stephan

Thanks for reaching out. Apologies for the delay in reply. The order conditions

com.ib.client.OrderConditions

is a feature of the +973 java API. Interactive brokers did a hard API rewrite at version 973 and beyond. IB4m uses “972” or the “old” API (arguably much better, but a few less “features").

At any rate, I have IB4m for 973 but most (all?) users still prefer 972 api.

No problem to share 973 jar file with you, if you’re interested. Just note the the 973 jar has different API and documentation than the 972 jar.

In many cases 973 API is same as 972 but not the case for order conditions.

Thanks again -abel

On May 2, 2018, at 2:18 PM, StTrader notifications@github.com wrote:

Hi Abel,

First of all, thank you very much for providing this very good matlab IB API!! I tested it and it runs smoothly for me. Now, I came across the order conditioning options which IB supports.

I would like to know how I may define or implement order conditions in your matlab IB API as described in the IB API manual (see below).

http://interactivebrokers.github.io/tws-api/order_conditions.html http://interactivebrokers.github.io/tws-api/order_conditions.html " Order mkt = OrderSamples.MarketOrder("BUY", 100); //Order will become active if conditioning criteria is met mkt.conditionsCancelOrder(true); mkt.conditions().add(OrderSamples.PriceCondition(208813720, "SMART", 600, false, false)); mkt.conditions().add(OrderSamples.ExecutionCondition("EUR.USD", "CASH", "IDEALPRO", true)); mkt.conditions().add(OrderSamples.MarginCondition(30, true, false)); mkt.conditions().add(OrderSamples.PercentageChangeCondition(15.0, 208813720, "SMART", true, true)); mkt.conditions().add(OrderSamples.TimeCondition("20160118 23:59:59", true, false)); mkt.conditions().add(OrderSamples.VolumeCondition(208813720, "SMART", false, 100, true)); client.placeOrder(nextOrderId++, ContractSamples.EuropeanStock(), mkt); "

Where may I find this feature in the TWS.jar? Or any tips on this?

Thanks, Stephan

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/26, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1HL6RQ9U_H4ewRm-UZXbFT80XXnvks5tuiKIgaJpZM4TwLMl.

StTrader commented 6 years ago

Hi Abel,

No problem. Thanks for your response and offer for the IB4m for 973 api version. I would be glad, if you could share the 973 jar file with me.

I understand the concerns with 973 and using IB4m + 972 API at the moment. But I would give it a try, just to understand better the order conditioning with API support.

Thanks again Stephan

Tukan-Kolor commented 6 years ago

Abel,

Hey I was just looking into using order conditioning as well and was pounding my head on the table. I am using it to globally optimize over years of historical data and place trades accordingly. This is a great interface to IB. Anyway I was hoping that you might make the 973 jar file available for me as well? I am already using 9.73 since I had to with the newest Matlab 64bit and Windows 10.

Thanks!

Troy

softwarespartan commented 5 years ago

Updates now pushed to HEAD for Interactive Brokers API version 9.73 which includes BracketOrders, OrderConditions, CommissionReports and more. You should now be able to use com.ib.client.OrderConditions. Let me know if you need anything additional

StTrader commented 5 years ago

Hi Abel,

Thanks a lot for sharing API version 9.73.

At first I am trying to make the 9.73 version to run togehter with my matlab trading software, before go further on with the OrderConditions. I observed that Matlab has not anymore access to the m* variables in Contract.class and Order.class. So, I cannot set specific values like you do it in your "bracketOrderSample.m" example. I decompiled the Contract and Order class to check for the reason. I saw that all m* variables were defined as privat instead of public as in the 9.63 version (see below). I think this causes the error in Matlab as it can only access public variables.

public class Order { public static final int CUSTOMER = 0; public static final int FIRM = 1; public static final char OPT_UNKNOWN = '?'; public static final char OPT_BROKER_DEALER = 'b'; public static final char OPT_CUSTOMER = 'c'; public static final char OPT_FIRM = 'f'; public static final char OPT_ISEMM = 'm'; public static final char OPT_FARMM = 'n'; public static final char OPT_SPECIALIST = 'y'; public static final int AUCTION_MATCH = 1; public static final int AUCTION_IMPROVEMENT = 2; public static final int AUCTION_TRANSPARENT = 3; public static final String EMPTY_STR = ""; private int m_clientId; private int m_orderId; private int m_permId; private int m_parentId; private String m_action = "BUY"; private double m_totalQuantity; private int m_displaySize; private String m_orderType = "LMT"; private double m_lmtPrice = Double.MAX_VALUE; private double m_auxPrice = Double.MAX_VALUE; private String m_tif = "DAY"; private String m_account; private String m_settlingFirm; private String m_clearingAccount; private String m_clearingIntent; ...

Could please check and if this the problem, recompile the source files with public declaration?

Thanks, Stephan

softwarespartan commented 5 years ago

Yes, this is the “correct” behavior :(

I very much dislike this in 9.73

Instead of public fields now have setter/getter methods for each field.

If you ask for “methods(obj)” you will see the defined functions for using the fields.

Also if you check out the doc folder in IB4m you will see examples of using these methods for configuring contracts etc

-abel

On Sun, Aug 5, 2018 at 12:30 PM StTrader notifications@github.com wrote:

Hi Abel,

Thanks a lot for sharing API version 9.73.

At first I am trying to make the 9.73 version to run togehter with my matlab trading software, before go further on with the OrderConditions. I observed that Matlab has not anymore access to the m* variables in Contract.class and Order.class. So, I cannot set specific values like you do it in your "bracketOrderSample.m" example. I decompiled the Contract and Order class to check for the reason. I saw that all m* variables were defined as privat instead of public as in the 9.63 version (see below). I think this causes the error in Matlab as it can only access public variables.

public class Order { public static final int CUSTOMER = 0; public static final int FIRM = 1; public static final char OPT_UNKNOWN = '?'; public static final char OPT_BROKER_DEALER = 'b'; public static final char OPT_CUSTOMER = 'c'; public static final char OPT_FIRM = 'f'; public static final char OPT_ISEMM = 'm'; public static final char OPT_FARMM = 'n'; public static final char OPT_SPECIALIST = 'y'; public static final int AUCTION_MATCH = 1; public static final int AUCTION_IMPROVEMENT = 2; public static final int AUCTION_TRANSPARENT = 3; public static final String EMPTY_STR = ""; private int m_clientId; private int m_orderId; private int m_permId; private int m_parentId; private String m_action = "BUY"; private double m_totalQuantity; private int m_displaySize; private String m_orderType = "LMT"; private double m_lmtPrice = Double.MAX_VALUE; private double m_auxPrice = Double.MAX_VALUE; private String m_tif = "DAY"; private String m_account; private String m_settlingFirm; private String m_clearingAccount; private String m_clearingIntent; ...

Could please check and if this the problem, recompile the source files with public declaration?

Thanks, Stephan

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/26#issuecomment-410531553, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1L49w69Fw8SsXoJVmPxS68dsnO1fks5uNx2LgaJpZM4TwLMl .

StTrader commented 5 years ago

Thanks, Abel!

I got it and tested the set and get methods. So, next days it is time to adapt some interface functions between my trading program and the API to make it run with 9.73.

Keep you posted, when I am working on the order conditioning.

Regards, Stephan

PeteAndP commented 5 years ago

Hi Stephan, Abel,

Abel, thank you and congratulations for such a fantastic work you've done with the API. I am currently using another implementation of the API and I am considering using your version which seems to be fast and robust.

Stephan, it seems that you worked out how to use the conditional order functionality of the latest API .. Could you please post an order's example to see how you've implemented it? I'm a good Matlab programmer but when it comes to using the methods & co of the API I'm a bit behind (I decompressed the jar file but not being a java programmer it didn't talk to me much!)

Regards, Pete

softwarespartan commented 5 years ago

Hi Pete

Thanks for reaching out

Not much need to decompile the jar, easier to download the API from Interactive Brokers. I will also post the src for the +9.72 on GitHub that includes the few modifications needed for IB4m (no api changes just java “pluming" stuff).

You can read about order conditioning in the docs here

https://interactivebrokers.github.io/tws-api/order_conditions.html https://interactivebrokers.github.io/tws-api/order_conditions.html

I am traveling right now but will try to share a simple example when I get a chance.

Cheers -abel

On Aug 18, 2018, at 8:16 AM, PeteAndP notifications@github.com wrote:

Hi Stephan, Abel,

Abel, thank you and congratulations for such a fantastic work you've done with the API. I am currently using another implementation of the API and I am considering using your version which seems to be fast and robust.

Stephan, it seems that you worked out how to use the conditional order functionality of the latest API .. Could you please post an order's example to see how you've implemented it? I'm a good Matlab programmer but when it comes to using the methods & co of the API I'm a bit behind (I decompressed the jar file but not being a java programmer it didn't talk to me much!)

Regards, Pete

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/26#issuecomment-414064992, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1LxCck96thIufXziTTCgeWVkDfhXks5uSC_TgaJpZM4TwLMl.

PeteAndP commented 5 years ago

Thanks Abel, Looking forward to getting an example to work with - I read all I could find in terms of documents but the way it's set up is so different compared to normal orders that I didn't go very far.

Also if I understand well the source you will upload for +9.72 will be the 9.72 with a few modifications (including order conditioning?); as I believe the 9.73 source is already in MASTER

TukanKolor commented 5 years ago

Hey I wanted to reach out for help or an example on order conditioning again. I am trying this:

order = com.ib.client.Order(); contractId = contract.conid; conditionString = ['PriceCondition(' num2str(contractId) ', "SMART", ' num2str(round(lmtPrice,2,'decimals')) ', false, false)']; order.conditions.add(conditionString);

But of course it is not working as a string. Anyway any help would be much appreciated.

TukanKolor commented 5 years ago

So this is the part I don't know yet how to do:

PriceCondition = OrderCondition.Create(OrderConditionType.Price)

After that you can:

order.conditions.add(PriceCondition)

I am not sure where to get the OrderCondition and OrderConditionType objects from. Thanks to anyone who can share if they got it to work.

softwarespartan commented 5 years ago

Should be ‘com.ib.client.OrderCondition’

You can see all the tws973 src here:

https://github.com/softwarespartan/TWS973/tree/master/src/com/ib/client

Let me know if that works for you.

Cheers -abel

Sent from my iPhone

On Nov 19, 2018, at 8:33 PM, TukanKolor notifications@github.com wrote:

So this is the part I don't know yet how to do:

PriceCondition = OrderCondition.Create(OrderConditionType.Price)

After that you can:

order.conditions.add(PriceCondition)

I am not sure where to get the OrderCondition and OrderConditionType objects from. Thanks to anyone who can share if they got it to work.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

TukanKolor commented 5 years ago

Abel, Hey thank you for the quick reply. I think I may be bad at reading Java. I have tried every combination I can think of (way more than these listed), but have not been able to make it work.

Thanks

com.ib.client.OrderCondition(com.ib.client.OrderConditionType('Price'))

methodsview(com.ib.client.OrderCondition) Java exception occurred: java.lang.InstantiationException: com.ib.client.OrderCondition

com.ib.client.OrderCondition(com.ib.client.OrderConditionType.Price) No constructor 'com.ib.client.OrderCondition' with matching signature found.

softwarespartan commented 5 years ago

Be sure to follow the docs

https://interactivebrokers.github.io/tws-api/order_conditions.html <https://interactivebrokers.github.io/tws-api/order_conditions.html>

For example:

type = com.ib.client.OrderConditionType.Price();
priceCondition = com.ib.client.OrderCondition.create(type);
…

Cheers -abel

On Nov 20, 2018, at 7:56 PM, TukanKolor notifications@github.com wrote:

Abel, Hey thank you for the quick reply. I think I may be bad at reading Java. I have tried every combination I can think of (way more than these listed), but have not been able to make it work.

Thanks

com.ib.client.OrderCondition(com.ib.client.OrderConditionType('Price'))

methodsview(com.ib.client.OrderCondition) Java exception occurred: java.lang.InstantiationException: com.ib.client.OrderCondition

com.ib.client.OrderCondition(com.ib.client.OrderConditionType.Price) No constructor 'com.ib.client.OrderCondition' with matching signature found.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/26#issuecomment-440487617, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1CuzRr4a2Q8Bs14yyV8v7noPnts4ks5uxKTEgaJpZM4TwLMl.

TukanKolor commented 5 years ago

Abel,

Hey that helped a lot thank you. I think I am very close. When I place the order I am getting:

133 Submit new order failed

The API order shows up in TWS with a price condition under the Conditional tab, but looks different than one done by hand:

last price is <= 117.00 %API version last price of ALXN STK on SMART is <= 117.00 %TWS by hand version

Here is my code so far:

type = com.ib.client.OrderConditionType.Price(); priceCondition = com.ib.client.OrderCondition.create(type); priceCondition.conId(id); %same as order id and contract id without conditions priceCondition.exchange('ISLAND'); priceCondition.price(round(lmtPrice,2,'decimals')); priceCondition.conjunctionConnection(false); %only one price condition priceCondition.triggerMethod(2); %last priceCondition.isMore(false); order.conditions.add(priceCondition);

I am wondering if I am specifying the contract incorrectly or using the wrong id. Thanks

ciaoitaly commented 4 years ago

Abel, any reason why the above code fails ? I am also getting the "133 Submit new order failed". Thanks much!