Advertisement
JustUncleL

Traders Dynamic Index Indicator Alert v1.3 by JustUncleL

Aug 6th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. //@version=3
  2. //
  3.  
  4. study("Traders Dynamic Index Indicator Alert v1.3 by JustUncleL", shorttitle="TDIALT")
  5.  
  6. //
  7. // author: JustUncleL
  8. // date: 1-Mar-2016
  9. //
  10. // Original code @author LazyBear for basic TDI indicator.
  11. // If you use this code in its orignal/modified form, do drop me a note.
  12. //
  13. // Description:
  14. // This is a Trend following system utilising the Traders Dynamic Index (TDI),
  15. // Price Action Channel (PAC) and Heikin Ashi candles.
  16. // About 6months ago I came across the use of TDI in "E.A.S.Y. Method" that I found in
  17. // forexfactory forums: http://www.forexfactory.com/attachment.php?attachmentid=686629&d=1303831008
  18. // and I was able to set up a chart based on the specifications by using Kurbelklaus scripts.
  19. // However, I found that the alerts were being generated one or two bars too late,
  20. // so I was not successful using it with Binary Options. Later I found a variation of the
  21. // method in the forums which generates alerts a bit earlier, so this indicator is a
  22. // variation of that early detection version.
  23. // The indicator can optionally use Heikin Ashi candles only for all it's calculations,
  24. // I would recommend viewing the chart with normal Heikin Ashi candles, these
  25. // smooth out the trends and makes them more visible.
  26. //
  27. // I found that this metod it works OK with currency pairs or commodities.
  28. // It also seems to work well with 5min+ timeframe charts,
  29. // and I would suggest expiry of 2 to 6 candles.
  30. //
  31. // ALERT GENERATION:
  32. // =================
  33. //
  34. // The TDI (Traders Dynamic Index)
  35. // -------------------------------
  36. // Volatility Band VB(34), color: Blue, buffer: UpZone, DnZone
  37. // Relative Strength Index RSI(13)
  38. // RSI PRICE LINE (2), color: Green, buffer: mab
  39. // RSI TRADE SIGNAL LINE (7), color: Red, buffer: mbb
  40. // MARKET BASE LINE MID VB(34), color: Orange, buffer: mid
  41. //
  42. // Indicator SignalLevels:
  43. // -----------------------
  44. // RSI_OversoldLevel : 22 (normally: 32)
  45. // RSI_OverboughtLevel : 78 (normally: 68)
  46. //
  47. // Alert Conditions:
  48. // -----------------
  49. // Strong Buy (yellow): HIGH>PAC upper && BULL Candle && Candle High>PAC High && RSI>TRADE SIGNAL LINE && RSI>RSI_OversoldLevel &&
  50. // && RSI<RSI_OverboughtLevel && TRADE SIGNAL LINE> MARKET BASE LINE
  51. // Medium Buy (aqua): HIGH>PAC upper && BULL Candle && Candle High>PAC High && RSI>TRADE SIGNAL LINE && RSI>RSI_OversoldLevel
  52. // && RSI<RSI_OverboughtLevel && RSI> MARKET BASE LINE && TRADE SIGNAL LINE< MARKET BASE LINE
  53. // Weak Buy (blue): HIGH>PAC upper && BULL Candle && Candle High>PAC High && RSI>TRADE SIGNAL LINE && RSI>RSI_OversoldLevel
  54. // && RSI<RSI_OverboughtLevel && TRADE SIGNAL LINE<MARKET BASE LINE && RSI< MARKET BASE LINE
  55. //
  56. // Strong Sell (fuchsia): LOW<PAC lower && BEAR Candle && Candle Low>PAC Low && RSI<TRADE SIGNAL LINE && RSI>RSI_OversoldLevel
  57. // && RSI<RSI_OverboughtLevel && TRADE SIGNAL LINE< MARKET BASE LINE
  58. // Medium Sell (purple): LOW<PAC lower && BEAR Candle && Candle Low>PAC Low && RSI<TRADE SIGNAL LINE && RSI>RSI_OversoldLevel
  59. // && RSI<RSI_OverboughtLevel && RSI< MARKET BASE LINE && TRADE SIGNAL LINE> MARKET BASE LINE
  60. // Weak Sell (black): LOW<PAC lower && BEAR Candle && Candle Low>PAC Low && RSI<TRADE SIGNAL LINE && RSI>RSI_OversoldLevel
  61. // && RSI<RSI_OverboughtLevel && TRADE SIGNAL LINE> MARKET BASE LINE && RSI> MARKET BASE LINE
  62. //
  63. // HIGH LEVEL FILTER (Overbought): RSI>=RSI_OverboughtLevel or MACD Histogram not green
  64. // LOW LEVEL FILTER (Oversold): RSI<=RSI_OversoldLevel or MACD Histogram not red
  65. //
  66. // Hints on How to use:
  67. // --------------------
  68. // - When a Medium or Strong alert is generated and MACD histogram colour matches the direction
  69. // of the alert, then place trade in direction of alert candle and MACD.
  70. // - Use the multi-Hull MA's for trend direction confirmation.
  71. // - Best positions occur near the MACD(8,16,1) Histogram crossing the zero line.
  72. // - The optional coloured Dots along the bottom of the indicator represent the first alert
  73. // of this type that was generated in this sequence.
  74. // - It is advisable to trade in the direction of the main trend as indicated the HULL MA red cloud:
  75. // if red cloud underneath PAC then BULLISH trend, if red cloud above PAC then BEARISH trend.
  76. // - Selecting the HeiKin Ashi candles does affect the MACD and MA caculations, so if you select
  77. // normal candles the result chart will change. Although the TDI calculations and alerts will stay the
  78. // same.
  79. // - When using the Heikin Ashi candles, a good buy entry is indicated by long top wick and no bottom wick
  80. // for bull (green) candles and good sell entry is indicated by long bottom wick and no top wick for
  81. // bear (red) candles.
  82. // - When the MACD histogram is flat and close to zero line,
  83. // this indicates a ranging market, do NOT trade when this occurs.
  84. // - When the PAC channel on the main chart is spread apart widely, this is an indication
  85. // of extreme volitity and choppy chart, do NOT try to trade during these periods.
  86. // A choppy chart is also indicated by Heikin Ashi candles with long wicks on both sides
  87. // of the candles.
  88. // - You can specify what strength level Alerts are generated (default 2):
  89. // Level (1) means only generate Strong Alerts only.
  90. // Level (2) means generate Strong and Medium Alerts.
  91. // Level (3) means generate Strong, Medium and Weak Alerts.
  92. //
  93. // Modifications:
  94. // --------------
  95. // 1.3 7-Aug-2017
  96. // - Modified Alertcondition code so that it only signals on the last completed alerted
  97. // candle. This should prevent some false alerts and multiple alerts being generated.
  98. // When setting alarms I suggest using Frequency "Once per bar (on condition)" to get
  99. // the earliest signal possible.
  100. // - Added optional alert long/short spikes that only occur on the first Long or Short
  101. // signals.
  102. // 1.2 5-Aug-2017:
  103. // - Added new alerts for Buy and Sell (Long and Short) signals seperately.
  104. // - Upgraded to version 3 Pinescript.
  105. // - Added work around patch for opaque bar colouring issue.
  106. //
  107. // 1.1 - Modified code so when viewing Top chart with Heikin Ashi candles, calculations
  108. // are still based on standard candles, unless Heikin Ashi calculation option selected.
  109. //
  110. // 0.2 - Simplified MACD direction calculation to use just rising/falling.
  111. // 0.1 - Oroginal Version
  112. // References:
  113. // -----------
  114. // Traders Dynamic Index [LazyBear]
  115. // KK_Traders Dynamic Index_Bar Highlighting by Kurbelklaus
  116. // KK_Price Action Channel (TDI BH) by Kurbelklaus
  117. // http://www.forexfactory.com/attachment.php?attachmentid=686629&d=1303831008
  118. // http://www.forexstrategiesresources.com/trading-system-metatrader-4-iv/504-traders-dynamic-index-how-to-use/
  119. // http://www.forexfactory.com/showthread.php?t=460148
  120. // http://www.forexstrategiesresources.com/scalping-forex-strategies-ii/205-scalping-with-tdi-real-macd-stochrainbow/
  121. //
  122. lengthrsi=input(13)
  123. lengthband=input(34)
  124. lengthrsipl=input(2)
  125. lengthtradesl=input(7)
  126. lenH = input(5, minval=1, title="Price Action Channel Length")
  127. lenL = lenH
  128. rsiOSL= input(22, minval=0,maxval=49,title="RSI Oversold Level")
  129. rsiOBL= input(78, minval=51,maxval=100,title="RSI Overbought Level")
  130. strength = input(2,minval=1,maxval=3,step=1,title="Strength Level: (1)Strong (2)Medium (3)All")
  131. sgb = input(false, title="Check Box To Turn Bars Gray")
  132. sbr = input(true, title="Highlight TDI Alert Bars")
  133. sal = input(true, title="Show Alert Condition Status")
  134. uha = input(false, title="Use Heikin Ashi Candles for Calculations")
  135. sspikes = input(false, title="Show Spike LONG and SHORT Signals")
  136. //
  137.  
  138. // Constants colours that include fully non-transparent option.
  139. blue100 = #0000FFFF
  140. aqua100 = #00FFFFFF
  141. fuchsia100 = #FF00FFFF
  142. purple100 = #800080FF
  143. gray100 = #808080FF
  144. gold100 = #FFD700FF
  145. white100 = #FFFFFFFF
  146. black100 = #000000FF
  147. gold = #FFD700
  148.  
  149.  
  150. // Use only Heikinashi Candles for all calculations or use Standard Candles for calculations.
  151. srcClose = uha ? security(heikinashi(tickerid), period, close) : security(ticker, period, close)
  152. srcOpen = uha ? security(heikinashi(tickerid), period, open) : security(ticker, period, open)
  153. srcHigh = uha ? security(heikinashi(tickerid), period, high) : security(ticker, period, high)
  154. srcLow = uha ?security(heikinashi(tickerid), period, low) : security(ticker, period, low)
  155. //
  156. r=rsi(srcClose, lengthrsi)
  157. ma=sma(r,lengthband)
  158. offs=(1.6185 * stdev(r, lengthband))
  159. upZone=ma+offs
  160. dnZone=ma-offs
  161. mid=(upZone+dnZone)/2
  162. mab=sma(r, lengthrsipl)
  163. mbb=sma(r, lengthtradesl)
  164. //
  165. hline(rsiOSL, color=red, linewidth=1)
  166. hline(50, color=black, linewidth=1)
  167. hline(rsiOBL, color=lime, linewidth=1)
  168. // Plot the TDI
  169. upl=plot(upZone, color=blue, title="VB Channel High",linewidth=2)
  170. dnl=plot(dnZone, color=blue, title="VB Channel Low",linewidth=2)
  171. midl=plot(mid, color=orange, linewidth=2, title="MBL")
  172. mabl=plot(mab, color=green, linewidth=2, title="RSI PL")
  173. mbbl=plot(mbb, color=red, linewidth=2, title="TSL Signal")
  174. //
  175. //create RSI TSL cloud to indicate trend direction.
  176. fill(mabl,mbbl, color=mab>mbb?green:red,transp=90)
  177.  
  178. // Calculate Price Action Channel (PAC)
  179. smmaH = 0.0
  180. smmaL = 0.0
  181. smmaH := na(smmaH[1]) ? sma(srcHigh, lenH) : (smmaH[1] * (lenH - 1) + srcHigh) / lenH
  182. smmaL := na(smmaL[1]) ? sma(srcLow, lenL) : (smmaL[1] * (lenL - 1) + srcLow) / lenL
  183. //
  184. umacd = input(false,title="Use MACD Filtering")
  185. fastMA = input(title="MACD Fast MA Length", type = integer, defval = 8, minval = 2)
  186. slowMA = input(title="MACD Slow MA Length", type = integer, defval = 16, minval = 7)
  187. signal = input(title="MACD Signal Length",type=integer,defval=1,minval=1)
  188. //
  189. //
  190. [currMacd,_,_] = macd(srcClose[0], fastMA, slowMA, signal)
  191. macdH = currMacd > 0 ? rising(currMacd,2) ? green : red : falling(currMacd, 2) ? red : green
  192.  
  193. //
  194. // Bar - Highlighting based on indication strength
  195. long= (not umacd or macdH==green) and mab > mbb and mab < rsiOBL and mab > rsiOSL and srcHigh > smmaH and srcClose > srcOpen ?
  196. mbb > mid ? 1 : mab>mid and mbb<mid ? 2 : mab<mid and mbb<mid ? 3 : 0: 0
  197. short= (not umacd or macdH==red) and mab < mbb and mab <rsiOBL and mab > rsiOSL and srcLow < smmaL and srcClose < srcOpen ?
  198. mbb < mid ? 1 : mab<mid and mbb>mid ? 2 :mab>mid and mbb>mid ? 3 : 0 : 0
  199. //
  200. // Find the right Bar colour if enabled.
  201. bcolor = not sbr? na : long==1? gold100: long==2 and strength>1? aqua100:long==3 and strength>2? blue100:
  202. short==1? fuchsia100: short==2 and strength>1? purple100:short==3 and strength>2? black100 :
  203. sgb ? gray100 : na
  204. //
  205. barcolor(color=bcolor,title="Bars Colours")
  206. //
  207. //
  208. // create alerts only once per sequence type.
  209. //
  210. long_ = (long>0 and long!=long[1] and long<=strength)
  211. short_ = (short>0 and short!=short[1] and short<=strength)
  212. c_alert = long_ or short_
  213. alertcondition(c_alert[1] and barstate.isnew, title="TDIALT Alert", message="TDIALT Alert")
  214. alertcondition(long_[1] and barstate.isnew, title="TDIALT Long Alert", message="TDIALT Long")
  215. alertcondition(short_[1] and barstate.isnew, title="TDIALT Short Alert", message="TDIALT Short")
  216.  
  217. // Single Alert Strategy
  218. stateLong = 0
  219. stateShort = 0
  220. stateLong_ = nz(stateLong[1])
  221. stateShort_ = nz(stateShort[1])
  222.  
  223. //Count the long signals before next short
  224. if (long[1]>0 and long[1]<=strength)
  225. stateLong_ := stateLong_>0 ? stateLong_+1 : 1
  226. stateShort_:= 0
  227.  
  228. //Count the short signals before next long.
  229. if (short[1]>0 and short[1]<=strength)
  230. stateShort_ := stateShort_>0 ? stateShort_+1 : 1
  231. stateLong_ := 0
  232.  
  233. //create spikes, hide them by setting transp=100
  234. plot( sspikes?stateLong_==1? 100 : 0:na, color=blue, title="LONG",transp=50)
  235. plot( sspikes?stateShort_==1? 100 :0:na, color=red, title="SHORT",transp=50)
  236.  
  237. //keep current state counts.
  238. stateLong := stateLong_>0? stateLong_+1 : 0
  239. stateShort := stateShort_>0? stateShort_+1 : 0
  240.  
  241.  
  242. //
  243. // show dot only when alert condition is met and bar closed.
  244. plotshape(sal and c_alert[1],title= "Alert Indicator", location=location.bottom,
  245. color=long[1]==1? gold: long[1]==2?aqua:long[1]==3?blue:short[1]==1?fuchsia:
  246. short[1]==2?purple:short[1]==3? black : na, transp=0, style=shape.circle,offset=-1)
  247. //
  248. //EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement