I didn't know if you wanted the average highs and lows combined (i.e. Average(H + L / 2, 5) or if you wanted them separate. This version has them separate, but you could easily modify this to just calculate the combined value.
Additionally, I don't know if you mean "prior" bars when you say "last 5 bars", so this one will include the last bar as one of the 5. It would have to be modified if you wanted to exclude the last bar and show bars 2-6 instead of 1-5.
There are also some other assumptions made, like you only want it plotted on the last 5 bars. Again, it would require modification if that's not the case, but it wasn't specified, so this is what was done.
I didn't know if you wanted the average highs and lows combined (i.e. Average(H + L / 2, 5) or if you wanted them separate. This version has them separate, but you could easily modify this to just calculate the combined value.
Additionally, I don't know if you mean "prior" bars when you say "last 5 bars", so this one will include the last bar as one of the 5. It would have to be modified if you wanted to exclude the last bar and show bars 2-6 instead of 1-5.
There are also some other assumptions made, like you only want it plotted on the last 5 bars. Again, it would require modification if that's not the case, but it wasn't specified, so this is what was done.
Ruby:
# DECLARATIONS
declare upper;
#USER INPUTS
input barsBack = 5;
input extendRight = no;
#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));
#DEFINITIONS
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def isTargetPeriod = !IsNaN(close) and IsNaN(close[-barsBack]);
def avgHighs = if !IsNaN(close) and IsNaN(close[-1]) then Average(high, barsBack) else avgHighs[1];
def lastAvgHigh = if isNaN(close[-barsBack]) then avgHighs[-barsBack] else Double.NaN;
def avgLows = if !IsNaN(close) and IsNaN(close[-1]) then Average(low, barsBack) else avgLows[1];
def lastAvgLow = if isNaN(close[-barsBack]) then avgLows[-barsBack] else Double.NaN;
#PLOTS
plot highLine =
if !extendRight and IsNaN(close) then Double.NaN
else lastAvgHigh
;
plot lowLine =
if !extendRight and IsNaN(close) then Double.NaN
else lastAvgLow
;
plot midline = (highLine + lowLine) / 2;
#FORMATTING
highLine.SetDefaultColor(GlobalColor("Green"));
highLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine.HideTitle();
highLine.HideBubble();
lowLine.SetDefaultColor(GlobalColor("Red"));
lowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine.HideTitle();
lowLine.HideBubble();
midLine.SetDefaultColor(GlobalColor("Gray"));
midLine.SetPaintingStrategy(PaintingStrategy.DASHES);
midLine.HideTitle();
midLine.HideBubble();
I didn't know if you wanted the average highs and lows combined (i.e. Average(H + L / 2, 5) or if you wanted them separate. This version has them separate, but you could easily modify this to just calculate the combined value.
Additionally, I don't know if you mean "prior" bars when you say "last 5 bars", so this one will include the last bar as one of the 5. It would have to be modified if you wanted to exclude the last bar and show bars 2-6 instead of 1-5.
There are also some other assumptions made, like you only want it plotted on the last 5 bars. Again, it would require modification if that's not the case, but it wasn't specified, so this is what was done.
Ruby:
# DECLARATIONS
declare upper;
#USER INPUTS
input barsBack = 5;
input extendRight = no;
#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));
#DEFINITIONS
def isLastBar = !IsNaN(close) and IsNaN(close[-1]);
def isTargetPeriod = !IsNaN(close) and IsNaN(close[-barsBack]);
def avgHighs = if !IsNaN(close) and IsNaN(close[-1]) then Average(high, barsBack) else avgHighs[1];
def lastAvgHigh = if isNaN(close[-barsBack]) then avgHighs[-barsBack] else Double.NaN;
def avgLows = if !IsNaN(close) and IsNaN(close[-1]) then Average(low, barsBack) else avgLows[1];
def lastAvgLow = if isNaN(close[-barsBack]) then avgLows[-barsBack] else Double.NaN;
#PLOTS
plot highLine =
if !extendRight and IsNaN(close) then Double.NaN
else lastAvgHigh
;
plot lowLine =
if !extendRight and IsNaN(close) then Double.NaN
else lastAvgLow
;
plot midline = (highLine + lowLine) / 2;
#FORMATTING
highLine.SetDefaultColor(GlobalColor("Green"));
highLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine.HideTitle();
highLine.HideBubble();
lowLine.SetDefaultColor(GlobalColor("Red"));
lowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine.HideTitle();
lowLine.HideBubble();
midLine.SetDefaultColor(GlobalColor("Gray"));
midLine.SetPaintingStrategy(PaintingStrategy.DASHES);
midLine.HideTitle();
midLine.HideBubble();
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.
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.