xmd79

SR, MAs, VWAP, Pivots

Oct 13th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. //@version=4
  2. study(title="SR, MAs, VWAP, Pivots", shorttitle="SR, MAs, VWAP, Pivots", overlay=true, max_labels_count=500)
  3.  
  4.  
  5. // ***************************************************
  6. // SUPPORT/RESISTANCE LINES
  7. // ***************************************************
  8. groupSR = "--- SUPPORT/RESISTANCE LINES ---"
  9. s1 = input(title="S1", type=input.float, defval=0.0,group=groupSR)
  10. s2 = input(title="S2", type=input.float, defval=0.0,group=groupSR)
  11. s3 = input(title="S3", type=input.float, defval=0.0,group=groupSR)
  12. s4 = input(title="S4", type=input.float, defval=0.0,group=groupSR)
  13.  
  14. r1 = input(title="R1", type=input.float, defval=0.0,group=groupSR)
  15. r2 = input(title="R2", type=input.float, defval=0.0,group=groupSR)
  16. r3 = input(title="R3", type=input.float, defval=0.0,group=groupSR)
  17. r4 = input(title="R4", type=input.float, defval=0.0,group=groupSR)
  18.  
  19. hline(s1, "S1", color=color.rgb(255,0,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  20. hline(s2, "S2", color=color.rgb(255,0,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  21. hline(s3, "S3", color=color.rgb(255,0,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  22. hline(s4, "S4", color=color.rgb(255,0,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  23.  
  24. hline(r1, "R1", color=color.rgb(0,255,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  25. hline(r2, "R2", color=color.rgb(0,255,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  26. hline(r3, "R3", color=color.rgb(0,255,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  27. hline(r4, "R4", color=color.rgb(0,255,0,50), linestyle=hline.style_dashed, linewidth=1, editable=true)
  28.  
  29.  
  30.  
  31.  
  32. // ***************************************************
  33. // PIVOTS
  34. // ***************************************************
  35. groupPivots = "--- PIVOTS ---"
  36. leftLenH = input(title="Pivot High", type=input.integer, defval=10, minval=1, inline="Pivot High",group=groupPivots)
  37. rightLenH = input(title="/", type=input.integer, defval=10, minval=1, inline="Pivot High",group=groupPivots)
  38. colorH = input(title="Background High", defval=color.rgb(0,0,0,0), group=groupPivots)
  39. textH = input(title="Text High", type=input.color, defval=color.rgb(255,255,0,0), group=groupPivots)
  40.  
  41. leftLenL = input(title="Pivot Low", type=input.integer, defval=10, minval=1, inline="Pivot Low", group=groupPivots)
  42. rightLenL = input(title="/", type=input.integer, defval=10, minval=1, inline="Pivot Low", group=groupPivots)
  43. colorL = input(title="Background Low", defval=color.rgb(0,0,0,0), group=groupPivots)
  44. textL = input(title="Text Low", type=input.color, defval=color.rgb(255,255,0,0), group=groupPivots)
  45.  
  46. ph = pivothigh(leftLenH, rightLenH)
  47. pl = pivotlow( leftLenL, rightLenL)
  48.  
  49. drawLabel(_offset, _pivot, _style, _yloc, _color) =>
  50. if not na(_pivot)
  51. label.new(bar_index[_offset], _pivot, tostring(_pivot, format.mintick), style=_style, yloc=_yloc, color=_color, textcolor= (_pivot==ph ? textH : textL) )
  52.  
  53. drawLabel(rightLenH, ph, label.style_label_down, yloc.abovebar, colorH)
  54. drawLabel(rightLenL, pl, label.style_label_up, yloc.belowbar, colorL)
  55.  
  56.  
  57.  
  58. // ***************************************************
  59. // VWAP
  60. // ***************************************************
  61.  
  62. groupVWAP="--- VWAP ---"
  63.  
  64. computeVWAP(src, isNewPeriod, stDevMultiplier) =>
  65. var float sumSrcVol = na
  66. var float sumVol = na
  67. var float sumSrcSrcVol = na
  68.  
  69. sumSrcVol := isNewPeriod ? src * volume : src * volume + sumSrcVol[1]
  70. sumVol := isNewPeriod ? volume : volume + sumVol[1]
  71. // sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
  72. sumSrcSrcVol := isNewPeriod ? volume * pow(src, 2) : volume * pow(src, 2) + sumSrcSrcVol[1]
  73.  
  74. _vwap = sumSrcVol / sumVol
  75. variance = sumSrcSrcVol / sumVol - pow(_vwap, 2)
  76. variance := variance < 0 ? 0 : variance
  77. stDev = sqrt(variance)
  78.  
  79. lowerBand = _vwap - stDev * stDevMultiplier
  80. upperBand = _vwap + stDev * stDevMultiplier
  81.  
  82. [_vwap, lowerBand, upperBand]
  83.  
  84. hideonDWM = input(false, title="Hide VWAP on 1D or Above", group=groupVWAP)
  85. var anchor = input(defval = "Session", title="Anchor Period", type=input.string,
  86. options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group=groupVWAP)
  87. src = input(title = "Source", type = input.source, defval = hlc3, group=groupVWAP)
  88. offset = input(0, title="Offset", group=groupVWAP)
  89.  
  90. showBands = input(true, title="Calculate Bands", group="Standard Deviation Bands Settings")
  91. stdevMult = input(1.0, title="Bands Multiplier", group="Standard Deviation Bands Settings")
  92.  
  93. timeChange(period) =>
  94. change(time(period))
  95.  
  96. new_earnings = earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on)
  97. new_dividends = dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on)
  98. new_split = splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on)
  99.  
  100. isNewPeriod = anchor == "Earnings" ? new_earnings :
  101. anchor == "Dividends" ? new_dividends :
  102. anchor == "Splits" ? new_split :
  103. na(src[1]) ? true :
  104. anchor == "Session" ? timeChange("D") :
  105. anchor == "Week" ? timeChange("W") :
  106. anchor == "Month" ? timeChange("M") :
  107. anchor == "Quarter" ? timeChange("3M") :
  108. anchor == "Year" ? timeChange("12M") :
  109. anchor == "Decade" ? timeChange("12M") and year % 10 == 0 :
  110. anchor == "Century" ? timeChange("12M") and year % 100 == 0 :
  111. false
  112.  
  113. float vwapValue = na
  114. float std = na
  115. float upperBandValue = na
  116. float lowerBandValue = na
  117.  
  118. if not (hideonDWM and timeframe.isdwm)
  119. [_vwap, bottom, top] = computeVWAP(src, isNewPeriod, stdevMult)
  120. vwapValue := _vwap
  121. upperBandValue := showBands ? top : na
  122. lowerBandValue := showBands ? bottom : na
  123.  
  124. plot(vwapValue, title="VWAP",color=color.rgb(255,255,0,15), style=plot.style_line, linewidth=3, offset=offset)
  125.  
  126. ////upperBand = plot(upperBandValue, title="Upper Band", color=color.green, offset=offset)
  127. ////lowerBand = plot(lowerBandValue, title="Lower Band", color=color.green, offset=offset)
  128.  
  129. ////fill(upperBand, lowerBand, title="Bands Fill", color= showBands ? color.new(color.green, 95) : na)
  130.  
  131.  
  132.  
  133. // ***************************************************
  134. // MOVING AVERAGES
  135. // ***************************************************
  136.  
  137. groupMA="--- MOVING AVERAGES ---"
  138.  
  139. // === INPUTS ===
  140. emasrc = input(close)
  141.  
  142. ema1 = input(8,title='EMA 8', group=groupMA)
  143. ema2 = input(21,title='EMA 21', group=groupMA)
  144. ema3 = input(34,title='EMA 34', group=groupMA)
  145. pema1 = ema(emasrc, ema1)
  146. pema2 = ema(emasrc, ema2)
  147. pema3 = ema(emasrc, ema3)
  148.  
  149. sma1 = input(50,title='SMA 50', group=groupMA)
  150. psma1 = sma(emasrc, sma1)
  151.  
  152. sma2 = input(100,title='SMA 100', group=groupMA)
  153. psma2 = sma(emasrc, sma2)
  154.  
  155.  
  156. sma3 = input(200,title='SMA 200', group=groupMA)
  157. psma3 = sma(emasrc, sma3)
  158.  
  159.  
  160. ema1plot = plot(pema1, color=color.rgb(255,0,0,15), style=plot.style_line, linewidth=2, title='EMA 8')
  161. ema2plot = plot(pema2, color=color.rgb(255,255,255,15), style=plot.style_line, linewidth=2, title='EMA 21')
  162. ema3plot = plot(pema3, color=color.rgb(0,0,255,15), style=plot.style_line, linewidth=2, title='EMA 34')
  163.  
  164. sma1plot = plot(psma1, color=color.rgb(128,0,128,15), style=plot.style_line, linewidth=2, title='SMA 50')
  165. sma2plot = plot(psma2, color=color.rgb(0,255,255,15), style=plot.style_line, linewidth=2, title='SMA 100')
  166. sma3plot = plot(psma3, color=color.rgb(255,0,255,15), style=plot.style_line, linewidth=2, title='SMA 200')
  167.  
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment