Autotrading with Interactive Brokers
Author: Gamba
Creation Date: 5/26/2017 7:39 AM
profile picture

Gamba

#1
I am using ib-automated-trading to auto trade with Wealth-Lab and IB.

I am using a daily strategy with limit orders. This orders are sent successfully to TWS, but after that no stop order or target order is placed. It is a portfolio based strategy, for up to 50 symbols.

I tested from Quotes and from Strategy Monitor but every time the stop order, which is coded immediataly after sendig the market, order isn't placed.

The guys from ib-automated-trading says that this behaviour is undependent from their tool it is basic Wealth-Lab functionality so I guess there is an error or I missed something in my code.
Maybe I need an intraday strategy instead of an daily one?

This is the code:

CODE:
Please log in to see this code.
profile picture

Eugene

#2
As per the WealthScript Programming Guide (Programming Trading Strategies > Peeking), it's a subtle peeking error to base exit logic on the current bar on the results of a trade that may or may not occur on the next bar. That's also often a mistake to combine same-bar ALimit/AtStop entries and exits: I want to test a strategy that buys and sells with stop/limit orders on the same bar.

However, you might have success with "Allow Same Bar Exits for Auto-Trading" option enabled in Wealth-Lab's Preferences > Trading if you assign RiskStopLevel and AutoProfitLevel to the next Position i.e. on top of your BuyAtLimit call.
profile picture

Gamba

#3
Thank you Eugene for the hint.

Allow same bar exits is enabled. After implementing RiskStopLevel nothing has changed. Limit order ist triggered but no subsequent order is send to TWS. Should I use the daily strategy in strategy monitor or in quotes window?
profile picture

Eugene

#4
So how have you implemented RiskStopLevel?

Look, the sell order can't be reached in real life because the Position p is always null by the time the Strategy is making a check for it. The BuyAtLimit is an Alert that becomes a Position only on the next bar. And by this time it's too late to check for != null. Hence the need for "Allow Same Bar Exits..."

See Cone's post #6 on how to implement it correctly for live trading: Same Bar Exit
profile picture

Gamba

#5
I read the post and have an additional question.
I understand the problem with asking for not null, but is it correct that
same bar exits only possible with AtMarket orders?

So my approach with AtLimit won't be sucessfull with IBProvider?
The code is the same as in my first post, with an additional

RiskStopLevel = stopLevel;

above Position p. That doesn't seem to work, but would this work in live trading

CODE:
Please log in to see this code.


"Allow same bar exits" is enabled

Should I use the daily strategy in strategy monitor or in quotes window?
profile picture

Cone

#6
QUOTE:
but is it correct that same bar exits only possible with AtMarket orders?
No, it can be any order type. Same bar exits are controlled by WLD's Orders tool logic. As the User Guide explains, "When this option is selected, upon receiving confirmation that the entry order Filled the Orders tool will activate entry-bar stop loss and/or profit target limit orders based on the values of RiskStopLevel and AutoProfitLevel assigned to the next Position in the Strategy code".

QUOTE:
Should I use the daily strategy in strategy monitor or in quotes window?
The Preference is, "Allow Same Bar Exits for Auto-Trading" so for this feature to work, it's imperative that the order is Auto-Placed. In other words, if you manually Place (click) the order, it won't work.

Therefore, you must:
1. select Auto-Trading: Live Accounts, and,
2a. select Auto-Place in a Streaming Strategy Window, or,
2b. select Auto-Place in a Quotes window, or,
2c. select Auto-Stage in the Strategy Monitor.
profile picture

Gamba

#7
Thank you Cone for clarification. I will give it a try.

I was searching for option "Auto-Place" in a Quotes window, but only find Auto-Stage?
Do you have a hint in which context menu I will find it to enable the option?
profile picture

Eugene

#8
Clicking on "Help" in a Quotes window brings this up:

QUOTE:
With Auto-Stage enabled, triggered Alerts are immediately Staged in the Orders tool without any manual intervention required. When Auto-Trading is enabled in the Orders tool, this button changes to Auto-Place to indicate that new Alerts (orders) are immediately placed with brokerage.
profile picture

Gamba

#9
Today a Limit-Order has triggered and there was an interaction with the running strategy.
But the Stop-Order was not send to TWS. This is the error from Orders-Panel:

Account Date/Time Status Symbol Action Type Quantity Order Price Filled Fill Price Route Strategy Signal Scale Message TIF Trade Type
UXXX 31.05.2017 00:00 Filled ARL.DE Buy Limit 274 36,38 274 35,70 SMART TEST_042017 E-LONG-LIMIT Daily 0: Filled DAY Margin

UXXX 31.05.2017 00:00 Error ARL.DE Sell Stop 274 32,75 0 0,00 SMART TEST_042017 Daily

110: The price does not conform to the minimum price variation for this contract. DAY Margin

I cannot see a price violation at the two prices in my last message.

Entry was at 36,38, Exit at 32,75.

What is the background of the message?
Is it possible to roundToTickSize with a given method?
Maybe this could solve this issue.
profile picture

Eugene

#10
QUOTE:
110: The price does not conform to the minimum price variation for this contract. DAY Margin

