ThinkScript strategy orders seem off

tayman

New member
Hi all, Im new to thinkscript. I was trying a really simple strategy script playing with moving averages, where I define a fast and slow EMA.
Works great. Then I wanted to play with the backtesting, just to make sure I understand how it works. But when I run it, I notice that the buy/sell orders its placing are several bars away from when the slow EMA crosses the fast, and vice versa. Im using 6 and 12 as the fast/slow periods. I would expect to see it placing buy/sells right when the fast/slow EMA crosses, but instead they show up several bars later. I've set the timeframe on the chart to 1 minute.

I tried to search for similar questions, but didn't see any pop up. Could anyone help clue me in on what Im doing wrong. See code below:


Code:
input price = close;
input fast_length = 6;
input slow_length = 12;

def fast_EmaPrice = ExpAverage(price, fast_length);
def slow_EmaPrice = ExpAverage(price, slow_length);

plot fast_EMA = fast_EmaPrice;
fast_EMA.SetDefaultColor(GetColor(0));

plot slow_EMA = 2 * slow_EmaPrice;
slow_EMA.SetDefaultColor(GetColor(4));

def STATE;
if fast_EmaPrice > slow_EmaPrice
then {
    STATE = 100;
} else if slow_EmaPrice > fast_EmaPrice
then {
    STATE = -100;
} else {
    STATE = 50;
}

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

AddOrder(OrderType.BUY_AUTO, condition = (STATE == 100),  name = "BUY_AUTO: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, condition = (STATE == -100),  name = "SEL_AUTO: " + BarNumber());

See screenshot, I highlighted where a couple of the first orders should show, instead of what Im actually seeing when I run it through the backtesting in tos
z8KAve3.png


Im wondering if I need to set an aggregation period, or if there is an offset at the start of the chart due to EMA period length?

Any help is much appreciated.

Thanks,
Taylor
 

rad14733

Well-known member
VIP
@tayman You're making it way too complicated... Review the many Strategies that come with Thinkorswim and you'll see how to properly code AddOrder()... There is no need to use BarNumber() and that may well be your issue... The order criteria should be based on crossover and whatever other criteria you might want to include... But a bit of warning, backtesting a Strategy will by no means reflect live trading... It might come close at times but it will never be as accurate as watching live trading and documenting entries and exits based on your charts, indicators, and Active Trader... Also, don't bother trying to implement Stop Loss or Trailing Stop Loss because they never calculate accurately... Just a heads-up from someone who has worked with them way more than I'd care to admit...
 

Pensar

Well-known member
VIP
Hi all, Im new to thinkscript. I was trying a really simple strategy script playing with moving averages, where I define a fast and slow EMA.
Works great. Then I wanted to play with the backtesting, just to make sure I understand how it works. But when I run it, I notice that the buy/sell orders its placing are several bars away from when the slow EMA crosses the fast, and vice versa. Im using 6 and 12 as the fast/slow periods. I would expect to see it placing buy/sells right when the fast/slow EMA crosses, but instead they show up several bars later. I've set the timeframe on the chart to 1 minute.

I tried to search for similar questions, but didn't see any pop up. Could anyone help clue me in on what Im doing wrong. See code below:


Code:
input price = close;
input fast_length = 6;
input slow_length = 12;

def fast_EmaPrice = ExpAverage(price, fast_length);
def slow_EmaPrice = ExpAverage(price, slow_length);

plot fast_EMA = fast_EmaPrice;
fast_EMA.SetDefaultColor(GetColor(0));

plot slow_EMA = 2 * slow_EmaPrice;
slow_EMA.SetDefaultColor(GetColor(4));

def STATE;
if fast_EmaPrice > slow_EmaPrice
then {
    STATE = 100;
} else if slow_EmaPrice > fast_EmaPrice
then {
    STATE = -100;
} else {
    STATE = 50;
}

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

AddOrder(OrderType.BUY_AUTO, condition = (STATE == 100),  name = "BUY_AUTO: " + BarNumber());
AddOrder(OrderType.SELL_AUTO, condition = (STATE == -100),  name = "SEL_AUTO: " + BarNumber());

See screenshot, I highlighted where a couple of the first orders should show, instead of what Im actually seeing when I run it through the backtesting in tos
z8KAve3.png


Im wondering if I need to set an aggregation period, or if there is an offset at the start of the chart due to EMA period length?

Any help is much appreciated.

Thanks,
Taylor
@tayman When looking over the code, it appears that the longer moving average is multiplied by 2. That may be causing the issue of misplaced buy / sell signals.

By the way, welcome to the forum!
 
Top