xmd79

MACD/Moving Average/Linear Regression/Supertrend/VWAP - Threefold_Ghost

Mar 3rd, 2022
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.22 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © Threefold_Ghost
  3. //Script contains several indicators, all plotted on graph along with candles.
  4. //- MACD
  5. //- Moving Average
  6. //- Linear Regression
  7. //- Supertrend
  8. //- VWAP
  9. //
  10. //MACD:
  11. //When inside of bars are gray, MACD above 0 which is bullish momentum. When inside of bars are black, MACD below 0 which is bearish momentum.
  12. //MACD is crossing above signal on green up arrow alert which is bullish and MACD is crossing below signal on red down arrow alert which is bearish.
  13. //
  14. //MOVING AVERAGE:
  15. //EMA and SMA are both plotted. Since EMA applies more weight to data that is more current, EMA is defaulted to 50 bars and is a dotted line whereas
  16. //SMA is defaulted to 200 bars and is a solid line. Both will change color, green when the average is moving upwards and black when the average is moving downwards.
  17. //There is a Golden Cross alert when EMA crosses above SMA which is bullish and a Death Cross alert when EMA crosses below SMA which is bearish.
  18. //
  19. //LINEAR REGRESSION:
  20. //Colors will change based on trend channel, green when trend is bullish and maroon when trend is bearish
  21. //
  22. //SUPERTREND:
  23. //Short term and a long term supertrends are calculated. There is a Long alert and a lime green support line plotted when short term supertrend is bullish
  24. //and a Short alert and a red line plotted when short term supertrend is bearish. There are larger Trend alerts and support lines plotted with a color fill
  25. //when the long term supertrend is either bullish or bearish. Use Long and Short alerts as a guide to confirm entries and not as actual buy/sell signals.
  26. //
  27. //VWAP:
  28. //Color changes to white line when VWAP is rising which is bullish and changes to a purple line when VWAP is dropping which is bearish
  29. //
  30. //@version=5
  31. indicator(title="MACD/Moving Average/Linear Regression/Supertrend/VWAP - Threefold_Ghost", shorttitle="MACD/MA/LR/S/VWAP - T_G", overlay=true, max_lines_count=500, max_labels_count=500)
  32.  
  33. //track macd
  34. fastLength = input.int(8, minval=1, title="Macd Short EMA", group="MACD Settings")
  35. slowLength = input.int(16, minval=1, title="Macd Long EMA")
  36. signalLength = input.int(11, minval=1, title="Macd Signal SMA")
  37. fastMA = ta.ema(close, fastLength)
  38. slowMA = ta.ema(close, slowLength)
  39. macd = fastMA - slowMA
  40. signal = ta.sma(macd, signalLength)
  41. if ta.crossover(macd,signal)
  42. label.new(bar_index, na, text="", yloc=yloc.belowbar, color=color.lime, textcolor=color.gray, size=size.small, style=label.style_arrowup)
  43. if ta.crossover(signal, macd)
  44. label.new(bar_index, na, text="", yloc=yloc.abovebar, color=color.red, textcolor=color.gray, size=size.small, style=label.style_arrowdown)
  45. barcolor(macd > 0 ? color.new(color.gray, 0): macd < 0 ? color.new(color.black, 0) : na, title="Macd +/- Bar Color")
  46.  
  47. //calculate EMA
  48. emaplot = input (true, title="Show EMA1 on chart", group="EMA/SMA Settings")
  49. len9 = input.int(50, minval=1, title="EMA1 Length")
  50. src9 = close
  51. out9 = ta.ema(src9, len9)
  52. up9 = out9 > out9[1]
  53. down9 = out9 < out9[1]
  54. mycolor = up9 ? color.lime : down9 ? color.black : color.white
  55. plot(out9 and emaplot ? out9 :na, title="EMA1", color=mycolor, linewidth=1, style=plot.style_circles)
  56.  
  57. emaplot2 = input (false, title="Show EMA2 on chart")
  58. len6 = input.int(200, minval=1, title="EMA2 Length")
  59. src6 = close
  60. out6 = ta.ema(src6, len6)
  61. up6 = out6 > out6[1]
  62. down6 = out6 < out6[1]
  63. mycolor3 = up6 ? color.lime : down6 ? color.black : color.white
  64. plot(out6 and emaplot2 ? out6 :na, title="EMA2", color=mycolor3, linewidth=3, style=plot.style_cross)
  65.  
  66. smaplot = input (true, title="Show SMA on chart")
  67. len7 = input.int(200, minval=1, title="SMA Length")
  68. src7 = close
  69. out7 = ta.sma(src7, len7)
  70. up7 = out7 > out7[1]
  71. down7 = out7 < out7[1]
  72. mycolor2 = up7 ? color.new(color.lime, 36) : down7 ? color.new(color.black, 36) : color.new(color.white, 36)
  73. plot(out7 and smaplot ? out7 :na , title="SMA", color=mycolor2, linewidth=6)
  74.  
  75. if ta.crossover(out9, out7)
  76. label.new(bar_index, na, text="Golden Cross", yloc=yloc.belowbar, color=color.gray, textcolor=color.yellow, style=label.style_xcross, size=size.small)
  77. if ta.crossover(out7, out9)
  78. label.new(bar_index, na, text="Death Cross", yloc=yloc.abovebar, color=color.gray, textcolor=color.red, style=label.style_xcross, size=size.small)
  79. alertcondition(ta.crossover(out9, out7), title="Golden Cross", message="Golden Cross")
  80. alertcondition(ta.crossover(out7, out9), title="Death Cross", message="Death Cross")
  81.  
  82. //calculate linear regression lines
  83. src = input(defval = close, title = "Linear Regression Source", group="Linear Regression Settings")
  84. len = input.int(defval = 100, title = "Linear Regression Length", minval = 10)
  85. devlen = input.float(defval = 2., title = "Linear Regression Deviation", minval = 0.1, step = 0.1)
  86. extendit = input(defval = true, title = "Extend Linear Regression Lines")
  87. showfibo = input(defval = false, title = "Show Linear Regression Fibonacci Levels")
  88. showbroken = input(defval = false, title = "Show Linear Regression Broken Channel", inline = "brk")
  89. brokencol = input(defval = color.blue, title = "", inline = "brk")
  90. upcol = input(defval = color.new(#024b30, 6), title = "Up/Down Linear Regression Trend Colors", inline = "trcols")
  91. dncol = input(defval = color.maroon, title = "", inline = "trcols")
  92. widt = input(defval = 3, title = "Linear Regression Line Width")
  93.  
  94. var fibo_ratios = array.new_float(0)
  95. var colors = array.new_color(2)
  96. if barstate.isfirst
  97. array.unshift(colors, upcol)
  98. array.unshift(colors, dncol)
  99. array.push(fibo_ratios, 0.236)
  100. array.push(fibo_ratios, 0.382)
  101. array.push(fibo_ratios, 0.618)
  102. array.push(fibo_ratios, 0.786)
  103.  
  104.  
  105. get_channel(src, len)=>
  106. mid = math.sum(src, len) / len
  107. slope = ta.linreg(src, len, 0) - ta.linreg(src, len, 1)
  108. intercept = mid - slope * math.floor(len / 2) + ((1 - (len % 2)) / 2) * slope
  109. endy = intercept + slope * (len - 1)
  110. dev = 0.0
  111. for x = 0 to len - 1
  112. dev := dev + math.pow(src[x] - (slope * (len - x) + intercept), 2)
  113. dev := math.sqrt(dev/len)
  114. [intercept, endy, dev, slope]
  115.  
  116. [y1_, y2_, dev, slope] = get_channel(src, len)
  117.  
  118. outofchannel = (slope > 0 and close < y2_ - dev * devlen) ? 0 : (slope < 0 and close > y2_ + dev * devlen) ? 2 : -1
  119.  
  120. var reglines = array.new_line(3)
  121. var fibolines = array.new_line(4)
  122. for x = 0 to 2
  123. if not showbroken or outofchannel != x or nz(outofchannel[1], -1) != -1
  124. line.delete(array.get(reglines, x))
  125. else
  126. line.set_color(array.get(reglines, x), color = brokencol)
  127. line.set_width(array.get(reglines, x), width = 2)
  128. line.set_style(array.get(reglines, x), style = line.style_dotted)
  129. line.set_extend(array.get(reglines, x), extend = extend.none)
  130.  
  131. array.set(reglines, x,
  132. line.new(x1 = bar_index - (len - 1),
  133. y1 = y1_ + dev * devlen * (x - 1),
  134. x2 = bar_index,
  135. y2 = y2_ + dev * devlen * (x - 1),
  136. color = array.get(colors, math.round(math.max(math.sign(slope), 0))),
  137. style = x % 2 == 1 ? line.style_solid : line.style_dashed,
  138. width = widt,
  139. extend = extendit ? extend.right : extend.none))
  140.  
  141. if showfibo
  142. for x = 0 to 3
  143. line.delete(array.get(fibolines, x))
  144. array.set(fibolines, x,
  145. line.new(x1 = bar_index - (len - 1),
  146. y1 = y1_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x),
  147. x2 = bar_index,
  148. y2 = y2_ - dev * devlen + dev * devlen * 2 * array.get(fibo_ratios, x),
  149. color = array.get(colors, math.round(math.max(math.sign(slope), 0))),
  150. style = line.style_dotted,
  151. width = widt,
  152. extend = extendit ? extend.right : extend.none))
  153.  
  154. var label sidelab = label.new(x = bar_index - (len - 1), y = y1_, text = "S", size = size.large)
  155. txt = slope > 0 ? slope > slope[1] ? "⇑" : "⇗" : slope < 0 ? slope < slope[1] ? "⇓" : "⇘" : "⇒"
  156. stl = slope > 0 ? slope > slope[1] ? label.style_label_up : label.style_label_upper_right : slope < 0 ? slope < slope[1] ? label.style_label_down : label.style_label_lower_right : label.style_label_right
  157. label.set_style(sidelab, stl)
  158. label.set_text(sidelab, txt)
  159. label.set_x(sidelab, bar_index - (len - 1))
  160. label.set_y(sidelab, slope > 0 ? y1_ - dev * devlen : slope < 0 ? y1_ + dev * devlen : y1_)
  161. label.set_color(sidelab, slope > 0 ? upcol : slope < 0 ? dncol : color.white)
  162.  
  163. alertcondition(outofchannel, title='Channel Broken', message='Channel Broken')
  164.  
  165. // direction
  166. trendisup = math.sign(slope) != math.sign(slope[1]) and slope > 0
  167. trendisdown = math.sign(slope) != math.sign(slope[1]) and slope < 0
  168. alertcondition(trendisup, title='Up trend', message='Up trend')
  169. alertcondition(trendisdown, title='Down trend', message='Down trend')
  170.  
  171. //supertrend 1
  172. Factor=input.int(3, minval=1,maxval = 100,title="ATR Multiplier", group="Short Term Supertrend Settings")
  173. Pd=input.int(10, minval=1,maxval = 200, title="Length")
  174. source3=input(ohlc4, title="Source")
  175.  
  176. //get the atr
  177. Up=source3-(Factor*ta.atr(Pd))
  178. Dn=source3+(Factor*ta.atr(Pd))
  179.  
  180. float TrendUp = 0
  181. float TrendDown= 0
  182. float Trend = 0
  183. if close[1]>TrendUp[1]
  184. TrendUp := math.max(Up,TrendUp[1])
  185. else
  186. TrendUp := Up
  187.  
  188. if close[1]<TrendDown[1]
  189. TrendDown := math.min(Dn,TrendDown[1])
  190. else
  191. TrendDown := Dn
  192.  
  193. if(close > TrendDown[1])
  194. Trend := 1
  195. else if(close < TrendUp[1])
  196. Trend := -1
  197. else
  198. Trend := nz(Trend[1],1)
  199.  
  200. Tsl = Trend==1? TrendUp: TrendDown
  201.  
  202. linecolor = Trend == 1 ? color.lime : color.new(#9a0000, 0)
  203.  
  204. plot(Tsl, color = linecolor, linewidth = 2, style = plot.style_line, title = "Short Term SuperTrend")
  205.  
  206. if ta.cross(close,Tsl) and close>Tsl
  207. label.new(bar_index, na, text="Long", yloc=yloc.belowbar, color=color.black, textcolor=color.lime, size=size.small, style=label.style_label_up)
  208. if ta.cross(Tsl,close) and close<Tsl
  209. label.new(bar_index, na, text="Short", yloc=yloc.abovebar, color=color.black, textcolor=color.red, size=size.small, style=label.style_label_down)
  210.  
  211. //supertrend 2
  212. Factor2=input.int(5, minval=1,maxval = 100,title="ATR Multiplier", group="Long Term Supertrend Settings")
  213. Pd2=input.int(20, minval=1,maxval = 200, title="Length")
  214. source6=input(ohlc4, title="Source")
  215.  
  216. //get the atr
  217. Up2=source6-(Factor2*ta.atr(Pd2))
  218. Dn2=source6+(Factor2*ta.atr(Pd2))
  219.  
  220. float TrendUp2 = 0
  221. float TrendDown2= 0
  222. float Trend2 = 0
  223. if close[1]>TrendUp2[1]
  224. TrendUp2 := math.max(Up2,TrendUp2[1])
  225. else
  226. TrendUp2 := Up2
  227.  
  228. if close[1]<TrendDown2[1]
  229. TrendDown2 := math.min(Dn2,TrendDown2[1])
  230. else
  231. TrendDown2 := Dn2
  232.  
  233. if(close > TrendDown2[1])
  234. Trend2 := 1
  235. else if(close < TrendUp2[1])
  236. Trend2 := -1
  237. else
  238. Trend2 := nz(Trend2[1],1)
  239.  
  240. Tsl2 = Trend2==1? TrendUp2: TrendDown2
  241.  
  242. linecolor2 = Trend2 == 1 ? color.lime : color.new(#9a0000, 0)
  243.  
  244. plot(Tsl2, color = linecolor2, linewidth = 6, style = plot.style_stepline, title = "Long Term SuperTrend")
  245.  
  246. if ta.cross(close,Tsl2) and close>Tsl2
  247. label.new(bar_index, na, text="Trend", yloc=yloc.belowbar, color=color.black, textcolor=color.lime, size=size.large, style=label.style_label_up)
  248. if ta.cross(Tsl2,close) and close<Tsl2
  249. label.new(bar_index, na, text="Trend", yloc=yloc.abovebar, color=color.black, textcolor=color.red, size=size.large, style=label.style_label_down)
  250.  
  251. alertcondition(ta.cross(close,Tsl2) and close>Tsl2, title="Bull Trend", message="Go Long")
  252. alertcondition(ta.cross(Tsl2,close) and close<Tsl2, title="Bear Trend", message="Go Short")
  253. point1 = plot(Tsl, color = linecolor, linewidth = 2, style = plot.style_line, title = "Short Term SuperTrend")
  254. point2 = plot(Tsl2, color = linecolor2, linewidth = 6, style = plot.style_stepline, title = "Long Term SuperTrend")
  255. fill(point1,point2, color.new(linecolor2,90))
  256.  
  257. //vwap
  258. var cumVol = 0.
  259. cumVol += nz(volume)
  260. if barstate.islast and cumVol == 0
  261. runtime.error("No volume is provided by the data vendor.")
  262.  
  263. computeVWAP(srcVWAP, isNewPeriod, stDevMultiplier) =>
  264. var float sumSrcVol = na
  265. var float sumVol = na
  266. var float sumSrcSrcVol = na
  267.  
  268. sumSrcVol := isNewPeriod ? srcVWAP * volume : srcVWAP * volume + sumSrcVol[1]
  269. sumVol := isNewPeriod ? volume : volume + sumVol[1]
  270. // sumSrcSrcVol calculates the dividend of the equation that is later used to calculate the standard deviation
  271. sumSrcSrcVol := isNewPeriod ? volume * math.pow(src, 2) : volume * math.pow(src, 2) + sumSrcSrcVol[1]
  272.  
  273. _vwap = sumSrcVol / sumVol
  274. variance = sumSrcSrcVol / sumVol - math.pow(_vwap, 2)
  275. variance := variance < 0 ? 0 : variance
  276. stDev = math.sqrt(variance)
  277.  
  278. lowerBand = _vwap - stDev * stDevMultiplier
  279. upperBand = _vwap + stDev * stDevMultiplier
  280.  
  281. [_vwap, lowerBand, upperBand]
  282.  
  283. hideonDWM = input(false, title="Hide VWAP on 1D or Above", group="VWAP Settings")
  284. var anchor = input.string(defval = "Session", title="Anchor Period",
  285. options=["Session", "Week", "Month", "Quarter", "Year", "Decade", "Century", "Earnings", "Dividends", "Splits"], group="VWAP Settings")
  286. srcVWAP = input(title = "Source", defval = hlc3, group="VWAP Settings")
  287. offset = input(0, title="Offset", group="VWAP Settings")
  288.  
  289. showBands = input(false, title="Calculate Bands", group="VWAP Standard Deviation Bands Settings")
  290. stdevMult = input(1.0, title="Bands Multiplier", group="VWAP Standard Deviation Bands Settings")
  291.  
  292. timeChange(period) =>
  293. ta.change(time(period))
  294.  
  295. new_earnings = request.earnings(syminfo.tickerid, earnings.actual, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
  296. new_dividends = request.dividends(syminfo.tickerid, dividends.gross, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
  297. new_split = request.splits(syminfo.tickerid, splits.denominator, barmerge.gaps_on, barmerge.lookahead_on, ignore_invalid_symbol=true)
  298.  
  299. isNewPeriod = switch anchor
  300. "Earnings" => not na(new_earnings)
  301. "Dividends" => not na(new_dividends)
  302. "Splits" => not na(new_split)
  303. "Session" => timeChange("D")
  304. "Week" => timeChange("W")
  305. "Month" => timeChange("M")
  306. "Quarter" => timeChange("3M")
  307. "Year" => timeChange("12M")
  308. "Decade" => timeChange("12M") and year % 10 == 0
  309. "Century" => timeChange("12M") and year % 100 == 0
  310. => false
  311.  
  312. isEsdAnchor = anchor == "Earnings" or anchor == "Dividends" or anchor == "Splits"
  313. if na(srcVWAP[1]) and not isEsdAnchor
  314. isNewPeriod := true
  315.  
  316. float vwapValue = na
  317. float std = na
  318. float upperBandValue = na
  319. float lowerBandValue = na
  320. color colorVWAP = na
  321.  
  322. if not (hideonDWM and timeframe.isdwm)
  323. [_vwap, bottom, top] = computeVWAP(srcVWAP, isNewPeriod, stdevMult)
  324. vwapValue := _vwap
  325. upperBandValue := showBands ? top : na
  326. lowerBandValue := showBands ? bottom : na
  327. colorVWAP := _vwap > _vwap[1] ? color.white : _vwap < _vwap[1] ? color.purple : color.gray
  328.  
  329. plot(vwapValue, title="VWAP", color=colorVWAP, offset=offset)
  330.  
  331. upperBand = plot(upperBandValue, title="VWAP Upper Band", color=color.green, offset=offset)
  332. lowerBand = plot(lowerBandValue, title="VWAP Lower Band", color=color.green, offset=offset)
  333.  
  334. fill(upperBand, lowerBand, title="Bands Fill", color= showBands ? color.new(color.green, 95) : na)
Advertisement
Add Comment
Please, Sign In to add comment