AddOrder isn't Entering on EMA / MA Cross as Expected

buywall

New member
Hi all,

I'm trying to write a stategy backtest for ema crossovers and I'm using a couple of videos as reference (the attached code is just adjustments of what I'm finding). Right now, I'm just trying to solve a problem where the entry isn't being done where the cross happens, but instead a bar, or a few bars later. I'm at a loss trying to solve this simple thing.

Any ideas?

Thank you -

Code:

Code:
input price = close;
input fastMALength = 10;
input slowMALength = 20;

input averageTypeFast = AverageType.EXPONENTIAL;
input averageTypeSlow = AverageType.EXPONENTIAL;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

AddOrder(OrderType.BUY_TO_OPEN, fastMA crosses above slowMA, close, quantity, name="open");
AddOrder(OrderType.SELL_TO_CLOSE, price crosses below fastMA, price, quantity, tickcolor = Color.ORANGE, arrowcolor = Color.ORANGE,name="close");

NdqCsZL.png
 
Hi all,

I'm trying to write a stategy backtest for ema crossovers and I'm using a couple of videos as reference (the attached code is just adjustments of what I'm finding). Right now, I'm just trying to solve a problem where the entry isn't being done where the cross happens, but instead a bar, or a few bars later. I'm at a loss trying to solve this simple thing.

Any ideas?

Thank you -

Code:

Code:
input price = close;
input fastMALength = 10;
input slowMALength = 20;

input averageTypeFast = AverageType.EXPONENTIAL;
input averageTypeSlow = AverageType.EXPONENTIAL;

input quantity = 100;

plot fastMA = MovingAverage(averageTypeFast, close, fastMALength);
plot slowMA = MovingAverage(averageTypeSlow, close, slowMALength);

AddOrder(OrderType.BUY_TO_OPEN, fastMA crosses above slowMA, close, quantity, name="open");
AddOrder(OrderType.SELL_TO_CLOSE, price crosses below fastMA, price, quantity, tickcolor = Color.ORANGE, arrowcolor = Color.ORANGE,name="close");

NdqCsZL.png
1. Your moving averages are defined to calculate at the "close" of the candle.
Therefore, you won't know until the candle closes if the cross actually happened, so the order is executed on the open of the next candle, which is what your image illustrates.

2. Your image does not display an order that is executing a "few bars later" so it is not possible to diagnose.
 
Last edited:
1. Your moving averages are defined to calculate at the "close" of the candle.
Therefore, you won't know until the candle closes if the cross actually happened, so the order is executed on the open of the next candle, which is what your image illustrates.

2. Your image does not display an order that is executing a "few bars later" so it is not possible to diagnose.

3. You can change your orders and moving averages to calculate on the "open" of the candle, but you will miss all the crosses that might happen during that candle and so it won't execute until the next candle, anyway.

Thank you for your response!

1. Should I use ohlc4 or open to calculate it?
2. Isn't the "open 100" an order and the cross happened at the lower (below the gap) red candle?
3. See number 1.

Also, this is the daily chart, in case that makes a difference.

What I'd like to eventually test is moving averages have crossed and the first time the price
closes above and / or closes above it. These will be separate tests.

rsZmt0A.png


Again, thank you for your response.
 
1. Your moving averages are defined to calculate at the "close" of the candle.
Therefore, you won't know until the candle closes if the cross actually happened, so the order is executed on the open of the next candle, which is what your image illustrates.

2. Your image does not display an order that is executing a "few bars later" so it is not possible to diagnose.
Ohh.. I'm seeing what you mean. I used OnDemand to replay the day I'm looking at.
 

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