Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- //
- // Central Pivot Range (CPR) Indicator for TradingView
- // This script is an implementation of Central Pivot Range described at http://pivotboss.com/2010/05/31/a-quick-guide-to-the-pivot-range
- // by Sherwin Daganato, 20171112
- // https://github.com/sherwind/pinescript-cpr
- //
- // Inputs:
- //
- // Number of Daily CPR Back - Set the number of calendar days back to plot historical daily CPR. The default value is 7.
- // Non-trading days are not taken into account.
- // A value of 7, for example, would display only 5 CPR for a 24x5 market.
- // Number of Weekly CPR Back - Set the number of calendar weeks back to plot historical weekly CPR. The default value is 0.
- // Number of Monthly CPR Back - Set the number of calendar months back to plot historical monthly CPR. The default value is 0.
- // Number of Yearly CPR Back - Set the number of calendar years back to plot historical yearly CPR. The default value is 0.
- //
- study(title="Central Pivot Ranges - All the things", shorttitle="CPR - all", overlay=true)
- daily_cpr = input(title="Number of Daily CPR Back", type=integer, defval=40, minval=0)
- weekly_cpr = input(title="Number of Weekly CPR Back", type=integer, defval=8, minval=0)
- monthly_cpr = input(title="Number of Monthly CPR Back", type=integer, defval=3, minval=0)
- yearly_cpr = input(title="Number of Yearly CPR Back", type=integer, defval=1, minval=0)
- show_daily_cpr = input(true, title="Show Daily CPR?")
- show_daily_pivots = input(false, title="Show Daily Pivots?")
- show_weekly_cpr = input(false, title="Show Weekly CPR?")
- show_weekly_pivots = input(false, title="Show Weekly Pivots?")
- show_monthly_cpr = input(false, title="Show Monthly CPR?")
- show_monthly_pivots = input(false, title="Show Monthly Pivots?")
- show_yearly_cpr = input(false, title="Show Yearly CPR?")
- show_yearly_pivots = input(false, title="Show Yearly Pivots?")
- show_developing_cpr = input(false, title="Show Developing CPR?")
- res = input(title="Developing CPR resolution", defval="D", options=["D", "W", "M", "12M"])
- new_bar(res) => change(time(res)) != 0
- new_period(condition, src) =>
- result = 0.0
- result := condition ? src : result[1]
- result
- pivot = (high + low + close) / 3.0
- bc = (high + low) / 2.0
- tc = (pivot - bc) + pivot
- s1 = (2 * pivot) - high
- r1 = (2 * pivot) - low
- s2 = pivot - (high -low)
- r2 = pivot + (high -low)
- s3 = low - (2 * (pivot - low))
- r3 = high + (2 * (pivot - low))
- //Daily Central Pivot Range
- dpp = security(tickerid, 'D', pivot[1], lookahead=barmerge.lookahead_on)
- dbc = security(tickerid, 'D', bc[1], lookahead=barmerge.lookahead_on)
- dtc = security(tickerid, 'D', tc[1], lookahead=barmerge.lookahead_on)
- // daily pivots
- ds1 = security(tickerid, 'D', s1[1], lookahead=barmerge.lookahead_on)
- ds2 = security(tickerid, 'D', s2[1], lookahead=barmerge.lookahead_on)
- ds3 = security(tickerid, 'D', s3[1], lookahead=barmerge.lookahead_on)
- dr1 = security(tickerid, 'D', r1[1], lookahead=barmerge.lookahead_on)
- dr2 = security(tickerid, 'D', r2[1], lookahead=barmerge.lookahead_on)
- dr3 = security(tickerid, 'D', r3[1], lookahead=barmerge.lookahead_on)
- one_day = 1000 * 60 * 60 * 24
- new_day = daily_cpr > 0 and timenow - time < one_day * daily_cpr and new_bar("D")
- dr3_ = new_period(new_day, dr3)
- dr2_ = new_period(new_day, dr2)
- dr1_ = new_period(new_day, dr1)
- dtc_ = new_period(new_day, dtc)
- dpp_ = new_period(new_day, dpp)
- dbc_ = new_period(new_day, dbc)
- ds1_ = new_period(new_day, ds1)
- ds2_ = new_period(new_day, ds2)
- ds3_ = new_period(new_day, ds3)
- plot(isintraday and show_daily_cpr and show_daily_pivots ? dr3_ : na, title="Daily R3", style=circles, color=purple, linewidth=2)
- plot(isintraday and show_daily_cpr and show_daily_pivots ? dr2_ : na, title="Daily R2", style=circles, color=purple, linewidth=2)
- plot(isintraday and show_daily_cpr and show_daily_pivots ? dr1_ : na, title="Daily R1", style=circles, color=purple, linewidth=2)
- plot(isintraday and show_daily_cpr ? (dtc_ >= dbc_ ? dtc_ : dbc_) : na, title="Daily TC", style=circles, color=#d328ef, linewidth=2)
- plot(isintraday and show_daily_cpr ? dpp_ : na, title="Daily PP", style=circles, color=#d328ef, linewidth=2)
- plot(isintraday and show_daily_cpr ? (dtc_ >= dbc_ ? dbc_ : dtc_) : na, title="Daily BC", style=circles, color=#d328ef, linewidth=2)
- plot(isintraday and show_daily_cpr and show_daily_pivots ? ds1_ : na, title="Daily S1", style=circles, color=purple, linewidth=2)
- plot(isintraday and show_daily_cpr and show_daily_pivots ? ds2_ : na, title="Daily S2", style=circles, color=purple, linewidth=2)
- plot(isintraday and show_daily_cpr and show_daily_pivots ? ds3_ : na, title="Daily S3", style=circles, color=purple, linewidth=2)
- //Weekly Central Pivot Range
- wpp = security(tickerid, 'W', pivot[1], lookahead=barmerge.lookahead_on)
- wbc = security(tickerid, 'W', bc[1], lookahead=barmerge.lookahead_on)
- wtc = security(tickerid, 'W', tc[1], lookahead=barmerge.lookahead_on)
- ws1 = security(tickerid, 'W', s1[1], lookahead=barmerge.lookahead_on)
- ws2 = security(tickerid, 'W', s2[1], lookahead=barmerge.lookahead_on)
- ws3 = security(tickerid, 'W', s3[1], lookahead=barmerge.lookahead_on)
- wr1 = security(tickerid, 'W', r1[1], lookahead=barmerge.lookahead_on)
- wr2 = security(tickerid, 'W', r2[1], lookahead=barmerge.lookahead_on)
- wr3 = security(tickerid, 'W', r3[1], lookahead=barmerge.lookahead_on)
- one_week = one_day * 7
- new_week = weekly_cpr > 0 and timenow - time < one_week * weekly_cpr and new_bar("W")
- wpp_ = new_period(new_week, wpp)
- wtc_ = new_period(new_week, wtc)
- wbc_ = new_period(new_week, wbc)
- ws1_ = new_period(new_week, ws1)
- ws2_ = new_period(new_week, ws2)
- ws3_ = new_period(new_week, ws3)
- wr1_ = new_period(new_week, wr1)
- wr2_ = new_period(new_week, wr2)
- wr3_ = new_period(new_week, wr3)
- plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? wr3_ : na, title="Weekly R3", style=circles, color=#5b85bf, linewidth=2)
- plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? wr2_ : na, title="Weekly R2", style=circles, color=#5b85bf, linewidth=2)
- plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? wr1_ : na, title="Weekly R1", style=circles, color=#5b85bf, linewidth=2)
- plot(show_weekly_cpr and (isintraday or isdaily) ? (wtc_ >= wbc_ ? wtc_ : wbc_) : na, title="Weekly TC", style=circles, color=#93bbf2, linewidth=2)
- plot(show_weekly_cpr and (isintraday or isdaily) ? wpp_ : na, title="Weekly PP", style=circles, color=#93bbf2, linewidth=2)
- plot(show_weekly_cpr and (isintraday or isdaily) ? (wtc_ >= wbc_ ? wbc_ : wtc_) : na, title="Weekly BC", style=circles, color=#93bbf2, linewidth=2)
- plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? ws1_ : na, title="Weekly S1", style=circles, color=#5b85bf, linewidth=2)
- plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? ws2_ : na, title="Weekly S2", style=circles, color=#5b85bf, linewidth=2)
- plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? ws3_ : na, title="Weekly S3", style=circles, color=#5b85bf, linewidth=2)
- //Monthly Central Pivot Range
- mr3 = security(tickerid, 'M', r3[1], lookahead=barmerge.lookahead_on)
- mr2 = security(tickerid, 'M', r2[1], lookahead=barmerge.lookahead_on)
- mr1 = security(tickerid, 'M', r1[1], lookahead=barmerge.lookahead_on)
- mbc = security(tickerid, 'M', bc[1], lookahead=barmerge.lookahead_on)
- mpp = security(tickerid, 'M', pivot[1], lookahead=barmerge.lookahead_on)
- mtc = security(tickerid, 'M', tc[1], lookahead=barmerge.lookahead_on)
- ms1 = security(tickerid, 'M', s1[1], lookahead=barmerge.lookahead_on)
- ms2 = security(tickerid, 'M', s2[1], lookahead=barmerge.lookahead_on)
- ms3 = security(tickerid, 'M', s3[1], lookahead=barmerge.lookahead_on)
- one_month = one_day * 30
- new_month = monthly_cpr > 0 and timenow - time < one_month * monthly_cpr and new_bar("M")
- mpp_ = new_period(new_month, mpp)
- mtc_ = new_period(new_month, mtc)
- mbc_ = new_period(new_month, mbc)
- ms1_ = new_period(new_month, ms1)
- ms2_ = new_period(new_month, ms2)
- ms3_ = new_period(new_month, ms3)
- mr1_ = new_period(new_month, mr1)
- mr2_ = new_period(new_month, mr2)
- mr3_ = new_period(new_month, mr3)
- plot(show_monthly_cpr and show_monthly_pivots and (isintraday or isdaily or isweekly) ? mr3_ : na, title="Monthly R3", style=circles, color=#b35b46, linewidth=3)
- plot(show_monthly_cpr and show_monthly_pivots and (isintraday or isdaily or isweekly) ? mr2_ : na, title="Monthly R2", style=circles, color=#b35b46, linewidth=3)
- plot(show_monthly_cpr and show_monthly_pivots and (isintraday or isdaily or isweekly) ? mr1_ : na, title="Monthly R1", style=circles, color=#b35b46, linewidth=3)
- plot(show_monthly_cpr and (isintraday or isdaily or isweekly) ? (mtc_ >= mbc_ ? mtc_ : mbc_) : na, title="Monthly TC", style=circles, color=#ed8608, linewidth=3)
- plot(show_monthly_cpr and (isintraday or isdaily or isweekly) ? mpp_ : na, title="Monthly PP", style=circles, color=#ed8608, linewidth=3)
- plot(show_monthly_cpr and (isintraday or isdaily or isweekly) ? (mtc_ >= mbc_ ? mbc_ : mtc_) : na, title="Monthly BC", style=circles, color=#ed8608, linewidth=3)
- plot(show_monthly_cpr and show_monthly_pivots and (isintraday or isdaily or isweekly) ? ms1_ : na, title="Monthly S1", style=circles, color=#b35b46, linewidth=3)
- plot(show_monthly_cpr and show_monthly_pivots and (isintraday or isdaily or isweekly) ? ms2_ : na, title="Monthly S2", style=circles, color=#b35b46, linewidth=3)
- plot(show_monthly_cpr and show_monthly_pivots and (isintraday or isdaily or isweekly) ? ms3_ : na, title="Monthly S3", style=circles, color=#b35b46, linewidth=3)
- //Yearly Central Pivot Range
- yr3 = security(tickerid, '12M', r3[1], lookahead=barmerge.lookahead_on)
- yr2 = security(tickerid, '12M', r2[1], lookahead=barmerge.lookahead_on)
- yr1 = security(tickerid, '12M', r1[1], lookahead=barmerge.lookahead_on)
- ypp = security(tickerid, '12M', pivot[1], lookahead=barmerge.lookahead_on)
- ybc = security(tickerid, '12M', bc[1], lookahead=barmerge.lookahead_on)
- ytc = security(tickerid, '12M', tc[1], lookahead=barmerge.lookahead_on)
- ys1 = security(tickerid, '12M', s1[1], lookahead=barmerge.lookahead_on)
- ys2 = security(tickerid, '12M', s2[1], lookahead=barmerge.lookahead_on)
- ys3 = security(tickerid, '12M', s3[1], lookahead=barmerge.lookahead_on)
- one_year = one_day * 365
- new_year = yearly_cpr > 0 and timenow - time < one_year * yearly_cpr and new_bar("12M")
- ypp_ = new_period(new_year, ypp)
- ytc_ = new_period(new_year, ytc)
- ybc_ = new_period(new_year, ybc)
- ys1_ = new_period(new_year, ys1)
- ys2_ = new_period(new_year, ys2)
- ys3_ = new_period(new_year, ys3)
- yr1_ = new_period(new_year, yr1)
- yr2_ = new_period(new_year, yr2)
- yr3_ = new_period(new_year, yr3)
- plot((show_yearly_cpr and show_yearly_pivots) and (isintraday or isdaily or isweekly or ismonthly) ? yr3_ : na, title="Yearly R3", style=circles, color=#911f1f, linewidth=4)
- plot((show_yearly_cpr and show_yearly_pivots) and (isintraday or isdaily or isweekly or ismonthly) ? yr2_ : na, title="Yearly R2", style=circles, color=#911f1f, linewidth=4)
- plot((show_yearly_cpr and show_yearly_pivots) and (isintraday or isdaily or isweekly or ismonthly) ? yr1_ : na, title="Yearly R1", style=circles, color=#911f1f, linewidth=4)
- plot(show_yearly_cpr and (isintraday or isdaily or isweekly or ismonthly) ? (ytc_ >= ybc_ ? ytc_ : ybc_) : na, title="Yearly TC", style=circles, color=#f92c2c, linewidth=4)
- plot(show_yearly_cpr and (isintraday or isdaily or isweekly or ismonthly) ? ypp_ : na, title="Yearly PP", style=circles, color=#f92c2c, linewidth=4)
- plot(show_yearly_cpr and (isintraday or isdaily or isweekly or ismonthly) ? (ytc_ >= ybc_ ? ybc_ : ytc_) : na, title="Yearly BC", style=circles, color=#f92c2c, linewidth=4)
- plot((show_yearly_cpr and show_yearly_pivots) and (isintraday or isdaily or isweekly or ismonthly) ? ys1_ : na, title="Yearly S1", style=circles, color=#911f1f, linewidth=4)
- plot((show_yearly_cpr and show_yearly_pivots) and (isintraday or isdaily or isweekly or ismonthly) ? ys2_ : na, title="Yearly S2", style=circles, color=#911f1f, linewidth=4)
- plot((show_yearly_cpr and show_yearly_pivots) and (isintraday or isdaily or isweekly or ismonthly) ? ys3_ : na, title="Yearly S3", style=circles, color=#911f1f, linewidth=4)
- //
- // Developing Central Pivot Range (CPR) Indicator for TradingView
- // This script implements a developing version of Central Pivot Range described at http://pivotboss.com/2010/05/31/a-quick-guide-to-the-pivot-range
- // by Sherwin Daganato, 20171126
- // https://github.com/sherwind/pinescript-developing_cpr
- //
- // Inputs:
- //
- // Developing CPR resolution - Either D (Daily), W (Weekly), M (Monthly) or 12M (Yearly). The default value is M.
- //
- // study(title="SD - Developing Central Pivot Range - Daily Weekly Monthly Yearly", shorttitle="[SD]Developing CPR", overlay=true)
- dev_tc = security(tickerid, res, tc, lookahead=barmerge.lookahead_on)
- dev_pp = security(tickerid, res, pivot, lookahead=barmerge.lookahead_on)
- dev_bc = security(tickerid, res, bc, lookahead=barmerge.lookahead_on)
- plot(show_developing_cpr ? (dev_tc >= dev_bc ? dev_tc : dev_bc) : na , title="Developing TC", style=line, color=lime, linewidth=2)
- plot(show_developing_cpr ? dev_pp : na, title="Developing PP", style=line, color=lime, linewidth=2)
- plot(show_developing_cpr ? (dev_tc >= dev_bc ? dev_bc : dev_tc) : na, title="Developing BC", style=line, color=lime, linewidth=2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement