This is my version of code. but its not even close to that in the picture in plotting the lines.
input LookFrontPeriod = 50;
def vClose = Round(Close, 1);
def vOpen = Round(Open, 1);
def bn = barnumber();
def nan = double.NaN;
#--------------------------------------------------------------
def _CloseInPeriod = vClose;
#--------------------------------------------------------------
##Logic: for every bar's close value, look front LookFrontPeriod bars for same close, if true then return close else move ahead one bar front
def marketClose1 = if _CloseInPeriod == _CloseInPeriod[LookFrontPeriod] then _CloseInPeriod else nan;
def marketClose1bn = if _CloseInPeriod == _CloseInPeriod[LookFrontPeriod] then bn else marketClose1bn[1];
##if above def is true then capture the close price of the bar where the def first is true
def _markedClose1 = vClose == marketClose1;
def _markedClose1bn = bn == marketClose1bn;
##Loop until no close price exist on the chart i.e until last bar of the chart
rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedClose1) then _lastMarkedLow1[1] else if _markedClose1 then vClose else _lastMarkedLow1[1], vClose);
rec _lastMarkedLow1bn = CompoundValue(1, if IsNaN(_markedClose1bn) then _lastMarkedLow1bn[1] else if _markedClose1bn then _markedClose1bn else _lastMarkedLow1bn[1], bn);
#--------------------------------------------------------------
##Plots and formatting
plot SR1 = Lowestall(_lastMarkedLow1);
#plot SR1 = Highestall(_lastMarkedLow1);
#SR1.SetStyle(Curve.Short_Dash);
SR1.SetLineWeight(1);
#SR1.SetDefaultColor(color.gray);
SR1.AssignValueColor(if vClose == SR1 then color.gray else
if vClose > SR1 then Color.Dark_Green else
if vClose < SR1 then Color.Dark_Red else color.gray);
AssignPriceColor(if vClose == SR1 then color.magenta else color.current);