displaced stacked moving averages

dbenz45

New member
displaced stacked moving averages indicator, issues having worked already checked all other questions

here is my code for 2 indicators, I want 2 small boxes to appear in the upper left and show green box if stacked bullish and grey if not stacked and red if bearish stacked, the 2 boxes (1 will be for 3 stacked moving averages and the other 1 will be for 4 moving averages as a stricter criteria), the issue is with this code there are two black and blank boxes that pop up and they don't work as intended, not sure if the indicator is having issues with the negative displacement number to displace these moving averages into the future. i want the displacement values to be negative to make these moving averages be displaced into the future. any help is much appreciated. have tried a few different ways of putting this together and always give a black box with no words in it

def price1 = hl2;
def price2 = hl2;
def price3 = hl2;
def price4 = hl2;
def length1 = 4;
def length2 = 9;
def length3 = 17;
def length4 = 34;
def displace1 = 2;
def displace2 = 4;
def displace3 = 9;
def displace4 = 17;

def sma5 = Average(price1[-displace1], length1);
def sma10 = Average(price2[-displace2], length2);
def sma20 = Average(price3[-displace3], length3);
def sma40 = Average(price4[-displace4], length4);

def bullishcascading1 = sma10 > sma20 and sma20 > sma40;
def bullishcascading2 = sma5 > sma10 and sma10 > sma20 and sma20 > sma40;
def bearishcascading1 = sma10 < sma20 and sma20 < sma40;
def bearishcascading2 = sma5 < sma10 and sma10 < sma20 and sma20 < sma40;

AddLabel(yes, " Stacked ", if bullishcascading1 then Color.GREEN else if bearishcascading1 then Color.RED else Color.GRAY);

AddLabel(yes, " Stacked ", if bullishcascading2 then Color.GREEN else if bearishcascading2 then Color.RED else Color.GRAY);
 
Solution
@dbenz45
Displacement does not work the same as offset. With a negative displacement all the SMA definitions are shifted to the left (past) and are Double.NaN at the current bar. Use a positive displacement to shift the SMA to the right (future).
@dbenz45
Displacement does not work the same as offset. With a negative displacement all the SMA definitions are shifted to the left (past) and are Double.NaN at the current bar. Use a positive displacement to shift the SMA to the right (future).
 
Solution

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