SHARE
TWEET

Untitled

a guest May 26th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=3
  2.  
  3. study("Sniper", overlay=true)
  4.  
  5.  
  6. /////////////////////////////////////
  7. //* Put your strategy logic below *//
  8. /////////////////////////////////////
  9.  
  10. sourcein=high
  11. haclose = security(heikinashi(tickerid), period, close)
  12. hullper=input(title="Hull Length", type=integer, defval=1, minval=1,maxval=500)
  13. hull = vwma(2*sma(sourcein,round(hullper/2))-sma(sourcein,hullper),round(sqrt(hullper)))
  14. price = ema(close,1)
  15.  
  16. topdecay = input(17, "Top Decay")
  17. decayt = atr(topdecay)*0.005
  18. topstage = input(20, "Top Stage Length")
  19. topmalength = input(60, "TopMa Length")
  20.  
  21. botdecay = input(60, "Bot Decay")
  22. decayb = atr(botdecay)*0.005
  23. botstage = input(50, "Bot Stage Length")
  24. botmalength = input(6, "Bot Ma Length")
  25.  
  26. topvector01 = 0.0
  27. topvector01finalcounter = 0.0
  28. pretopvector01 = nz(topvector01[2], sourcein)
  29. topvector01 := sourcein >= pretopvector01 ? sourcein : pretopvector01 - (decayt * topvector01finalcounter[1])
  30. topvector01counter = n - valuewhen(sourcein >= topvector01, n, 0)
  31. topvector01finalcounter := topvector01counter <= 87 ? topstage : topvector01counter <= 87*4 ? 15 : 12
  32.  
  33. botvector01 = 0.0
  34. botvector01finalcounter = 0.0
  35. prebotvector01 = nz(botvector01[1], sourcein)
  36. botvector01 := sourcein <= prebotvector01 ? sourcein : prebotvector01 + (decayb * botvector01finalcounter[1])
  37. botvector01counter = n - valuewhen(sourcein <= botvector01, n, 0)
  38. botvector01finalcounter := botvector01counter <= 87 ? botstage : botvector01counter <= 87*4 ? 15 : 12
  39.  
  40. topvecMA = sma(topvector01,topmalength)
  41. botvecMA = sma(botvector01,botmalength)
  42.  
  43. // Long/Short Logic
  44. // Long/Short Logic
  45. longLogic =   crossunder(hull+160,botvecMA) ? 1 : 0 //crossover(hull,topvecMA) ? 1 : 0 or
  46. shortLogic = crossunder(hull,botvecMA) ? 1 : 0//or crossunder(hull,price) ? 1 : 0
  47.  
  48. shortflipvalue = valuewhen(shortLogic,close,0)
  49. longflipvalue = valuewhen(longLogic,close,0)
  50.  
  51. shortflip = crossover(price-30,shortflipvalue)
  52.  
  53.  
  54. //////////////////////////
  55. //* Strategy Component *//
  56. //////////////////////////
  57.  
  58.  
  59. long = longLogic
  60. short = shortLogic
  61.  
  62.  
  63.    
  64. ////////////////////////////////
  65. //======[ Signal Count ]======//
  66. ////////////////////////////////
  67.  
  68. sectionLongs = 0
  69. sectionLongs := nz(sectionLongs[1])
  70. sectionShorts = 0
  71. sectionShorts := nz(sectionShorts[1])
  72.  
  73. if long
  74.     sectionLongs := sectionLongs + 1
  75.     sectionShorts := 0
  76.  
  77. if short
  78.     sectionLongs := 0
  79.     sectionShorts := sectionShorts + 1
  80.  
  81.  
  82. longCondition = long or shortflip and sectionLongs <= 1
  83. shortCondition = short and sectionShorts <= 1
  84.  
  85. //longCondition = long and sectionLongs <= 1 or shortflip and sectionLongs <= 1
  86. //shortCondition = short and sectionShorts <= 1 or longflip and sectionShorts <= 2
  87.  
  88.  
  89. ////////////////////////////////
  90. //======[ Entry Prices ]======//
  91. ////////////////////////////////
  92.  
  93. last_open_longCondition = na
  94. last_open_shortCondition = na
  95. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  96. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  97.  
  98. ////////////////////////////////////
  99. //======[ Open Order Count ]======//
  100. ////////////////////////////////////
  101.  
  102. sectionLongConditions = 0
  103. sectionLongConditions := nz(sectionLongConditions[1])
  104. sectionShortConditions = 0
  105. sectionShortConditions := nz(sectionShortConditions[1])
  106.  
  107. if longCondition
  108.     sectionLongConditions := sectionLongConditions + 1
  109.     sectionShortConditions := 0
  110.  
  111. if shortCondition
  112.     sectionLongConditions := 0
  113.     sectionShortConditions := sectionShortConditions + 1
  114.    
  115.  
  116. ///////////////////////////////////////////////
  117. //======[ Position Check (long/short) ]======//
  118. ///////////////////////////////////////////////
  119.  
  120. last_longCondition = na
  121. last_shortCondition = na
  122. last_shortCondition2 = na
  123. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  124. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  125.  
  126.  
  127. in_longCondition = last_longCondition > last_shortCondition
  128. in_shortCondition = last_shortCondition > last_longCondition
  129.  ///////////////////////////////
  130. //======[ Take Profit ]======//
  131. ///////////////////////////////
  132.  
  133. isSL = input(true, "Take Profit")
  134. longSLlevel = input(80, "Short TP $$")
  135. shortSLlevel = input(160, "Long TP $$ ")
  136. longentry = valuewhen(longCondition,close,0)
  137. shortentry = valuewhen(shortCondition,close,0)
  138.  
  139. // longmath = (longentry * longSLlevel) / 100
  140. // shortmath = (shortentry * shortSLlevel) / 100
  141.  
  142. longstop = longentry + longSLlevel
  143. shortstop = shortentry - shortSLlevel
  144. long_sl = isSL and in_longCondition and crossover(price,longstop)
  145. short_sl = isSL and in_shortCondition and crossunder(price,shortstop)
  146.  
  147. longSLcol = in_longCondition[1] ? lime : black
  148. shortSLcol = in_shortCondition[1] ? red : black  
  149.  
  150. /////////////////////////////////////
  151. //======[ Position Averages ]======//
  152. /////////////////////////////////////
  153.  
  154. totalLongs = 0.0
  155. totalLongs := nz(totalLongs[1])
  156. totalShorts = 0.0
  157. totalShorts := nz(totalShorts[1])
  158. averageLongs = 0.0
  159. averageLongs := nz(averageLongs[1])
  160. averageShorts = 0.0
  161. averageShorts := nz(averageShorts[1])
  162.  
  163. if longCondition
  164.     totalLongs := totalLongs + last_open_longCondition
  165.     totalShorts := 0.0
  166.  
  167. if shortCondition
  168.     totalLongs := 0.0
  169.     totalShorts := totalShorts + last_open_shortCondition
  170.  
  171. averageLongs := totalLongs / sectionLongConditions
  172. averageShorts := totalShorts / sectionShortConditions
  173.  
  174. /////////////////////////////////
  175. //======[ Trailing Stop ]======//
  176. /////////////////////////////////
  177.  
  178. last_high = na
  179. last_low = na
  180. last_high_short = na
  181. last_low_short = na
  182. last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  183. 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])
  184. last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  185. 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])
  186.  
  187.  
  188.  
  189. ///////////////////////////////
  190. //======[ Plot Colors ]======//
  191. ///////////////////////////////
  192.  
  193. // longCloseCol = na
  194. // shortCloseCol = na
  195. // longCloseCol := long_tp ? purple : long_sl ? maroon : longCloseCol[1]
  196. // shortCloseCol := short_tp ? purple : short_sl ? maroon : shortCloseCol[1]
  197. // tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
  198. 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
  199. //==Plots==
  200.  
  201.  
  202. c2 = sectionLongConditions > 0 ? lime : sectionShortConditions > 0 ? red : white// slColor// in_shortCondition and isSL ? red : isSL and in_longCondition ? lime : black
  203. n1t=plot(topvecMA, "Top MA", color = c2, linewidth= 8, transp=65)
  204. n1b=plot(botvecMA, "Bot MA", color = c2, linewidth= 8, transp=65)
  205. n2b=plot(hull, "Signal MA", color=c2, transp=5)
  206. fill(n1b, n1t, color=c2, transp=85)
  207.  
  208.  
  209. ///////////////////////////////
  210. //======[ Alert Plots ]======//
  211. ///////////////////////////////
  212.  
  213.  
  214. // New Signal Plots
  215. plotshape(series=longCondition, title="Long",text = "Sniper long", style=shape.triangleup, location=location.belowbar, color=lime, size=size.small)
  216. plotshape(series=shortCondition, title="Short",text = "Sniper short", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
  217.  
  218. plotshape(series=long_sl, title="Take money long", text = "Take money short", style=shape.triangledown, location=location.abovebar, color=blue, size=size.normal)
  219.  
  220. plotshape(series=short_sl, title="Take money short", text = "Take money long", style=shape.triangleup, location=location.belowbar, color=blue, size=size.normal)
  221.  
  222.  
  223.  
  224. alertcondition(condition=longCondition, title="Long", message="/buy*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper long new")
  225. alertcondition(condition=shortCondition, title="Short", message="/sell*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper short new")
  226. alertcondition(condition=long_sl, title="Take money long", message="/buy*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper Take money long")
  227. alertcondition(condition=short_sl, title="Take money short", message="/close*v5000*Bitmex*XBTUSD*3*1*40*1*Sniper short newTP close")
  228.  
  229.  
  230. ///////////////////////////////////
  231. //======[ Reset Variables ]======//
  232. ///////////////////////////////////
  233.  
  234. if long_sl or not in_longCondition
  235.     averageLongs := 0
  236.     totalLongs := 0.0
  237.     sectionLongs := 0
  238.     sectionLongConditions := 0
  239.  
  240. if short_sl or not in_shortCondition
  241.     averageShorts := 0
  242.     totalShorts := 0.0
  243.     sectionShorts := 0
  244.     sectionShortConditions := 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top