//@version=3
// Step 1. Script settings
study(title="Five Point Pivots", overlay=true)
// Input settings
// Create a pull-down menu for the pivot type
pivotType = input(title="Pivot Type",
options=["Daily", "Intraday", "Weekly"], defval="Intraday")
// Make toggles for pivot level options
plotPP = input(title="Plot PP", type=bool, defval=true)
plotS1R1 = input(title="Plot S1 and R1", type=bool, defval=true)
plotS2R2 = input(title="Plot S2 and R2", type=bool, defval=true)
plotS3R3 = input(title="Plot S3 and R3", type=bool, defval=false)
plotS4R4 = input(title="Plot S4 and R4", type=bool, defval=false)
plotS5R5 = input(title="Plot S5 and R5", type=bool, defval=false)
// Configure session options
sessRange = input(title="Trading Session", type=session, defval="0930-1530")
showSess = input(title="Highlight Session?", type=bool, defval=false)
// Enable or disable pivot labels
showLabels = input(title="Show Labels?", type=bool, defval=false)
// Step 2. Calculate indicator values
// Create a function to fetch daily and weekly data
GetData(res, data) =>
security(tickerid, res, data[1],
lookahead=barmerge.lookahead_on)
// Fetch daily and weekly price data
dailyHigh = GetData("D", high)
dailyLow = GetData("D", low)
dailyClose = GetData("D", close)
weeklyHigh = GetData("W", high)
weeklyLow = GetData("W", low)
weeklyClose = GetData("W", close)
// Determine session pivot data
// First see how the price bar relates to
// the session time range
inSession = not na(time(period, sessRange)[1])
sessStart = inSession and not inSession[1]
sessEnd = not inSession and inSession[1]
// Determine session price data
sessHigh = 0.0
sessLow = 0.0
sessClose = 0.0
sessHigh := sessStart ? high :
inSession ? max(high, sessHigh[1]) : na
sessLow := sessStart ? low :
inSession ? min(low, sessLow[1]) : na
sessClose := sessEnd ? close[1] : na
// Compute high, low, close from previous intra-day session
highPrevSess = 0.0
lowPrevSess = 0.0
closePrevSess = 0.0
highPrevSess := sessEnd ? fixnan(sessHigh) : highPrevSess[1]
lowPrevSess := sessEnd ? fixnan(sessLow) : lowPrevSess[1]
closePrevSess := sessEnd ? fixnan(sessClose) : closePrevSess[1]
// Now figure out which kind of price data
// to use for the pivot calculation
theHigh = if (pivotType == "Daily")
dailyHigh
else
if (pivotType == "Intraday")
highPrevSess
else
weeklyHigh
theLow = if (pivotType == "Daily")
dailyLow
else
if (pivotType == "Intraday")
lowPrevSess
else
weeklyLow
theClose = if (pivotType == "Daily")
dailyClose
else
if (pivotType == "Intraday")
closePrevSess
else
weeklyClose
// Finally calculate the pivot levels
pp = (theHigh + theLow + theClose) / 3
r1 = (pp * 2) - theLow
s1 = (pp * 2) - theHigh
r2 = (pp - s1) + r1
s2 = pp - (r1 - s1)
r3 = (pp - s1) + r2
s3 = pp - (r2 - s1)
r4 = (pp - s2) + r3
s4 = pp - (r3 - s2)
r5 = (pp - s3) + r4
s5 = pp - (r4 - s3)
// Step 3. Output indicator data
// Step 3. Output indicator data
// Plot the various pivot levels
plot(series=plotS5R5 ? r5 : na, title="R5",
style=line, linewidth=2, color= #363A45)
plot(series=plotS4R4 ? r4 : na, title="R4",
style=line, linewidth=2, color= #363A45)
plot(series=plotS3R3 ? r3 : na, title="R3",
style=line, linewidth=2, color= #363A45)
plot(series=plotS2R2 ? r2 : na, title="R2",
style=line, linewidth=2, color= #363A45)
plot(series=plotS1R1 ? r1 : na, title="R1",
style=line, linewidth=2, color= #363A45)
plot(series=plotPP ? pp : na, title="PP",
style=line, linewidth=2, color= #4CAF50)
plot(series=plotS1R1 ? s1 : na, title="S1",
style=line, linewidth=2, color= #363A45)
plot(series=plotS2R2 ? s2 : na, title="S2",
style=line, linewidth=2, color= #363A45)
plot(series=plotS3R3 ? s3 : na, title="S3",
style=line, linewidth=2, color= #363A45)
plot(series=plotS4R4 ? s4 : na, title="S4",
style=line, linewidth=2, color= #363A45)
plot(series=plotS5R5 ? s5 : na, title="S5",
style=line, linewidth=2, color= #363A45)
// Display the pivot names on the chart, if applicable
newPivots = (showLabels == false) ? false :
(pivotType == "Intraday") ? sessEnd :
(pivotType == "Daily") ? dayofmonth != dayofmonth[1] :
dayofweek == monday and dayofmonth != dayofmonth[1]
plotchar(series=newPivots and plotS5R5 ? r5 : na,
char='', text="R5", offset=1,
location=[Link],
color=#363A45, title="R5 label")
plotchar(series=newPivots and plotS4R4 ? r4 : na,
char='', text="R4", offset=1,
location=[Link],
color=#363A45, title="R4 label")
plotchar(series=newPivots and plotS3R3 ? r3 : na,
char='', text="R3", offset=1,
location=[Link],
color=#363A45, title="R3 label")
plotchar(series=newPivots and plotS2R2 ? r2 : na,
char='', text="R2", offset=1,
location=[Link],
color=#363A45, title="R2 label")
plotchar(series=newPivots and plotS1R1 ? r1 : na,
char='', text="R1", offset=1,
location=[Link],
color=#363A45, title="R1 label")
plotchar(series=newPivots and plotS1R1 ? s1 : na,
char='', text="S1", offset=1,
location=[Link],
color=#363A45, title="S1 label")
plotchar(series=newPivots and plotS2R2 ? s2 : na,
char='', text="S2", offset=1,
location=[Link],
color=#363A45, title="S2 label")
plotchar(series=newPivots and plotS3R3 ? s3 : na,
char='', text="S3", offset=1,
location=[Link],
color=#363A45, title="S3 label")
plotchar(series=newPivots and plotS4R4 ? s4 : na,
char='', text="S4", offset=1,
location=[Link],
color=#363A45, title="S4 label")
plotchar(series=newPivots and plotS5R5 ? s5 : na,
char='', text="S5", offset=1,
location=[Link],
color=#363A45, title="S5 label")
// Highlight the intra-day price data session on the chart
bgcolor(color=showSess and inSession and (pivotType == "Intraday") ?
orange : na, transp=95)
// Step 4. Create indicator alerts
alertcondition(condition=cross(close, s5),
title="Pivot S5 Cross",
message="Prices crossed Pivot S5 level")
alertcondition(condition=cross(close, s4),
title="Pivot S4 Cross",
message="Prices crossed Pivot S4 level")
alertcondition(condition=cross(close, s3),
title="Pivot S3 Cross",
message="Prices crossed Pivot S3 level")
alertcondition(condition=cross(close, s2),
title="Pivot S2 Cross",
message="Prices crossed Pivot S2 level")
alertcondition(condition=cross(close, s1),
title="Pivot S1 Cross",
message="Prices crossed Pivot S1 level")
alertcondition(condition=cross(close, pp),
title="Pivot PP Cross",
message="Prices crossed the main Pivot Point level")
alertcondition(condition=cross(close, r1),
title="Pivot R1 Cross",
message="Prices crossed Pivot R1 level")
alertcondition(condition=cross(close, r2),
title="Pivot R2 Cross",
message="Prices crossed Pivot R2 level")
alertcondition(condition=cross(close, r3),
title="Pivot R3 Cross",
message="Prices crossed Pivot R3 level")
alertcondition(condition=cross(close, r4),
title="Pivot R4 Cross",
message="Prices crossed Pivot R4 level")
alertcondition(condition=cross(close, r5),
title="Pivot R5 Cross",
message="Prices crossed Pivot R5 level")