#// Indicator for TOS
#// Based on "3 Day Lookback [TFO]" indicator by © tradeforopp
#// Request from useThinkScript.com member
# Created by Sam4Cok@Samer800 - 07/2024
input showHighLowPrice = yes;
input showTodayPeriod = yes;
input showAverageLines = no;
input noOfLookbackPeriod = 3;
input aggPeriod = AggregationPeriod.DAY;
input extendLines = no;
def na = Double.NaN;
def last = isNaN(close);
def extend = if extendLines then yes else !last;
def noOfDays = Min(Max(noOfLookbackPeriod, 1), 5);
def bar = barNumber();
#--color
def colFact = floor(255/noOfDays);
def col1 = if noOfDays > 4 then colFact * 5 else 0;
def col2 = if noOfDays > 3 then colFact * 4 else 0;
def col3 = if noOfDays > 2 then colFact * 3 else 0;
def col4 = if noOfDays > 1 then colFact * 2 else 0;
def col5 = if noOfDays > 0 then colFact else 0;
#--High/Low
def highDay = if !last then high(Period = aggPeriod) else highDay[1];
def lowDay = if !last then low(Period = aggPeriod) else lowDay[1];
def openDay = if !last then open(Period = aggPeriod) else openDay[1];
def anchor = CompoundValue(1, openDay!=openDay[-1], yes);
def today = CompoundValue(1, !anchor and showTodayPeriod and GetDay()==GetLastDay(), yes);;
#-- func
def day5cnt = noOfDays;
def day4cnt = noOfDays - 1;
def day3cnt = noOfDays - 2;
def day2cnt = noOfDays - 3;
def day1cnt = noOfDays - 4;
def cnt = if bar < 2 then 0 else
if anchor then cnt[1] + 1 else cnt[1];
def HICnt = highestAll(cnt);
def lastCnt = if isNaN(HICnt) then if(isNaN(lastCnt[1]), 0, lastCnt[1]) else HICnt;
def cond5 = !last and cnt[1] >= (lastCnt - day5cnt) and cnt[1] < (lastCnt - day4cnt);
def cond4 = !last and cnt[1] >= (lastCnt - day4cnt) and cnt[1] < (lastCnt - day3cnt);
def cond3 = !last and cnt[1] >= (lastCnt - day3cnt) and cnt[1] < (lastCnt - day2cnt);
def cond2 = !last and cnt[1] >= (lastCnt - day2cnt) and cnt[1] < (lastCnt - day1cnt);
def cond1 = !last and cnt[1] >= (lastCnt - day1cnt) and cnt[1] < (lastCnt);
def day5H = if cond5 then highDay else day5H[1];
def day5L = if cond5 then lowDay else day5L[1];
def day4H = if cond4 then highDay else day4H[1];
def day4L = if cond4 then lowDay else day4L[1];
def day3H = if cond3 then highDay else day3H[1];
def day3L = if cond3 then lowDay else day3L[1];
def day2H = if cond2 then highDay else day2H[1];
def day2L = if cond2 then lowDay else day2L[1];
def day1H = if cond1 then highDay else day1H[1];
def day1L = if cond1 then lowDay else day1L[1];
def val5H = if cond5 and high==highDay and !val5H[1] then high else val5H[1];
def val5L = if cond5 and low ==lowDay and !val5L[1] then low else val5L[1];
def val4H = if cond4 and high==highDay and !val4H[1] then high else val4H[1];
def val4L = if cond4 and low ==lowDay and !val4L[1] then low else val4L[1];
def val3H = if cond3 and high==highDay and !val3H[1] then high else val3H[1];
def val3L = if cond3 and low ==lowDay and !val3L[1] then low else val3L[1];
def val2H = if cond2 and high==highDay and !val2H[1] then high else val2H[1];
def val2L = if cond2 and low ==lowDay and !val2L[1] then low else val2L[1];
def val1H = if cond1 and high==highDay and !val1H[1] then high else val1H[1];
def val1L = if cond1 and low ==lowDay and !val1L[1] then low else val1L[1];
plot todayHi = if today and extend and highDay then highDay else na;
plot todayLo = if today and extend and lowDay then lowDay else na;
plot hh1 = if noOfDays > 4 and extend and day1H then day1H else na;
plot ll1 = if noOfDays > 4 and extend and day1L then day1L else na;
plot hh2 = if noOfDays > 3 and extend and day2H then day2H else na;
plot ll2 = if noOfDays > 3 and extend and day2L then day2L else na;
plot hh3 = if noOfDays > 2 and extend and day3H then day3H else na;
plot ll3 = if noOfDays > 2 and extend and day3L then day3L else na;
plot hh4 = if noOfDays > 1 and extend and day4H then day4H else na;
plot ll4 = if noOfDays > 1 and extend and day4L then day4L else na;
plot hh5 = if noOfDays > 0 and extend and day5H then day5H else na;
plot ll5 = if noOfDays > 0 and extend and day5L then day5L else na;
todayHi.SetStyle(Curve.MEDIUM_DASH);
todayLo.SetStyle(Curve.MEDIUM_DASH);
todayHi.SetDefaultColor(Color.GREEN);
todayLo.SetDefaultColor(Color.RED);
hh1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ll1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hh2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ll2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hh3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ll3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hh4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ll4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hh5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ll5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
hh1.AssignValueColor(CreateColor(0, col1, col1));
ll1.AssignValueColor(CreateColor(col1, 0, col1));
hh2.AssignValueColor(CreateColor(0, col2, col2));
ll2.AssignValueColor(CreateColor(col2, 0, col2));
hh3.AssignValueColor(CreateColor(0, col3, col3));
ll3.AssignValueColor(CreateColor(col3, 0, col3));
hh4.AssignValueColor(CreateColor(0, col4, col4));
ll4.AssignValueColor(CreateColor(col4, 0, col4));
hh5.AssignValueColor(CreateColor(0, col5, col5));
ll5.AssignValueColor(CreateColor(col5, 0, col5));
#-- Avg
plot avg5 = if !showAverageLines then na else (hh5 + ll5) / 2;
plot avg4 = if !showAverageLines then na else (hh4 + ll4) / 2;
plot avg3 = if !showAverageLines then na else (hh3 + ll3) / 2;
plot avg2 = if !showAverageLines then na else (hh2 + ll2) / 2;
plot avg1 = if !showAverageLines then na else (hh1 + ll1) / 2;
avg5.SetStyle(Curve.SHORT_DASH);
avg4.SetStyle(Curve.SHORT_DASH);
avg3.SetStyle(Curve.SHORT_DASH);
avg2.SetStyle(Curve.SHORT_DASH);
avg1.SetStyle(Curve.SHORT_DASH);
avg5.AssignValueColor(CreateColor(col5, col5, col5));
avg4.AssignValueColor(CreateColor(col4, col4, col4));
avg3.AssignValueColor(CreateColor(col3, col3, col3));
avg2.AssignValueColor(CreateColor(col2, col2, col2));
avg1.AssignValueColor(CreateColor(col1, col1, col1));
#-- high low price
plot hhVal5 = if showHighLowPrice and (val5H!=val5H[1]) then val5H else na;
plot llVal5 = if showHighLowPrice and (val5L!=val5L[1]) then val5L else na;
plot hhVal4 = if showHighLowPrice and (val4H!=val4H[1]) then val4H else na;
plot llVal4 = if showHighLowPrice and (val4L!=val4L[1]) then val4L else na;
plot hhVal3 = if showHighLowPrice and (val3H!=val3H[1]) then val3H else na;
plot llVal3 = if showHighLowPrice and (val3L!=val3L[1]) then val3L else na;
plot hhVal2 = if showHighLowPrice and (val2H!=val2H[1]) then val2H else na;
plot llVal2 = if showHighLowPrice and (val2L!=val2L[1]) then val2L else na;
plot hhVal1 = if showHighLowPrice and (val1H!=val1H[1]) then val1H else na;
plot llVal1 = if showHighLowPrice and (val1L!=val1L[1]) then val1L else na;
hhVal5.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
llVal5.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
hhVal4.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
llVal4.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
hhVal3.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
llVal3.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
hhVal2.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
llVal2.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
hhVal1.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
llVal1.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
hhVal1.AssignValueColor(CreateColor(0, col1, col1));
llVal1.AssignValueColor(CreateColor(col1, 0, col1));
hhVal2.AssignValueColor(CreateColor(0, col2, col2));
llVal2.AssignValueColor(CreateColor(col2, 0, col2));
hhVal3.AssignValueColor(CreateColor(0, col3, col3));
llVal3.AssignValueColor(CreateColor(col3, 0, col3));
hhVal4.AssignValueColor(CreateColor(0, col4, col4));
llVal4.AssignValueColor(CreateColor(col4, 0, col4));
hhVal5.AssignValueColor(CreateColor(0, col5, col5));
llVal5.AssignValueColor(CreateColor(col5, 0, col5));
#-- crosses
plot crosUp5 = (close > hh5) and (close[1] <= hh5[1]);
plot crosDn5 = (close < ll5) and (close[1] >= ll5[1]);
plot crosUp4 = (close > hh4) and (close[1] <= hh4[1]);
plot crosDn4 = (close < ll4) and (close[1] >= ll4[1]);
plot crosUp3 = (close > hh3) and (close[1] <= hh3[1]);
plot crosDn3 = (close < ll3) and (close[1] >= ll3[1]);
plot crosUp2 = (close > hh2) and (close[1] <= hh2[1]);
plot crosDn2 = (close < ll2) and (close[1] >= ll2[1]);
plot crosUp1 = (close > hh1) and (close[1] <= hh1[1]);
plot crosDn1 = (close < ll1) and (close[1] >= ll1[1]);
crosUp5.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
crosDn5.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
crosUp4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
crosDn4.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
crosUp3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
crosDn3.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
crosUp2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
crosDn2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
crosUp1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
crosDn1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
crosUp1.AssignValueColor(CreateColor(0, col1, col1));
crosDn1.AssignValueColor(CreateColor(col1, 0, col1));
crosUp2.AssignValueColor(CreateColor(0, col2, col2));
crosDn2.AssignValueColor(CreateColor(col2, 0, col2));
crosUp3.AssignValueColor(CreateColor(0, col3, col3));
crosDn3.AssignValueColor(CreateColor(col3, 0, col3));
crosUp4.AssignValueColor(CreateColor(0, col4, col4));
crosDn4.AssignValueColor(CreateColor(col4, 0, col4));
crosUp5.AssignValueColor(CreateColor(0, col5, col5));
crosDn5.AssignValueColor(CreateColor(col5, 0, col5));
#-- END of CODE