#prev_week_mtf
#https://usethinkscript.com/threads/mtf-previous-week-open-close.19623/
#MTF Previous Week Open / Close
def na = double.nan;
def bn = barnumber();
input ShowTodayOnly = yes;
input week_offset = 1;
def today = if !ShowTodayOnly then 1 else GetDay() == GetLastDay();
plot HighW = if !today then na else high(period = "week")[week_offset];
HighW.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highW.SetDefaultColor(Color.gray);
plot LowW = if !today then na else low(period = "week")[week_offset];
LowW.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowW.SetDefaultColor(Color.gray);
plot openW = if !today then na else open(period = "week")[week_offset];
openW.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
openW.SetDefaultColor(Color.CYAN);
plot closeW = if !today then na else close(period = "week")[week_offset];
closeW.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
closeW.SetDefaultColor(Color.YELLOW);
AddLabel(1, week_offset + " weeks ago", Color.YELLOW);
input candle_offset = 3;
def x = (!IsNaN(close[candle_offset]) and IsNaN(close[candle_offset - 1]));
def o = openW[candle_offset];
def h = HighW[candle_offset];
def l = LowW[candle_offset];
def c = closeW[candle_offset];
def cond = if x and c >= o then 1 else 0;
def o1 = if cond then if o < c then c else o else na;
def c1 = if cond then if o < c then o else c else na;
#def o1 = if cond then if o < c then o else c else Double.NaN;
#def c1 = if cond then if o < c then c else o else Double.NaN;
# solid green , low to high
#def o1 = if cond then if o < c then l else h else Double.NaN;
#def c1 = if cond then if o < c then h else l else Double.NaN;
def h1 = if cond then h else na;
def l1 = if cond then l else na;
#AddChart(growColor = Color.green, fallColor = Color.green, neutralColor = Color.green, high = h1, low = l1, open = c1, close = o1, type = ChartType.CANDLE);
AddChart(growColor = Color.CYAN, fallColor = Color.GREEN, neutralColor = Color.GREEN, high = h1, low = l1, open = c1, close = o1, type = ChartType.CANDLE);
def cond1 = if x and c < o then 1 else 0;
def o2 = if cond1 then if o < c then c else o else na;
def c2 = if cond1 then if o < c then o else c else na;
def h2 = if cond1 then h else na;
def l2 = if cond1 then l else na;
#AddChart(growColor = Color.red, fallColor = Color.RED, neutralColor = Color.RED, high = h2, low = l2, open = c2, close = o2, type = ChartType.CANDLE);
AddChart(growColor = Color.MAGENTA, fallColor = Color.RED, neutralColor = Color.RED, high = h2, low = l2, open = c2, close = o2, type = ChartType.CANDLE);
#to draw a filled bar with addchart(), with grow color,
#swap these in addchart(),
#set open to lower level and close to higher level.
#