Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- strategy(title="SFP 30m", shorttitle="SFP 30m", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.15)
- FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
- FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
- FromYear = input(defval = 2019, title = "From Year", minval = 2015)
- ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
- ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
- ToYear = input(defval = 9999, title = "To Year", minval = 2017)
- start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
- finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
- testPeriod() => time >= start and time <= finish ? true : false
- //HA plot
- handleHA = heikinashi(tickerid)
- openHA = security(handleHA, period, open)
- highHA = security(handleHA, period, high)
- lowHA = security(handleHA, period, low)
- closeHA = security(handleHA, period, close)
- src = openHA
- //EMA trend
- exponential = input(true, title="Exponential MA")
- MA1 = input(33, title="MA 1")
- MA2 = input(67, title="MA 2")
- ma21 = exponential ? ema(src, MA1) : sma(src, MA1)
- ma55 = exponential ? ema(src, MA2) : sma(src, MA2)
- fastemalength = input(20, title="fastema") //----------------------------------------------------------
- fastema = ema(src, fastemalength)//-------------------------------------------------
- //BANDS---------------------------------------------------------------------------------------------------------------------------------
- mult1 = input(50, minval=0.001, maxval=50, step=1, title="Trailing stop band length" )
- mult2 = input(3, minval=0.001, maxval=50, title="2nd Stdev Mult")
- mult3 = input(1, minval=0.001, maxval=50, title="3rd Stdev Mult")
- basis = fastema
- dev1 = mult1 * stdev(src, fastemalength)
- dev2 = mult2 * stdev(src, fastemalength)
- dev3 = mult3 * stdev(src, fastemalength)
- upper1 = basis + dev1
- lower1 = basis - dev1
- upper2 = basis + dev2
- lower2 = basis - dev2
- upper3 = basis + dev3
- lower3 = basis - dev3
- ////
- //RSI
- rsiLength = input(2, minval=1, title="RSILength")
- up = rma(max(change(src), 0), rsiLength)
- down = rma(-min(change(src), 0), rsiLength)
- rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
- rsin= input(13)
- sn = 100 - rsin
- ln = 0 + rsin
- /////trendmaster
- Factor=input(16, minval=1,maxval = 140)
- Pd=input(1, minval=1,maxval = 140)
- Up=(closeHA)-(Factor*atr(Pd))
- Dn=(closeHA)+(Factor*atr(Pd))
- TrendUp=na
- TrendUp:= closeHA[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
- TrendDown=na
- TrendDown:= closeHA[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
- Trend=na
- Trend := closeHA > TrendDown[1] ? 1: closeHA< TrendUp[1]? -1: nz(Trend[1],1)
- Tsl = Trend==1? TrendUp: TrendDown
- linecolor = Trend == 1 ? green : red
- plot(Tsl, color = linecolor , style = line , linewidth = 2,title = "SuperTrend")
- //Stop runs
- lookbacklength = input(20, minval=1, step=5, title="stop run lookback length")
- //Volumethreshold
- volumethreshold = input(1, minval=1, maxval=3, step=0.05, title="candle vol multiplier")
- //
- //Long wick
- redcandle = closeHA<openHA
- greencandle = closeHA>openHA
- topWickLen = redcandle ? highHA-openHA:highHA-closeHA
- botWickLen = greencandle ? openHA-lowHA:closeHA-lowHA
- bodyLen = redcandle ? openHA-closeHA:closeHA-openHA
- //HMA
- f_hma(_src, _length)=>
- //HMA = WMA(2*WMA(n/2) − WMA(n)),sqrt(n))
- _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
- f_hma2(_src, _length)=>
- //HMA = WMA(2*WMA(n/2) − WMA(n)),sqrt(n))
- _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
- hma1length = input(14)
- hma2length = input(27)
- hma1 = f_hma(close, hma1length)
- hma2 = f_hma2(close, hma2length)
- // Put your long and short rules here
- longCondition = close>Tsl
- //low<lowest(low, lookbacklength)[1] and close>lowest(close[1], lookbacklength) and not na(close[lookbacklength]) and close>Tsl
- //close>lowest(low, lookbacklength) and low<lowest(low, lookbacklength)[1] and volume>volumethreshold*max(volume[1], volume[lookbacklength]) and close>lowest(close[1], lookbacklength) and not na(close[lookbacklength])
- //Trend == 1 and closeHA>openHA and closeHA>highHA[1]
- //and Trend[1] == -1 ? Trend: na
- //botWickLen>bodyLen and closeHA>openHA and rsi > sn
- //and closeHA>openHA and closeHA>highHA[1]
- //closeHA>openHA and closeHA>highHA[1] and volema1>volema2 and ((fastema>ma21 and ma21>ma55) or (fastema<ma21 and ma21<ma55))
- shortCondition = close<Tsl
- //high>highest(high, lookbacklength)[1] and close<highest(close[1], lookbacklength) and not na(close[lookbacklength]) and close<Tsl
- //close<highest(high, lookbacklength) and high>highest(high, lookbacklength)[1] and volume>volumethreshold*max(volume[1], volume[lookbacklength]) and close<highest(close[1], lookbacklength) and not na(close[lookbacklength])
- //Trend == -1 and closeHA<openHA and closeHA<lowHA[1]
- //and Trend[1] == 1 ? Trend: na
- //topWickLen>bodyLen and closeHA<openHA and rsi < ln
- //and closeHA<openHA and closeHA<lowHA[1]
- //closeHA<openHA and closeHA<lowHA[1] and volema1<volema2 and ((fastema<ma21 and ma21<ma55) or (fastema>ma21 and ma21>ma55))
- //hold time (in bars)
- holdbarslen = input(40, minval=1, step=5, title="Hold time in number of bars: ")
- holdbars = barssince(longCondition or shortCondition)>holdbarslen
- longClose = crossunder(close, Tsl)
- //crossover(src, upper2) or closeHA<lower1 or crossunder(closeHA, Tsl) or holdbars
- shortClose = crossover(close, Tsl)
- //crossunder(src, lower2) or closeHA>upper1 or crossover(closeHA, Tsl) or holdbars
- if testPeriod()
- strategy.entry("Long", 1, when=longCondition)
- strategy.entry("Short", 0, when=shortCondition)
- strategy.close("Long", when=longClose)
- strategy.close("Short", when=shortClose)
- //SL
- stopLow = na
- stopHigh = na
- if (longCondition and strategy.position_size <= 0) or (shortCondition and strategy.position_size >= 0)
- stopLow := low
- stopHigh := high
- else
- if strategy.position_size == 0
- stopLow := na
- stopHigh := na
- else
- stopLow := stopLow[1]
- stopHigh := stopHigh[1]
- slPercent = input(0, title='Stop Loss %', type=float, step=0.1) / 100
- tsPercent = input(0, title='Trailing Stop Loss %', type=float, step=0.1) / 100
- stopLevelLong = strategy.position_size > 0 ? strategy.position_avg_price * (1 - slPercent) : na
- stopLevelShort = strategy.position_size < 0 ? strategy.position_avg_price * (1 + slPercent) : na
- trailingStopLevelLong = na
- trailingStopLevelShort = na
- trailingStopLevelLong := strategy.position_size > 0 ? (na(trailingStopLevelLong[1]) ? high * (1 - tsPercent) : max(high * (1 - tsPercent), trailingStopLevelLong[1])) : na
- trailingStopLevelShort := strategy.position_size < 0 ? (na(trailingStopLevelShort[1]) ? low * (1 + tsPercent) : min(low * (1 + tsPercent), trailingStopLevelShort[1])) : na
- plotshape(slPercent > 0 ? stopLevelLong : na, location=location.absolute, style=shape.xcross, color=red)
- plotshape(slPercent > 0 ? stopLevelShort : na, location=location.absolute, style=shape.xcross, color=red)
- //plotshape(tsPercent > 0 ? trailingStopLevelLong : na, location=location.absolute, style=shape.xcross, color=blue)
- //plotshape(tsPercent > 0 ? trailingStopLevelShort : na, location=location.absolute, style=shape.xcross, color=blue)
- //plotshape(strategy.position_avg_price, location=location.absolute, style=shape.circle, color=white)
- dynStop = input(false, title='Dynamic stop', type=bool)
- if dynStop and not (longCondition or shortCondition or longClose or shortClose)
- strategy.exit("Dynamic Stop Loss (Long)", "Long", stop=stopLow)
- strategy.exit("Dynamic Stop Loss (Short)", "Short", stop=stopHigh)
- else
- strategy.cancel("Dynamic Stop Loss (Long)")
- strategy.cancel("Dynamic Stop Loss (Short)")
- if slPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
- strategy.exit("Stop Loss (Long)", "Long", stop=stopLevelLong)
- strategy.exit("Stop Loss (Short)", "Short", stop=stopLevelShort)
- else
- strategy.cancel("Stop Loss (Long)")
- strategy.cancel("Stop Loss (Short)")
- if tsPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
- strategy.exit("Trailing Stop Loss (Long)", "Long", stop=trailingStopLevelLong)
- strategy.exit("Trailing Stop Loss (Short)", "Short", stop=trailingStopLevelShort)
- else
- strategy.cancel("Trailing Stop Loss (Long)")
- strategy.cancel("Trailing Stop Loss (Short)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement