Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- // ***** Switch the "//" from study to strategy for strategy use *****
- //strategy(title="Ratnieks Moving Averages", shorttitle='RMA', overlay=true)
- study(title="Ratnieks Moving Averages", shorttitle='RMA', overlay=true)
- sourcein=close
- sourcein2=close[1]
- n = input(title = "HullMA period", type=integer, defval=16)
- Dclose = security(tickerid,"D",close)
- Dopen = security(tickerid, "D",open)
- src = close
- bg = valuewhen(close,close,0)
- //bgcolor(bg>Dopen?lime:red)
- // ==Functions==
- n1 = ema(2*sma(sourcein,round(n/2))-sma(sourcein,n),round(sqrt(n)))
- n2 = ema(2*sma(sourcein2,round(n/2))-sma(sourcein2,n),round(sqrt(n)))
- c=n1>n2?lime:red
- longLogic = crossover(n1,n2) ?1:0//and bg>Dopen
- shortLogic = crossunder(n1,n2) ?1:0 //and bg<Dopen
- long = longLogic
- short = shortLogic
- ////////////////////////////////
- //======[ Signal Count ]======//
- ////////////////////////////////
- sectionLongs = 0
- sectionLongs := nz(sectionLongs[1])
- sectionShorts = 0
- sectionShorts := nz(sectionShorts[1])
- if long
- sectionLongs := sectionLongs + 1
- sectionShorts := 0
- if short
- sectionLongs := 0
- sectionShorts := sectionShorts + 1
- //////////////////////////////
- //======[ Pyramiding ]======//
- //////////////////////////////
- pyrl = input(1, "Pyramiding less than") // If your count is less than this number
- pyre = input(0, "Pyramiding equal to") // If your count is equal to this number
- pyrg = input(1000000, "Pyramiding greater than") // If your count is greater than this number
- longCondition = long and sectionLongs <= pyrl or long and sectionLongs >= pyrg or long and sectionLongs == pyre ? 1 : 0
- shortCondition = short and sectionShorts <= pyrl or short and sectionShorts >= pyrg or short and sectionShorts == pyre ? 1 : 0
- ////////////////////////////////
- //======[ Entry Prices ]======//
- ////////////////////////////////
- last_open_longCondition = na
- last_open_shortCondition = na
- last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
- last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
- ////////////////////////////////////
- //======[ Open Order Count ]======//
- ////////////////////////////////////
- sectionLongConditions = 0
- sectionLongConditions := nz(sectionLongConditions[1])
- sectionShortConditions = 0
- sectionShortConditions := nz(sectionShortConditions[1])
- if longCondition
- sectionLongConditions := sectionLongConditions + 1
- sectionShortConditions := 0
- if shortCondition
- sectionLongConditions := 0
- sectionShortConditions := sectionShortConditions + 1
- ///////////////////////////////////////////////
- //======[ Position Check (long/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
- /////////////////////////////////////
- //======[ Position Averages ]======//
- /////////////////////////////////////
- totalLongs = 0.0
- totalLongs := nz(totalLongs[1])
- totalShorts = 0.0
- totalShorts := nz(totalShorts[1])
- averageLongs = 0.0
- averageLongs := nz(averageLongs[1])
- averageShorts = 0.0
- averageShorts := nz(averageShorts[1])
- if longCondition
- totalLongs := totalLongs + last_open_longCondition
- totalShorts := 0.0
- if shortCondition
- totalLongs := 0.0
- totalShorts := totalShorts + last_open_shortCondition
- averageLongs := totalLongs / sectionLongConditions
- averageShorts := totalShorts / sectionShortConditions
- /////////////////////////////////
- //======[ Trailing Stop ]======//
- /////////////////////////////////
- isTS = input(false, "Trailing Stop")
- tsi = input(1300, "Activate Trailing Stop Price (%). Divided by 100 (1 = 0.01%)") / 100
- ts = input(400, "Trailing Stop (%). Divided by 100 (1 = 0.01%)") / 100
- last_high = na
- last_low = na
- last_high_short = na
- last_low_short = na
- last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
- last_high_short := not in_shortCondition ? na : in_shortCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
- last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
- last_low_short := not in_longCondition ? na : in_longCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
- long_ts = isTS and not na(last_high) and low <= last_high - last_high / 100 * ts and longCondition == 0 and last_high >= averageLongs + averageLongs / 100 * tsi
- short_ts = isTS and not na(last_low) and high >= last_low + last_low / 100 * ts and shortCondition == 0 and last_low <= averageShorts - averageShorts/ 100 * tsi
- ///////////////////////////////
- //======[ Take Profit ]======//
- ///////////////////////////////
- isTP = input(false, "Take Profit")
- tp = input(150, "Take Profit (%). Divided by 100 (1 = 0.01%)") / 100
- long_tp = isTP and close > averageLongs + averageLongs / 100 * tp and not longCondition
- short_tp = isTP and close < averageShorts - averageShorts / 100 * tp and not shortCondition
- /////////////////////////////
- //======[ Stop Loss ]======//
- /////////////////////////////
- isSL = input(false, "Stop Loss")
- sl = input(150, "Stop Loss (%). Divided by 100 (1 = 0.01%)") / 100
- long_sl = isSL and close < averageLongs - averageLongs / 100 * sl and longCondition == 0
- short_sl = isSL and close > averageShorts + averageShorts / 100 * sl and shortCondition == 0
- /////////////////////////////////
- //======[ Close Signals ]======//
- /////////////////////////////////
- longCloseTP = long_tp ? 1 : 0
- longCloseSL = long_sl ? 1 : 0
- longCloseTS = long_ts ? 1 : 0
- shortCloseTP = short_tp ? 1 : 0
- shortCloseSL = short_sl ? 1 : 0
- shortCloseTS = short_ts ? 1 : 0
- longClose = long_tp or long_sl or long_ts ? 1 : 0
- shortClose = short_tp or short_sl or short_ts ? 1: 0
- /////////////////////////////TS
- //======[ Plot Colors ]======//
- ///////////////////////////////
- longCloseCol = na
- shortCloseCol = na
- longCloseCol := long_tp ? purple : long_sl ? maroon : long_ts ? blue : longCloseCol[1]
- shortCloseCol := short_tp ? purple : short_sl ? maroon : short_ts ? blue : shortCloseCol[1]
- tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
- slColor = isSL and in_longCondition ? red : isSL and in_shortCondition ? red : white
- // === INPUT BACKTEST RANGE ===
- FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
- FromMonth = input(defval = 12, title = "From Month", minval = 1, maxval = 12)
- FromYear = input(defval = 2018, title = "From Year", minval = 2014)
- ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
- ToMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)
- ToYear = input(defval = 9999, title = "To Year", minval = 2014)
- start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
- finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
- inTimeFrame() => time >= start and time <= finish ? true : false
- //////////////////////////////////
- //======[ Strategy Plots ]======//
- //////////////////////////////////
- plot(isTS and in_longCondition ? averageLongs + averageLongs / 100 * tsi : na, "Long Trailing Activate", aqua, style=3, linewidth=2)
- plot(isTS and in_longCondition and last_high >= averageLongs + averageLongs / 100 * tsi ? last_high - last_high / 100 * ts : na, "Long Trailing", fuchsia, style=2, linewidth=3)
- plot(isTS and in_shortCondition ? averageShorts - averageShorts/ 100 * tsi : na, "Short Trailing Activate", aqua, style=3, linewidth=2)
- plot(isTS and in_shortCondition and last_low <= averageShorts - averageShorts/ 100 * tsi ? last_low + last_low / 100 * ts : na, "Short Trailing", fuchsia, style=2, linewidth=3)
- plot(isTP and in_longCondition and last_high < averageLongs + averageLongs / 100 * tp ? averageLongs + averageLongs / 100 * tp : na, "Long TP", tpColor, style=3, linewidth=2)
- plot(isTP and in_shortCondition and last_low > averageShorts - averageShorts / 100 * tp ? averageShorts - averageShorts / 100 * tp : na, "Short TP", tpColor, style=3, linewidth=2)
- plot(isSL and in_longCondition and last_low_short > averageLongs - averageLongs / 100 * sl ? averageLongs - averageLongs / 100 * sl : na, "Long SL", slColor, style=3, linewidth=2)
- plot(isSL and in_shortCondition and last_high_short < averageShorts + averageShorts / 100 * sl ? averageShorts + averageShorts / 100 * sl : na, "Short SL", slColor, style=3, linewidth=2)
- ///////////////////////////////
- //======[ Alert Plots ]======//
- ///////////////////////////////
- // New Signal Plots
- plotshape(series=longCondition, title="Long", style=shape.triangleup, location=location.belowbar, color=lime, size=size.small)
- plotshape(series=shortCondition, title="Short", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
- plotshape(series=longCloseTP, title="Long Close", style=shape.xcross, location=location.belowbar, text="Take", color=lime, size=size.tiny)
- plotshape(series=shortCloseTP, title="Short Close", style=shape.xcross, location=location.abovebar, text="Take", color=red, size=size.tiny)
- plotshape(series=longCloseTS, title="Long Close", style=shape.xcross, location=location.belowbar, text="Trail", color=lime, size=size.tiny)
- plotshape(series=shortCloseTS, title="Short Close", style=shape.xcross, location=location.belowbar, text="Trail", color=red, size=size.tiny)
- plotshape(series=longCloseSL, title="Long Close", style=shape.xcross, location=location.belowbar, text="Stop", color=lime, size=size.tiny)
- plotshape(series=shortCloseSL, title="Short Close", style=shape.xcross, location=location.belowbar, text="Stop", color=red, size=size.tiny)
- // Plots
- n1e=plot(n1, color=c, linewidth=1)
- n2e=plot(n2, color=c, linewidth=1)
- fill(n1e, n2e, color=c, transp=75)
- plotshape(long, style=shape.labelup, location=location.belowbar, size=size.tiny, color=lime)
- plotshape(short, style=shape.labeldown, location=location.abovebar, size=size.tiny, color=red)
- //bgcolor(in_longCondition?lime:red)
- //== Alerts ==
- //**Change strategy to study for alerts**
- alertcondition(longCondition, "LongMex", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd b=buy t=post p=-0.5 q=10% l=10 | e=bitmex s=xbtusd t=limit b=buy q=100% ts=-10 ro=1")
- alertcondition(shortCondition, "ShortMex", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd b=sell t=post p=0.5 q=10% l=10 | e=bitmex s=xbtusd t=limit b=sell q=100% ts=10 ro=1")
- //alertcondition(in_longCondition, "LongMexTS", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd t=limit b=buy q=100% ts=-10 ro=1")
- //alertcondition(in_shortCondition, "ShortMexTS", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd t=limit b=sell q=100% ts=10 ro=1")
- //== Strategy ==
- //** Grey these out for study use**
- //***** Remove the "//" on the strategy.entry(s) for strategy use *****
- //strategy.entry("Long", strategy.long, limit = close, when = long)
- //strategy.entry("Short",strategy.short, limit = close, when = short)
- //strategy.close("Long", when = longCloseTS)
- //strategy.close("Short", when = shortCloseTS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement