Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- study("Sniper", overlay=true)
- /////////////////////////////////////
- //* Put your strategy logic below *//
- /////////////////////////////////////
- sourcein=high
- haclose = security(heikinashi(tickerid), period, close)
- hullper=input(title="Hull Length", type=integer, defval=1, minval=1,maxval=500)
- hull = vwma(2*sma(sourcein,round(hullper/2))-sma(sourcein,hullper),round(sqrt(hullper)))
- price = ema(close,1)
- topdecay = input(17, "Top Decay")
- decayt = atr(topdecay)*0.005
- topstage = input(20, "Top Stage Length")
- topmalength = input(60, "TopMa Length")
- botdecay = input(60, "Bot Decay")
- decayb = atr(botdecay)*0.005
- botstage = input(50, "Bot Stage Length")
- botmalength = input(6, "Bot Ma Length")
- topvector01 = 0.0
- topvector01finalcounter = 0.0
- pretopvector01 = nz(topvector01[2], sourcein)
- topvector01 := sourcein >= pretopvector01 ? sourcein : pretopvector01 - (decayt * topvector01finalcounter[1])
- topvector01counter = n - valuewhen(sourcein >= topvector01, n, 0)
- topvector01finalcounter := topvector01counter <= 87 ? topstage : topvector01counter <= 87*4 ? 15 : 12
- botvector01 = 0.0
- botvector01finalcounter = 0.0
- prebotvector01 = nz(botvector01[1], sourcein)
- botvector01 := sourcein <= prebotvector01 ? sourcein : prebotvector01 + (decayb * botvector01finalcounter[1])
- botvector01counter = n - valuewhen(sourcein <= botvector01, n, 0)
- botvector01finalcounter := botvector01counter <= 87 ? botstage : botvector01counter <= 87*4 ? 15 : 12
- topvecMA = sma(topvector01,topmalength)
- botvecMA = sma(botvector01,botmalength)
- // Long/Short Logic
- // Long/Short Logic
- longLogic = crossunder(hull+160,botvecMA) ? 1 : 0 //crossover(hull,topvecMA) ? 1 : 0 or
- shortLogic = crossunder(hull,botvecMA) ? 1 : 0//or crossunder(hull,price) ? 1 : 0
- shortflipvalue = valuewhen(shortLogic,close,0)
- longflipvalue = valuewhen(longLogic,close,0)
- shortflip = crossover(price-30,shortflipvalue)
- //////////////////////////
- //* Strategy Component *//
- //////////////////////////
- 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
- longCondition = long or shortflip and sectionLongs <= 1
- shortCondition = short and sectionShorts <= 1
- //longCondition = long and sectionLongs <= 1 or shortflip and sectionLongs <= 1
- //shortCondition = short and sectionShorts <= 1 or longflip and sectionShorts <= 2
- ////////////////////////////////
- //======[ 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_shortCondition2 = 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 ]======//
- ///////////////////////////////
- isSL = input(true, "Take Profit")
- longSLlevel = input(80, "Short TP $$")
- shortSLlevel = input(160, "Long TP $$ ")
- longentry = valuewhen(longCondition,close,0)
- shortentry = valuewhen(shortCondition,close,0)
- // longmath = (longentry * longSLlevel) / 100
- // shortmath = (shortentry * shortSLlevel) / 100
- longstop = longentry + longSLlevel
- shortstop = shortentry - shortSLlevel
- long_sl = isSL and in_longCondition and crossover(price,longstop)
- short_sl = isSL and in_shortCondition and crossunder(price,shortstop)
- longSLcol = in_longCondition[1] ? lime : black
- shortSLcol = in_shortCondition[1] ? red : black
- /////////////////////////////////////
- //======[ 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 ]======//
- /////////////////////////////////
- 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])
- ///////////////////////////////
- //======[ Plot Colors ]======//
- ///////////////////////////////
- // longCloseCol = na
- // shortCloseCol = na
- // longCloseCol := long_tp ? purple : long_sl ? maroon : longCloseCol[1]
- // shortCloseCol := short_tp ? purple : short_sl ? maroon : shortCloseCol[1]
- // tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
- slColor = isSL and in_shortCondition and crossunder(price,shortstop)? lime : isSL and in_longCondition? lime : isSL and in_longCondition and crossover(price,longstop) ? white : red
- //==Plots==
- c2 = sectionLongConditions > 0 ? lime : sectionShortConditions > 0 ? red : white// slColor// in_shortCondition and isSL ? red : isSL and in_longCondition ? lime : black
- n1t=plot(topvecMA, "Top MA", color = c2, linewidth= 8, transp=65)
- n1b=plot(botvecMA, "Bot MA", color = c2, linewidth= 8, transp=65)
- n2b=plot(hull, "Signal MA", color=c2, transp=5)
- fill(n1b, n1t, color=c2, transp=85)
- ///////////////////////////////
- //======[ Alert Plots ]======//
- ///////////////////////////////
- // New Signal Plots
- plotshape(series=longCondition, title="Long",text = "Sniper long", style=shape.triangleup, location=location.belowbar, color=lime, size=size.small)
- plotshape(series=shortCondition, title="Short",text = "Sniper short", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
- plotshape(series=long_sl, title="Take money long", text = "Take money short", style=shape.triangledown, location=location.abovebar, color=blue, size=size.normal)
- plotshape(series=short_sl, title="Take money short", text = "Take money long", style=shape.triangleup, location=location.belowbar, color=blue, size=size.normal)
- alertcondition(condition=longCondition, title="Long", message="/buy*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper long new")
- alertcondition(condition=shortCondition, title="Short", message="/sell*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper short new")
- alertcondition(condition=long_sl, title="Take money long", message="/buy*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper Take money long")
- alertcondition(condition=short_sl, title="Take money short", message="/close*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper short newTP close")
- ///////////////////////////////////
- //======[ Reset Variables ]======//
- ///////////////////////////////////
- if long_sl or not in_longCondition
- averageLongs := 0
- totalLongs := 0.0
- sectionLongs := 0
- sectionLongConditions := 0
- if short_sl or not in_shortCondition
- averageShorts := 0
- totalShorts := 0.0
- sectionShorts := 0
- sectionShortConditions := 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement