Question on AutoTrade Almost..


Category:
0
0

Hello Pete

I’m a bit confused as how this order gets executed, (attached is the pic) since those 2 statements, BUY_TO_OPEN and then SELL_TO_CLOSE, seems to be opening a position based on the when the signal’s condition is met and then same for exiting out.

But Once I click on ASK and then click on the gear icon, and define the condition, the order is already open and waiting for the condition to met and then get executed, so how this BUY_TO_OPEN and SELL_TO_CLOSE works.

Thanks.

Attachments:
Marked as spam
Posted by (Questions: 9, Answers: 8)
Asked on May 16, 2018 11:16 am
269 views
1
Private answer

Ok, based on the previous comments from Robert on 5/17/18, I think I still have some things to explain here. I believe the issue is a problem in defining the terms.

Conditional Orders: Do NOT contain AddOrder() statements. Do NOT contain both a buy and sell signal. Conditional orders are applied to an actual order. The order is sent to market when the condition of the code evaluates to true.

Theoretical Orders: Are plotted on a chart and DO contain the AddOrder() statements.

So let’s make sure you understand the terms and are using them correctly.

From the webpage of the video named “Thinkorswim AutoTrade Almost” we have this template:

This is the code for the Strategy Template:
input tradeSize = 100;
def signal = 0;
addOrder(OrderType.BUY_TO_OPEN, signal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
def exit = 0;
addOrder(OrderType.SELL_TO_CLOSE, exit, open[-1], tradeSize, Color.MAGENTA, Color.MAGENTA);

You see that? This is the code for the Strategy Template. This has absolutely nothing to do with a Conditional Order. No connection whatsoever. This is used to plot Theoretical orders on a Chart Strategy.

Taking the code from your example screenshot:

input tradeSize = 100;
def signal = RSI()."RSI" crosses above 25 and close crosses below BollingerBands()."LowerBand" within 8 bars;
addOrder(OrderType.BUY_TO_OPEN, signal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
def exit = RSI()."RSI" < RSI()."RSI"[1] and high > BollingerBands()."UpperBand" within 4 bars;
addOrder(OrderType.SELL_TO_CLOSE, exit, open[-1], tradeSize, Color.MAGENTA, Color.MAGENTA);

This is NOT for a Conditional Order. This is for a Chart Strategy. There are two AddOrder() statements in this code. One for a buy to open and one for a sell to close. This can only plot long sided trades. It cannot plot a sell order on the chart before a buy order has been plotted. You cannot use this for a conditional order. Why? The order types. BUY_TO_OPEN and SELL_TO_CLOSE. This expressly excludes SELL_TO_OPEN. So shorts are impossible here.

So, you want to see what part of that code CAN be used for a conditional order? Code that you can apply to an actual order that gets submitted to the market?

input tradeSize = 100;
def signal = RSI()."RSI" crosses above 25 and close crosses below BollingerBands()."LowerBand" within 8 bars;

That’s it. That is your buy condition that you can apply to an order that gets submitted to market. When the condition evaluates to true the order gets sent to market.

What about the exit?

input tradeSize = 100;
def exit = RSI()."RSI" < RSI()."RSI"[1] and high > BollingerBands()."UpperBand" within 4 bars;

Hope this clears things up.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 17, 2018 2:14 pm
0

Great, Thanks.!

( at May 17, 2018 5:36 pm)
0
Private answer

What you have in the screenshot you provided is for the Chart Strategy. This only get’s plotted on the chart. This is not the code you would use to create a Conditional Order. It would not work at all for a Conditional Order. So to address your initial statement directly: “I’m a bit confused as how this order gets executed,”. Well that’s just it. The order never gets executed.

I hate to say this. But your question indicates you are missing a very important step. Did you watch the entire video without skipping? Because you entirely the missed the parts where I explained how to convert this code from a Chart Strategy to a Conditional Order. The solution is far more complex than we can cover here. So I can only direct you back to the video to search for the parts you missed, or skipped over.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 16, 2018 11:34 am
0

Hello Pete

I did watched the video few times, it seems complicated in the beginning, now that it clear that the order never get executed I understood the video.

The RSI and Bollinger band sets up the condition to open order and then also closes order, but I see in order for this to happen there has to be a buy conditional order and another one as a sell conditional order.

What happens if the sell condition is met on the sell order first, wouldn’t it open a short position?

( at May 17, 2018 12:04 pm)