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:
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
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
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
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