Need help creating a Smart Exponential MA Indicator

sheldon365

New member
Hello Forum Members,

I would like your thoughts and help to create the ThinkScript for a Smart Moving Average Buy and Sell Indicator with some logic embedded in it. I do have pieces of the logic that I was easily able to develop using the Moving Avg Crossover Indicator but I am struggling to get the script for the 50MA over 200MA part right.

Here is the logic: let me know your thoughts and if you can backtest this and let me know the probability of profit.

  • Bull Market
  • Display Buy (Yellow Indicator) when
    • Price crosses 20 EMA from above
    • 50 EMA is above 10 EMA
    • 20 EMA is above 50 EMA
    • 50 EMA is above 200 EMA
  • Display Sell (Red Indicator) when
    • Price crosses 10 EMA from above
    • 10 EMA is above 20 EMA
    • 20 EMA is above 50 EMA
    • 50 EMA is above 200 EMA
  • Bear Market
  • Display Sell (Orange Indicator)when
    • Price crosses 20 EMA from below
    • 200 EMA is above 50 EMA
    • 50 EMA is above 20 EMA
  • Display Buy (Blue Indicator) when
    • Price crosses 10 EMA from below
    • 200 EMA is above 50 EMA
    • 50 EMA is above 20 EMA
    • 20 EMA is above 10 EMA
 
Any help and insights are highly appreciated!! Please and thank you🤲🙏

typing on my cell, forgive if there are typos....


"Price crosses 20 EMA from above"

i would change your wording to match what will be used. if something came from above , then crossed it, it will be below it. do you mean when price crosses the 20 and ends up below it?

Price crosses below 20 EMA


you don't need to use a crossover study. just create the formulas to compare the variables.

define variables for 4 averages

here is code for 2 averages.
copy 1 set , 2 more times and change things for 50 and 200.

i copied what i already had, so the variable names are generic, i didn't change them.


input avg1_type = AverageType.EXPONENTIAL;
input avg1_price = close;
input avg1_len = 10;
def ma1 = MovingAverage(avg1_type, avg1_price, avg1_len);

input avg2_type = AverageType.EXPONENTIAL;
input avg2_price = close;
input avg2_len = 20;
def ma2 = MovingAverage(avg2_type, avg2_price, avg2_len);

if you want, set new variables if it makes the code easier to figure out what is happening.

def ema10 = ma1;
def ema20 = ma2;
def ema50 = ma3;
drf ema200 = ma4;


then create 4 formulas for the buys and sells. here is one formula.
( check if cross below is what you intended, your description is confusing)

def bullbuy =
(close crosses below ema20
and EMA50 > EMA10
and EMA20 > EMA50
and EMA50 > EMA200);

# draw an up arrow under the bar , of a bullbuy
plot zbullbuy = if bullbuy then low else double.nan;
zbullbuy.setpaintingStrategy(PaintingStrategy.ARROW_UP);
zbullbuy.setdefaultColor(color.yellow);


https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/MovingAverage

https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
 
Last edited:

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

Hello Forum Members,

I would like your thoughts and help to create the ThinkScript for a Smart Moving Average Buy and Sell Indicator with some logic embedded in it. I do have pieces of the logic that I was easily able to develop using the Moving Avg Crossover Indicator but I am struggling to get the script for the 50MA over 200MA part right.

Here is the logic: let me know your thoughts and if you can backtest this and let me know the probability of profit.

  • Bull Market
  • Display Buy (Yellow Indicator) when
    • Price crosses 20 EMA from above
    • 50 EMA is above 10 EMA
    • 20 EMA is above 50 EMA
    • 50 EMA is above 200 EMA
  • Display Sell (Red Indicator) when
    • Price crosses 10 EMA from above
    • 10 EMA is above 20 EMA
    • 20 EMA is above 50 EMA
    • 50 EMA is above 200 EMA
  • Bear Market
  • Display Sell (Orange Indicator)when
    • Price crosses 20 EMA from below
    • 200 EMA is above 50 EMA
    • 50 EMA is above 20 EMA
  • Display Buy (Blue Indicator) when
    • Price crosses 10 EMA from below
    • 200 EMA is above 50 EMA
    • 50 EMA is above 20 EMA
    • 20 EMA is above 10 EMA

A good reference to start with would be to review how TOS's built-in moving average strategy works. See image below.

The Education Tab has a numerous information on how to use Strategies. Here are some links
Capture.jpg
 
typing on my cell, forgive if there are typos....


"Price crosses 20 EMA from above"

i would change your wording to match what will be used. if something came from above , then crossed it, it will be below it. do you mean when price crosses the 20 and ends up below it?

Price crosses below 20 EMA


you don't need to use a crossover study. just create the formulas to compare the variables.

define variables for 4 averages

here is code for 2 averages.
copy 1 set , 2 more times and change things for 50 and 200.

i copied what i already had, so the variable names are generic, i didn't change them.


input avg1_type = AverageType.EXPONENTIAL;
input avg1_price = close;
input avg1_len = 10;
def ma1 = MovingAverage(avg1_type, avg1_price, avg1_len);

input avg2_type = AverageType.EXPONENTIAL;
input avg2_price = close;
input avg2_len = 20;
def ma2 = MovingAverage(avg2_type, avg2_price, avg2_len);

if you want, set new variables if it makes the code easier to figure out what is happening.

def ema10 = ma1;
def ema20 = ma2;
def ema50 = ma3;
drf ema200 = ma4;


then create 4 formulas for the buys and sells. here is one formula.
( check if cross below is what you intended, your description is confusing)

def bullbuy =
(close crosses below ema20
and EMA50 > EMA10
and EMA20 > EMA50
and EMA50 > EMA200);

# draw an up arrow under the bar , of a bullbuy
plot zbullbuy = if bullbuy then low else double.nan;
zbullbuy.setpaintingStrategy(PaintingStrategy.ARROW_UP);
zbullbuy.setdefaultColor(color.yellow);


https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/MovingAverage

https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
Thank you so much, I was able to figure out the script using your direction. I have a question on how to prevent false signals when the moving averages are flat(range-bound market), is there a code that can be added to prevent Moving averages that are flat from showing up a signal?

I currently have is EMA20 > EMA10 (for my BullBuy signal) to prevent that from happening but it still shows me a signal although it should not show me one.
 
Thank you so much, I was able to figure out the script using your direction. I have a question on how to prevent false signals when the moving averages are flat(range-bound market), is there a code that can be added to prevent Moving averages that are flat from showing up a signal?

I currently have is EMA20 > EMA10 (for my BullBuy signal) to prevent that from happening but it still shows me a signal although it should not show me one.

maybe check if the bar to bar change of an average is small, less than some %.
guessing at a % of 0.3.

Code:
input flat_limit_percent = 0.3;
def avg_chg = ma1 - ma1[1];
def isflat = ( (100 * ( avg_chg / ma1)) <= flat_limit_percent);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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