Here's a version that will work.
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();