AddOrder thinkScript - Backtest Auto Buy & Sell in ThinkorSwim

diazlaz

Well-known member
2019 Donor
VIP
This thread demonstrates how you can use the AddOrder logic to create your own ThinkorSwim backtesting script. It also contains requests and code examples made by other members—worth looking over.

Hi Everyone,

Inspired by @RobertPayne and this post, for a while, I been trying to replicate the auto functionality in the AddOrder. Seems the behavior is different when from Auto and Manual on how AddOrder is executed.

I'm trying to simulate the buy and sell and simulate the reversal from going long to short or vice-versa in the same bar. Auto it seems the entry is taken at the same bar while manual is on the next bar.

Let's use this as an example:

YvfBUdf.png


Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;

AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);

AddOrder(OrderType.BUY_AUTO,no);

In this example, if close > ema then go long else go short, if you're in a long trade and trend changes, reverse and go short, vice-versa.

1) Using Auto:

Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;

AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);

AddOrder(OrderType.BUY_AUTO,no);

##Automatic
AddOrder(OrderType.BUY_AUTO, condition = (STATE == 100),  name = "BAUTO: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, condition = (STATE == -100),  name = "SAUTO: " + BarNumber());

mrrSK4Q.png


This works as expected, notice that is opening and closing the position at the same bar with a buy to open and a sell to close, vice-versa.

2) Using Manual

Ruby:
def EMA = ExpAverage(close, 8);
def STATE = if close > ema then 100 else -100;
plot EMA8 = EMA;

AssignPriceColor (
if STATE == 100 then COLOR.GREEN else COLOR.RED);

##Long
AddOrder(OrderType.BUY_TO_OPEN, condition = (STATE == 100),  name = "BTO: " + BarNumber());
AddOrder(OrderType.SELL_TO_CLOSE, condition = (STATE == -100),  name = "STC: " + BarNumber());

##Short
AddOrder(OrderType.SELL_TO_OPEN, condition = (STATE == -100),  name = "STO: " + BarNumber());
AddOrder(OrderType.BUY_TO_CLOSE, condition = (STATE == 100),  name = "BTC: " + BarNumber());

The behavior is different, it executes in the next bar as such:

wVMuAnH.png


The reason, is i'm trying to build a strategy that will reverse on trend, but at times, I will pause getting into new positions, but want to close the position when trend change occurs BUT entire in the direction of the trend when that condition is cleared.

Any suggestions or ideas would be greatly appreciated.
 
Last edited by a moderator:

kalivibe

New member
VIP
Thanks for all your help. I have another question regarding strategy exits. Again, just learning. I am trying to exit when the first of two actions occurs. When I use this looking for just one parameter it works properly.

Code:
input barexit = 2;
def LongExt= LongEnt from barexit bars ago;
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, high, TradeSize);

Where LongEnt is also already defined.

However when I try this, I don't get an error but it seems like half of the trades are no longer there?

Code:
input TargetRatio = 1.015;
input barexit = 2;
def LongTarget = LongEnt * TargetRatio;
def LongExt= LongTarget or LongEnt from barexit bars ago;
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, close, TradeSize);

Any help would be great!

Is EntryPrice() known by thinkscript? Also can I change (close >= LongTarget) to (high >= LongTarget)? Would I need to change the Sell to close code as well then?
 

kalivibe

New member
VIP
Thanks!! Really appreciate it. Sorry for all newbie questions. Is changing the following OK?

(close >= LongTarget) to (high >= LongTarget)?

Would I need to change the Sell to close code as well then?
 

YungTraderFromMontana

Well-known member
I have the credentials for what I want to trigger the orders in the strategy but am a novice at coding and can't figure out how to create the automatic orders. All I need is code where I can quickly plug in my credentials for when a order takes place. (ex buy put when , sell put when).

I finally have the signals and orders on my strategy finished but now want to backtest it. Ideally it would allow me to fine tune my strategy by showing me an updating P/L. When I would make a change in the strategy, there would also be a change in the results. This could allow me to perfect my strategy and squeeze every possible dollar or cut every preventable loss. Any feedback will be helpful in passing this final step so thanks everyone.
 

kalivibe

New member
VIP
Hi - I am still struggling to get this to work. I am trying to basically put in a target and if that is not after bars exit. With the code below it always exits after 2 bars only. Any advice would be great. Thanks.

Code:
def TradeSize =100;
input TargetRatio = 1.015;
input TargetRatioShort = .015;
input barexit = 2;
def LongEnt = UpsideBreakout and high[-1] > high;
def LongTarget = EntryPrice () * TargetRatio;
def LongExt= (high >= LongTarget) or (LongEnt from barexit bars ago);

AddOrder(OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, close, TradeSize);
def ShortEnt= DownsideBreakout and low[-1] < low;
def ShortTarget = EntryPrice ()  - (TargetRatioShort * EntryPrice ());

def ShortExt = ShortTarget or ShortEnt from barexit bars ago;

AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, low, TradeSize);

AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, close, TradeSize);

Wondering if there is anyone that would help me with some programming for payment? I have indicators I have created that I use for entry. I would like to test them using various exit methods . . . taking partial profit at an ATR based target, exiting remainder with a ATR trail stop or based on an indicator.
 

kalivibe

New member
VIP
Thanks. Here is an example. Right now this exits after 2 bars. However I would like to create the exits I described above additionally. Very, very new to attempting to program.

