Advertisement
Guest User

TD Sequential Setup and Countdown

a guest
Jul 19th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.94 KB | None | 0 0
  1. // TD Sequential Setup and Countdown //
  2. //
  3. // Features:
  4. // ==========
  5. // Setup counts long/short | Encircled number above bar | Close exceeds the close 4 bars earlier in the direction of the setup. |
  6. // | Setup must be contiguous. It resets if a close fails the test. |
  7. //
  8. // 'Perfect' setups | Filled encircled 8 or 9 setup above bar | The high exceeds the high 2 bars earlier in the direction of the setup |
  9. //
  10. // Countdown long/short | Encircled number below bar | On and after a complete (9) setup, increment count for each close: |
  11. // | -- above the high of two bars earlier (rising short countdown). |
  12. // | -- below the low of two bars earlier (falling long countdown). |
  13. // | Is not contiguous, but resets if a new reversing setup completes, or |
  14. // | if the high or low exceeds the extreme high or low of the triggering setup. |
  15. // | The setup extreme high/low recycles based on the fib rules desccribed. |
  16. //
  17. // 'Perfect' countdown | Filled encircled 13 countdown below bar | When the 13th high or low exceeds the close of countdown bar 8 |
  18. //
  19. // Colour | Green for rising setups/countdowns | Reversible in configuration. |
  20. // | Red for falling setups/countdowns | |
  21. // How to trade:
  22. // ==============
  23. //
  24. // Works more reliably on longer time-frames, but is functional on any time-frame.
  25. // A '9' (or perfect '8') signals a potential reversal. Use with price action and candle stick patterns and other trends & indicators for support analysis.
  26. // A '13' countdown is a stronger reversal indicator, with the same caveats as above.
  27. // A '13' on or near a '9' setup is an extremely strong signal.
  28. // '9' setups or '13' countdowns across multiple time-frames are the best possible signal. e.g. a monthly 9, a weekly 9, a daily 9.
  29. //
  30. //@version=3
  31.  
  32. study("TD Setup and Countdown", "TD Sequential", overlay=true)
  33.  
  34. // TD Setup and Countdown //
  35.  
  36. ShowEMAs = input(false)
  37. timeframe = input('W', title="VWAP Close Time Frame")
  38. showPrevVWAP = input(false, type=bool, title="Show Previous VWAP close")
  39. showSetup = input(true, "Show Setup")
  40. showCountdown = input(true, "Show Countdown")
  41. showPerfect = input(true, "Show Perfect Counts")
  42. invertColours = input(false, "Invert Colours")
  43. showOnlyCompletion = input(false, "Only Show Completions")
  44. showTDST = input(true, "Show TDST Support & Resistance")
  45. setupRecyclesOnNextBar = input(true, "Setup Recycles On Next Bar")
  46. flip = input(false, "Setup Commences After TD Price Flip")
  47. showRisk = input(false, "Show Risk Line")
  48. volatilitySmoothing = input(false, "Volatility Smoothing (Relaxed Setup)")
  49.  
  50. // EMA's //
  51.  
  52. EMA8 = ema(close,8)
  53. EMA21 = ema(close,21)
  54. EMA55 = ema(close,55)
  55. EMA200 = ema(close,200)
  56.  
  57. plot(ShowEMAs?EMA8:na, color=#ffffff, linewidth=1, transp=0, title="EMA 8")
  58. plot(ShowEMAs?EMA21:na, color=#ffa800, linewidth=1, transp=0, title="EMA 21")
  59. plot(ShowEMAs?EMA55:na, color=#00a3ff, linewidth=1, transp=0, title="EMA 55")
  60. plot(ShowEMAs?EMA200:na, color=#ff006b, linewidth=1, transp=0, title="EMA 200")
  61.  
  62. // TD Code //
  63.  
  64.  
  65. shortColour = invertColours ? red : green
  66. longColour = invertColours ? green : red
  67. smoothedColour = aqua
  68.  
  69. setupShort = na, setupLong = na, setupCount = na, setupColour = na, perfectSetup = na
  70.  
  71. setupShort := if (close < close[4] and (not(volatilitySmoothing) or close < close[5]))
  72. 0
  73. else
  74. if (flip)
  75. if (nz(setupShort[1]) == 0 and close[1] < close[5] and close > close[4])
  76. 1
  77. else
  78. if (nz(setupShort[1]) == 0)
  79. 0
  80. else
  81. nz(setupShort[1] + 1)
  82. else
  83. if (setupShort[1] == 9)
  84. setupRecyclesOnNextBar ? 1 : 0
  85. else
  86. nz(setupShort[1] + 1)
  87.  
  88. setupLong := if (close > close[4] and (not(volatilitySmoothing) or close > close[5]))
  89. 0
  90. else
  91. if (flip)
  92. if (nz(setupLong[1]) == 0 and close[1] > close[5] and close < close[4])
  93. 1
  94. else
  95. if (nz(setupLong[1]) == 0)
  96. 0
  97. else
  98. nz(setupLong[1] + 1)
  99. else
  100. if (setupLong[1] == 9)
  101. setupRecyclesOnNextBar ? 1 : 0
  102. else
  103. nz(setupLong[1] + 1)
  104.  
  105. setupCount := max(setupShort, setupLong)
  106. setupColour := setupShort > 0 ? setupLong > 0 ? smoothedColour : shortColour : longColour
  107.  
  108. perfectSetup := showPerfect and ((setupShort == 8 and (high >= high[1] and high >= high[2])) or (setupShort == 9 and (high >= high[2] and high >= high[3])) or (setupLong == 8 and (low <= low[1] and low <= low[2])) or (setupLong == 9 and (low <= low[2] and low <= low[3])))
  109.  
  110.  
  111. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 1, title="Setup 1", char="1", color=setupColour, transp=0)
  112. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 2, title="Setup 2", char="2", color=setupColour, transp=0)
  113. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 3, title="Setup 3", char="3", color=setupColour, transp=0)
  114. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 4, title="Setup 4", char="4", color=setupColour, transp=0)
  115. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 5, title="Setup 5", char="5", color=setupColour, transp=0)
  116. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 6, title="Setup 6", char="6", color=setupColour, transp=0)
  117. plotchar(showSetup and (not(showOnlyCompletion) or barstate.islast) and setupCount == 7, title="Setup 7", char="7", color=setupColour, transp=0)
  118. plotchar(not(perfectSetup) and (not(showOnlyCompletion) or barstate.islast) and showSetup and setupCount == 8, title="Setup 8", char="8", color=setupColour, transp=0)
  119. plotchar(not(perfectSetup) and showSetup and setupCount == 9, title="Setup 9", char="⓽", color=setupColour, transp=0)
  120.  
  121. plotchar(perfectSetup and showSetup and setupCount == 8, title="Perfect setup 8", char="➑", color=setupColour, transp=0)
  122. plotchar(perfectSetup and showSetup and setupCount == 9, title="Perfect setup 9", char="➒", color=setupColour, transp=0)
  123.  
  124.  
  125. cdownShort = na, cdownLong = na, cdownColour = na, cdownCount = na, perfect8 = na, perfectCountdown = na
  126. lastCompletedSetup = na, setupHigh = na, setupLow = na, cancelCountdown = na, recycleCountdown = na
  127. LONG = -1, SHORT = 1, CANCELLED = 0
  128.  
  129. recycleCountdown := if (setupShort == 9 and lastCompletedSetup[1] == SHORT) or (setupLong == 9 and lastCompletedSetup[1] == LONG)
  130. (highest(9) - lowest(9)) >= (setupHigh[1] - setupLow[1]) and (setupHigh[1] - setupLow[1]) * 1.618 > (highest(9) - lowest(9))
  131.  
  132. setupHigh := recycleCountdown or (setupShort == 9 and lastCompletedSetup[1] != SHORT) or (setupLong == 9 and lastCompletedSetup[1] != LONG) ? highest(9) : setupHigh[1]
  133. setupLow := recycleCountdown or (setupShort == 9 and lastCompletedSetup[1] != SHORT) or (setupLong == 9 and lastCompletedSetup[1] != LONG) ? lowest(9) : setupLow[1]
  134.  
  135. cancelCountdown := (lastCompletedSetup[1] == SHORT and high < setupLow) or
  136. (lastCompletedSetup[1] == LONG and low > setupHigh) or
  137. recycleCountdown
  138.  
  139. lastCompletedSetup := setupLong == 9 ? LONG : (setupShort == 9 ? SHORT : (((cdownShort[1] == 13 and setupShort[1] != 9) or (cdownLong[1] == 13 and setupLong[1] != 9) or cancelCountdown) ? CANCELLED : lastCompletedSetup[1]))
  140.  
  141. cdownShort := (lastCompletedSetup != SHORT or cancelCountdown) ? 0 : (close >= high[2] ? (nz(cdownShort[1]) % 13) + 1 : nz(cdownShort[1]) % 13)
  142. cdownLong := (lastCompletedSetup != LONG or cancelCountdown) ? 0 : (close <= low[2] ? (nz(cdownLong[1]) % 13) + 1 : nz(cdownLong[1]) % 13)
  143.  
  144. cdownCount := max(cdownShort, cdownLong)
  145. cdownColour := cdownShort > 0 ? shortColour : longColour
  146.  
  147. perfect8 := cdownCount < 8 ? na : cdownCount == 8 ? close : perfect8[1]
  148. perfectCountdown := showPerfect and ((cdownShort == 13 and high >= perfect8) or (cdownLong and low <= perfect8))
  149.  
  150. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 1 and cdownCount[1] != 1, title="Countdown 1", char="①", location=location.belowbar, color=cdownColour, transp=0)
  151. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 2 and cdownCount[1] == 1, title="Countdown 2", char="②", location=location.belowbar, color=cdownColour, transp=0)
  152. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 3 and cdownCount[1] == 2, title="Countdown 3", char="③", location=location.belowbar, color=cdownColour, transp=0)
  153. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 4 and cdownCount[1] == 3, title="Countdown 4", char="④", location=location.belowbar, color=cdownColour, transp=0)
  154. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 5 and cdownCount[1] == 4, title="Countdown 5", char="⑤", location=location.belowbar, color=cdownColour, transp=0)
  155. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 6 and cdownCount[1] == 5, title="Countdown 6", char="⑥", location=location.belowbar, color=cdownColour, transp=0)
  156. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 7 and cdownCount[1] == 6, title="Countdown 7", char="⑦", location=location.belowbar, color=cdownColour, transp=0)
  157. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 8 and cdownCount[1] == 7, title="Countdown 8", char="⑧", location=location.belowbar, color=cdownColour, transp=0)
  158. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 9 and cdownCount[1] == 8, title="Countdown 9", char="⑨", location=location.belowbar, color=cdownColour, transp=0)
  159. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 10 and cdownCount[1] == 9, title="Countdown 10", char="⑩", location=location.belowbar, color=cdownColour, transp=0)
  160. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 11 and cdownCount[1] == 10, title="Countdown 11", char="⑪", location=location.belowbar, color=cdownColour, transp=0)
  161. plotchar(showCountdown and (not(showOnlyCompletion) or barstate.islast) and cdownCount == 12 and cdownCount[1] == 11, title="Countdown 12", char="⑫", location=location.belowbar, color=cdownColour, transp=0)
  162. plotchar(not(perfectCountdown) and showCountdown and cdownCount == 13 and cdownCount[1] == 12, title="Countdown 13", char="⑬", location=location.belowbar, color=cdownColour, transp=0)
  163. plotchar(perfectCountdown and showCountdown and cdownCount == 13 and cdownCount[1] == 12, title="Perfect Countdown 13", char="⓭", location=location.belowbar, color=cdownColour, transp=0)
  164.  
  165. plotshape(showCountdown and cancelCountdown, title="Countdown broke setup support/resistance", location=location.belowbar, style=shape.xcross, color=cdownColour, transp=0)
  166.  
  167. tdstSupport = na, tdstResistance = na, tdRisk = na, riskbar = na
  168.  
  169. tdstResistance := not(showTDST) ? na : (setupLong == 9 ? highest(9) : (close > tdstResistance[1] ? na : tdstResistance[1]))
  170. tdstSupport := not(showTDST) ? na : (setupShort == 9 ? lowest(9) : (close < tdstSupport[1] ? na : tdstSupport[1]))
  171.  
  172. alertcondition(close > tdstResistance[1] or close < tdstSupport[1], title="Close outside TDST range", message="Close outside TDST range")
  173.  
  174. plot(tdstResistance, title="TDST Resistance", color=longColour, style=linebr)
  175. plot(tdstSupport, title="TDST Support", color=shortColour, style=linebr)
  176.  
  177. if (not(showRisk))
  178. tdRisk := na
  179. else
  180. if (setupLong == 9)
  181. riskbar := -1 * lowestbars(9)[0]
  182. tdRisk := low[riskbar] - tr[riskbar]
  183. else
  184. if (setupShort == 9)
  185. riskbar = -1 * highestbars(9)[0]
  186. tdRisk := high[riskbar] + tr[riskbar]
  187. else
  188. tdRisk := min(open, close) < tdRisk[1] and max(open, close) > tdRisk[1] ? na : tdRisk[1]
  189.  
  190. plot(tdRisk, title="TD Risk", color=aqua, style=circles, linewidth=1)
  191.  
  192. // VWAP Stdev Previous Day Close //
  193.  
  194. start = security(tickerid, timeframe, time)
  195.  
  196. newSession = iff(change(start), 1, 0)
  197.  
  198. vwapsum = 0.0
  199. vwapsum :=iff(newSession, hl2*volume, vwapsum[1]+hl2*volume)
  200. volumesum = 0.0
  201. volumesum := iff(newSession, volume, volumesum[1]+volume)
  202. v2sum = 0.0
  203. v2sum := iff(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2)
  204. myvwap = 0.0
  205. myvwap := vwapsum/volumesum
  206. dev = 0.0
  207. dev := sqrt(max(v2sum/volumesum - myvwap*myvwap, 0))
  208. prevwap = 0.0
  209. prevwap := iff(newSession, myvwap[1], prevwap[1])
  210.  
  211. plot(showPrevVWAP ? prevwap : na, style=circles, color=close > prevwap ? green : red,title="VWAP Stdev Previous Day Close")
  212.  
  213. // Volume Bars //
  214. //PRICE BARS ARE:
  215.  
  216. //Bearish:
  217. //DARK RED when prices goes down and VOLUME is bigger than 150% of its (default 20 day) average, this indicates that price action is supported by a strong BEARISH VOLUME.
  218. //RED when prices goes down and VOLUME is BETWEEN 50% AND 150% of its (default 20 day) average, this indicates that volume is neither strong or weak.
  219. //ORANGE when prices goes down and VOLUME is less than 50% of its (default 20 day) average, so the volume is weak and doesn't support the price action.
  220.  
  221. //Bullish:
  222. //DARK GREEN when prices goes UP and VOLUME is bigger than 150% of its (default 20 day) average, this indicates that the price action is supported by a strong BULLISH VOLUME.
  223. //GREEN when prices goes UP and VOLUME is BETWEEN 50% AND 150% of its (default 20 day) average, this indicates that volume is neither strong or weak.
  224. //LIGHT GREEN when prices goes UP and VOLUME is less than 50% of its (default 20 day) average, so the volume is weak and doesn't support the price action.
  225.  
  226. length1=input(20, "Volume Bars Length", minval=1)
  227. avrg=sma(volume,length1)
  228. vold1 = volume > avrg*1.5 and close<open
  229. vold2 = volume >= avrg*0.5 and volume<=avrg*1.5 and close<open
  230. vold3 = volume < avrg *0.5 and close<open
  231. volu1 = volume > avrg*1.5 and close>open
  232. volu2 = volume >= avrg*0.5 and volume<=avrg*1.5 and close>open
  233. volu3 = volume< avrg*0.5 and close>open
  234. cold1=#800000
  235. cold2=#FF0000
  236. cold3=orange
  237. colu1=#006400
  238. colu2=lime
  239. colu3=#7FFFD4
  240. color = vold1 ? cold1 : vold2 ? cold2 : vold3 ? cold3 : volu1 ? colu1 : volu2 ? colu2 : volu3 ? colu3 : na
  241. barcolor(color)
  242.  
  243. //End//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement