# pre_pivots
#https://usethinkscript.com/threads/premarket-pivot-indicator.16829/
# Premarket Pivot Indicator
def bn = BarNumber();
def na = Double.NaN;
def diffday = GetDay() != GetDay()[1];
def lastday = GetDay() != GetLastDay();
# open/close times (EST)
input prestart = 0100;
input preend = 0930;
input daystart = 0930;
input dayend = 1600;
def ispre = if SecondsFromTime(prestart) >= 0 and SecondsTillTime(preend) > 0 then 1 else 0;
def isday = if SecondsFromTime(daystart) >= 0 and SecondsTillTime(dayend) > 0 then 1 else 0;
def prefirst = !ispre[1] and ispre;
def preopen = if bn == 1 then na
else if prefirst then open
else preopen[1];
def prehigh = if bn == 1 then na
else if prefirst then high
else if ispre then Max(high, prehigh[1])
else prehigh[1];
def prelow = if bn == 1 then na
else if prefirst then low
else if ispre then Min(low, prelow[1])
else prelow[1];
def preclose = if bn == 1 then na
else if ispre and !ispre[-1] then close
else preclose[1];
#---------------------------
def ph = if isday then prehigh else na;
def pl = if isday then prelow else na;
def pc = if isday then preclose else na;
# svepivots
plot R3;
plot R2;
plot R1;
plot PP;
plot S1;
plot S2;
plot S3;
plot R3M;
plot R2M;
plot R1M;
plot HH;
plot LL;
plot S1M;
plot S2M;
plot S3M;
HH = PH;
LL = PL;
PP = (PH + PL + PC) / 3;
R1 = 2 * PP - PL;
R1M = (R1 - PP) / 2 + PP;
R2 = PP + (PH - PL);
R2M = (R2 - R1) / 2 + R1;
R3 = 2 * PP + (PH - 2 * PL);
R3M = (R3 - R2) / 2 + R2;
S1 = 2 * PP - PH;
S1M = (PP - S1) / 2 + S1;
S2 = PP - (PH - PL);
S2M = (S1 - S2) / 2 + S2;
S3 = 2 * PP - (2 * PH - PL);
S3M = (S2 - S3) / 2 + S3;
R3.SetDefaultColor(GetColor(5));
R3M.SetDefaultColor(GetColor(5));
R2.SetDefaultColor(GetColor(5));
R2M.SetDefaultColor(GetColor(5));
R1.SetDefaultColor(GetColor(5));
R1M.SetDefaultColor(GetColor(5));
HH.SetDefaultColor(GetColor(4));
PP.SetDefaultColor(GetColor(0));
LL.SetDefaultColor(GetColor(1));
S1.SetDefaultColor(GetColor(6));
S1M.SetDefaultColor(GetColor(6));
S2.SetDefaultColor(GetColor(6));
S2M.SetDefaultColor(GetColor(6));
S3.SetDefaultColor(GetColor(6));
S3M.SetDefaultColor(GetColor(6));
R3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R3M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R2M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R1M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HH.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S1M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S2M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S3M.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R3M.SetStyle(Curve.SHORT_DASH);
R2M.SetStyle(Curve.SHORT_DASH);
R1M.SetStyle(Curve.SHORT_DASH);
HH.SetStyle(Curve.MEDIUM_DASH);
LL.SetStyle(Curve.MEDIUM_DASH);
S1M.SetStyle(Curve.SHORT_DASH);
S2M.SetStyle(Curve.SHORT_DASH);
S3M.SetStyle(Curve.SHORT_DASH);
R3M.Hide();
R2M.Hide();
R1M.Hide();
HH.Hide();
LL.Hide();
S1M.Hide();
S2M.Hide();
S3M.Hide();
#