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
 
@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...
 
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!
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
389 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top