Show Moving Average and AddChartBubble when price is near

cds231

New member
I've written a script that works ok, but I'd love for it to be more automated. The gist is that I like to see multi-time frame moving averages, but only when the price is getting close to them. Otherwise the price action is too "squished" on the screen when the price scale settings are automatic and set to fit studies. I realize I can just uncheck "fit studies" but I like to see what's just outside the current price action highs and lows. I'm currently targeting showing the moving average if it's within 1% of the price (on /ES).

I've done the moving averages part fine, but I also want to use AddChartBubble to dynamically show the bubble when it's close (to help distinguish which time frame it's from) and that's what I'm struggling with. I can toggle with an input, but it appears I can't prevent repainting with the boolean time condition parameter AND use another variable to decide when to show. I typically add this study several times and like to see 15min 8EMA, 30m 21EMA, 1hr 50SMA, Day 5 and 21 EMA, etc.

Here are the scenarios I've tested:
AddChartBubble (IsNaN(close[1]) and !IsNaN(close[2]) and ShowBubble, location, custom text, custom color, boolean up); - shows a single bubble off to the right of the price action (good) but shows strictly based on the ShowBubble input (not ideal)
AddChartBubble (IsNaN(close[1]) and !IsNaN(close[2]) and PriceNear, location, custom text, custom color, boolean up); - doesn't work at all
AddChartBubble (PriceNear, location, custom text, custom color, boolean up); - works from a dynamic standpoint, but shows on every candle along the moving average

Apologies if I got any terminology incorrect (first time posting). Appreciate if anyone has any clever thoughts about either the code or settings I may be able to tweak.

Code:
# MTF Moving Average

input Period = AggregationPeriod.HOUR;
input AvgType = AverageType.EXPONENTIAL;
input Length = 21;
input priceclose = close;
input ShowCloud = No;
input ShowBubble = No;

DefineGlobalColor("Cloud", Color.GRAY);

def MoveAvg = MovingAverage(AvgType, close(period = Period), Length);
def PriceNear = ((AbsValue(close - MoveAvg)) / ((close + MoveAvg) / 2)) <= 0.01;

plot AVG = if PriceNear then MovingAverage(AvgType, close(period = Period), Length) else Double.NaN;
AVG.SetDefaultColor(Color.WHITE);

AddChartBubble(IsNaN(close[1]) and !IsNaN(close[2]) and ShowBubble, MoveAvg, if Period == AggregationPeriod.DAY then "D " + Length else if Period == AggregationPeriod.HOUR then "H " + Length else if Period == AggregationPeriod.THIRTY_MIN then "30m " + Length else if Period == AggregationPeriod.FIFTEEN_MIN then "15m " + Length else "W " + Length, Color.GRAY, no);

def OuterLimits = close < (AVG - (AVG * 0.005)) or close > (AVG + (AVG * 0.005));

AddCloud(if ShowCloud == 1 and OuterLimits == 1 then (AVG - (AVG * 0.01)) else Double.NaN, if ShowCloud == 1 and OuterLimits == 1 then (AVG + (AVG * 0.01)) else Double.NaN, Color.MAGENTA, GlobalColor("Cloud"), yes);
 
I've written a script that works ok, but I'd love for it to be more automated. The gist is that I like to see multi-time frame moving averages, but only when the price is getting close to them. Otherwise the price action is too "squished" on the screen when the price scale settings are automatic and set to fit studies. I realize I can just uncheck "fit studies" but I like to see what's just outside the current price action highs and lows. I'm currently targeting showing the moving average if it's within 1% of the price (on /ES).

I've done the moving averages part fine, but I also want to use AddChartBubble to dynamically show the bubble when it's close (to help distinguish which time frame it's from) and that's what I'm struggling with. I can toggle with an input, but it appears I can't prevent repainting with the boolean time condition parameter AND use another variable to decide when to show. I typically add this study several times and like to see 15min 8EMA, 30m 21EMA, 1hr 50SMA, Day 5 and 21 EMA, etc.

Here are the scenarios I've tested:
AddChartBubble (IsNaN(close[1]) and !IsNaN(close[2]) and ShowBubble, location, custom text, custom color, boolean up); - shows a single bubble off to the right of the price action (good) but shows strictly based on the ShowBubble input (not ideal)
AddChartBubble (IsNaN(close[1]) and !IsNaN(close[2]) and PriceNear, location, custom text, custom color, boolean up); - doesn't work at all
AddChartBubble (PriceNear, location, custom text, custom color, boolean up); - works from a dynamic standpoint, but shows on every candle along the moving average

Apologies if I got any terminology incorrect (first time posting). Appreciate if anyone has any clever thoughts about either the code or settings I may be able to tweak.

Code:
# MTF Moving Average

input Period = AggregationPeriod.HOUR;
input AvgType = AverageType.EXPONENTIAL;
input Length = 21;
input priceclose = close;
input ShowCloud = No;
input ShowBubble = No;

DefineGlobalColor("Cloud", Color.GRAY);

def MoveAvg = MovingAverage(AvgType, close(period = Period), Length);
def PriceNear = ((AbsValue(close - MoveAvg)) / ((close + MoveAvg) / 2)) <= 0.01;

plot AVG = if PriceNear then MovingAverage(AvgType, close(period = Period), Length) else Double.NaN;
AVG.SetDefaultColor(Color.WHITE);

AddChartBubble(IsNaN(close[1]) and !IsNaN(close[2]) and ShowBubble, MoveAvg, if Period == AggregationPeriod.DAY then "D " + Length else if Period == AggregationPeriod.HOUR then "H " + Length else if Period == AggregationPeriod.THIRTY_MIN then "30m " + Length else if Period == AggregationPeriod.FIFTEEN_MIN then "15m " + Length else "W " + Length, Color.GRAY, no);

def OuterLimits = close < (AVG - (AVG * 0.005)) or close > (AVG + (AVG * 0.005));

AddCloud(if ShowCloud == 1 and OuterLimits == 1 then (AVG - (AVG * 0.01)) else Double.NaN, if ShowCloud == 1 and OuterLimits == 1 then (AVG + (AVG * 0.01)) else Double.NaN, Color.MAGENTA, GlobalColor("Cloud"), yes);

you are comparing prices just on the current bar.
if price moves up, then drops a bit, it will trigger a line not to draw. even though the line would be in the middle of the price range of bars on the chart.
Dj1HwvK.jpg



a better way would be to find the highest price and lowest price on the chart, and not show lines when lines go x% beyond those price levels.

---------------------------

it would be easier to turn off fit studies , so they wouldn't force the chart to compress vertically.

chart settings
price axis tab
fit studies , uncheck this

FwliPeh.jpg
 
you are comparing prices just on the current bar.
if price moves up, then drops a bit, it will trigger a line not to draw. even though the line would be in the middle of the price range of bars on the chart.
Dj1HwvK.jpg



a better way would be to find the highest price and lowest price on the chart, and not show lines when lines go x% beyond those price levels.

---------------------------

it would be easier to turn off fit studies , so they wouldn't force the chart to compress vertically.

chart settings
price axis tab
fit studies , uncheck this

FwliPeh.jpg
Thanks, I appreciate that suggestion about just using high and low of the chart to calc the 1% and draw the moving averages based on that. It didn't really bother me being spotty, but I may make that change to have more consistency.

Yeah, I mentioned that I looked at unchecking "Fit studies" but I was really hoping for a coding solution so I can see a little beyond the price action. If I'm looking at taking a position, I want to quickly see what's just below/above and could act as support/resistance. Plus, frankly, it's one of those things that I just really want to do for the sake of learning and because it sure feels like it should be possible. I'm not quite ready to give up on a coding 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
350 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