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.
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);