Advertisement
xmd79

Gaann medians with linear regression

Nov 17th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. //@version=5
  2. indicator("Gaann medians MA targets", overlay=true, max_bars_back=500, max_lines_count=500, max_labels_count=500)
  3.  
  4. // Inputs
  5. method1 = input.string('Median', options=['EMA', 'Median', 'SMA'])
  6. length1 = input(56)
  7. mult1 = input.int(1, minval=0, maxval=1)
  8.  
  9. method2 = input.string('Median', options=['EMA', 'Median', 'SMA'])
  10. length2 = input(100)
  11. mult2 = input.int(1, minval=0, maxval=1)
  12.  
  13. method3 = input.string('Median', options=['EMA', 'Median', 'SMA'])
  14. length3 = input(200)
  15. mult3 = input.int(1, minval=0, maxval=1)
  16.  
  17. // Calculation of medians
  18. Avg(x, length, method) =>
  19. sma_1 = ta.sma(x, length)
  20. ema_1 = ta.ema(x, length)
  21. percentile_linear_interpolation_1 = ta.percentile_linear_interpolation(x, length, 50)
  22. method == 'SMA' ? sma_1 : method == 'EMA' ? ema_1 : percentile_linear_interpolation_1
  23.  
  24. a1 = ta.highest(length1) - math.max(close, open)
  25. b1 = math.min(close, open) - ta.lowest(length1)
  26. c1 = math.max(close, open) + a1 * mult1
  27. d1 = math.min(close, open) - b1 * mult1
  28.  
  29. a2 = ta.highest(length2) - math.max(close, open)
  30. b2 = math.min(close, open) - ta.lowest(length2)
  31. c2 = math.max(close, open) + a2 * mult2
  32. d2 = math.min(close, open) - b2 * mult2
  33.  
  34. a3 = ta.highest(length3) - math.max(close, open)
  35. b3 = math.min(close, open) - ta.lowest(length3)
  36. c3 = math.max(close, open) + a3 * mult3
  37. d3 = math.min(close, open) - b3 * mult3
  38.  
  39. // Calculation of volume
  40. volLength = input.int(20, minval=1, title='Volume Length')
  41. volMultiplier = input.float(1.0, title='Volume Multiplier')
  42.  
  43. volAvg = ta.sma(volume, volLength) * volMultiplier
  44.  
  45. // Calculation of signals and liquidity levels
  46. e1 = Avg(c1, length1, method1)
  47. f1 = Avg(d1, length1, method1)
  48. gx1 = 0
  49. cross_1 = ta.cross(close, f1)
  50. gx1 := ta.cross(close, e1) ? 1 : cross_1 ? 0 : nz(gx1[1])
  51.  
  52. e2 = Avg(c2, length2, method2)
  53. f2 = Avg(d2, length2, method2)
  54. gx2 = 0
  55. cross_2 = ta.cross(close, f2)
  56. gx2 := ta.cross(close, e2) ? 1 : cross_2 ? 0 : nz(gx2[1])
  57.  
  58. e3 = Avg(c3, length3, method3)
  59. f3 = Avg(d3, length3, method3)
  60. gx3 = 0
  61. cross_3 = ta.cross(close, f3)
  62. gx3 := ta.cross(close, e3) ? 1 : cross_3 ? 0 : nz(gx3[1])
  63.  
  64. // Calculation of liquidity levels with volume
  65. hilo1 = gx1 * f1 + (1 - gx1) * e1
  66. css1 = gx1 == 1 ? color.green : color.red
  67. plot(hilo1, color=css1, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
  68.  
  69. hilo2 = gx2 * f2 + (1 - gx2) * e2
  70. css2 = gx2 == 1 ? color.green : color.red
  71. plot(hilo2, color=css2, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
  72.  
  73. hilo3 = gx3 * f3 + (1 - gx3) * e3
  74. css3 = gx3 == 1 ? color.green : color.red
  75. plot(hilo3, color=css3, style=plot.style_line, show_last=1, linewidth=1, trackprice=true, transp=0)
  76.  
  77.  
  78. //end of the code
  79.  
  80. //@version=5
  81.  
  82. //indicator(title='HiLo Activator', overlay=true)
  83. length = input.int(3, minval=1)
  84. displace = input.int(0, minval=0)
  85. highsma = ta.sma(high, length)
  86. lowsma = ta.sma(low, length)
  87. iff_1 = close < lowsma[displace] ? -1 : 0
  88. swing = close > highsma[displace] ? 1 : iff_1
  89. mah = ta.highest(high, length)
  90. mal = ta.lowest(low, length)
  91. var stop = 0.0
  92. iff_2 = swing == -1 ? mah : stop[1]
  93. stop := swing == 1 ? mal : iff_2
  94.  
  95. linecolor = stop < low ? color.green : color.red
  96. plot(stop, show_last=1, linewidth=1, trackprice=true, transp=0)
  97.  
  98. //end of this part
  99.  
  100. //study(title="TrapTrading (Study)", overlay=true)
  101. // input
  102. counterTrend = input(defval=false, title='Trade Mode (ON: Counter Trend OFF: Trend Following)')
  103. len1 = input.int(defval=20, title='Period (1-200)', minval=1, maxval=200)
  104. multiple = input(defval=0.7, title='Multiple')
  105.  
  106. m1 = close - close[len1]
  107. lowest_1 = ta.lowest(math.abs(m1), len1)
  108. highest_1 = ta.highest(math.abs(m1), len1)
  109. controlPoint = counterTrend ? lowest_1 == math.abs(m1) : highest_1 == math.abs(m1)
  110. baseLine = ta.valuewhen(controlPoint, math.avg(close, close[len1]), 0)
  111.  
  112. // trap line
  113. atr = ta.valuewhen(controlPoint, ta.highest(math.max(math.max(ta.atr(len1), ta.atr(len1 * 2)), ta.atr(len1 * 3)), len1), 0)
  114. line1Up = baseLine + atr * multiple
  115. line2Up = baseLine + atr * 2 * multiple
  116. line3Up = baseLine + atr * 3 * multiple
  117. line4Up = baseLine + atr * 4 * multiple
  118. line5Up = baseLine + atr * 5 * multiple
  119. line6Up = baseLine + atr * 6 * multiple
  120. line7Up = baseLine + atr * 7 * multiple
  121. line8Up = baseLine + atr * 8 * multiple
  122. line9Up = baseLine + atr * 9 * multiple
  123. line10Up = baseLine + atr * 10 * multiple
  124. line1Down = baseLine - atr * multiple
  125. line2Down = baseLine - atr * 2 * multiple
  126. line3Down = baseLine - atr * 3 * multiple
  127. line4Down = baseLine - atr * 4 * multiple
  128. line5Down = baseLine - atr * 5 * multiple
  129. line6Down = baseLine - atr * 6 * multiple
  130. line7Down = baseLine - atr * 7 * multiple
  131. line8Down = baseLine - atr * 8 * multiple
  132. line9Down = baseLine - atr * 9 * multiple
  133. line10Down = baseLine - atr * 10 * multiple
  134.  
  135. // draw
  136. //barcolor(controlPoint ? color.yellow : close >= baseLine ? color.teal : color.red, title="Candle Color")
  137.  
  138.  
  139. // find which bar is 5 days away from the current time
  140. milliseconds_in_5days = 1000 * 60 * 60 * 24 * 1 // millisecs * secs * min * hours * days
  141. leftborder = timenow - time < milliseconds_in_5days // true or na when false
  142. rightborder = barstate.islast
  143.  
  144.  
  145. plot(baseLine, title='Base Line', color=color.new(color.purple, 0), linewidth=1, style=plot.style_stepline, show_last=1, linewidth=1, trackprice=true, transp=0)
  146. //plot(leftborder?line1Up:na, title="1Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  147. //plot(leftborder?line2Up:na, title="2Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  148. //plot(leftborder?line3Up:na, title="3Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  149. //plot(leftborder?line4Up:na, title="4Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  150. //plot(leftborder?line5Up:na, title="5Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  151. //plot(leftborder?line6Up:na, title="6Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  152. //plot(leftborder?line7Up:na, title="7Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  153. //plot(leftborder?line8Up:na, title="8Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  154. //plot(leftborder?line9Up:na, title="9Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  155. //plot(leftborder?line10Up:na, title="10Up Line", color=color.red, linewidth=1, style=plot.style_stepline, transp=0)
  156. //plot(leftborder?line1Down:na, title="1Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  157. //plot(leftborder?line2Down:na, title="2Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  158. //plot(leftborder?line3Down:na, title="3Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  159. //plot(leftborder?line4Down:na, title="4Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  160. //plot(leftborder?line5Down:na, title="5Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  161. //plot(leftborder?line6Down:na, title="6Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  162. //plot(leftborder?line7Down:na, title="7Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  163. //plot(leftborder?line8Down:na, title="8Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  164. //plot(leftborder?line9Down:na, title="9Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  165. //plot(leftborder?line10Down:na, title="10Down Line", color=color.green, linewidth=1, style=plot.style_stepline, transp=0)
  166.  
  167. //end of this part
  168.  
  169. upperMult = input(title='Upper Deviation', defval=2)
  170. lowerMult = input(title='Lower Deviation', defval=-2)
  171.  
  172. useUpperDev = input(title='Use Upper Deviation', defval=true)
  173. useLowerDev = input(title='Use Lower Deviation', defval=true)
  174. showPearson = input(title='Show Pearson\'s R', defval=true)
  175. extendLines = input(title='Extend Lines', defval=false)
  176.  
  177. len = input(title='Count', defval=100)
  178. src = input(title='Source', defval=close)
  179.  
  180. extend = extendLines ? extend.right : extend.none
  181.  
  182. calcSlope(src, len) =>
  183. if not barstate.islast or len <= 1
  184. [float(na), float(na), float(na)]
  185. else
  186. sumX = 0.0
  187. sumY = 0.0
  188. sumXSqr = 0.0
  189. sumXY = 0.0
  190. for i = 0 to len - 1 by 1
  191. val = src[i]
  192. per = i + 1.0
  193. sumX += per
  194. sumY += val
  195. sumXSqr += per * per
  196. sumXY += val * per
  197. sumXY
  198. slope = (len * sumXY - sumX * sumY) / (len * sumXSqr - sumX * sumX)
  199. average = sumY / len
  200. intercept = average - slope * sumX / len + slope
  201. [slope, average, intercept]
  202.  
  203. [s, aLR, i] = calcSlope(src, len)
  204.  
  205. startPrice = i + s * (len - 1)
  206. endPrice = i
  207. var line baseLineLR = na
  208.  
  209. if na(baseLineLR) and not na(startPrice)
  210. baseLineLR := line.new(bar_index - len + 1, startPrice, bar_index, endPrice, width=4, extend=extend, color=color.red)
  211. baseLineLR
  212. else
  213. line.set_xy1(baseLineLR, bar_index - len + 1, startPrice)
  214. line.set_xy2(baseLineLR, bar_index, endPrice)
  215. na
  216.  
  217. calcDev(src, len, slope, average, intercept) =>
  218. upDev = 0.0
  219. dnDev = 0.0
  220. stdDevAcc = 0.0
  221. dsxx = 0.0
  222. dsyy = 0.0
  223. dsxy = 0.0
  224.  
  225. periods = len - 1
  226.  
  227. daY = intercept + slope * periods / 2
  228. val = intercept
  229.  
  230. for i = 0 to periods by 1
  231. price = high[i] - val
  232. if price > upDev
  233. upDev := price
  234. upDev
  235.  
  236. price := val - low[i]
  237. if price > dnDev
  238. dnDev := price
  239. dnDev
  240.  
  241. price := src[i]
  242. dxt = price - average
  243. dyt = val - daY
  244.  
  245. price -= val
  246. stdDevAcc += price * price
  247. dsxx += dxt * dxt
  248. dsyy += dyt * dyt
  249. dsxy += dxt * dyt
  250. val += slope
  251. val
  252.  
  253. stdDev = math.sqrt(stdDevAcc / (periods == 0 ? 1 : periods))
  254. pearsonR = dsxx == 0 or dsyy == 0 ? 0 : dsxy / math.sqrt(dsxx * dsyy)
  255. [stdDev, pearsonR, upDev, dnDev]
  256.  
  257. [stdDev, pearsonR, upDev, dnDev] = calcDev(src, len, s, aLR, i)
  258.  
  259. upperStartPrice = startPrice + (useUpperDev ? upperMult * stdDev : upDev)
  260. upperEndPrice = endPrice + (useUpperDev ? upperMult * stdDev : upDev)
  261. var line upper = na
  262.  
  263. lowerStartPrice = startPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
  264. lowerEndPrice = endPrice + (useLowerDev ? lowerMult * stdDev : -dnDev)
  265. var line lower = na
  266.  
  267. if na(upper) and not na(upperStartPrice)
  268. upper := line.new(bar_index - len + 1, upperStartPrice, bar_index, upperEndPrice, width=4, extend=extend, color=#0000ff)
  269. upper
  270. else
  271. line.set_xy1(upper, bar_index - len + 1, upperStartPrice)
  272. line.set_xy2(upper, bar_index, upperEndPrice)
  273. na
  274.  
  275. if na(lower) and not na(lowerStartPrice)
  276. lower := line.new(bar_index - len + 1, lowerStartPrice, bar_index, lowerEndPrice, width=4, extend=extend, color=#0000ff)
  277. lower
  278. else
  279. line.set_xy1(lower, bar_index - len + 1, lowerStartPrice)
  280. line.set_xy2(lower, bar_index, lowerEndPrice)
  281. na
  282.  
  283. // Pearson's R
  284. var label r = na
  285. transparent = color.new(color.white, 100)
  286. label.delete(r[1])
  287. if showPearson and not na(pearsonR)
  288. r := label.new(bar_index - len + 1, lowerStartPrice, str.tostring(pearsonR, '#.################'), color=transparent, textcolor=#0000ff, size=size.normal, style=label.style_label_up)
  289. r
  290. //end of this part
  291.  
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement