xmd79

StairMaster TTF

Jan 2nd, 2023
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.25 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. // © Yidu_A_S_O_T_L_J_C
  3.  
  4. //@version=5
  5. indicator("StairMaster TTF", overlay = true)
  6.  
  7. uiGroup = "Config . UI"
  8. repaint = input(false, "Allow Signal Repainting", group=uiGroup)
  9. confirmed = repaint ? true : barstate.isconfirmed
  10.  
  11. sensitivity = input(1, "Stair Sensitivity", group="Config . Stair")
  12. useAlternateFractal = input(true, "Use Alternate Fractal", group="Config . Stair")
  13.  
  14. tsvGroup= "Config . Time Segmented Volume (TSV)"
  15. tsvLength = input(13, "TSV Length", group=tsvGroup)
  16. tsvma = input(7, "TSV MA (Filter) Length", group=tsvGroup)
  17.  
  18. baselineGroup = "Config . BaseLine"
  19. blsource = input.source(close, "Source", group=baselineGroup)
  20. blLength = input(75, "Length", group=baselineGroup)
  21. blType = input.string("Smoothed", "Type", ["Smoothed", "Linear", "Exponential", "Weighted", "Simple", "Trend Regularity Adaptive", "Arnaud Legoux", "Hull", "Double Exponential", "Triple Exponential"], group=baselineGroup)
  22. blBias = input(true, "Use baseline as a bias filter", group=baselineGroup)
  23.  
  24. rsiGroup = "Config . RSI"
  25. rsiLength = input(150, "RSI Length", group=rsiGroup)
  26. rsiSource = input.source(close, "RSI Source", group=rsiGroup)
  27. rsiMAType = input.string("Simple", "RSI MA Type", ["Smoothed", "Linear", "Exponential", "Weighted", "Simple", "Trend Regularity Adaptive", "Arnaud Legoux", "Hull", "Double Exponential", "Triple Exponential"], group=rsiGroup)
  28. rsiMALength = input(35, "RSI MA Length", group=rsiGroup)
  29.  
  30. consolidationGroup = "Config . Consolidation Zone"
  31. enableConsolidationZone = input(true, "Show Consolidation Zones", group=consolidationGroup)
  32. timeframe = input.timeframe(defval="5", title="Timeframe", group=consolidationGroup)
  33. prd = input.int(defval = 10, title="Loopback Period", minval = 2, maxval = 50, group=consolidationGroup)
  34. conslen = input.int(defval = 5, title="Min Consolidation Length", minval = 2, maxval = 20, group=consolidationGroup)
  35. zonecol = input.color(defval = color.new(color.red, 90), title = "Zone Color", group=consolidationGroup)
  36.  
  37. stochGroup = "Config . Stochastic RSI"
  38. enableStoch = input(true, "Enable Stochastic RSI", group=stochGroup)
  39. smoothK = input.int(3, "K", minval=1, group=stochGroup)
  40. smoothD = input.int(3, "D", minval=1, group=stochGroup)
  41. lengthRSI = input.int(14, "RSI Length", minval=1, group=stochGroup)
  42. lengthStoch = input.int(14, "Stochastic Length", minval=1, group=stochGroup)
  43.  
  44. srGroup = "Config . Support & Resistance"
  45. showSR = input(false, "Show Support & Resistance", group=srGroup)
  46. left=input.int(50,title="Left Pivot", group=srGroup)
  47. right=input.int(25,title="Right Pivot", group=srGroup)
  48. quick_right=input.int(5,title="Quick Right Pivot", group=srGroup)
  49.  
  50. // Session
  51. ts = input(false, title="Show Sessions", group="Trade Session")
  52. enableL = input(true, title="London", group="Trade Session")
  53. enableNY = input(true, title="NewYork", group="Trade Session")
  54. enableT = input(true, title="Asian", group="Trade Session")
  55. string dayG = "---------------"
  56. showMon = input(title="Monday", defval=true, group=dayG)
  57. showTue = input(title="Tuesday", defval=true, group=dayG)
  58. showWed = input(title="Wednesday", defval=true, group=dayG)
  59. showThu = input(title="Thursday", defval=true, group=dayG)
  60. showFri = input(title="Friday", defval=true, group=dayG)
  61. showSat = input(title="Saturday", defval=false, group=dayG)
  62. showSun = input(title="Sunday", defval=false, group=dayG)
  63.  
  64. indexHighTf = barstate.isrealtime ? 1 : 0
  65. buyColor = #00e1ff
  66. sellColor = #ff00dd
  67.  
  68. // Stair
  69. th = high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
  70. bl = low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0
  71.  
  72. tot = th + bl
  73. pl = math.abs(tot)>=1 ? 1 : 0
  74.  
  75. lowline = (ta.valuewhen(tot==1, low[2], 0) + (sensitivity>1 ? ta.valuewhen(tot==1, low[2], 1) : 0)
  76. + (sensitivity>2 ? ta.valuewhen(tot==1, low[2], 2) : 0) + (sensitivity>3 ? ta.valuewhen(tot==1, low[2], 3) : 0)
  77. + (sensitivity>4 ? ta.valuewhen(tot==1, low[2], 4) : 0) + (sensitivity>5 ? ta.valuewhen(tot==1, low[2], 5) : 0)
  78. + (sensitivity>6 ? ta.valuewhen(tot==1, low[2], 6) : 0) + (sensitivity>7 ? ta.valuewhen(tot==1, low[2], 7) : 0)
  79. + (sensitivity>8 ? ta.valuewhen(tot==1, low[2], 8) : 0) + (sensitivity>9 ? ta.valuewhen(tot==1, low[2], 9) : 0)
  80. )/sensitivity
  81.  
  82. highline = (ta.valuewhen(tot==-1, high[2], 0) + (sensitivity>1 ? ta.valuewhen(tot==-1, high[2], 1) : 0)
  83. + (sensitivity>2 ? ta.valuewhen(tot==-1, high[2], 2) : 0) + (sensitivity>3 ? ta.valuewhen(tot==-1, high[2], 3) : 0)
  84. + (sensitivity>4 ? ta.valuewhen(tot==-1, high[2], 4) : 0) + (sensitivity>5 ? ta.valuewhen(tot==-1, high[2], 5) : 0)
  85. + (sensitivity>6 ? ta.valuewhen(tot==-1, high[2], 6) : 0) + (sensitivity>7 ? ta.valuewhen(tot==-1, high[2], 7) : 0)
  86. + (sensitivity>8 ? ta.valuewhen(tot==-1, high[2], 8) : 0) + (sensitivity>9 ? ta.valuewhen(tot==-1, high[2], 9) : 0)
  87. )/sensitivity
  88.  
  89. FUp = high[4] < high[2] and high[3] < high[2] and high[1] < high[2] and high < high[2] or
  90. high[5] < high[2] and high[4] < high[2] and high[3] <= high[2] and
  91. high[1] < high[2] and high < high[2] or
  92. high[6] < high[2] and high[5] < high[2] and high[4] <= high[2] and
  93. high[3] <= high[2] and high[1] < high[2] and high < high[2] or
  94. high[7] < high[2] and high[6] < high[2] and high[5] <= high[2] and
  95. high[4] <= high[2] and high[3] <= high[2] and high[1] < high[2] and
  96. high < high[2] or
  97. high[8] < high[2] and high[7] < high[2] and high[6] <= high[2] and
  98. high[5] <= high[2] and high[4] <= high[2] and high[3] <= high[2] and
  99. high[1] < high[2] and high < high[2]
  100. FractalUp = useAlternateFractal ? highline : ta.valuewhen(FUp, high[2], sensitivity)
  101.  
  102. FDown = low[4] > low[2] and low[3] > low[2] and low[1] > low[2] and low > low[2] or
  103. low[5] > low[2] and low[4] > low[2] and low[3] >= low[2] and low[1] > low[2] and
  104. low > low[2] or
  105. low[6] > low[2] and low[5] > low[2] and low[4] >= low[2] and low[3] >= low[2] and
  106. low[1] > low[2] and low > low[2] or
  107. low[7] > low[2] and low[6] > low[2] and low[5] >= low[2] and low[4] >= low[2] and
  108. low[3] >= low[2] and low[1] > low[2] and low > low[2] or
  109. low[8] > low[2] and low[7] > low[2] and low[6] >= low[2] and low[5] >= low[2] and
  110. low[4] >= low[2] and low[3] >= low[2] and low[1] > low[2] and low > low[2]
  111. FractalDown = useAlternateFractal ? lowline : ta.valuewhen(FDown, low[2], sensitivity)
  112.  
  113. plot(FractalUp, "Ceiling", buyColor, 2, plot.style_stepline)
  114. plot(FractalDown, "Floor", sellColor, 2, plot.style_stepline)
  115.  
  116. fractalUpCondition = ta.crossover(close, FractalUp)
  117. fractalDnCondition = ta.crossunder(close,FractalDown)
  118.  
  119. // tsv
  120. tsv = math.sum(close>close[1]?volume*(close-close[1]):close<close[1]?volume*(close-close[1]):0, tsvLength)
  121. _tsvma = ta.sma(tsv, tsvma)
  122.  
  123. tsvUpCondition = tsv > 0 and _tsvma > 0
  124. tsvDnCondition = tsv < 0 and _tsvma < 0
  125.  
  126. tsvColor = tsvUpCondition ? buyColor : tsvDnCondition ? sellColor : na
  127.  
  128. // baseline
  129. smma(src, length) =>
  130. smma = 0.0
  131. sma = ta.sma(src, length)
  132. smma := na(smma[1]) ? sma : (smma[1] * (length - 1) + src) / length
  133. smma
  134.  
  135. trama(src, length) =>
  136. ama = 0.
  137. hh = math.max(math.sign(ta.change(ta.highest(length))),0)
  138. ll = math.max(math.sign(ta.change(ta.lowest(length))*-1),0)
  139. tc = math.pow(ta.sma(hh or ll ? 1 : 0,length),2)
  140. ama := nz(ama[1]+tc*(src-ama[1]),src)
  141. ama
  142.  
  143. hma(src, length) =>
  144. ta.wma(2 * ta.wma(src, length / 2) - ta.wma(src, length), math.round(math.sqrt(length)))
  145.  
  146. dema(src, length) =>
  147. ema = ta.ema(src, length)
  148. dema = 2 * ema - ta.ema(ema, length)
  149. dema
  150.  
  151. tema(src, length) =>
  152. ema = ta.ema(src, length)
  153. ema2 = ta.ema(ema, length)
  154. tema = 3 * (ema - ema2) + ta.ema(ema2, length)
  155. tema
  156.  
  157. ma(type, length, src) =>
  158. result = type == "Smoothed" ? smma(src, length) :
  159. type == "Linear" ? ta.linreg(src, length, 0) :
  160. type == "Exponential" ? ta.ema(src, length) :
  161. type == "Weighted" ? ta.wma(src, length) :
  162. type == "Simple" ? ta.sma(src, length) :
  163. type == "Trend Regularity Adaptive" ? trama(src, length) :
  164. type == "Arnaud Legoux" ? ta.alma(src, length, 0.86, 5) :
  165. type == "Hull" ? hma(src, length) :
  166. type == "Double Exponential" ? dema(src, length) :
  167. type == "Triple Exponential" ? tema(src, length) : ta.sma(src, length)
  168. result
  169.  
  170. baseline = ma(blType, blLength, blsource)
  171.  
  172. baselineUpCondition = blBias ? close > baseline : true
  173. baselineDnCondition = blBias ? close < baseline : true
  174.  
  175. // rsi
  176. up = ta.rma(math.max(ta.change(rsiSource), 0), rsiLength)
  177. down = ta.rma(-math.min(ta.change(rsiSource), 0), rsiLength)
  178. rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  179.  
  180. rsima = ma(rsiMAType, rsiMALength, rsi)
  181.  
  182. rsiUpCondition = rsi > rsima
  183. rsiDnCondition = rsi < rsima
  184.  
  185. // Consolidation Zones
  186. consolidationZone() =>
  187. float hb_ = ta.highestbars(prd) == 0 ? high : na
  188. float lb_ = ta.lowestbars(prd) == 0 ? low : na
  189. var int dir = 0
  190. float zz = na
  191. float pp = na
  192.  
  193. dir := hb_ and na(lb_) ? 1 : lb_ and na(hb_) ? -1 : dir
  194. if hb_ and lb_
  195. if dir == 1
  196. zz := hb_
  197. else
  198. zz := lb_
  199. else
  200. zz := hb_ ? hb_ : lb_ ? lb_ : na
  201.  
  202. for x = 0 to 1000
  203. if na(close[indexHighTf]) or dir != dir[x]
  204. break
  205. if zz[x]
  206. if na(pp)
  207. pp := zz[x]
  208. else
  209. if dir[x] == 1 and zz[x] > pp
  210. pp := zz[x]
  211. if dir[x] == -1 and zz[x] < pp
  212. pp := zz[x]
  213.  
  214. var int conscnt = 0
  215. var float condhigh = na
  216. var float condlow = na
  217.  
  218. float H_ = ta.highest(conslen)
  219. float L_ = ta.lowest(conslen)
  220.  
  221. if ta.change(pp)
  222. if conscnt > 0 and pp <= condhigh and pp >= condlow
  223. conscnt := conscnt + 1
  224. else
  225. conscnt := 0
  226. else
  227. conscnt := conscnt + 1
  228.  
  229. if conscnt >= conslen
  230. if conscnt == conslen
  231. condhigh := H_
  232. condlow := L_
  233. else
  234. condhigh := math.max(condhigh, high)
  235. condlow := math.min(condlow, low)
  236.  
  237.  
  238. [conscnt, condhigh, condlow]
  239.  
  240. [_conscnt, _condhigh, _condlow] = request.security(syminfo.ticker, timeframe, consolidationZone(), lookahead=barmerge.lookahead_on)
  241.  
  242. // Stochastic RSI
  243. src = close
  244. rsi1 = ta.rsi(src, lengthRSI)
  245. k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
  246. d = ta.sma(k, smoothD)
  247.  
  248. stochUpCondition = enableStoch ? k > d : true
  249. stochDnCondition = enableStoch ? k < d : true
  250.  
  251. // S&R
  252. pivot_high = ta.pivothigh(high,left,right)
  253. pivot_lows = ta.pivotlow(low, left,right)
  254.  
  255. quick_pivot_high = ta.pivothigh(high,left,quick_right)
  256. quick_pivot_lows = ta.pivotlow(low, left,quick_right)
  257.  
  258. level1 = ta.valuewhen(quick_pivot_high, high[quick_right], 0)
  259. level2 = ta.valuewhen(quick_pivot_lows, low[quick_right], 0)
  260. level3 = ta.valuewhen(pivot_high, high[right], 0)
  261. level4 = ta.valuewhen(pivot_lows, low[right], 0)
  262. level5 = ta.valuewhen(pivot_high, high[right], 1)
  263. level6 = ta.valuewhen(pivot_lows, low[right], 1)
  264. level7 = ta.valuewhen(pivot_high, high[right], 2)
  265. level8 = ta.valuewhen(pivot_lows, low[right], 2)
  266.  
  267. level1_col = close >= level1 ? buyColor : sellColor
  268. level2_col = close >= level2 ? buyColor : sellColor
  269. level3_col = close >= level3 ? buyColor : sellColor
  270. level4_col = close >= level4 ? buyColor : sellColor
  271. level5_col = close >= level5 ? buyColor : sellColor
  272. level6_col = close >= level6 ? buyColor : sellColor
  273. level7_col = close >= level7 ? buyColor : sellColor
  274. level8_col = close >= level8 ? buyColor : sellColor
  275.  
  276. // Session
  277. tzDayOfWeek = dayofweek(time, "Europe/London")
  278.  
  279. L = enableL ? time(timeframe.period,"0300-1200") : na
  280. NY= enableNY ? time(timeframe.period,"0800-1700") : na
  281. T = enableT ? time(timeframe.period,"2000-0300") : na
  282.  
  283. active = if ts
  284. if tzDayOfWeek == dayofweek.monday and showMon
  285. true
  286. else if tzDayOfWeek == dayofweek.tuesday and showTue
  287. true
  288. else if tzDayOfWeek == dayofweek.wednesday and showWed
  289. true
  290. else if tzDayOfWeek == dayofweek.thursday and showThu
  291. true
  292. else if tzDayOfWeek == dayofweek.friday and showFri
  293. true
  294. else if tzDayOfWeek == dayofweek.saturday and showSat
  295. true
  296. else if tzDayOfWeek == dayofweek.sunday and showSun
  297. true
  298. else
  299. false
  300. else
  301. false
  302.  
  303. // Signal
  304. upSignal = fractalUpCondition and rsiUpCondition and tsvUpCondition and stochUpCondition and baselineUpCondition and confirmed
  305. dnSignal = fractalDnCondition and rsiDnCondition and tsvDnCondition and stochDnCondition and baselineDnCondition and confirmed
  306.  
  307. // alert
  308. if upSignal or dnSignal
  309. alert("StairMaster TTF > Signal Alert", repaint ? alert.freq_once_per_bar : alert.freq_once_per_bar_close)
  310.  
  311. // Plot
  312. bgcolor(color=not na(L) and active ? color.new(color.green, 90) : na, title='London Session')
  313. bgcolor(color=not na(NY) and active ? color.new(color.red, 90) : na, title='New York Session')
  314. bgcolor(color=not na(T) and active ? color.new(color.orange, 90) : na, title='Tokyo Session')
  315.  
  316. plot((showSR) ? level1 : na, title="S&R1", style=plot.style_circles, color=level1_col, show_last=1, linewidth=2, trackprice=true)
  317. plot((showSR) ? level2 : na, title="S&R2", style=plot.style_circles, color=level2_col, show_last=1, linewidth=2, trackprice=true)
  318. plot((showSR) ? level3 : na, title="S&R3", style=plot.style_circles, color=level3_col, show_last=1, linewidth=2, trackprice=true)
  319. plot((showSR) ? level4 : na, title="S&R4", style=plot.style_circles, color=level4_col, show_last=1, linewidth=2, trackprice=true)
  320. plot((showSR) ? level5 : na, title="S&R5", style=plot.style_circles, color=level5_col, show_last=1, linewidth=2, trackprice=true)
  321. plot((showSR) ? level6 : na, title="S&R6", style=plot.style_circles, color=level6_col, show_last=1, linewidth=2, trackprice=true)
  322. plot((showSR) ? level7 : na, title="S&R7", style=plot.style_circles, color=level7_col, show_last=1, linewidth=2, trackprice=true)
  323. plot((showSR) ? level8 : na, title="S&R8", style=plot.style_circles, color=level8_col, show_last=1, linewidth=2, trackprice=true)
  324.  
  325. var line upline = na
  326. var line dnline = na
  327.  
  328. if _conscnt >= conslen
  329. if _conscnt != conslen
  330. line.delete(upline)
  331. line.delete(dnline)
  332.  
  333. upline := line.new(bar_index, enableConsolidationZone ? _condhigh : na, bar_index - _conscnt, enableConsolidationZone ? _condhigh : na, color = buyColor, style = line.style_dashed)
  334. dnline := line.new(bar_index, enableConsolidationZone ? _condlow : na , bar_index - _conscnt, enableConsolidationZone ? _condlow : na, color = sellColor, style = line.style_dashed)
  335.  
  336. consolidationZoneHigh = plot(enableConsolidationZone ? _condhigh : na, title="consolidation zone high", color=na, style = plot.style_stepline)
  337. consolidationZoneLow = plot(enableConsolidationZone ? _condlow : na, title="consolidation zone low", color=na, style = plot.style_stepline)
  338. fill(consolidationZoneHigh, consolidationZoneLow, title="consolidation zone background", color = _conscnt > conslen ? zonecol : color.new(color.white, 100))
  339.  
  340. plot(baseline, "BaseLine", tsvColor, 2)
  341. plotchar(upSignal, "Buy", "👟", location.belowbar, editable=false, size=size.tiny)
  342. plotchar(dnSignal, "Sell", "👟", location.abovebar, editable=false, size=size.tiny)
Advertisement
Add Comment
Please, Sign In to add comment