Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study(title="strategy MTF candles by yatrader2 signals", shorttitle="[BT] Simple Template 1.0", overlay=true)
- ////////////////////////////////////////////////////////////////
- //* Common functions *//
- ////////////////////////////////////////////////////////////////
- //Different options of security()
- get_sec(sid, res, src, look_ahead, shift_back) =>
- indHighTF = barstate.isrealtime ? 1 : 0
- indCurrTF = barstate.isrealtime ? 0 : 1
- x1 = security(sid, res, src[indHighTF], barmerge.gaps_off, barmerge.lookahead_off)
- x2 = security(sid, res, src[indHighTF], barmerge.gaps_off, barmerge.lookahead_on)
- x = look_ahead ? x2 : x1
- info = if shift_back
- x[indCurrTF]
- else
- x
- // Check trending of a bar, i.e. Close vs Open
- get_trend(x, y, rerr, gerr) =>
- t = if x > y and abs(x - y) / y > gerr
- 1
- else
- if x < y and abs(x - y) / y > rerr
- -1
- else
- 0
- get_kama(src, length) =>
- xvnoise = abs(src - src[1])
- nfastend = 0.618
- nslowend = 0.0618
- nsignal = abs(src - src[length])
- nnoise = sum(xvnoise, length)
- nefratio = iff(nnoise != 0, nsignal / nnoise, 0)
- nsmooth = pow(nefratio * (nfastend - nslowend) + nslowend, 2)
- KAMA = na
- KAMA := nz(KAMA[1]) + nsmooth * (src - nz(KAMA[1]))
- ////////////////////////////////////////////////////////////////
- //* Source and Alternative Candlestick *//
- ////////////////////////////////////////////////////////////////
- altSrc = input("ohlc4", 'SOURCE', options=["close", "high", "low", "open", "tr", "vwap", "ohlc4", "hl2", "hlc3"])
- altSLength = input(1, 'SMOOTHING LENGTH (1 to turnoff)', type= integer, minval=1)
- altSMethod = input("ema", '- Smoothing method', options=["swma", "sma", "ema", "rma", "wma", "vwma", "alma", "kama"])
- altRes = input("1", 'ALTERNATIVE TIMEFRAME (1 to turnoff)', type=resolution)
- symRes = altRes == "1" ? period : altRes
- isHA = input(false, '- Heikin-ashi', type=bool)
- symID = isHA ? heikenashi(tickerid) : tickerid
- lookAh = input(false, '- Lookahead (disable to resolve repaint)', type=bool)
- shiftB = input(true, '- Shiftback (enable to resolve repaint)', type=bool)
- // For any calculation relating to the actual price
- tv_o = open
- tv_h = high
- tv_l = low
- tv_c = close
- tv_hl2 = hl2
- tv_hlc3 = hlc3
- tv_ohlc4 = ohlc4
- tv_tr = tr
- tv_vwap = vwap
- // Get specific source for the NZ center
- alt_src = iff(altSrc == "high", tv_h, iff(altSrc == "low", tv_l,
- iff(altSrc == "open", tv_o, iff(altSrc == "tr", tv_tr,
- iff(altSrc == "vwap", tv_vwap, iff(altSrc == "ohlc4", tv_ohlc4,
- iff(altSrc == "hlc3", tv_hlc3, iff(altSrc == "hl2", tv_hl2, iff(altSrc == "close", tv_c, tv_ohlc4)))))))))
- // Smoothing and transform the center with the specific timeframe and candlestick
- alt_smoothSrc = altSLength > 1 ? iff(altSMethod == "sma", sma(alt_src, altSLength), iff(altSMethod == "ema", ema(alt_src, altSLength), iff(altSMethod == "rma", rma(alt_src, altSLength),
- iff(altSMethod == "wma", wma(alt_src, altSLength), iff(altSMethod == "vwma", vwma(alt_src, altSLength), iff(altSMethod == "swma", swma(alt_src),
- iff(altSMethod == "alma", alma(alt_src, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(alt_src, altSLength), sma(alt_src, altSLength))))))))) : alt_src
- alt_s = altRes == "1" ? alt_smoothSrc : get_sec(symID, symRes, alt_smoothSrc, lookAh, shiftB)
- // Smoothing and transform the high with the specific timeframe and candlestick
- alt_smoothHigh = altSLength > 1 ? iff(altSMethod == "sma", sma(tv_h, altSLength), iff(altSMethod == "ema", ema(tv_h, altSLength), iff(altSMethod == "rma", rma(tv_h, altSLength),
- iff(altSMethod == "wma", wma(tv_h, altSLength), iff(altSMethod == "vwma", vwma(tv_h, altSLength), iff(altSMethod == "swma", swma(tv_h),
- iff(altSMethod == "alma", alma(tv_h, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(tv_h, altSLength), sma(tv_h, altSLength))))))))) : tv_h
- alt_h = altRes == "1" ? alt_smoothHigh : get_sec(symID, symRes, alt_smoothHigh, lookAh, shiftB)
- // Smoothing and transform the low with the specific timeframe and candlestick
- alt_smoothLow = altSLength > 1 ? iff(altSMethod == "sma", sma(tv_l, altSLength), iff(altSMethod == "ema", ema(tv_l, altSLength), iff(altSMethod == "rma", rma(tv_l, altSLength),
- iff(altSMethod == "wma", wma(tv_l, altSLength), iff(altSMethod == "vwma", vwma(tv_l, altSLength), iff(altSMethod == "swma", swma(tv_l),
- iff(altSMethod == "alma", alma(tv_l, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(tv_l, altSLength), sma(tv_l, altSLength))))))))) : tv_l
- alt_l = altRes == "1" ? alt_smoothLow : get_sec(symID, symRes, alt_smoothLow, lookAh, shiftB)
- // Get specific source for the center
- alt_srcCenter = iff(altSrc == "high", tv_h, iff(altSrc == "low", tv_l,
- iff(altSrc == "open", tv_o, iff(altSrc == "tr", tv_hlc3 + tv_tr,
- iff(altSrc == "vwap", tv_vwap, iff(altSrc == "ohlc4", tv_ohlc4,
- iff(altSrc == "hlc3", tv_hlc3, iff(altSrc == "hl2", tv_hl2, iff(altSrc == "close", tv_c, tv_ohlc4)))))))))
- // Smoothing and transform the center with the specific timeframe and candlestick
- alt_smoothCenter = altSLength > 1 ? iff(altSMethod == "sma", sma(alt_srcCenter, altSLength), iff(altSMethod == "ema", ema(alt_srcCenter, altSLength), iff(altSMethod == "rma", rma(alt_srcCenter, altSLength),
- iff(altSMethod == "wma", wma(alt_srcCenter, altSLength), iff(altSMethod == "vwma", vwma(alt_srcCenter, altSLength), iff(altSMethod == "swma", swma(alt_srcCenter),
- iff(altSMethod == "alma", alma(alt_srcCenter, altSLength, 0, 6), iff(altSMethod == "kama", get_kama(alt_srcCenter, altSLength), sma(alt_srcCenter, altSLength))))))))) : alt_src
- alt_center = altRes == "1" ? alt_smoothCenter : get_sec(symID, symRes, alt_smoothCenter, lookAh, shiftB)
- ////////////////////////////////////////////////////////////////
- //* Common Variables *//
- ////////////////////////////////////////////////////////////////
- // Transform setting for plotting with overlay=false
- plotCenter = 0
- plot_rangeWidth = 300
- plot_signalWidth = 200
- intv=security(tickerid, altRes , isintraday?interval:interval*1440)
- o = security(tickerid, altRes , open, barmerge.gaps_off, barmerge.lookahead_on)
- c = security(tickerid, altRes , close, barmerge.gaps_off, barmerge.lookahead_on)
- h = security(tickerid, altRes , high, barmerge.gaps_off, barmerge.lookahead_on)
- l = security(tickerid, altRes , low, barmerge.gaps_off, barmerge.lookahead_on)
- min_of_day = (hour*60)+minute
- midpoint=floor(intv/2)
- wickwidth=input(1.0, title="Wick width in bars")*interval/2
- step= input(true, title="Gray Borders with square edges of candles")
- topsbots= input(true, title="Color top/bottom edge of candles")
- pointyline= input(false, title="Colored Borders with non-square edges of candles")
- wicktime= ((min_of_day % intv > midpoint-wickwidth) and (min_of_day % intv <= midpoint+wickwidth)) ?true:false
- col = c >= o ? lime : red
- // why so many options... see below
- plot(not pointyline?na:wicktime?o>c?h:l:o, color=col, title="Open", style=line, transp=40, title="pointyline open")
- plot(not pointyline?na:wicktime?o<=c?h:l:c, color=col, title="Close", style=line, transp=40, title="pointyline close")
- plot(not step?na:wicktime?o>c?h:l:o, color=gray, title="Open", style=stepline, transp=20, title="stepline open")
- plot(not step?na:wicktime?o<=c?h:l:c, color=gray, title="Close", style=stepline, transp=20, title="stepline close")
- plot(not topsbots?na:wicktime?o>c?h:l:o, color=col, title="Open", style=circles, transp=80)
- plot(not topsbots?na:wicktime?o<=c?h:l:c, color=col, title="Close", style=circles, transp=80)
- po = plot(wicktime?o>c?h:l:o, color=col, title="Open", style=stepline, transp=100, title="hidden open", editable=false)
- pc = plot(wicktime?o<=c?h:l:c, color=col, title="Close", style=stepline, transp=100, title="hidden close", editable=false)
- // wick tips
- plot(wicktime and h>c?h:na, color=col, title="High", style=circles, transp=60)
- plot(wicktime and l<c?l:na, color=col, title="Low", style=circles, transp=60)
- fill(po, pc, col)
- // INPUTS
- useRenko = input(false, title="This a RENKO Chart?")
- // WARNING: Using Heikin-Ashi may result in inaccurate backtest results
- useHeikin = input(false, title="Use Heikin-Ashi Candles? (BT WILL BE UNRELIABLE)")
- // Use Alternate Anchor TF for MAs
- anchor = input(0,minval=0,maxval=1440,title="Alternate TimeFrame Multiplier (0=none)")
- // If have anchor specified, calculate the base multiplier.
- //mult = isintraday ? anchor==0 or interval<=0 or interval>=anchor or anchor>1440? 1 : round(anchor/interval) : 1
- //mult := isdwm? 1 : mult // Only available Daily or less
- mult = anchor>0 ? anchor : 1
- open_ = useHeikin ? security(heikinashi(tickerid), period, open) : security(tickerid, period, open)
- close_ = useHeikin ? security(heikinashi(tickerid), period, close) : security(tickerid, period, close)
- high_ = useRenko? max(close_,open_) : useHeikin ? security(heikinashi(tickerid), period, high) : security(tickerid, period, high)
- low_ = useRenko? min(close_,open_) : useHeikin ? security(heikinashi(tickerid), period, low) : security(tickerid, period, low)
- uPrice = input(title="Price", defval="close", options=["close", "high", "low"])
- src= uPrice=="close" ? close_ : uPrice=="high" ? high_ : uPrice=="low" ? low_ : na
- // Conditions
- longCond = na
- shortCond = na
- longCond := c >= o and c[1] <= o[1]
- shortCond := c <= o and c[1] >= o[1]
- // Count your long short conditions for more control with Pyramiding
- sectionLongs = 0
- sectionLongs := nz(sectionLongs[1])
- sectionShorts = 0
- sectionShorts := nz(sectionShorts[1])
- if longCond
- sectionLongs := sectionLongs + 1
- sectionShorts := 0
- if shortCond
- sectionLongs := 0
- sectionShorts := sectionShorts + 1
- // Pyramiding
- pyrl = 1
- // These check to see your signal and cross references it against the pyramiding settings above
- longCondition = longCond and sectionLongs <= pyrl
- shortCondition = shortCond and sectionShorts <= pyrl
- // Get the price of the last opened long or short
- last_open_longCondition = na
- last_open_shortCondition = na
- last_open_longCondition := longCondition ? open : nz(last_open_longCondition[1])
- last_open_shortCondition := shortCondition ? open : nz(last_open_shortCondition[1])
- // Check if your last postion was a long or a short
- last_longCondition = na
- last_shortCondition = na
- last_longCondition := longCondition ? time : nz(last_longCondition[1])
- last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
- in_longCondition = last_longCondition > last_shortCondition
- in_shortCondition = last_shortCondition > last_longCondition
- // Take profit
- isTPl = input(false, "Take Profit Long")
- isTPs = input(false, "Take Profit Short")
- tp = input(5, "Take Profit %", type=float)
- long_tp = isTPl and crossover(high, (1+(tp/100))*last_open_longCondition) and longCondition == 0 and in_longCondition == 1
- short_tp = isTPs and crossunder(low, (1-(tp/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1
- // Stop Loss
- isSLl = input(false, "Stop Loss Long")
- isSLs = input(false, "Stop Loss Short")
- sl= 0.0
- sl := input(3, "Stop Loss %", type=float)
- long_sl = isSLl and crossunder(low, (1-(sl/100))*last_open_longCondition) and longCondition == 0 and in_longCondition == 1
- short_sl = isSLs and crossover(high, (1+(sl/100))*last_open_shortCondition) and shortCondition == 0 and in_shortCondition == 1
- // Create a single close for all the different closing conditions.
- long_close = long_tp or long_sl ? 1 : 0
- short_close = short_tp or short_sl ? 1 : 0
- // Get the time of the last close
- last_long_close = na
- last_short_close = na
- last_long_close := long_close ? time : nz(last_long_close[1])
- last_short_close := short_close ? time : nz(last_short_close[1])
- // Alerts & Signals
- bton(b) => b ? 1 : 0
- plotshape(longCondition, title = "Buy Signal", text = "B", style=shape.triangleup, location=location.belowbar, color = green, editable = false, transp = 0)
- plotshape(shortCondition, title = "Sell Signal", text = "S", style=shape.triangledown, location=location.abovebar, color = red, editable = false, transp = 0)
- plotshape(long_tp and last_longCondition > nz(last_long_close[1]), text ="TP", title="Take Profit Long", style=shape.triangledown,
- location=location.abovebar, color = red, editable = false, transp = 0)
- plotshape(short_tp and last_shortCondition > nz(last_short_close[1]) , text ="TP", title="Take Profit Short", style=shape.triangleup,
- location=location.belowbar, color = lime, editable = false, transp = 0)
- ltp = iff(long_tp and last_longCondition > nz(last_long_close[1]), (1+(tp/100))*last_open_longCondition, na)
- plot(ltp, style=cross, linewidth=3, color = white, editable = false)
- stp = iff(short_tp and last_shortCondition > nz(last_short_close[1]), (1-(tp/100))*last_open_shortCondition, na)
- plot(stp, style = cross, linewidth=3, color = white, editable = false)
- plotshape(long_sl and last_longCondition > nz(last_long_close[1]), text ="SL", title="Stop Loss Long", style=shape.triangledown,
- location=location.abovebar, color = red, editable = false, transp = 0)
- plotshape(short_sl and last_shortCondition > nz(last_short_close[1]), text ="SL", title="Stop Loss Short", style=shape.triangleup,
- location=location.belowbar, color = lime, editable = false, transp = 0)
- lsl = iff(long_sl and last_longCondition > nz(last_long_close[1]), (1-(sl/100))*last_open_longCondition, na)
- plot(lsl, style=cross, linewidth=3, color = white, editable = false)
- ssl = iff(short_sl and last_shortCondition > nz(last_short_close[1]), (1+(sl/100))*last_open_shortCondition, na)
- plot(ssl, style = cross, linewidth=3, color = white, editable = false)
- alertcondition(bton(longCondition), title="Buy Alert")
- alertcondition(bton(shortCondition), title="Sell Alert")
- alertcondition(bton(long_tp and last_longCondition > nz(last_long_close[1])), title="Take Profit Long")
- alertcondition(bton(short_tp and last_shortCondition > nz(last_short_close[1])), title="Take Profit Short")
- alertcondition(bton(long_sl and last_longCondition > nz(last_long_close[1])), title="Stop Loss Long")
- alertcondition(bton(short_sl and last_shortCondition > nz(last_short_close[1])), title="Stop Loss Short")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement