Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- strategy(title="[Strategy]T3MA Ribbon R4.2 by JustUncleL", shorttitle="[STRATEGY]T3RIBBON", overlay = true)
- //
- // Revision: R4
- // Revision Author: JustUncleL
- //
- // Description:
- // Strategy Backtester Version
- // This study draws a T3 Moving average Coloured Ribbon based on a Fast and Slow T3 MAs.
- //
- // References:
- // - https://www.forexstrategiesresources.com/binary-options-trading-strategies/84-winner-binary-system/
- // =======
- // - T3 Average by HPotter v1.0 21/05/2014
- // This indicator plots the moving average described in the January, 1998 issue
- // of S&C, p.57, "Smoothing Techniques for More Accurate Signals", by Tim Tillson.
- // This indicator plots T3 moving average presented in Figure 4 in the article.
- // T3 indicator is a moving average which is calculated according to formula:
- // T3(n) = GD(GD(GD(n))),
- // where GD - generalized DEMA (Double EMA) and calculating according to this:
- // GD(n,v) = EMA(n) * (1+v)-EMA(EMA(n)) * v,
- // where "v" is volume factor, which determines how hot the moving average’s response
- // to linear trends will be. The author advises to use v=0.7.
- // When v = 0, GD = EMA, and when v = 1, GD = DEMA. In between, GD is a less aggressive
- // version of DEMA. By using a value for v less than1, trader cure the multiple DEMA
- // overshoot problem but at the cost of accepting some additional phase delay.
- // In filter theory terminology, T3 is a six-pole nonlinear Kalman filter. Kalman
- // filters are ones that use the error — in this case, (time series - EMA(n)) —
- // to correct themselves. In the realm of technical analysis, these are called adaptive
- // moving averages; they track the time series more aggres-sively when it is making large
- // moves. Tim Tillson is a software project manager at Hewlett-Packard, with degrees in
- // mathematics and computer science. He has privately traded options and equities for 15 years.
- // =======
- // Options:
- //
- // 1) Option to display coloured Candles around the Ribbon, the coulouring uses
- // the Standard candle colours:
- // - Lime = candle closed above Ribbon.
- // - Red = candle closed below Ribbon.
- // - Gray = Candle Closed inside Ribbon.
- // the Grab candles scheme:
- // - Lime = Bull candle closed above Ribbon.
- // - Green = Bear candle closed above Ribbon.
- // - Red = Bull candle closed below Ribbon.
- // - DarkRed = Bear candle closed below Ribbon.
- // - Aqua = Bull candle closed inside Ribbon.
- // - Blue = Bear candle closed inside Ribbon.
- //
- // 2) Option to base the candles on a higher time frame (HFT), this performed by increasing
- // the MA length to create equivalent lengths from the HFT. So no re-painting.
- // NOTE: The script will time out if the MA lengths get too long after resizing.
- //
- // Modifications:
- // R1 - Original
- // R2 - Added optional Bar colouring
- // - Added option to Anchor chart to a higher Time Frame (1440 max)
- //
- // 10-Aug-2017 - R3:
- // - Made updates for Pinescript version 3 Compliance.
- // - Allow anchor option to be used for Days and Weeks.
- // - Allow option to use standard 3-tone bar colouring, or 6-tone Grab candle Colouring
- // - Added named colour constants, to include fully opaque colours, a work around
- // for bar colouring transparency bug.
- //
- // 7-Jan-2018 R4:
- // - Added Alert Buy/Sell exit options.
- // - Added alarms for TradingView's Alarm Subsystem.
- // - Added implied GPL Copyright notice.
- // - Some coding efficiency and some variable name changes to make it easier
- // to read.
- //
- //
- // -----------------------------------------------------------------------------
- // Copyright 2014 HPotter
- // Copyright 2017,2018 JustUncleL
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // The GNU General Public License can be found here
- // <http://www.gnu.org/licenses/>.
- //
- // -----------------------------------------------------------------------------
- //
- // Use Alternate Anchor TF for MAs
- anchor = input(0,minval=0,maxval=1440,title="Use Alternate Anchor TimeFrame (0=none, max=1440mins)")
- LengthFast_ = input(8, minval=1,title="Fast T3MA length")
- VolFactorFast= input(0.7,minval=0.0,maxval=1.0,title="Fast T3 Volume Factor")
- LengthSlow__ = input(13, minval=2,title="Slow T3MA length")
- VolFactorSlow= input(0.6,minval=0.0,maxval=1.0,title="Slow T3 Volume Factor")
- //ma_src = input(close,title="T3MA Source")
- showRibbon = input(false,title="Display T3MA Ribbon")
- sBars = input(false,title="Show Coloured Trend Bars")
- uGrabClr = input(false,title="Use Grab Bar 6-tone Colours, instead of Standard 3-tone")
- //
- dFilter = input(false,title="Filter Signals to T3MA Ribbon Colour")
- // LRS Filter
- //sFilter = input(false,title="Apply ADX Filter to Signals")
- //clen = input(defval = 15, minval = 1, title = "Linear Regression Length")
- //slimit = input(defval=0.0025,minval=0.0,maxval=100.0,type=float,title="Slope Filter Limit (0=none)")
- isHA = input(true, "Use HA Candles for Calculations")
- data = isHA ? heikenashi(tickerid) : tickerid
- open_ = security(data, period, open)
- high_ = security(data, period, high)
- low_ = security(data, period, low)
- close_ = security(data, period, close)
- col = close_ > open_ ? green : maroon
- plotbar(open_, high_, low_, close_, "Calc Bar", col)
- // Constants colours that include fully non-transparent option.
- green100 = #008000FF
- lime100 = #00FF00FF
- red100 = #FF0000FF
- blue100 = #0000FFFF
- aqua100 = #00FFFFFF
- darkred100 = #8B0000FF
- gray100 = #808080FF
- // Make sure we have minimum channel spread.
- LengthSlow_ = (LengthSlow__-LengthFast_)<1?LengthFast_+1:LengthSlow__
- // If this is 5min or less Time Frame select EMAs
- mult = not isintraday or anchor==0 or interval<=0 or interval>=anchor or anchor>1440 ? 1 : round(anchor/interval)>1? round(anchor/interval) : 1
- mult := isintraday or anchor==0 or interval<=0 or interval>=anchor or anchor>52 ? mult : round(anchor/interval)>1? round(anchor/interval) : 1
- //
- LengthFast = mult==1 ? LengthFast_ : (LengthFast_*mult)-1
- LengthSlow = mult==1 ? LengthSlow_ : (LengthSlow_*mult)-1
- // T3 Moving Average Calculation Function.
- T3MA(src, Length, VolFactor) =>
- xe1 = ema(src, Length)
- xe2 = ema(xe1, Length)
- xe3 = ema(xe2, Length)
- xe4 = ema(xe3, Length)
- xe5 = ema(xe4, Length)
- xe6 = ema(xe5, Length)
- b = VolFactor
- c1 = -b*b*b
- c2 = 3*b*b+3*b*b*b
- c3 = -6*b*b-3*b-3*b*b*b
- c4 = 1+3*b+b*b*b+3*b*b
- c1 * xe6 + c2 * xe5 + c3 * xe4 + c4 * xe3
- // Get the two T3MAs
- t3maFast = T3MA(close_, LengthFast, VolFactorFast)
- t3maSlow = T3MA(close_, LengthSlow, VolFactorSlow)
- //Plot the Ribbon
- ma1=plot( showRibbon?t3maFast:na,color=rising(t3maFast,2)?green:red,linewidth=1,join=true,transp=20,title="Fast t3ma")
- ma2=plot( showRibbon?t3maSlow:na,color=rising(t3maSlow,2)?green:red,linewidth=1,join=true,transp=20,title="Slow t3ma")
- fcolor = t3maFast>t3maSlow?green:red
- fill(ma1,ma2,color=fcolor,transp=80,title="Ribbon Fill")
- // Colour bars according to the close position relative to the MA selected
- // Or Grab candle colour code bars according to the close position relative to the MA selected
- grabcol = close_>=open_? close_>t3maFast and close_>t3maSlow? lime100 : close_<t3maFast and close_<t3maSlow? red100 : aqua100 :
- close_>t3maFast and close_>t3maSlow? green100 : close_<t3maFast and close_<t3maSlow? darkred100 : blue100
- stdcol = close_>t3maFast and close_>t3maSlow ? lime100 : close_<t3maFast and close_<t3maSlow? red100 : gray100
- barcolor(sBars?uGrabClr? grabcol: stdcol:na, title = "Bar Colours")
- // Generate Alert Arrows
- //
- buy = 0
- sell=0
- buyT = 0
- sellT =0
- // Generate signal by Grab Candle Colour
- buy := grabcol==lime100? (nz(buy[1])+1) : grabcol==green100? (nz(buy[1])>0? nz(buy[1])+1: 0) : 0
- sell := grabcol==darkred100? (nz(sell[1])+1) : grabcol==red100? (nz(sell[1])>0? nz(sell[1])+1: 0) : 0
- // Trend Filter
- buyT := buy==0? 0 : (dFilter and t3maFast<t3maSlow) ? 0 : nz(buyT[1])+1
- sellT := sell==0? 0 : (dFilter and t3maFast>t3maSlow) ? 0 : nz(sellT[1])+1
- // Exit conditions
- exitbuy = nz(buyT[1])>0 and buyT==0
- exitsell = nz(sellT[1])>0 and sellT==0
- //
- // Strategy: (Thanks to JayRogers)
- // === STRATEGY RELATED INPUTS ===
- //tradeInvert = input(defval = false, title = "Invert Trade Direction?")
- // the risk management inputs
- ebar = input(defval = 10000, title="Number of Bars for Back Testing", minval=0)
- dummy = input(false, title="- SET to ZERO for Daily or Longer Timeframes" )
- inpTakeProfit = input(defval = 0, title = "Take Profit in Pips", minval = 0)*10
- inpStopLoss = input(defval = 0, title = "Stop Loss in Pips", minval = 0)*10
- inpTrailStop = 0//input(defval = 0, title = "Trailing Stop Loss Pips", minval = 0)*10
- inpTrailOffset = 0//input(defval = 0, title = "Trailing Stop Loss Offset Pips", minval = 0)*10
- // Alternative Method to Restrict bars is to use Days since:
- //last = security(ticker,period, valuewhen(barstate.islast, n,0), barmerge.gaps_off, barmerge.lookahead_on)
- //tdays = last // number of minutes since last bar
- //tdays := abs(tdays-n) // number of bars since last bar, for periods less than 1Day.
- // Calculate how many mars since last bar
- tdays = (timenow-time)/60000.0 // number of minutes since last bar
- 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
- // === RISK MANAGEMENT VALUE PREP ===
- // if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.
- useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na
- useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na
- useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na
- useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na
- // === STRATEGY - CLOSE ALL IF TOO MANY CANDLES ===
- if (ebar>0 and tdays>ebar)
- strategy.close_all()// ...and when to get out
- // Still Have candles to process?
- if (ebar==0 or tdays<=ebar)
- // === STRATEGY - LONG POSITION EXECUTION ===
- strategy.entry(id = "Buy", long = true, when = buyT==1)// use function or simple condition to decide when to get in
- strategy.close(id = "Buy", when = exitbuy ) // ...and when to get out
- // === STRATEGY - SHORT POSITION EXECUTION ===
- strategy.entry(id = "Sell", long = false, when = sellT==1)
- strategy.close(id = "Sell", when = exitsell )
- // === STRATEGY RISK MANAGEMENT EXECUTION ===
- // finally, make use of all the earlier values we prepped
- strategy.exit("Exit Buy", from_entry = "Buy", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
- strategy.exit("Exit Sell", from_entry = "Sell", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
- //plotshape(n,location=location.top)
- //plotshape(barstate.islast? n : 0,location=location.top)
- //plotshape(barstate.isfirst? n : 0,location=location.top)
- //
- //eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement