Question about using IF with AddOrder() in ThinkorSwim

K

kalivibe

New member
VIP
Maybe you can help with this. Just learning coding in TOS. I have a signal in a strategy as follows:

input barexit = 2;
def LongExt= LongEnt from barexit bars ago;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open, TradeSize);

where LongEnt is defined. The above is working properly. However, I am looking to actually only have a signal if have the entry of the next bar takes out the high of the current bar.

I am trying an if statement but can’t get it to work. This is what I tried.

AddOrder(If(high[-1] > high, (OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize), null);

Any help would be great as I am trying to learn.
 
RobertPayne

RobertPayne

Member
@RobertPayne Maybe you can help with this. Just learning coding in TOS. I have a signal in a strategy as follows:

input barexit = 2;
def LongExt= LongEnt from barexit bars ago;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open, TradeSize);

where LongEnt is defined. The above is working properly. However, I am looking to actually only have a signal if have the entry of the next bar takes out the high of the current bar.

I am trying an if statement but can’t get it to work. This is what I tried.

AddOrder(If(high[-1] > high, (OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize), null);

Any help would be great as I am trying to learn.

Rather than using an "IF" statement in the AddOrder, I would use an "AND" statement when you define your LongEnt variable.

For example, since you don't define your LongEnt condition above, I'm just going to say that the close must be above the 8 period exponential moving average AND the next open must be above the current high.

Ruby:
input barexit = 2;
input TradeSize = 1000;
def EMA = ExpAverage(close, 8);
def LongEnt = close > EMA and open[-1] > high;
def LongExt = LongEnt from barexit bars ago;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open, TradeSize);
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open, TradeSize);

You will see in the image below that the green bar is above the EMA8 (condition1) AND the next bar's open is above the green bar's high (condition2), so the order was opened.

 
K

kalivibe

New member
VIP
That worked great. Do you know of an easy way to export strategy results to excel in column format. When I hit the export button it saves a csv will all data in the same column.
 
BenTen

BenTen

Administrative
Staff
VIP
@kalivibe Export it as html and you should have all the proper columns. Then copy and paste them into an excel or Google Sheet.
 
A

arihantbaid

New member
VIP
Is it possible to use strategy to make buy and sell automatically once we have script on any stock without manual effort to buy and sell based on signals?
 

Similar threads

Top