Code:
def RBup = close > open and high < high[1]  and high[1] < high[2] and low < low[1] and close[1] < open[1] and high[-1] > high;
plot bullish = if RBup then high else Double.NaN;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
alert(bullish,"RB Up", alert.Bar,sound.ding);
def RBdown = close < open and low > low[1]  and low[1] > low[2] and high > high[1] and close[1] > open[1] and low[-1] < low;
plot bearish = if RBdown then low else Double.NaN;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
alert(bearish,"RB Down", alert.Bar, sound.ding);

def TradeSize =100;
input barexit = 2;
def LongEnt = RBup;
def LongExt= LongEnt from barexit bars ago;

AddOrder(OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, high, TradeSize);
def ShortEnt= RBdown;
def ShortExt = ShortEnt from barexit bars ago;

AddOrder(OrderType.SELL_TO_OPEN, ShortEnt, low, TradeSize);

AddOrder(OrderType.BUY_TO_CLOSE, ShortExt, low, TradeSize);
 

tomsk

Well-known member
VIP
How do you guys backtest a strategy on ThinkorSwim? I'd like to take a look at the results for the Sequence Counter found in TOS.

@Miket You can backtest any studies by using the Strategy functionality on TOS.
Here is a very simple example of a strategy
Bullish when price closes above the 34 EMA and bearish when prices closes below the 34 EMA

Code:
input length = 34;
input price = close;

plot EMA = ExpAverage(price, length);
EMA.AssignValueColor(if EMA > EMA[1]
                     then Color.Green
                     else Color.Red);
AssignPriceColor(if EMA > EMA[1]
                 then Color.Green
                 else Color.Red);
AddOrder(OrderType.BUY_AUTO, EMA crosses above EMA[1]);
AddOrder(OrderType.Sell_AUTO, EMA crosses below EMA[1]);

To see the results of your strategy, position your mouse on any of the strategy points on your chart
Right click the mouse and select "Show report"
You will then see a table of your buy/sell strategy with date of trade, price as well as P/L from that strategy
 

msammons

Member
In OnDemand TOS back testing is it possible to use an if statement for an AddOrder?

Something that would, for example, sell short every cross-below but buy long every cross-above (and closes trade when it crosses the other way): all in one TOS strategy

Something like:

def cross-above= cross above EMA;
def cross-below = cross below EMA;

if cross-above then AddOrder(OrderType.BUY_TO_OPEN, cross-above, open[-1], 100) else if cross-below then AddOrder(OrderType.SELL_TO_OPEN, cross-below, open[-1], 100) else no;
if close > EMA then AddOrder(OrderType.SELL_TO_CLOSE, cross-below, open[-1], 100) else if close < EMA then AddOrder(OrderType.BUY_TO_CLOSE, cross-above, open[-1], 100) else no;
 

tomsk

Well-known member
VIP
Don't think that is a valid syntax and from all the different AddOrder statements I have seen, it is generally used without any if-then-else construct. Probably best to vary the condition upon which a buy/sell order is triggered, so if the condition is not triggered, no buy/sell order is effected.
 
Last edited:

kaflesu

New member
I am trying to backtest the strategy using the thinkorswim. Whenever I add the strategy and add the FloatingPL, after pressing apply it disappear from the Lower. Does anyone know the reason?

Nevermind, It seems like I have created study, not the strategy.
 

Rainmaker

New member
Hi everyone, I have been trying this out for hours and would appreciate some help. I am trying to set up a simple conditional order such that when a stock on a 5-minute chart closes past a certain level, it triggers a market buy order.

I am using other timeframes for the chart, so I didn't use the default "<current period>" and used the AggregationPeriod.FIVE_MIN

This is the entire code on thinkScript editor:
close("period" = AggregationPeriod.FIVE_MIN) more than 100 within 60 bars

I got the error "secondary period cannot be less than primary." Can someone tell me what went wrong?
 

Bobbydigital83

Member
VIP
I'm looking all over the internet to for an idea but cannot find one. I have created these lines of code but results are not producing the results that I was expecting. I have basic TOS coding skills but this one is really frustrating me on figuring out.

Code:
AddOrder(OrderType.SELL_TO_CLOSE, EntryPrice() + 2.5 or EntryPrice() - 1); #TP at  EP +2.5 points or SL at EP -1 points
AddOrder(OrderType.buy_TO_CLOSE, EntryPrice() - 2.5 or EntryPrice() + 1); #TP at EP -2.5 points or SL at EP +1 points

Any help would be greatly appreciated!
 

dvorakm

New member
2019 Donor
VIP
I am trying to test out a strategy, but the sell orders are not behaving (selling at wrong time) and therefore jacking with the open orders. Could someone please help me with a command to test out the buying only part of a strategy? That is, implement only the buying portion of the strategy so I can test and refine that portion.

When I comment out the "sell" potion, I only get one "buy" order, because it is expecting a sell to close. What I am trying to do is bypass that and have multiple buy orders triggered by the strategy, ignoring the "sell" part. Then, I would be able to refine the selling part of the script so that it sells at the correct time.
 

gwbabcock

New member
One way to do it is to keep your logic out of AddOrder statement. Here is an example:

Code:
input EMALenShort = 9;
input EMALenLong = 20;

def EMAPlus = (MovAvgExponential(close, EMALenShort) - MovAvgExponential(close, EMALenLong)) >= 0;

AddOrder(OrderType.BUY_AUTO, EMAPlus,   open[1],  tickcolor = GetColor(0), arrowcolor = GetColor(0),name = "BAuto: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, !EMAPlus,   open[1],  tickcolor = GetColor(1), arrowcolor = GetColor(1),name = "SAuto: " + BarNumber());

Also note BUY_AUTO and SELL_AUTO will automatically switch when EMAPlus goes from positive to negative and vv.
 

Similar threads

Top