Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- // Author: JustUncleL
- //
- // Description:
- // This script idea is designed to be used with 10pip brick (recommened) Renko charts. It combines the Renko
- // price action with a directional coloured EMA (default length 6) and a RMI (instead of the usual RSI) indicator
- // to provide entry and exit signals. RMI is bit like RSI with a built-in momentum factor and works well with Renko.
- // Signals can optionally be filtered by Daily or Weekly Open, where by only trade long above open and short below
- // open (this option is enabled by default).
- // Exit occur when EMA or RMI reverses direction, or optionally (disabled by default) when the Renko prints
- // a brick in the reverse direction.
- // Each Entry and Exit signal creates an Alertcondition that can be picked up by the TradingView Alarm system.
- //
- // TIP: To get 10pip Bricks set Renko to "Traditional" type bricks and 0.001 for non-JPY currency pairs, and
- // 0.1 for JPY currency pairs. Also set chart Time frame to 5min or 15mins.
- //
- // References:
- // - TheLark Relative Momentum Index (RMI)
- strategy("Renko Magic", overlay=true, precision=8, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type=strategy.commission.percent, commission_value=0.2)
- // === INPUTS
- // Medium Fast MA - type, source, length
- ma_type = input(defval="EMA", title="MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])
- ma_len = input(defval=6, title="MA - Length", minval=1)
- ma_src = input(close, title="MA - Source")
- exitfirst = input(false,title="Exit Trade on 1st Opposite Renko Brick")
- // inputs for RMI function
- rmilen = input(4, title="RMI/RSI MA Length")
- mom = input(5, title="Momentum Length",minval=0)
- ob = input(70,title="Overbought")
- os = input(30,title="Oversold")
- uRSI = input(false,title="Use RSI instead of RMI")
- udopen = input(true,title="Use Daily Open Line Filter")
- dperiod = input("1D", title="Period for Daily Open Line", options=["1D","1W","1M"])
- //
- // === /INPUTS
- // Constants colours that include fully non-transparent option.
- green100 = #008000FF
- lime100 = #00FF00FF
- red100 = #FF0000FF
- blue100 = #0000FFFF
- aqua100 = #00FFFFFF
- darkred100 = #8B0000FF
- gray100 = #808080FF
- // === FUNCTIONS
- // - variant(type, src, len)
- // Returns MA input selection variant, default to SMA if blank or typo.
- // SuperSmoother filter
- // © 2013 John F. Ehlers
- variant_supersmoother(src,len) =>
- a1 = exp(-1.414*3.14159 / len)
- b1 = 2*a1*cos(1.414*3.14159 / len)
- c2 = b1
- c3 = (-a1)*a1
- c1 = 1 - c2 - c3
- v9 = 0.0
- v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
- v9
- variant_smoothed(src,len) =>
- v5 = 0.0
- v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len
- v5
- variant_zerolagema(src,len) =>
- ema1 = ema(src, len)
- ema2 = ema(ema1, len)
- v10 = ema1+(ema1-ema2)
- v10
- variant_doubleema(src,len) =>
- v2 = ema(src, len)
- v6 = 2 * v2 - ema(v2, len)
- v6
- variant_tripleema(src,len) =>
- v2 = ema(src, len)
- v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
- v7
- // return variant, defaults to SMA
- variant(type, src, len) =>
- type=="EMA" ? ema(src,len) :
- type=="WMA" ? wma(src,len):
- type=="VWMA" ? vwma(src,len) :
- type=="SMMA" ? variant_smoothed(src,len) :
- type=="DEMA" ? variant_doubleema(src,len):
- type=="TEMA" ? variant_tripleema(src,len):
- type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :
- type=="SSMA" ? variant_supersmoother(src,len) :
- type=="ZEMA" ? variant_zerolagema(src,len) :
- type=="TMA" ? sma(sma(src,len),len) : sma(src,len)
- // - /variant
- // === /FUNCTIONS
- dodgerblue = #1E90FF
- // === Moving Average
- ma_series = variant(ma_type,ma_src,ma_len)
- // Get direction based on MA
- direction = 0
- direction := rising(ma_series,3) ? 1 : falling(ma_series,3) ? -1 : nz(direction[1])
- // Plot MA series and color it according too direction
- pcol = direction>0 ? lime : direction<0 ? red : na
- plot(ma_series, title="MA Plot", color=pcol, linewidth=2,style=line,join=true, transp=10)
- // === /Moving Average
- // === DAILY OPEN LINE
- //
- // Test for new Daily Session or start of new month for Daily.
- start = security(ticker, dperiod, time, barmerge.gaps_off, barmerge.lookahead_on)
- newDay = change(start)
- // Calculate Annualised Volatility
- dopen = 0.0
- dopen_ = valuewhen(start[1]<=time, ma_src, 0)
- dopen := newDay ? dopen_ : nz(dopen[1],dopen_)
- // Plot all Days open
- plot(dopen, color = newDay?na:purple, title = "Daily Open",linewidth=2, transp=40)
- // === /DAILY OPEN LINE
- // === RMI (Relative Momentum Index)
- //
- // "... The Relative Momentum Index was developed by Roger Altman
- // and was introduced in his article in the February, 1993 issue of
- // Technical Analysis of Stocks & Commodities magazine. "
- // "... While RSI counts up and down days from close to close, the Relative
- // Momentum Index counts up and down days from the close relative to a
- // close x number of days ago. "
- //calc
- up = ema(max(ma_src- ma_src[mom],0),rmilen)
- dn = ema(max(ma_src[mom] - ma_src,0),rmilen)
- //rmi =
- rmi = dn == 0 ? 0 : 100 - 100 / (1 + up / dn)
- // Use RSI instead of RMI
- rmi := uRSI ? rsi(ma_src, rmilen) : rmi
- // === /RMI
- // === Calculate Alerts
- PA = (close>open) and (not udopen or close>dopen)? 1 : (close<open) and (not udopen or close<dopen)? -1 : 0
- HAS = direction
- RMI = rmi>=rmi[1] and rmi>=ob ? 1 : rmi<=rmi[1] and rmi<=os ? -1 : 0
- long = PA==1 and HAS==1 and RMI==1
- short = PA==-1 and HAS==-1 and RMI==-1
- olong = 0
- olong := olong[1]>0 and (HAS<0 or (not uRSI and RMI<=0) or (exitfirst and (close<open and close[1]>open[1])))? 0 : nz(olong[1])==0 and long? 1 : olong[1]>0 and HAS==1? olong[1]+1 : 0
- oshort = 0
- oshort := oshort[1]>0 and (HAS>1 or (not uRSI and RMI>=0) or (exitfirst and (close>open and close[1]<open[1])))? 0 : nz(oshort[1])==0 and short? 1 : oshort[1]>0 and HAS==-1? oshort[1]+1 : 0
- // debug
- //plotshape(PA,location=location.bottom)
- //plotshape(HAS,location=location.bottom)
- //plotshape(RMI,location=location.bottom)
- //plotshape(olong,location=location.bottom)
- //plotshape(oshort,location=location.bottom)
- // === Upgraded Conditions Framework ===
- ////////////////////////////////////////////////////////////////////////////
- long_entry = olong==1 //Long Or Buy Condition Here
- short_entry = oshort==1 //Short Or Sell Condition Here
- long_exit = olong[1]>0 and olong==0 //Close Long Condition Here (Optional)
- short_exit = oshort[1]>0 and oshort==0 //Close Short Condition Here (Optional)
- ///////////////////////////////////////////////////////////////////////////
- // init these values here, they will get updated later as more decisions are made
- last_long_close = na
- last_short_close = na
- // === Long position detection ===
- // longs open
- longo = 0
- longo := nz(longo[1])
- // longs closed
- longc = 0
- longc := nz(longc[1])
- if long_entry
- longo := longo + 1
- longc := 0
- if long_exit
- longc := longc + 1
- longo := 0
- // === /END
- // === Short position detection ===
- shorto = 0
- shorto := nz(shorto[1])
- shortc = 0
- shortc := nz(shortc[1])
- if short_entry
- shorto := shorto + 1
- shortc := 0
- if short_exit
- shortc := shortc + 1
- shorto := 0
- // === /END
- // === Pyramiding Settings ===
- pyr = input(1, title="Pyramiding Setting")
- //pyr = 1
- longCondition = long_entry and longo <= pyr
- longX = long_exit and longc <= pyr
- shortCondition = short_entry and shorto <=pyr
- shortX = short_exit and shortc <=pyr
- // === /END
- // === Get Last Position Price ===
- last_open_longCondition = na
- last_open_shortCondition = na
- // last open prices
- last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
- last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
- // === /END
- // === Check For Long/Short ===
- last_longCondition = na
- last_shortCondition = na
- // last open times
- last_longCondition := longCondition ? time : nz(last_longCondition[1])
- last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
- last_longClose = longX ? time : nz(last_long_close[1])
- last_shortClose = shortX ? time : nz(last_short_close[1])
- in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
- in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
- // === /END
- // === Stop Loss (Long) ===
- isSLl = input(false, "Stop Loss (Long)")
- sll = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- long_call_sl = last_open_longCondition * (1 - sll)
- long_sl = isSLl and low <= long_call_sl and longCondition == 0
- // === /END
- // === Stop Loss (Short) ===
- isSLs = input(false, "Stop Loss (Short)")
- sls = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- short_call_sl = last_open_shortCondition * (1 + sls)
- short_sl = isSLs and high >= short_call_sl and shortCondition == 0
- // === /END
- // === Trailing Stop ===
- last_high = na
- last_low = na
- last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
- last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
- isTSl = input(false, "Trailing Stop Long")
- tsil = input(25, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- tsl = input(8, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- long_call_ts = last_high * (1 - tsl)
- long_call_tsi = last_open_longCondition * (1 + tsil)
- long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
- isTSs = input(false, "Trailing Stop Short")
- tsis = input(25, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- tss = input(8, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- short_call_ts = last_low * (1 + tss)
- short_call_tsi = last_open_shortCondition * (1 - tsis)
- short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
- // === /END
- // === Create Single Close For All Closing Conditions ===
- closelong = long_sl or long_ts or longX
- closeshort = short_sl or short_ts or shortX
- // Get Last Close
- last_long_close := closelong ? time : nz(last_long_close[1])
- last_short_close := closeshort ? time : nz(last_short_close[1])
- // Check For Close Since Last Open
- if closelong and last_long_close[1] > last_longCondition
- closelong := 0
- if closeshort and last_short_close[1] > last_shortCondition
- closeshort := 0
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Alarm Settings ===
- //alertcondition(longCondition==1, title='LONG', message='LONG')
- //alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
- //alertcondition(shortCondition==1, title='SHORT', message='SHORT')
- //alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Visuals & Debugs Here ===
- //Remove "//" To Check/Debug The Code Above
- // Signal Shapes
- //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
- //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
- //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
- //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
- // SL Plot
- //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
- //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
- //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
- // TP Plot
- //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
- //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
- //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
- // TS Plot
- //tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
- //tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
- //plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
- //plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
- //plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
- //plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
- // //
- ////////////////////////////////////////////////////////////////////////////
- // === Strategy Direction Switch ===
- dir = input(title = "Strategy Direction", defval="Both", options=["Both", "Long", "Short"])
- // === /END
- // === Backtesting Dates ===
- testPeriodSwitch = input(true, "Custom Backtesting Dates")
- testStartYear = input(2017, "Backtest Start Year")
- testStartMonth = input(1, "Backtest Start Month")
- testStartDay = input(1, "Backtest Start Day")
- testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
- testStopYear = input(9999, "Backtest Stop Year")
- testStopMonth = input(1, "Backtest Stop Month")
- testStopDay = input(1, "Backtest Stop Day")
- testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
- testPeriod() =>
- time >= testPeriodStart and time <= testPeriodStop ? true : false
- isPeriod = testPeriodSwitch == true ? testPeriod() : true
- // === /END
- // === Strategy ===
- if isPeriod and dir=="Both"
- if (longCondition)
- strategy.entry("Long",strategy.long)
- if (closelong) and not shortCondition
- strategy.close("Long")
- if (shortCondition)
- strategy.entry("Short",strategy.short)
- if (closeshort) and not longCondition
- strategy.close("Short")
- if isPeriod and dir=="Long"
- if (longCondition)
- strategy.entry("Long",strategy.long)
- if (closelong)
- strategy.close("Long")
- if isPeriod and dir=="Short"
- if (shortCondition)
- strategy.entry("Short",strategy.short)
- if (closeshort)
- strategy.close("Short")
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // ULTIMATE PINE INJECTOR V1.2 //
- // //
- //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement