#//@version=4
#study(title="Trad Pivot Points Alerts", shorttitle="Trad Pivot Points Alerts", overlay = true)
#// Function outputs 1 when it's the first bar of the D/W/M/Y
# Converted by Sam4Cok@samer800 = 09 / 2023
#is_newbar(res) =>
script is_newbar {
input res = "Daily";
def isLast = !isNaN(close);
def day = GetDay();
def week = GetWeek();
def month = GetMonth();
def year = GetYear();
def ch = if res == "Daily" then day!=day[1] else
if res == "Weekly" then week!=week[1] else
if res == "Monthly" then month!=month[1] else
if res == "Yearly" then year!=year[1] else ch[1];
def con = if ch then yes else con[1];
plot out = if con and islast then ch else Double.NaN;
}
script nround {
input x = 0;
def n = if x < 10 then 3 else if x < 100 then 2 else if x < 1000 then 1 else 0;
def round1 = Round(x * Power(10, n), 0);
def nround = round1 / Power(10, n);
plot out = nround;
}
input pp_period = {default "Daily", "Weekly", "Monthly", "Yearly"};
input show_levels = yes;
input show_label = yes;
def na = Double.NaN;
def last = !isNaN(close);
DefineGlobalColor("line", Color.WHITE);
DefineGlobalColor("s", Color.CYAN);
DefineGlobalColor("r", Color.MAGENTA);
#// Calc High
AddLabel(show_label, pp_period + " Pivot Points", Color.WHITE);
def TimeSelect = is_newbar(pp_period);
def pp_res = TimeSelect;
def high_cur = if pp_res then high else Max(high_cur[1], high);
def phigh = if pp_res then high_cur[1] else phigh[1];
#// Calc Low
def low_cur = if pp_res then low else Min(low_cur[1], low);
def plow = if pp_res then low_cur[1] else plow[1];
#// Calc Close
def pclose = if pp_res then close[1] else pclose[1];
#// CALCULATE traditional pivots //
def vPP = (phigh + plow + pclose) / 3;
def vR1 = vPP + (vPP - plow);
def vS1 = vPP - (phigh - vPP);
def vR2 = vPP + (phigh - plow);
def vS2 = vPP - (phigh - plow);
def vR3 = phigh + 2 * (vPP - plow);
def vS3 = plow - 2 * (phigh - vPP);
#// PLOTTING //
#// PLOT PIVOTS LEVELS //
plot vpp_p = vPP;
plot vs1_p = vS1;
plot vs2_p = vS2;
plot vs3_p = vS3;
plot vr1_p = vR1;
plot vr2_p = vR2;
plot vr3_p = vR3;
vpp_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vs1_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vs2_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vs3_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vr1_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vr2_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vr3_p.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
vpp_p.SetDefaultColor(GlobalColor("line"));
vs1_p.SetDefaultColor(GlobalColor("s"));
vs2_p.SetDefaultColor(GlobalColor("s"));
vs3_p.SetDefaultColor(GlobalColor("s"));
vr1_p.SetDefaultColor(GlobalColor("r"));
vr2_p.SetDefaultColor(GlobalColor("r"));
vr3_p.SetDefaultColor(GlobalColor("r"));
#-- Levels
def labCond = pp_res!=pp_res[1] and show_levels;
def vpp_r = nround(vPP);
def vs1_r = nround(vS1);
def vs2_r = nround(vS2);
def vs3_r = nround(vS3);
def vr1_r = nround(vR1);
def vr2_r = nround(vR2);
def vr3_r = nround(vR3);
AddChartBubble(labCond and !labCond[1], vPP, "P " + vpp_r, GlobalColor("line"), yes);
AddChartBubble(labCond and !labCond[1], vS1, "S " + vS1_r, GlobalColor("s"), no);
AddChartBubble(labCond and !labCond[1], vS2, "S " + vS2_r, GlobalColor("s"), no);
AddChartBubble(labCond and !labCond[1], vS3, "S " + vS3_r, GlobalColor("s"), no);
AddChartBubble(labCond and !labCond[1], vR1, "R " + vR1_r, GlobalColor("r"), yes);
AddChartBubble(labCond and !labCond[1], vR2, "R " + vR2_r, GlobalColor("r"), yes);
AddChartBubble(labCond and !labCond[1], vR3, "R " + vR3_r, GlobalColor("r"), yes);
#-- END of CODE