This error message originates from Interactive Brokers, not Wealth-Lab. The 3rd party broker provider only retranslates it into the Orders tool.

However, it's strange to see it in this context given the circumstances i.e.g "32,75" seems to be a valid price for the tick size of ARL.DE (which is 0.01 I guess). Just a couple of questions:

1. I guess you're using German regional settings?
2. What about the Symbol Info Manager and Preferences > Advanced > Decimal Places, any chance to see an invalid tick value there?

Your best bet is to contact the developer, though. Looks like it's their issue.
profile picture

Gamba

#11
Yes, I am using German regional settings. No weird tick values in Symbol Info Manager or Preferences. I will conact the developer and will test it with a decimal point separator again.

I wonder why entry order are sent without error because it looks like the same separator...
profile picture

Cone

#12
I used to import orders in IB TWS using their BasketTrader feature. If limit (or stop) orders had too many decimals specified, then the order would fail. You wouldn't be able to see that in the Order's list because Wealth-Lab rounds the prices based on Preferences > Advanced Options > Decimal Places (or the Symbol Info Manager settings for Futures).
profile picture

Gamba

#13
It seems to work now. I have to use a price method to round the price regarding the exchange:

CODE:
Please log in to see this code.

After that a stop order was submitted.


One additional question. Is it possible to send OCO orders the same way?
At the moment I use this piece of code to sned a stop order and a market on close order,
but only stop order is submitted:

CODE:
Please log in to see this code.


profile picture

Eugene

#14
Cone will correct me if I'm wrong but since that's an AtClose order, it should not get recognized. Quoting the User Guide:
QUOTE:
OCO orders are identified by placing a stop and limit order simultaneously for the same symbol. The OCO behavior is automatic and not optional.
profile picture

Cone

#15
The reason that only the Stop order is submitted is because "SellAt/CoverAt/ExitAt" signals all return a boolean. This will always be false on the Alert bar because bar + 1 (the future bar on which the trade occurs) does not yet exist. Consequently the next statement (SellAtClose) is not executed on that bar, i.e., no Alert. So, although it's good, logical programming structure for backtesting, this will not work for live trading.

Now to tackle the OCO "One Cancels Other" question.. it's complicated.. first, OCO would have to be supported by the broker (and therefore broker adapter). Wealth-Lab can't control that on the broker's end.

Second, if the broker/adapter were smart enough to create an OCO order, and,
IF the broker/adapter supported MOC orders, and,
IF the MOC order were sent in time to be valid on the exchange (15:45 for NYSE, 15:50 for Nasdaq), and,
IF you tested that you were processing the penultimate bar of the day you could write the exits like the following and it would work.
CODE:
Please log in to see this code.


But that's a lot of IFs, and if any of them do not exist, then you're out of luck.
profile picture

Cone

#16
I just found another rule about MOC orders - they cannot be canceled! Consequently, MOC orders are not compatible with OCO.

Really the best thing you can do in an automated way is decide if you want "out" at 15:59 and ExitAtMarket at that time.
profile picture

Gamba

#17
Thank you for the detailed explanation.

How should I send the MarketOrder?
I have no chart to stream the data, only the quote window.

Will my strategy be called after every quote update?
I am using daily bars with this strategy.
profile picture

Eugene

#18
QUOTE:
Will my strategy be called after every quote update?

No. It's executed on every bar update according to this FAQ: Is it necessary to have access to intra-bar tick data to daytrade with Wealth-Lab?

QUOTE:
I have no chart to stream the data, only the quote window.

Quotes windows work with orders, not strategies...
profile picture

Cone

#19
At the top, you said this was a daily strategy. The only way you'd be able to automatically trigger that 1559 order at market is to run a strategy with 1-minute bars for that purpose. And, to do it for 50 symbols, you'd need to use the Strategy Monitor.

The market exit would only need to know if the stop order were executed during the day. So, you just need to read a file of symbols and their stop prices that you generate with the original strategy, check for the low of the day, and if the price didn't reach the stop trigger, you place a market order with the 1559 bar update*.

*Depending on the intraday data provider, it may be required that a trade occurs between 1558 and 1559 for a bar to be generated in order to execute the script!

If you think that's a route you want to take, I can help you with the modifications. Who's your intraday data provider?
profile picture

Gamba

#20
Thank you Cone, but I have no intraday data for my portfolio. I will go further on with daily bars and will manually check the orders.
profile picture

Eugene

#21
QUOTE:
I have no intraday data for my portfolio

But what about the IB provider? It's supplying intraday data, isn't it?
profile picture

jgimene1

#22
Hello,

I want to confirm that I can use an automated trading strategy with order types MOO (market on open) and MOC(market on close) using API on Interactive brokers.

However here https://www.wealth-lab.com/extension/detail/InteractiveBrokers#information under information tab I only see Order types: Market, Limit, Stop, and Market On Close NO MOO (market on open) .

Thanks.

profile picture

Eugene

#23
Hello,

Please discuss WL7 and its Interactive Brokers integration on the WL7 forum:

https://www.wealth-lab.com/Discussions

This old forum is for WL6 questions and WL6 does not support trading with Interactive Brokers.

Thanks.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).