dusun1

Untitled

Jul 7th, 2023
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. //@version=5
  2. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  3. // © TJ_667
  4.  
  5. // Volume Weighted Average Price for multiple periods with Standard Deviation band selection - Daily, Weekly, Monthly, Quarterly, Yearly
  6.  
  7.  
  8. indicator("Koalafied VWAP D/W/M/Q/Y ", overlay = true)
  9.  
  10. // ---------- Functions ---------- //
  11.  
  12. // TradingView vwap function. Taken out stdev calcs to put in seperate function.
  13. computeVWAP(src, isNewPeriod) =>
  14. var float sumSrcVol = na
  15. var float sumVol = na
  16. var float sumSrcSrcVol = na
  17.  
  18. sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]
  19. sumVol := isNewPeriod ? volume : volume + sumVol[1]
  20. // sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
  21. sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume * math.pow(src, 2) + sumSrcSrcVol[1]
  22.  
  23. _vwap = sumSrcVol / sumVol
  24. variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
  25. variance := variance < 0 ? 0 : variance
  26. stDev = math.sqrt(variance)
  27.  
  28. [_vwap, stDev]
  29.  
  30. // Standard Deviation Band calculation function
  31. compute_stDEV(_vwap, stDev, stDevMultiplier_1, stDevMultiplier_2) =>
  32.  
  33. upperBand_2 = _vwap + stDev * stDevMultiplier_2
  34. upperBand_1 = _vwap + stDev * stDevMultiplier_1
  35. lowerBand_1 = _vwap - stDev * stDevMultiplier_1
  36. lowerBand_2 = _vwap - stDev * stDevMultiplier_2
  37.  
  38. [upperBand_2, upperBand_1, lowerBand_1, lowerBand_2]
  39.  
  40. // Label function from sbtnc - Daily Weekly Monthly Opens script
  41. f_drawLabel(_x, _y, _text, _textcolor, _style, _size) =>
  42. var _label = label.new(
  43. x = _x,
  44. y = _y,
  45. text = _text,
  46. textcolor = _textcolor,
  47. style = _style,
  48. size = _size,
  49. xloc = xloc.bar_time
  50. )
  51.  
  52. label.set_xy(_label, _x, _y)
  53.  
  54. // ---------- VWAP selection ---------- //
  55.  
  56. src = input(hlc3, "VWAP Source")
  57.  
  58. plot_vD = input(true, "Plot Daily VWAP")
  59. plot_vW = input(true, "Plot Weekly VWAP")
  60. plot_vM = input(true, "Plot Monthly VWAP")
  61. plot_vQ = input(true, "Plot Quarterly VWAP")
  62. plot_vY = input(true, "Plot Yearly VWAP")
  63.  
  64. showStd = input(false, "Plot Standard Deviation")
  65. Std_selection = input.string(title = "Standard Deviation Option", defval="Day", options = ["Day", "Week", "Month", "Quarter", "Year"])
  66.  
  67. stDevMultiplier_1 = input(1.0, "StDev mult 1")
  68. stDevMultiplier_2 = input(2.0, "StDev mult 2")
  69.  
  70. show_labels = input(true, "Show Labels")
  71. fill_bands = input(false, "Fill Bands")
  72.  
  73. plot_pL1 = input(false, "Plot Previous Period 1+- Levels")
  74. plot_pL2 = input(false, "Plot Previous Period 2+- Levels")
  75.  
  76. show_plabels = input(true, "Show Previous Labels")
  77. fill_pbands = input(false, "Fill Previous Bands")
  78.  
  79. off_mult = input(15, "Label offset")
  80. var DEFAULT_LABEL_SIZE = size.small
  81. var DEFAULT_LABEL_STYLE = label.style_none
  82. ll_offset = timenow + math.round(ta.change(time)*off_mult)
  83.  
  84. // ---------- VWAP Calculations ---------- //
  85.  
  86. timeChange(period) =>
  87. ta.change(time(period))
  88.  
  89. newSessionD = timeChange("D")
  90. newSessionW = timeChange("W")
  91. newSessionM = timeChange("M")
  92. newSessionQ = timeChange("3M")
  93. newSessionY = timeChange("12M")
  94.  
  95.  
  96. // ---------- VWAP Function Calls ---------- //
  97.  
  98. [vD, stdevD] = computeVWAP(src, newSessionD)
  99. [vW, stdevW] = computeVWAP(src, newSessionW)
  100. [vM, stdevM] = computeVWAP(src, newSessionM)
  101. [vQ, stdevQ] = computeVWAP(src, newSessionQ)
  102. [vY, stdevY] = computeVWAP(src, newSessionY)
  103.  
  104.  
  105.  
  106.  
  107. // ---------- Plot VWAPs ---------- //
  108.  
  109. vDplot = plot(plot_vD ? vD : na, title = "VWAP - Daily", color = color.red, style = plot.style_line, transp = 0, linewidth = 1)
  110. f_drawLabel(ll_offset, show_labels and plot_vD ? vD : na, "--------- vD ", color.red, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  111.  
  112. vWplot = plot(plot_vW ? vW : na, title = "VWAP - Weekly", color = color.white, style = plot.style_line, transp = 0, linewidth = 1)
  113. f_drawLabel(ll_offset, show_labels and plot_vW ? vW : na, "--------- vW ", color.white, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  114.  
  115. vMplot = plot(plot_vM ? vM : na, title = "VWAP - Monthly", color = color.blue, style = plot.style_line, transp = 0, linewidth = 1)
  116. f_drawLabel(ll_offset, show_labels and plot_vM ? vM : na, "--------- vM ", color.blue, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  117.  
  118. vQplot = plot(plot_vQ ? vQ : na, title = "VWAP - Quarter", color = color.green, style = plot.style_line, transp = 0, linewidth = 1)
  119. f_drawLabel(ll_offset, show_labels and plot_vQ ? vQ : na, "--------- vQ ", color.green, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  120.  
  121. vYplot = plot(plot_vY ? vY : na, title = "VWAP - Year", color = color.yellow, style = plot.style_line, transp = 0, linewidth = 1)
  122. f_drawLabel(ll_offset, show_labels and plot_vY ? vY : na, "--------- vY ", color.yellow, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  123.  
  124.  
  125. // ---------- Standard Deviation Calc ---------- //
  126.  
  127. stdev_sel = Std_selection == "Day" ? stdevD :
  128. Std_selection == "Week" ? stdevW :
  129. Std_selection == "Month" ? stdevM :
  130. Std_selection == "Quarter" ? stdevQ :
  131. Std_selection == "Year" ? stdevY : na
  132.  
  133. vwap_sel = Std_selection == "Day" ? vD :
  134. Std_selection == "Week" ? vW :
  135. Std_selection == "Month" ? vM :
  136. Std_selection == "Quarter" ? vQ :
  137. Std_selection == "Year" ? vY : na
  138.  
  139. prev_period = Std_selection == "Day" ? newSessionD :
  140. Std_selection == "Week" ? newSessionW :
  141. Std_selection == "Month" ? newSessionM :
  142. Std_selection == "Quarter" ? newSessionQ :
  143. Std_selection == "Year" ? newSessionY : na
  144.  
  145. [s2up, s1up, s1dn, s2dn] = compute_stDEV(vwap_sel, stdev_sel, stDevMultiplier_1, stDevMultiplier_2)
  146.  
  147. a = plot(showStd ? s2up : na, title = "VWAP - STDEV +2", color = color.red, style = plot.style_linebr, linewidth = 3, transp = 70)
  148. f_drawLabel(ll_offset, show_labels and showStd ? s2up : na , " -------- SD+2 ", color.red, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  149.  
  150. b = plot(showStd ? s1up : na, title = "VWAP - STDEV +1", color = color.gray, style = plot.style_linebr, linewidth = 3, transp = 60)
  151. f_drawLabel(ll_offset, show_labels and showStd ? s1up : na , " -------- SD+1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  152.  
  153. c = plot(showStd ? s1dn : na, title = "VWAP - STDEV -1", color = color.gray, style = plot.style_linebr, linewidth = 3, transp = 60)
  154. f_drawLabel(ll_offset, show_labels and showStd ? s1dn : na , " -------- SD-1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  155.  
  156. d = plot(showStd ? s2dn : na, title = "VWAP - STDEV -2", color = color.blue, style = plot.style_linebr, linewidth = 3, transp = 70)
  157. f_drawLabel(ll_offset, show_labels and showStd ? s2dn : na , " -------- SD-2 ", color.blue, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  158.  
  159. fill_col_up = color.new(color.red, 95)
  160. fill_col_mid = color.new(color.gray, 90)
  161. fill_col_down = color.new(color.blue, 95)
  162.  
  163. fill(a, b, fill_bands ? fill_col_up : na)
  164. fill(b, c, fill_bands ? fill_col_mid : na)
  165. fill(c, d, fill_bands ? fill_col_down : na)
  166.  
  167.  
  168. // ---------- Previous period levels ---------- //
  169.  
  170. previouslevels(period, VWAP, s2up, s1up, s1dn, s2dn) =>
  171. var float pV = na
  172. var float ps2up = na
  173. var float ps1up = na
  174. var float ps1dn = na
  175. var float ps2dn = na
  176. pV := period ? VWAP[1] : pV
  177. ps2up := period ? s2up[1] : ps2up
  178. ps1up := period ? s1up[1] : ps1up
  179. ps1dn := period ? s1dn[1] : ps1dn
  180. ps2dn := period ? s2dn[1] : ps2dn
  181.  
  182. [ pV, ps2up, ps1up, ps1dn, ps2dn ]
  183.  
  184. [ pV, ps2up, ps1up, ps1dn, ps2dn ] = previouslevels(prev_period, vwap_sel, s2up, s1up, s1dn, s2dn)
  185.  
  186. // ---------- Plot previous levels ---------- //
  187.  
  188. pVplot = plot(plot_pL1 ? pV : na, title = "Previous VWAP", color = color.orange, style = plot.style_linebr, transp = 60, linewidth = 1)
  189. f_drawLabel(ll_offset, show_plabels and plot_pL1 ? pV : na, "--------- pVWAP ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  190.  
  191. pa = plot(plot_pL2 ? ps2up : na, title = "Previous VWAP - STDEV +2", color = color.red, style = plot.style_linebr, linewidth = 1, transp = 70)
  192. f_drawLabel(ll_offset, show_plabels and plot_pL2 ? ps2up : na , " -------- pSD+2 ", color.red, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  193.  
  194. pb = plot(plot_pL1 ? ps1up : na, title = "Previous VWAP - STDEV +1", color = color.gray, style = plot.style_linebr, linewidth = 1, transp = 60)
  195. f_drawLabel(ll_offset, show_plabels and plot_pL1 ? ps1up : na , " -------- pSD+1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  196.  
  197. pc = plot(plot_pL1 ? ps1dn : na, title = "Previous VWAP - STDEV -1", color = color.gray, style = plot.style_linebr, linewidth = 1, transp = 60)
  198. f_drawLabel(ll_offset, show_plabels and plot_pL1 ? ps1dn : na , " -------- pSD-1 ", color.gray, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  199.  
  200. pd = plot(plot_pL2 ? ps2dn : na, title = "Previous VWAP - STDEV -2", color = color.blue, style = plot.style_linebr, linewidth = 1, transp = 70)
  201. f_drawLabel(ll_offset, show_plabels and plot_pL2 ? ps2dn : na , " -------- pSD-2 ", color.blue, DEFAULT_LABEL_STYLE, DEFAULT_LABEL_SIZE)
  202.  
  203. fill(pa, pb, fill_pbands ? fill_col_up : na)
  204. fill(pb, pc, fill_pbands ? fill_col_mid : na)
  205. fill(pc, pd, fill_pbands ? fill_col_down : na)
Advertisement
Add Comment
Please, Sign In to add comment