Moving Avg in One Study Not Displaying

dmw4k4

New member
Hello,
Im trying to do a tidy little MA script with the 10 21 23e 50 65e all in one study. However, on recent IPOs there isnt enough data for most of these and if the average() returns nothing, it removes my plots of the averages that do return data. Ive tried all kinds of if statements but they seem to execute both sides of the statement and it then invalidates the plots again. Sorry if this has been asked before, but I tried to search and may have not used the right filter words.

Example of script:
Code:
#Establish the 10sma and query whether it is a daily or weekly chart
plot short_SMA10;
def SMA10;
if barNumber() > 10 {
    if GetAggregationPeriod() > AggregationPeriod.DAY {
        SMA10 = Average(price, 2);
    } else {
        SMA10 = Average(price, 10);
    }
    short_SMA10 = if show_sma10 then SMA10 else double.nan;
} else {
    SMA10 = double.nan;
    short_SMA10 = double.nan;
}   
short_SMA10.SetDefaultColor(Color.GREEN);

#Establish the 21sma and query whether it is a daily or weekly chart
def sma21 = average(price,21);
def blah = double.nan;
def graphme;

    if barNumber() < 21 {
       graphme = blah;
    } else {
      graphme = sma21 ;

    }

plot short_SMA21 = graphme;
short_SMA21.SetDefaultColor(Color.CYAN);
 
on recent IPOs there isnt enough data for most of these and if the average() returns nothing, it removes my plots of the averages that do return data.

You answered your own question.
 
@dmw4k4 The crux of the issue is you can't have a tidy little all-in-one study if all of the data isn't available yet... Without a lot of lines of code, your easiest solution is to load multiple MA Studies and let the ones that don't have enough data just sit dormant until they get enough data to display... Sometimes simpler is better... The only reason to combine all of the MA's would be if you are going to take actions based on all of the data which, again, isn't always available... So it becomes a circular problem, kinda like the tail chasing the dog instead of the dog chasing the tail... However, even if the MA's don't display you could still have a dashboard that displays even if not enough dat is available... You could have Green, Red, and Gray (or another color) to indicate not enough data...
 
@dmw4k4 The crux of the issue is you can't have a tidy little all-in-one study if all of the data isn't available yet... Without a lot of lines of code, your easiest solution is to load multiple MA Studies and let the ones that don't have enough data just sit dormant until they get enough data to display... Sometimes simpler is better... The only reason to combine all of the MA's would be if you are going to take actions based on all of the data which, again, isn't always available... So it becomes a circular problem, kinda like the tail chasing the dog instead of the dog chasing the tail... However, even if the MA's don't display you could still have a dashboard that displays even if not enough dat is available... You could have Green, Red, and Gray (or another color) to indicate not enough data...
Ah okay. I was hoping I just missed a clever trick. Bit frustrating that it just doesn't plot the data that's present but thinkscript has been hard to wrangle for some things for me. Appreciate the response and I'll prob go with data bars for quick viewing. Thanks and have a nice night
 

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