Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.29 KB | None | 0 0
  1. //@version=3
  2. //
  3. // Central Pivot Range (CPR) Indicator for TradingView
  4. // This script is an implementation of Central Pivot Range described at http://pivotboss.com/2010/05/31/a-quick-guide-to-the-pivot-range
  5. // by Sherwin Daganato, 20171112
  6. // https://github.com/sherwind/pinescript-cpr
  7. //
  8. // Inputs:
  9. //
  10. // Number of Daily CPR Back - Set the number of calendar days back to plot historical daily CPR. The default value is 7.
  11. // Non-trading days are not taken into account.
  12. // A value of 7, for example, would display only 5 CPR for a 24x5 market.
  13. // Number of Weekly CPR Back - Set the number of calendar weeks back to plot historical weekly CPR. The default value is 0.
  14. // Number of Monthly CPR Back - Set the number of calendar months back to plot historical monthly CPR. The default value is 0.
  15. // Number of Yearly CPR Back - Set the number of calendar years back to plot historical yearly CPR. The default value is 0.
  16. //
  17. study(title="Central Pivot Ranges - All the things", shorttitle="CPR - all", overlay=true)
  18.  
  19. daily_cpr = input(title="Number of Daily CPR Back", type=integer, defval=40, minval=0)
  20. weekly_cpr = input(title="Number of Weekly CPR Back", type=integer, defval=8, minval=0)
  21. monthly_cpr = input(title="Number of Monthly CPR Back", type=integer, defval=3, minval=0)
  22. yearly_cpr = input(title="Number of Yearly CPR Back", type=integer, defval=1, minval=0)
  23.  
  24. show_daily_cpr = input(true, title="Show Daily CPR?")
  25. show_daily_pivots = input(false, title="Show Daily Pivots?")
  26.  
  27. show_weekly_cpr = input(false, title="Show Weekly CPR?")
  28. show_weekly_pivots = input(false, title="Show Weekly Pivots?")
  29.  
  30. show_monthly_cpr = input(false, title="Show Monthly CPR?")
  31. show_monthly_pivots = input(false, title="Show Monthly Pivots?")
  32.  
  33. show_yearly_cpr = input(false, title="Show Yearly CPR?")
  34. show_yearly_pivots = input(false, title="Show Yearly Pivots?")
  35.  
  36. show_developing_cpr = input(false, title="Show Developing CPR?")
  37. res = input(title="Developing CPR resolution", defval="D", options=["D", "W", "M", "12M"])
  38.  
  39.  
  40. new_bar(res) => change(time(res)) != 0
  41. new_period(condition, src) =>
  42. result = 0.0
  43. result := condition ? src : result[1]
  44. result
  45.  
  46. pivot = (high + low + close) / 3.0
  47. bc = (high + low) / 2.0
  48. tc = (pivot - bc) + pivot
  49. s1 = (2 * pivot) - high
  50. r1 = (2 * pivot) - low
  51. s2 = pivot - (high -low)
  52. r2 = pivot + (high -low)
  53. s3 = low - (2 * (pivot - low))
  54. r3 = high + (2 * (pivot - low))
  55.  
  56. //Daily Central Pivot Range
  57. dpp = security(tickerid, 'D', pivot[1], lookahead=barmerge.lookahead_on)
  58. dbc = security(tickerid, 'D', bc[1], lookahead=barmerge.lookahead_on)
  59. dtc = security(tickerid, 'D', tc[1], lookahead=barmerge.lookahead_on)
  60.  
  61. // daily pivots
  62. ds1 = security(tickerid, 'D', s1[1], lookahead=barmerge.lookahead_on)
  63. ds2 = security(tickerid, 'D', s2[1], lookahead=barmerge.lookahead_on)
  64. ds3 = security(tickerid, 'D', s3[1], lookahead=barmerge.lookahead_on)
  65.  
  66. dr1 = security(tickerid, 'D', r1[1], lookahead=barmerge.lookahead_on)
  67. dr2 = security(tickerid, 'D', r2[1], lookahead=barmerge.lookahead_on)
  68. dr3 = security(tickerid, 'D', r3[1], lookahead=barmerge.lookahead_on)
  69.  
  70. one_day = 1000 * 60 * 60 * 24
  71. new_day = daily_cpr > 0 and timenow - time < one_day * daily_cpr and new_bar("D")
  72.  
  73. dr3_ = new_period(new_day, dr3)
  74. dr2_ = new_period(new_day, dr2)
  75. dr1_ = new_period(new_day, dr1)
  76. dtc_ = new_period(new_day, dtc)
  77. dpp_ = new_period(new_day, dpp)
  78. dbc_ = new_period(new_day, dbc)
  79. ds1_ = new_period(new_day, ds1)
  80. ds2_ = new_period(new_day, ds2)
  81. ds3_ = new_period(new_day, ds3)
  82.  
  83.  
  84. plot(isintraday and show_daily_cpr and show_daily_pivots ? dr3_ : na, title="Daily R3", style=circles, color=purple, linewidth=2)
  85. plot(isintraday and show_daily_cpr and show_daily_pivots ? dr2_ : na, title="Daily R2", style=circles, color=purple, linewidth=2)
  86. plot(isintraday and show_daily_cpr and show_daily_pivots ? dr1_ : na, title="Daily R1", style=circles, color=purple, linewidth=2)
  87. plot(isintraday and show_daily_cpr ? (dtc_ >= dbc_ ? dtc_ : dbc_) : na, title="Daily TC", style=circles, color=#d328ef, linewidth=2)
  88. plot(isintraday and show_daily_cpr ? dpp_ : na, title="Daily PP", style=circles, color=#d328ef, linewidth=2)
  89. plot(isintraday and show_daily_cpr ? (dtc_ >= dbc_ ? dbc_ : dtc_) : na, title="Daily BC", style=circles, color=#d328ef, linewidth=2)
  90. plot(isintraday and show_daily_cpr and show_daily_pivots ? ds1_ : na, title="Daily S1", style=circles, color=purple, linewidth=2)
  91. plot(isintraday and show_daily_cpr and show_daily_pivots ? ds2_ : na, title="Daily S2", style=circles, color=purple, linewidth=2)
  92. plot(isintraday and show_daily_cpr and show_daily_pivots ? ds3_ : na, title="Daily S3", style=circles, color=purple, linewidth=2)
  93.  
  94.  
  95. //Weekly Central Pivot Range
  96. wpp = security(tickerid, 'W', pivot[1], lookahead=barmerge.lookahead_on)
  97. wbc = security(tickerid, 'W', bc[1], lookahead=barmerge.lookahead_on)
  98. wtc = security(tickerid, 'W', tc[1], lookahead=barmerge.lookahead_on)
  99.  
  100. ws1 = security(tickerid, 'W', s1[1], lookahead=barmerge.lookahead_on)
  101. ws2 = security(tickerid, 'W', s2[1], lookahead=barmerge.lookahead_on)
  102. ws3 = security(tickerid, 'W', s3[1], lookahead=barmerge.lookahead_on)
  103.  
  104. wr1 = security(tickerid, 'W', r1[1], lookahead=barmerge.lookahead_on)
  105. wr2 = security(tickerid, 'W', r2[1], lookahead=barmerge.lookahead_on)
  106. wr3 = security(tickerid, 'W', r3[1], lookahead=barmerge.lookahead_on)
  107.  
  108.  
  109. one_week = one_day * 7
  110. new_week = weekly_cpr > 0 and timenow - time < one_week * weekly_cpr and new_bar("W")
  111. wpp_ = new_period(new_week, wpp)
  112. wtc_ = new_period(new_week, wtc)
  113. wbc_ = new_period(new_week, wbc)
  114.  
  115. ws1_ = new_period(new_week, ws1)
  116. ws2_ = new_period(new_week, ws2)
  117. ws3_ = new_period(new_week, ws3)
  118. wr1_ = new_period(new_week, wr1)
  119. wr2_ = new_period(new_week, wr2)
  120. wr3_ = new_period(new_week, wr3)
  121.  
  122. plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? wr3_ : na, title="Weekly R3", style=circles, color=#5b85bf, linewidth=2)
  123. plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? wr2_ : na, title="Weekly R2", style=circles, color=#5b85bf, linewidth=2)
  124. plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? wr1_ : na, title="Weekly R1", style=circles, color=#5b85bf, linewidth=2)
  125. plot(show_weekly_cpr and (isintraday or isdaily) ? (wtc_ >= wbc_ ? wtc_ : wbc_) : na, title="Weekly TC", style=circles, color=#93bbf2, linewidth=2)
  126. plot(show_weekly_cpr and (isintraday or isdaily) ? wpp_ : na, title="Weekly PP", style=circles, color=#93bbf2, linewidth=2)
  127. plot(show_weekly_cpr and (isintraday or isdaily) ? (wtc_ >= wbc_ ? wbc_ : wtc_) : na, title="Weekly BC", style=circles, color=#93bbf2, linewidth=2)
  128. plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? ws1_ : na, title="Weekly S1", style=circles, color=#5b85bf, linewidth=2)
  129. plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? ws2_ : na, title="Weekly S2", style=circles, color=#5b85bf, linewidth=2)
  130. plot(show_weekly_cpr and show_weekly_pivots and (isintraday or isdaily) ? ws3_ : na, title="Weekly S3", style=circles, color=#5b85bf, linewidth=2)
  131.  
  132.  
  133.  
  134. //Monthly Central Pivot Range
  135. mr3 = security(tickerid, 'M', r3[1], lookahead=barmerge.lookahead_on)
  136. mr2 = security(tickerid, 'M', r2[1], lookahead=barmerge.lookahead_on)
  137. mr1 = security(tickerid, 'M', r1[1], lookahead=barmerge.lookahead_on)
  138. mbc = security(tickerid, 'M', bc[1], lookahead=barmerge.lookahead_on)
  139. mpp = security(tickerid, 'M', pivot[1], lookahead=barmerge.lookahead_on)
  140. mtc = security(tickerid, 'M', tc[1], lookahead=barmerge.lookahead_on)
  141. ms1 = security(tickerid, 'M', s1[1], lookahead=barmerge.lookahead_on)
  142. ms2 = security(tickerid, 'M', s2[1], lookahead=barmerge.lookahead_on)
  143. ms3 = security(tickerid, 'M', s3[1], lookahead=barmerge.lookahead_on)
  144.  
  145. one_month = one_day * 30
  146. new_month = monthly_cpr > 0 and timenow - time < one_month * monthly_cpr and new_bar("M")
  147. mpp_ = new_period(new_month, mpp)
  148. mtc_ = new_period(new_month, mtc)
  149. mbc_ = new_period(new_month, mbc)
  150. ms1_ = new_period(new_month, ms1)
  151. ms2_ = new_period(new_month, ms2)
  152. ms3_ = new_period(new_month, ms3)
  153. mr1_ = new_period(new_month, mr1)
  154. mr2_ = new_period(new_month, mr2)
  155. mr3_ = new_period(new_month, mr3)
  156.  
  157.  
  158. 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)
  159. 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)
  160. 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)
  161.  
  162. plot(show_monthly_cpr and (isintraday or isdaily or isweekly) ? (mtc_ >= mbc_ ? mtc_ : mbc_) : na, title="Monthly TC", style=circles, color=#ed8608, linewidth=3)
  163. plot(show_monthly_cpr and (isintraday or isdaily or isweekly) ? mpp_ : na, title="Monthly PP", style=circles, color=#ed8608, linewidth=3)
  164. plot(show_monthly_cpr and (isintraday or isdaily or isweekly) ? (mtc_ >= mbc_ ? mbc_ : mtc_) : na, title="Monthly BC", style=circles, color=#ed8608, linewidth=3)
  165.  
  166. 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)
  167. 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)
  168. 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)
  169.  
  170.  
  171. //Yearly Central Pivot Range
  172. yr3 = security(tickerid, '12M', r3[1], lookahead=barmerge.lookahead_on)
  173. yr2 = security(tickerid, '12M', r2[1], lookahead=barmerge.lookahead_on)
  174. yr1 = security(tickerid, '12M', r1[1], lookahead=barmerge.lookahead_on)
  175. ypp = security(tickerid, '12M', pivot[1], lookahead=barmerge.lookahead_on)
  176. ybc = security(tickerid, '12M', bc[1], lookahead=barmerge.lookahead_on)
  177. ytc = security(tickerid, '12M', tc[1], lookahead=barmerge.lookahead_on)
  178. ys1 = security(tickerid, '12M', s1[1], lookahead=barmerge.lookahead_on)
  179. ys2 = security(tickerid, '12M', s2[1], lookahead=barmerge.lookahead_on)
  180. ys3 = security(tickerid, '12M', s3[1], lookahead=barmerge.lookahead_on)
  181.  
  182.  
  183. one_year = one_day * 365
  184. new_year = yearly_cpr > 0 and timenow - time < one_year * yearly_cpr and new_bar("12M")
  185. ypp_ = new_period(new_year, ypp)
  186. ytc_ = new_period(new_year, ytc)
  187. ybc_ = new_period(new_year, ybc)
  188. ys1_ = new_period(new_year, ys1)
  189. ys2_ = new_period(new_year, ys2)
  190. ys3_ = new_period(new_year, ys3)
  191. yr1_ = new_period(new_year, yr1)
  192. yr2_ = new_period(new_year, yr2)
  193. yr3_ = new_period(new_year, yr3)
  194.  
  195.  
  196. 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)
  197. 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)
  198. 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)
  199.  
  200. 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)
  201. plot(show_yearly_cpr and (isintraday or isdaily or isweekly or ismonthly) ? ypp_ : na, title="Yearly PP", style=circles, color=#f92c2c, linewidth=4)
  202. 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)
  203.  
  204. 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)
  205. 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)
  206. 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)
  207.  
  208.  
  209. //
  210. // Developing Central Pivot Range (CPR) Indicator for TradingView
  211. // 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
  212. // by Sherwin Daganato, 20171126
  213. // https://github.com/sherwind/pinescript-developing_cpr
  214. //
  215. // Inputs:
  216. //
  217. // Developing CPR resolution - Either D (Daily), W (Weekly), M (Monthly) or 12M (Yearly). The default value is M.
  218. //
  219. // study(title="SD - Developing Central Pivot Range - Daily Weekly Monthly Yearly", shorttitle="[SD]Developing CPR", overlay=true)
  220.  
  221. dev_tc = security(tickerid, res, tc, lookahead=barmerge.lookahead_on)
  222. dev_pp = security(tickerid, res, pivot, lookahead=barmerge.lookahead_on)
  223. dev_bc = security(tickerid, res, bc, lookahead=barmerge.lookahead_on)
  224.  
  225. plot(show_developing_cpr ? (dev_tc >= dev_bc ? dev_tc : dev_bc) : na , title="Developing TC", style=line, color=lime, linewidth=2)
  226. plot(show_developing_cpr ? dev_pp : na, title="Developing PP", style=line, color=lime, linewidth=2)
  227. 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