Advertisement
JustUncleL

[Strategy]T3MA Ribbon R4.2 by JustUncleL

Jan 6th, 2018
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. //@version=3
  2.  
  3. strategy(title="[Strategy]T3MA Ribbon R4.2 by JustUncleL", shorttitle="[STRATEGY]T3RIBBON", overlay = true)
  4.  
  5. //
  6. // Revision: R4
  7. // Revision Author: JustUncleL
  8. //
  9. // Description:
  10. // Strategy Backtester Version
  11. // This study draws a T3 Moving average Coloured Ribbon based on a Fast and Slow T3 MAs.
  12. //
  13. // References:
  14. // - https://www.forexstrategiesresources.com/binary-options-trading-strategies/84-winner-binary-system/
  15. // =======
  16. // - T3 Average by HPotter v1.0 21/05/2014
  17. // This indicator plots the moving average described in the January, 1998 issue
  18. // of S&C, p.57, "Smoothing Techniques for More Accurate Signals", by Tim Tillson.
  19. // This indicator plots T3 moving average presented in Figure 4 in the article.
  20. // T3 indicator is a moving average which is calculated according to formula:
  21. // T3(n) = GD(GD(GD(n))),
  22. // where GD - generalized DEMA (Double EMA) and calculating according to this:
  23. // GD(n,v) = EMA(n) * (1+v)-EMA(EMA(n)) * v,
  24. // where "v" is volume factor, which determines how hot the moving average’s response
  25. // to linear trends will be. The author advises to use v=0.7.
  26. // When v = 0, GD = EMA, and when v = 1, GD = DEMA. In between, GD is a less aggressive
  27. // version of DEMA. By using a value for v less than1, trader cure the multiple DEMA
  28. // overshoot problem but at the cost of accepting some additional phase delay.
  29. // In filter theory terminology, T3 is a six-pole nonlinear Kalman filter. Kalman
  30. // filters are ones that use the error — in this case, (time series - EMA(n)) —
  31. // to correct themselves. In the realm of technical analysis, these are called adaptive
  32. // moving averages; they track the time series more aggres-sively when it is making large
  33. // moves. Tim Tillson is a software project manager at Hewlett-Packard, with degrees in
  34. // mathematics and computer science. He has privately traded options and equities for 15 years.
  35. // =======
  36. // Options:
  37. //
  38. // 1) Option to display coloured Candles around the Ribbon, the coulouring uses
  39. // the Standard candle colours:
  40. // - Lime = candle closed above Ribbon.
  41. // - Red = candle closed below Ribbon.
  42. // - Gray = Candle Closed inside Ribbon.
  43. // the Grab candles scheme:
  44. // - Lime = Bull candle closed above Ribbon.
  45. // - Green = Bear candle closed above Ribbon.
  46. // - Red = Bull candle closed below Ribbon.
  47. // - DarkRed = Bear candle closed below Ribbon.
  48. // - Aqua = Bull candle closed inside Ribbon.
  49. // - Blue = Bear candle closed inside Ribbon.
  50. //
  51. // 2) Option to base the candles on a higher time frame (HFT), this performed by increasing
  52. // the MA length to create equivalent lengths from the HFT. So no re-painting.
  53. // NOTE: The script will time out if the MA lengths get too long after resizing.
  54. //
  55. // Modifications:
  56. // R1 - Original
  57. // R2 - Added optional Bar colouring
  58. // - Added option to Anchor chart to a higher Time Frame (1440 max)
  59. //
  60. // 10-Aug-2017 - R3:
  61. // - Made updates for Pinescript version 3 Compliance.
  62. // - Allow anchor option to be used for Days and Weeks.
  63. // - Allow option to use standard 3-tone bar colouring, or 6-tone Grab candle Colouring
  64. // - Added named colour constants, to include fully opaque colours, a work around
  65. // for bar colouring transparency bug.
  66. //
  67. // 7-Jan-2018 R4:
  68. // - Added Alert Buy/Sell exit options.
  69. // - Added alarms for TradingView's Alarm Subsystem.
  70. // - Added implied GPL Copyright notice.
  71. // - Some coding efficiency and some variable name changes to make it easier
  72. // to read.
  73. //
  74. //
  75. // -----------------------------------------------------------------------------
  76. // Copyright 2014 HPotter
  77. // Copyright 2017,2018 JustUncleL
  78. //
  79. // This program is free software: you can redistribute it and/or modify
  80. // it under the terms of the GNU General Public License as published by
  81. // the Free Software Foundation, either version 3 of the License, or
  82. // any later version.
  83. //
  84. // This program is distributed in the hope that it will be useful,
  85. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  86. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  87. // GNU General Public License for more details.
  88. //
  89. // The GNU General Public License can be found here
  90. // <http://www.gnu.org/licenses/>.
  91. //
  92. // -----------------------------------------------------------------------------
  93. //
  94.  
  95. // Use Alternate Anchor TF for MAs
  96. anchor = input(0,minval=0,maxval=1440,title="Use Alternate Anchor TimeFrame (0=none, max=1440mins)")
  97. LengthFast_ = input(8, minval=1,title="Fast T3MA length")
  98. VolFactorFast= input(0.7,minval=0.0,maxval=1.0,title="Fast T3 Volume Factor")
  99. LengthSlow__ = input(13, minval=2,title="Slow T3MA length")
  100. VolFactorSlow= input(0.6,minval=0.0,maxval=1.0,title="Slow T3 Volume Factor")
  101. //ma_src = input(close,title="T3MA Source")
  102. showRibbon = input(false,title="Display T3MA Ribbon")
  103. sBars = input(false,title="Show Coloured Trend Bars")
  104. uGrabClr = input(false,title="Use Grab Bar 6-tone Colours, instead of Standard 3-tone")
  105. //
  106. dFilter = input(false,title="Filter Signals to T3MA Ribbon Colour")
  107. // LRS Filter
  108. //sFilter = input(false,title="Apply ADX Filter to Signals")
  109. //clen = input(defval = 15, minval = 1, title = "Linear Regression Length")
  110. //slimit = input(defval=0.0025,minval=0.0,maxval=100.0,type=float,title="Slope Filter Limit (0=none)")
  111. isHA = input(true, "Use HA Candles for Calculations")
  112.  
  113. data = isHA ? heikenashi(tickerid) : tickerid
  114.  
  115. open_ = security(data, period, open)
  116. high_ = security(data, period, high)
  117. low_ = security(data, period, low)
  118. close_ = security(data, period, close)
  119.  
  120. col = close_ > open_ ? green : maroon
  121. plotbar(open_, high_, low_, close_, "Calc Bar", col)
  122.  
  123. // Constants colours that include fully non-transparent option.
  124. green100 = #008000FF
  125. lime100 = #00FF00FF
  126. red100 = #FF0000FF
  127. blue100 = #0000FFFF
  128. aqua100 = #00FFFFFF
  129. darkred100 = #8B0000FF
  130. gray100 = #808080FF
  131.  
  132. // Make sure we have minimum channel spread.
  133. LengthSlow_ = (LengthSlow__-LengthFast_)<1?LengthFast_+1:LengthSlow__
  134.  
  135. // If this is 5min or less Time Frame select EMAs
  136. mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor or anchor>1440 ? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
  137. mult := isintraday or anchor==0 or interval<=0 or interval>=anchor or anchor>52 ? mult : round(anchor/interval)>1? round(anchor/interval) : 1
  138. //
  139. LengthFast = mult==1 ? LengthFast_ : (LengthFast_*mult)-1
  140. LengthSlow = mult==1 ? LengthSlow_ : (LengthSlow_*mult)-1
  141.  
  142.  
  143. // T3 Moving Average Calculation Function.
  144. T3MA(src, Length, VolFactor) =>
  145. xe1 = ema(src, Length)
  146. xe2 = ema(xe1, Length)
  147. xe3 = ema(xe2, Length)
  148. xe4 = ema(xe3, Length)
  149. xe5 = ema(xe4, Length)
  150. xe6 = ema(xe5, Length)
  151. b = VolFactor
  152. c1 = -b*b*b
  153. c2 = 3*b*b+3*b*b*b
  154. c3 = -6*b*b-3*b-3*b*b*b
  155. c4 = 1+3*b+b*b*b+3*b*b
  156. c1 * xe6 + c2 * xe5 + c3 * xe4 + c4 * xe3
  157.  
  158. // Get the two T3MAs
  159. t3maFast = T3MA(close_, LengthFast, VolFactorFast)
  160. t3maSlow = T3MA(close_, LengthSlow, VolFactorSlow)
  161.  
  162. //Plot the Ribbon
  163. ma1=plot( showRibbon?t3maFast:na,color=rising(t3maFast,2)?green:red,linewidth=1,join=true,transp=20,title="Fast t3ma")
  164. ma2=plot( showRibbon?t3maSlow:na,color=rising(t3maSlow,2)?green:red,linewidth=1,join=true,transp=20,title="Slow t3ma")
  165. fcolor = t3maFast>t3maSlow?green:red
  166. fill(ma1,ma2,color=fcolor,transp=80,title="Ribbon Fill")
  167.  
  168. // Colour bars according to the close position relative to the MA selected
  169. // Or Grab candle colour code bars according to the close position relative to the MA selected
  170. grabcol = close_>=open_? close_>t3maFast and close_>t3maSlow? lime100 : close_<t3maFast and close_<t3maSlow? red100 : aqua100 :
  171. close_>t3maFast and close_>t3maSlow? green100 : close_<t3maFast and close_<t3maSlow? darkred100 : blue100
  172. stdcol = close_>t3maFast and close_>t3maSlow ? lime100 : close_<t3maFast and close_<t3maSlow? red100 : gray100
  173.  
  174. barcolor(sBars?uGrabClr? grabcol: stdcol:na, title = "Bar Colours")
  175.  
  176.  
  177. // Generate Alert Arrows
  178. //
  179. buy = 0
  180. sell=0
  181. buyT = 0
  182. sellT =0
  183. // Generate signal by Grab Candle Colour
  184. buy := grabcol==lime100? (nz(buy[1])+1) : grabcol==green100? (nz(buy[1])>0? nz(buy[1])+1: 0) : 0
  185. sell := grabcol==darkred100? (nz(sell[1])+1) : grabcol==red100? (nz(sell[1])>0? nz(sell[1])+1: 0) : 0
  186.  
  187. // Trend Filter
  188. buyT := buy==0? 0 : (dFilter and t3maFast<t3maSlow) ? 0 : nz(buyT[1])+1
  189. sellT := sell==0? 0 : (dFilter and t3maFast>t3maSlow) ? 0 : nz(sellT[1])+1
  190.  
  191. // Exit conditions
  192. exitbuy = nz(buyT[1])>0 and buyT==0
  193. exitsell = nz(sellT[1])>0 and sellT==0
  194.  
  195. //
  196. // Strategy: (Thanks to JayRogers)
  197. // === STRATEGY RELATED INPUTS ===
  198. //tradeInvert = input(defval = false, title = "Invert Trade Direction?")
  199. // the risk management inputs
  200. ebar = input(defval = 10000, title="Number of Bars for Back Testing", minval=0)
  201. dummy = input(false, title="- SET to ZERO for Daily or Longer Timeframes" )
  202. inpTakeProfit = input(defval = 0, title = "Take Profit in Pips", minval = 0)*10
  203. inpStopLoss = input(defval = 0, title = "Stop Loss in Pips", minval = 0)*10
  204. inpTrailStop = 0//input(defval = 0, title = "Trailing Stop Loss Pips", minval = 0)*10
  205. inpTrailOffset = 0//input(defval = 0, title = "Trailing Stop Loss Offset Pips", minval = 0)*10
  206.  
  207. // Alternative Method to Restrict bars is to use Days since:
  208. //last = security(ticker,period, valuewhen(barstate.islast, n,0), barmerge.gaps_off, barmerge.lookahead_on)
  209. //tdays = last // number of minutes since last bar
  210. //tdays := abs(tdays-n) // number of bars since last bar, for periods less than 1Day.
  211. // Calculate how many mars since last bar
  212. tdays = (timenow-time)/60000.0 // number of minutes since last bar
  213. tdays := ismonthly? tdays/1440.0/5.0/4.3/interval : isweekly? tdays/1440.0/5.0/interval : isdaily? tdays/1440.0/interval : tdays/interval // number of bars since last bar
  214.  
  215. // === RISK MANAGEMENT VALUE PREP ===
  216. // if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.
  217. useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na
  218. useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na
  219. useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na
  220. useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na
  221.  
  222. // === STRATEGY - CLOSE ALL IF TOO MANY CANDLES ===
  223. if (ebar>0 and tdays>ebar)
  224. strategy.close_all()// ...and when to get out
  225.  
  226. // Still Have candles to process?
  227. if (ebar==0 or tdays<=ebar)
  228. // === STRATEGY - LONG POSITION EXECUTION ===
  229. strategy.entry(id = "Buy", long = true, when = buyT==1)// use function or simple condition to decide when to get in
  230. strategy.close(id = "Buy", when = exitbuy ) // ...and when to get out
  231. // === STRATEGY - SHORT POSITION EXECUTION ===
  232. strategy.entry(id = "Sell", long = false, when = sellT==1)
  233. strategy.close(id = "Sell", when = exitsell )
  234. // === STRATEGY RISK MANAGEMENT EXECUTION ===
  235. // finally, make use of all the earlier values we prepped
  236. strategy.exit("Exit Buy", from_entry = "Buy", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
  237. strategy.exit("Exit Sell", from_entry = "Sell", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
  238.  
  239. //plotshape(n,location=location.top)
  240. //plotshape(barstate.islast? n : 0,location=location.top)
  241. //plotshape(barstate.isfirst? n : 0,location=location.top)
  242. //
  243. //eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement