Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study("Sniper @ XBTUSD 1 min Alerts and bots", overlay=true)//, default_qty_type=strategy.percent_of_equity, currency="USD", default_qty_value=100)
- RSISetbuy= true// input(true,title="==Sniper Alpha Long==")
- RSIlengthbuy = -input(-10, title="Zoom out X for Long")
- RSIDifferencebuy = -input(7, minval=-200, maxval=200, title="Long Difference")
- TDsetbuy = input(true, title="Sniper Alpha Long activated")
- TDlookbuy = input(1000, title="Max targets x")
- RSISetsell= true//input(true, title="==Sniper Alpha Short==")
- RSIlengthsell = -input(-11, title="Zoom out X for Short")
- RSIDifferencesell = -input(1, minval=-200, maxval=200, title="Short Difference")
- TDsetsell = input(true, title="Sniper Alpha Short activated")
- TDlooksell = input(1, title="targets")
- BBSet = false//input(false, title="==Correction wind==")
- BBbasis = 20//input(20, title="Side Left")
- BBdev = 20//input(20, title="Side Right")
- MACDset = input(false, title="==Reload Weapon==")
- MACD1 = 12//input(12, "Snipe Fast delay")
- MACD2 = 26//input(26, "Snipe Slow delay")
- MACD3 = 9//input(9, "Snipe Signal delay")
- useMAlong = input(false, title="Fast reload Weapon for filter Longs?")
- useMAshort = input(true, title="Fast reload Weapon for fiter Shorts?")
- MACDFLIP = input(false, "==Sniper Alpha flip the Targets==")
- isFlip = MACDFLIP
- TD = 0.0
- TD := close > close[TDlookbuy] ?nz(TD[1])+1:0
- TS = 0.0
- TS := close < close[TDlookbuy] ?nz(TS[1])+1:0
- TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
- TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
- TDcheckUP = iff(TD == 2, true, false)
- TDCheckDOWN = iff(TS == 2, true, false)
- TD2 = 0.0
- TD2 := close > close[TDlooksell] ?nz(TD2[1])+1:0
- TS2 = 0.0
- TS2 := close < close[TDlooksell] ?nz(TS2[1])+1:0
- TDUp2 = TD2 - valuewhen(TD2 < TD2[1], TD2 , 1 )
- TDDn2 = TS2 - valuewhen(TS2 < TS2[1], TS2 , 1 )
- TDcheckUP2 = iff(TD2 == 2, true, false)
- TDCheckDOWN2 = iff(TS2 == 2, true, false)
- [_, _, histLine] = macd(close, MACD1, MACD2, MACD3)
- MACDCheckDown = iff(histLine > 0 and histLine[1] > 0 and histLine[2] > 0 and histLine[3] > 0 and histLine[4] > 0, true, false)
- MACDCheckUp = iff(histLine < 0 and histLine[1] < 0 and histLine[2] < 0 and histLine[3] < 0 and histLine[4] < 0, true, false)
- RSICalbuy = rsi(close, RSIlengthbuy)
- RSICalNewUpbuy = 50 + RSIDifferencebuy
- RSICalNewDownbuy = 50 - RSIDifferencebuy
- RSICheckUpbuy = iff(RSICalbuy <= RSICalNewUpbuy, true, false)
- RSICheckDownbuy = iff(RSICalbuy >= RSICalNewDownbuy, true, false)
- RSICalsell = rsi(close, RSIlengthsell)
- RSICalNewUpsell = 50 + RSIDifferencesell
- RSICalNewDownsell = 50 - RSIDifferencesell
- RSICheckUpsell = iff(RSICalsell <= RSICalNewUpsell, true, false)
- RSICheckDownsell = iff(RSICalsell >= RSICalNewDownsell, true, false)
- basis = sma(close, BBbasis)
- dev = 2 * stdev(close, BBdev)
- upperBB = basis + dev
- lowerBB = basis - dev
- BBCheckUp = iff(close > upperBB, true, false)
- BBCheckDown = iff(close < lowerBB, true, false)
- //BBCheckUp = false
- //BBCheckDown = false
- slowma = ema(close,200)
- fastma = ema(close,9)
- price = ema(close,1)
- //iff(histLine>slowma and histLine[1]>slowma and histLine[2]>slowma and histLine[3]>slowma and histLine[4]>slowma, true, false)
- //BuyCheck = iff(TDcheckUP == true and MACDCheckUp == true and RSICheckUp == true and BBCheckUp == false, true, false)
- input_long = input(5, title="long")
- lb_t = linebreak (tickerid, "close", input_long)
- lb_close = security(lb_t, period, close)
- BuyCheck = security(lb_t, period, close)and iff(TDcheckUP == true and MACDCheckUp == true and RSICheckUpbuy == true and BBCheckUp == false, true, false)
- SellCheck = iff(TDCheckDOWN2 == true and MACDCheckDown == true and RSICheckDownsell == true and BBCheckDown == false, true, false)
- //////MA Filter/////
- fast = ema(close,9)
- slow = ema(close,200)
- avg = ((fast-slow) /fast) *100
- avg2 = ((slow-fast) /slow) *100
- //plot(-0.50)
- //plot(histLine)
- h = 0.24
- l = -0.50
- // Long/Short Logic
- longfilt = fastma > slowma
- shortfilt = fastma < slowma
- longLogic = BuyCheck ? 1 : 0
- shortLogic = SellCheck ? 1 : 0
- if useMAlong
- longLogic := BuyCheck and longfilt ? 1 : 0
- else
- longLogic := BuyCheck ? 1 : 0
- if useMAshort
- shortLogic := SellCheck and shortfilt ? 1 : 0
- else
- shortLogic := SellCheck ? 1 : 0
- //////////////////////////
- //* Strategy Component *//
- //////////////////////////
- isLong = input(false, "Longs Only")
- isShort = input(false, "Shorts Only")
- long = longLogic
- short = shortLogic
- if isFlip
- long := shortLogic
- short := longLogic
- else
- long := longLogic
- short := shortLogic
- if MACDFLIP
- long := longLogic
- short := shortLogic
- else
- long := shortLogic
- short := longLogic
- if MACDFLIP and isFlip
- long := shortLogic
- short := longLogic
- else
- long := longLogic
- short := shortLogic
- if isLong
- long := long
- short := na
- if isShort
- long := na
- short := short
- ////////////////////////////////
- //======[ 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
- shortCondition = short and sectionShorts <= pyrl or short and sectionShorts >= pyrg or short and sectionShorts == pyre ? 1 : 0
- longCondition = long and sectionLongs <= pyrl or long and sectionLongs >= pyrg or long and sectionLongs == 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
- len = input(1, minval=1, title="timeplay")
- plot (slowma)
- p(x) => fixnan(dev((x=='h' ? lowest(len) : highest(len)), len) ? na : (x=='h' ? lowest(len) : highest(len)))
- z(x) => p(x)[len-1]
- c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),len), len) ? na : highest((barssince(change(p(x)) != 0)),len)))+1
- d(x,y) => barssince(change(z(x)) !=0) + (y==1 ? 0 : y==2 ? c(x) : y==3 ? (c(x) + valuewhen(change(z(x))!=0,c(x),(1))) : y==4 ? (c(x) + valuewhen(change(z(x))!=0,c(x),(1))+ valuewhen(change(z(x))!=0,c(x),(2))) : na)
- s(x) => (z(x) - valuewhen(change(z(x))!=0,z(x),2))/c(x)
- s_(x,y) => valuewhen(change(s(x))!=0,s(x),(y-1))
- lb_t2 = linebreak (tickerid, "close", 1)
- lb_close2 = security(lb_t2, period, close)
- plot(lb_close2)
- line_(x,y) => s_(x,y)*d(x,y) + lb_close2
- plot(line_('h',1), style=circles, color=yellow, linewidth=1, offset=-2*len+3)
- /////////////////////////////
- //======[ Take Snipe ]======//
- /////////////////////////////
- isTSr = input(true, "Take Sniper")
- //sl = input(150, "Stop Loss (%). Divided by 100 (1 = 0.01%)") / 100
- z1 = line_('h',1)
- zh = highest(z1, 1)
- zl = lowest (z1, 1)
- plot(highest(z1, 100), style=circles, color=red, linewidth=2, offset=-2*len+3)
- plot(lowest (z1, 200), style=circles, color=blue, linewidth=2, offset=-2*len+3)
- buy = lowest (z1, 150)
- sell =highest (z1, 100)
- long_st= close+20 < buy
- short_st = crossover(sell+20, price)
- long_st2=close-10<buy+10 and buy> close //crossover(sell, price) and sell-30 > price
- short_st2 = crossover(sell-40, price)
- /////////////////////////////////
- //======[ 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(40, "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(50, "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 / 80 * tsi : na, "Long Trailing Activate", aqua, style=3, linewidth=2)
- plot(isTS and in_longCondition and last_high >= averageLongs + averageLongs / 80* tsi ? last_high - last_high / 80 * 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 / 80 * tp ? averageLongs + averageLongs / 80 * 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 / 80 * sl ? averageLongs - averageLongs / 80 * 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)
- plot(fastma, linewidth = 2, color = lime)
- plot(slowma, linewidth = 2, color = yellow)
- ///////////////////////////////
- //======[ Alert Plots ]======//
- ///////////////////////////////
- // New Signal Plots
- plotshape(series=longCondition, title="Long", style=shape.triangleup, location=location.belowbar, text="Full Long", color=lime, size=size.small)
- plotshape(series=shortCondition, title="Short", style=shape.triangledown, location=location.abovebar, text="Full Short", color=red, size=size.small)
- plotshape(series=longCloseTP, title="Long Close", style=shape.triangleup, location=location.belowbar, text="Take", color=lime, size=size.tiny)
- plotshape(series=shortCloseTP, title="Short Close", style=shape.triangledown, location=location.abovebar, text="Take", color=red, size=size.tiny)
- plotshape(series=longCloseTS, title="Long Close", style=shape.triangleup, location=location.belowbar, text="Trail", color=lime, size=size.tiny)
- plotshape(series=shortCloseTS, title="Short Close", style=shape.triangledown, location=location.abovebar, text="Trail", color=red, size=size.tiny)
- plotshape(series=longCloseSL, title="Long Close", style=shape.triangleup, location=location.belowbar, text="Stop", color=lime, size=size.tiny)
- plotshape(series=shortCloseSL, title="Short Close", style=shape.triangledown, location=location.abovebar, text="Stop", color=red, size=size.tiny)
- plotshape(series=long_st, title="Sniper 1", style=shape.triangleup, location=location.belowbar, text="Sniper 1 full Long", color=lime, size=size.tiny)
- plotshape(series=short_st, title="Sniper 1", style=shape.triangledown, location=location.abovebar, text="Sniper 1 full Short", color=red, size=size.tiny)
- plotshape(series=long_st2, title="Sniper 2", style=shape.triangleup, location=location.belowbar, text="Sniper 2", color=lime, size=size.tiny)
- plotshape(series=short_st2, title="Sniper 2", style=shape.triangledown, location=location.abovebar, text="Sniper 2", color=red, size=size.tiny)
- alertcondition(condition=long_st, title="Full Long Sniper ", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
- alertcondition(condition=short_st, title="Full Short Sniper ", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
- alertcondition(condition=long_st2, title="Long 100% Sniper", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
- alertcondition(condition=short_st2, title="Short 100% Sniper", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
- alertcondition(condition=longClose, title="Long Close All", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
- alertcondition(condition=shortClose, title="Short Close All", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
- alertcondition(condition=longCondition, title="Full Long", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
- alertcondition(condition=shortCondition, title="Full Short ", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
- //longalert = long_st //or long_st2 or longCondition or longClose
- //alertcondition(condition=longalert, title="Long", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=10% l=10")
- //shortalert = short_st or short_st2 or shortCondition or shortClose
- //alertcondition(condition=shortalert, title="Short", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=10% l=10")
- ///////////////////////////////////
- //======[ Reset Variables ]======//
- ///////////////////////////////////
- if longClose or not in_longCondition
- averageLongs := 0
- totalLongs := 0.0
- sectionLongs := 0
- sectionLongConditions := 0
- if shortClose or not in_shortCondition
- averageShorts := 0
- totalShorts := 0.0
- sectionShorts := 0
- sectionShortConditions := 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement