Strategy with 3 MAs?

Of course! Almost anything you can imagine can be coded. Some are ... easier than others. however.

Let's take three moving averages, a short, a medium and a long.
If the short crosses above the middle, we enter long but only if the long MA is trending upward.
similarly, we enter short if the short crosses below the mid and the long is trending downward.

Code:
declare upper;
input short = 10;
input medium = 50;
input long = 150;

def SMA = Average(CLOSE, short);
def MMA = Average(CLOSE, medium);
def LMA = Average(CLOSE, long);

def trend = if LMA > LMA[1] then 1 else if LMA < LMA[1] then -1 else 0;
def enter_long = if SMA crosses above MMA AND trend == 1 then low else double.nan;
def enter_short = if SMA crosses below MMA AND trend == -1 then high else double.nan;

AddOrder(OrderType.BUY_AUTO, !isNaN(enter_long), open[-1], 50, Color.ORANGE, Color.ORANGE, "Sample buy @ " + open[-1]);
AddOrder(OrderType.SELL_AUTO, !isNaN(enter_short), open[-1], 50, Color.ORANGE, Color.ORANGE, "Sample sell @ " + open[-1]);

I'm not sure this answered your question, but it certainly is a strategy that uses three moving averages. I doubt it would be too profitable, but one never knows. Perhaps your Holy Grail is in there somewhere.

Good luck and happy trading,
mashume
 

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

Thank you for your reply and for your help.
A friend swears by 9-SMA, 20-EMA, and 50-SMA he said it's a "Johnny Bravo swing strategy" and I wanted to backtest it against my strategy. I included a picture to show where a buy order was and then circled where a buy and sell order should be relative to the strategy.

Edit: To clarify, you would be a net buyer when the 9SMA is above the 50SMA and a net seller when 9SMA below the 50SMA. Example - when 9SMA is above 50SMA (UpTrend) and 9SMA is above the 20EMA it initiates a buy to open, and when 9SMA is above 50SMA (UpTrend) and 9SMA is below 20EMA it initiates a sell to close (stop). In reverse; when 9SMA is below 50SMA (DownTrend) and 9SMA is below the 20EMA it initiates a sell to open, and when 9SMA is below 50SMA (DownTrend) and 9SMA is above 20EMA it initiates a buy to close (stop).

https://photos.app.goo.gl/ovHFKr4dUXAM9uRJ8

I modified your code to fit the strategy defaults; however, I do not know how to write the code to meet all the conditions. This was my confusion around 3 MAs, haha it gets tricky in the details. Note, SMA is now slowMA not shortMA:
Code:
declare upper;
input Fast = 9;
input AverageTypeFast = AverageType.SIMPLE;
input Medium = 20;
input AverageTypeMedium = AverageType.EXPONENTIAL;
input Slow = 50;
input AverageTypeSlow = AverageType.SIMPLE;

plot FMA = MovingAverage (AverageTypeFast, CLOSE, Fast);
plot MMA = MovingAverage (AverageTypeMedium, CLOSE, Medium);
plot SMA = MovingAverage (AverageTypeSlow, CLOSE, Slow);

def trend = if SMA > SMA[1] then 1 else if SMA < SMA[1] then -1 else 0;
def enter_long = if SMA crosses above MMA AND trend == 1 then low else double.nan;
def enter_short = if SMA crosses below MMA AND trend == -1 then high else double.nan;

AddOrder(OrderType.BUY_AUTO, !isNaN(enter_long), open[-1], 50, Color.GREEN, Color.Green, "Sample buy @ " + open[-1]);
AddOrder(OrderType.SELL_AUTO, !isNaN(enter_short), open[-1], 50, Color.RED, Color.RED, "Sample sell @ " + open[-1]);

FMA.SetDefaultColor(Color.YELLOW);
MMA.SetDefaultColor(Color.ORANGE);
SMA.SetDefaultColor(Color.RED);
 
Last edited:
Most trading / investing strategies are completely dependent on the time frame you want to work in, and adjust your systems / indicators accordingly. For example, I trade NQ futures, and although you could "try" to use futures for longer term "investing", you are really well advised to keep your time frames within the minutes, hours, or at most a few days. My typical holding period is minutes to hours maximum. Lately, at $20 per point per contract, NQ has moved 100 points ($2000) in minutes, and 500 points ($10,000) during the day. So, I will use moving averages in my strategy, but they are on a 2 minute chart. Shorter time MAs ( 3, 5, 9, 10) for entries, longer term MAs (20 & 50) for trend and direction. The crosses are quick, and the signals are clear. (Please note that I also use ADX and RSI very heavily in my trading system.) Because of the lag, I could never use MA crosses on a 15 minute, hour, or daily chart to trade for minutes to hours.

For longer term investors ( years holding), you can certainly use a daily chart and watch for longer term MA crosses such as 50, 100, 200 day MA crosses. This strategy has helped some longer term investors avoid big declines after a big rally (like we have just seen, 2008 - 2022) and get in long after a big decline ( such as after 2000 & 2008 declines) or very possibly after the potential big decline that we are currently working out. More so than any MA crosses, the overall economy and investing climate is much more important than any technical system you could use for "investing."

Using MAs to build a trading system is a good start, but should be enhanced with other indicators to fine tune them for trend and direction and entry triggers.

I also agree with @mashume that some strategies using MAs will not have extremely generous outcomes.
 
Most trading / investing strategies are completely dependent on the time frame you want to work in, and adjust your systems / indicators accordingly. For example, I trade NQ futures, and although you could "try" to use futures for longer term "investing", you are really well advised to keep your time frames within the minutes, hours, or at most a few days. My typical holding period is minutes to hours maximum. Lately, at $20 per point per contract, NQ has moved 100 points ($2000) in minutes, and 500 points ($10,000) during the day. So, I will use moving averages in my strategy, but they are on a 2 minute chart. Shorter time MAs ( 3, 5, 9, 10) for entries, longer term MAs (20 & 50) for trend and direction. The crosses are quick, and the signals are clear. (Please note that I also use ADX and RSI very heavily in my trading system.) Because of the lag, I could never use MA crosses on a 15 minute, hour, or daily chart to trade for minutes to hours.

For longer term investors ( years holding), you can certainly use a daily chart and watch for longer term MA crosses such as 50, 100, 200 day MA crosses. This strategy has helped some longer term investors avoid big declines after a big rally (like we have just seen, 2008 - 2022) and get in long after a big decline ( such as after 2000 & 2008 declines) or very possibly after the potential big decline that we are currently working out. More so than any MA crosses, the overall economy and investing climate is much more important than any technical system you could use for "investing."

Using MAs to build a trading system is a good start, but should be enhanced with other indicators to fine tune them for trend and direction and entry triggers.

I also agree with @mashume that some strategies using MAs will not have extremely generous outcomes.
Scott can you share how you incorporate RSI into your trading strategy? I am leaning towards a lower period RSI. The 14 period to me just seems to slow. What are your thoughts? Thank you
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
382 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