Advertisement
Guest User

test

a guest
Dec 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.89 KB | None | 0 0
  1. //@version=3
  2. study("Sniper @ XBTUSD 1 min Alerts and bots", overlay=true)//, default_qty_type=strategy.percent_of_equity, currency="USD", default_qty_value=100)
  3. RSISetbuy= true// input(true,title="==Sniper Alpha Long==")
  4. RSIlengthbuy = -input(-10, title="Zoom out X for Long")
  5. RSIDifferencebuy = -input(7, minval=-200, maxval=200, title="Long Difference")
  6. TDsetbuy = input(true, title="Sniper Alpha Long activated")
  7. TDlookbuy = input(1000, title="Max targets x")
  8. RSISetsell= true//input(true, title="==Sniper Alpha Short==")
  9. RSIlengthsell = -input(-11, title="Zoom out X for Short")
  10. RSIDifferencesell = -input(1, minval=-200, maxval=200, title="Short Difference")
  11. TDsetsell = input(true, title="Sniper Alpha Short activated")
  12. TDlooksell = input(1, title="targets")
  13. BBSet = false//input(false, title="==Correction wind==")
  14. BBbasis = 20//input(20, title="Side Left")
  15. BBdev = 20//input(20, title="Side Right")
  16. MACDset = input(false, title="==Reload Weapon==")
  17. MACD1 = 12//input(12, "Snipe Fast delay")
  18. MACD2 = 26//input(26, "Snipe Slow delay")
  19. MACD3 = 9//input(9, "Snipe Signal delay")
  20. useMAlong = input(false, title="Fast reload Weapon for filter Longs?")
  21. useMAshort = input(true, title="Fast reload Weapon for fiter Shorts?")
  22. MACDFLIP = input(false, "==Sniper Alpha flip the Targets==")
  23. isFlip = MACDFLIP
  24.  
  25.  
  26. TD = 0.0
  27. TD := close > close[TDlookbuy] ?nz(TD[1])+1:0
  28. TS = 0.0
  29. TS := close < close[TDlookbuy] ?nz(TS[1])+1:0
  30. TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
  31. TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
  32. TDcheckUP = iff(TD == 2, true, false)
  33. TDCheckDOWN = iff(TS == 2, true, false)
  34. TD2 = 0.0
  35. TD2 := close > close[TDlooksell] ?nz(TD2[1])+1:0
  36. TS2 = 0.0
  37. TS2 := close < close[TDlooksell] ?nz(TS2[1])+1:0
  38. TDUp2 = TD2 - valuewhen(TD2 < TD2[1], TD2 , 1 )
  39. TDDn2 = TS2 - valuewhen(TS2 < TS2[1], TS2 , 1 )
  40. TDcheckUP2 = iff(TD2 == 2, true, false)
  41. TDCheckDOWN2 = iff(TS2 == 2, true, false)
  42.  
  43.  
  44. [_, _, histLine] = macd(close, MACD1, MACD2, MACD3)
  45. MACDCheckDown = iff(histLine > 0 and histLine[1] > 0 and histLine[2] > 0 and histLine[3] > 0 and histLine[4] > 0, true, false)
  46. MACDCheckUp = iff(histLine < 0 and histLine[1] < 0 and histLine[2] < 0 and histLine[3] < 0 and histLine[4] < 0, true, false)
  47.  
  48. RSICalbuy = rsi(close, RSIlengthbuy)
  49. RSICalNewUpbuy = 50 + RSIDifferencebuy
  50. RSICalNewDownbuy = 50 - RSIDifferencebuy
  51. RSICheckUpbuy = iff(RSICalbuy <= RSICalNewUpbuy, true, false)
  52. RSICheckDownbuy = iff(RSICalbuy >= RSICalNewDownbuy, true, false)
  53.  
  54. RSICalsell = rsi(close, RSIlengthsell)
  55. RSICalNewUpsell = 50 + RSIDifferencesell
  56. RSICalNewDownsell = 50 - RSIDifferencesell
  57. RSICheckUpsell = iff(RSICalsell <= RSICalNewUpsell, true, false)
  58. RSICheckDownsell = iff(RSICalsell >= RSICalNewDownsell, true, false)
  59. basis = sma(close, BBbasis)
  60. dev = 2 * stdev(close, BBdev)
  61. upperBB = basis + dev
  62. lowerBB = basis - dev
  63.  
  64. BBCheckUp = iff(close > upperBB, true, false)
  65. BBCheckDown = iff(close < lowerBB, true, false)
  66. //BBCheckUp = false
  67. //BBCheckDown = false
  68.  
  69. slowma = ema(close,200)
  70. fastma = ema(close,9)
  71. price = ema(close,1)
  72.  
  73. //iff(histLine>slowma and histLine[1]>slowma and histLine[2]>slowma and histLine[3]>slowma and histLine[4]>slowma, true, false)
  74.  
  75. //BuyCheck = iff(TDcheckUP == true and MACDCheckUp == true and RSICheckUp == true and BBCheckUp == false, true, false)
  76. input_long = input(5, title="long")
  77. lb_t = linebreak (tickerid, "close", input_long)
  78. lb_close = security(lb_t, period, close)
  79. BuyCheck = security(lb_t, period, close)and iff(TDcheckUP == true and MACDCheckUp == true and RSICheckUpbuy == true and BBCheckUp == false, true, false)
  80. SellCheck = iff(TDCheckDOWN2 == true and MACDCheckDown == true and RSICheckDownsell == true and BBCheckDown == false, true, false)
  81.  
  82. //////MA Filter/////
  83.  
  84. fast = ema(close,9)
  85. slow = ema(close,200)
  86. avg = ((fast-slow) /fast) *100
  87. avg2 = ((slow-fast) /slow) *100
  88. //plot(-0.50)
  89. //plot(histLine)
  90. h = 0.24
  91. l = -0.50
  92.  
  93.  
  94.  
  95. // Long/Short Logic
  96. longfilt = fastma > slowma
  97. shortfilt = fastma < slowma
  98.  
  99. longLogic = BuyCheck ? 1 : 0
  100. shortLogic = SellCheck ? 1 : 0
  101.  
  102. if useMAlong
  103. longLogic := BuyCheck and longfilt ? 1 : 0
  104. else
  105. longLogic := BuyCheck ? 1 : 0
  106.  
  107. if useMAshort
  108. shortLogic := SellCheck and shortfilt ? 1 : 0
  109. else
  110. shortLogic := SellCheck ? 1 : 0
  111.  
  112.  
  113. //////////////////////////
  114. //* Strategy Component *//
  115. //////////////////////////
  116.  
  117. isLong = input(false, "Longs Only")
  118. isShort = input(false, "Shorts Only")
  119.  
  120.  
  121. long = longLogic
  122. short = shortLogic
  123.  
  124. if isFlip
  125. long := shortLogic
  126. short := longLogic
  127. else
  128. long := longLogic
  129. short := shortLogic
  130. if MACDFLIP
  131. long := longLogic
  132. short := shortLogic
  133. else
  134. long := shortLogic
  135. short := longLogic
  136.  
  137. if MACDFLIP and isFlip
  138. long := shortLogic
  139. short := longLogic
  140. else
  141. long := longLogic
  142. short := shortLogic
  143. if isLong
  144. long := long
  145. short := na
  146.  
  147. if isShort
  148. long := na
  149. short := short
  150.  
  151. ////////////////////////////////
  152. //======[ Signal Count ]======//
  153. ////////////////////////////////
  154.  
  155. sectionLongs = 0
  156. sectionLongs := nz(sectionLongs[1])
  157. sectionShorts = 0
  158. sectionShorts := nz(sectionShorts[1])
  159.  
  160. if long
  161. sectionLongs := sectionLongs + 1
  162. sectionShorts := 0
  163.  
  164. if short
  165. sectionLongs := 0
  166. sectionShorts := sectionShorts + 1
  167.  
  168. //////////////////////////////
  169. //======[ Pyramiding ]======//
  170. //////////////////////////////
  171.  
  172. pyrl = input(1, "Pyramiding less than") // If your count is less than this number
  173. pyre = input(0, "Pyramiding equal to") // If your count is equal to this number
  174. pyrg = input(1000000, "Pyramiding greater than") // If your count is greater than this number
  175. shortCondition = short and sectionShorts <= pyrl or short and sectionShorts >= pyrg or short and sectionShorts == pyre ? 1 : 0
  176. longCondition = long and sectionLongs <= pyrl or long and sectionLongs >= pyrg or long and sectionLongs == pyre ? 1 : 0
  177.  
  178.  
  179. ////////////////////////////////
  180. //======[ Entry Prices ]======//
  181. ////////////////////////////////
  182.  
  183. last_open_longCondition = na
  184. last_open_shortCondition = na
  185. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  186. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  187.  
  188. ////////////////////////////////////
  189. //======[ Open Order Count ]======//
  190. ////////////////////////////////////
  191.  
  192. sectionLongConditions = 0
  193. sectionLongConditions := nz(sectionLongConditions[1])
  194. sectionShortConditions = 0
  195. sectionShortConditions := nz(sectionShortConditions[1])
  196.  
  197. if longCondition
  198. sectionLongConditions := sectionLongConditions + 1
  199. sectionShortConditions := 0
  200.  
  201. if shortCondition
  202. sectionLongConditions := 0
  203. sectionShortConditions := sectionShortConditions + 1
  204.  
  205. ///////////////////////////////////////////////
  206. //======[ Position Check (long/short) ]======//
  207. ///////////////////////////////////////////////
  208.  
  209. last_longCondition = na
  210. last_shortCondition = na
  211. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  212. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  213.  
  214. in_longCondition = last_longCondition > last_shortCondition
  215. in_shortCondition = last_shortCondition > last_longCondition
  216.  
  217. /////////////////////////////////////
  218. //======[ Position Averages ]======//
  219. /////////////////////////////////////
  220.  
  221. totalLongs = 0.0
  222. totalLongs := nz(totalLongs[1])
  223. totalShorts = 0.0
  224. totalShorts := nz(totalShorts[1])
  225. averageLongs = 0.0
  226. averageLongs := nz(averageLongs[1])
  227. averageShorts = 0.0
  228. averageShorts := nz(averageShorts[1])
  229.  
  230. if longCondition
  231. totalLongs := totalLongs + last_open_longCondition
  232. totalShorts := 0.0
  233.  
  234. if shortCondition
  235. totalLongs := 0.0
  236. totalShorts := totalShorts + last_open_shortCondition
  237.  
  238. averageLongs := totalLongs / sectionLongConditions
  239. averageShorts := totalShorts / sectionShortConditions
  240.  
  241. len = input(1, minval=1, title="timeplay")
  242. plot (slowma)
  243. p(x) => fixnan(dev((x=='h' ? lowest(len) : highest(len)), len) ? na : (x=='h' ? lowest(len) : highest(len)))
  244. z(x) => p(x)[len-1]
  245. c(x) => (fixnan(dev(highest((barssince(change(p(x)) != 0)),len), len) ? na : highest((barssince(change(p(x)) != 0)),len)))+1
  246. 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)
  247. s(x) => (z(x) - valuewhen(change(z(x))!=0,z(x),2))/c(x)
  248. s_(x,y) => valuewhen(change(s(x))!=0,s(x),(y-1))
  249. lb_t2 = linebreak (tickerid, "close", 1)
  250. lb_close2 = security(lb_t2, period, close)
  251. plot(lb_close2)
  252.  
  253. line_(x,y) => s_(x,y)*d(x,y) + lb_close2
  254.  
  255.  
  256. plot(line_('h',1), style=circles, color=yellow, linewidth=1, offset=-2*len+3)
  257. /////////////////////////////
  258. //======[ Take Snipe ]======//
  259. /////////////////////////////
  260.  
  261. isTSr = input(true, "Take Sniper")
  262. //sl = input(150, "Stop Loss (%). Divided by 100 (1 = 0.01%)") / 100
  263. z1 = line_('h',1)
  264.  
  265. zh = highest(z1, 1)
  266. zl = lowest (z1, 1)
  267.  
  268. plot(highest(z1, 100), style=circles, color=red, linewidth=2, offset=-2*len+3)
  269. plot(lowest (z1, 200), style=circles, color=blue, linewidth=2, offset=-2*len+3)
  270. buy = lowest (z1, 150)
  271. sell =highest (z1, 100)
  272.  
  273. long_st= close+20 < buy
  274. short_st = crossover(sell+20, price)
  275. long_st2=close-10<buy+10 and buy> close //crossover(sell, price) and sell-30 > price
  276. short_st2 = crossover(sell-40, price)
  277.  
  278. /////////////////////////////////
  279. //======[ Trailing Stop ]======//
  280. /////////////////////////////////
  281.  
  282. isTS = input(false, "Trailing Stop")
  283. tsi = input(1300, "Activate Trailing Stop Price (%). Divided by 100 (1 = 0.01%)") / 100
  284. ts = input(400, "Trailing Stop (%). Divided by 100 (1 = 0.01%)") / 100
  285.  
  286. last_high = na
  287. last_low = na
  288. last_high_short = na
  289. last_low_short = na
  290. last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  291. 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])
  292. last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  293. 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])
  294.  
  295. 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
  296. 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
  297.  
  298. ///////////////////////////////
  299. //======[ Take Profit ]======//
  300. ///////////////////////////////
  301.  
  302. isTP = input(false, "Take Profit")
  303. tp = input(40, "Take Profit (%). Divided by 100 (1 = 0.01%)") / 100
  304. long_tp = isTP and close > averageLongs + averageLongs / 100 * tp and not longCondition
  305. short_tp = isTP and close < averageShorts - averageShorts / 100 * tp and not shortCondition
  306.  
  307. /////////////////////////////
  308. //======[ Stop Loss ]======//
  309. /////////////////////////////
  310.  
  311. isSL = input(false, "Stop Loss")
  312. sl = input(50, "Stop Loss (%). Divided by 100 (1 = 0.01%)") / 100
  313. long_sl = isSL and close < averageLongs - averageLongs / 100 * sl and longCondition == 0
  314. short_sl = isSL and close > averageShorts + averageShorts / 100 * sl and shortCondition == 0
  315.  
  316. /////////////////////////////////
  317. //======[ Close Signals ]======//
  318. /////////////////////////////////
  319.  
  320. longCloseTP = long_tp ? 1 : 0
  321. longCloseSL = long_sl ? 1 : 0
  322. longCloseTS = long_ts ? 1 : 0
  323. shortCloseTP = short_tp ? 1 : 0
  324. shortCloseSL = short_sl ? 1 : 0
  325. shortCloseTS = short_ts ? 1 : 0
  326.  
  327. longClose = long_tp or long_sl or long_ts ? 1 : 0
  328. shortClose = short_tp or short_sl or short_ts ? 1: 0
  329. /////////////////////////////TS
  330. //======[ Plot Colors ]======//
  331. ///////////////////////////////
  332.  
  333. longCloseCol = na
  334. shortCloseCol = na
  335. longCloseCol := long_tp ? purple : long_sl ? maroon : long_ts ? blue : longCloseCol[1]
  336. shortCloseCol := short_tp ? purple : short_sl ? maroon : short_ts ? blue : shortCloseCol[1]
  337. tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
  338. slColor = isSL and in_longCondition ? red : isSL and in_shortCondition ? red : white
  339.  
  340. // === INPUT BACKTEST RANGE ===
  341. FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  342. FromMonth = input(defval = 12, title = "From Month", minval = 1, maxval = 12)
  343. FromYear = input(defval = 2018, title = "From Year", minval = 2014)
  344. ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  345. ToMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)
  346. ToYear = input(defval = 9999, title = "To Year", minval = 2014)
  347. start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
  348. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
  349. inTimeFrame() => time >= start and time <= finish ? true : false
  350.  
  351. //////////////////////////////////
  352. //======[ Strategy Plots ]======//
  353. //////////////////////////////////
  354.  
  355. plot(isTS and in_longCondition ? averageLongs + averageLongs / 80 * tsi : na, "Long Trailing Activate", aqua, style=3, linewidth=2)
  356. 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)
  357. plot(isTS and in_shortCondition ? averageShorts - averageShorts/ 100 * tsi : na, "Short Trailing Activate", aqua, style=3, linewidth=2)
  358. 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)
  359. plot(isTP and in_longCondition and last_high < averageLongs + averageLongs / 80 * tp ? averageLongs + averageLongs / 80 * tp : na, "Long TP", tpColor, style=3, linewidth=2)
  360. plot(isTP and in_shortCondition and last_low > averageShorts - averageShorts / 100 * tp ? averageShorts - averageShorts / 100 * tp : na, "Short TP", tpColor, style=3, linewidth=2)
  361. 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)
  362. 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)
  363. plot(fastma, linewidth = 2, color = lime)
  364. plot(slowma, linewidth = 2, color = yellow)
  365. ///////////////////////////////
  366. //======[ Alert Plots ]======//
  367. ///////////////////////////////
  368.  
  369.  
  370. // New Signal Plots
  371. plotshape(series=longCondition, title="Long", style=shape.triangleup, location=location.belowbar, text="Full Long", color=lime, size=size.small)
  372. plotshape(series=shortCondition, title="Short", style=shape.triangledown, location=location.abovebar, text="Full Short", color=red, size=size.small)
  373. plotshape(series=longCloseTP, title="Long Close", style=shape.triangleup, location=location.belowbar, text="Take", color=lime, size=size.tiny)
  374. plotshape(series=shortCloseTP, title="Short Close", style=shape.triangledown, location=location.abovebar, text="Take", color=red, size=size.tiny)
  375. plotshape(series=longCloseTS, title="Long Close", style=shape.triangleup, location=location.belowbar, text="Trail", color=lime, size=size.tiny)
  376. plotshape(series=shortCloseTS, title="Short Close", style=shape.triangledown, location=location.abovebar, text="Trail", color=red, size=size.tiny)
  377. plotshape(series=longCloseSL, title="Long Close", style=shape.triangleup, location=location.belowbar, text="Stop", color=lime, size=size.tiny)
  378. plotshape(series=shortCloseSL, title="Short Close", style=shape.triangledown, location=location.abovebar, text="Stop", color=red, size=size.tiny)
  379. plotshape(series=long_st, title="Sniper 1", style=shape.triangleup, location=location.belowbar, text="Sniper 1 full Long", color=lime, size=size.tiny)
  380. plotshape(series=short_st, title="Sniper 1", style=shape.triangledown, location=location.abovebar, text="Sniper 1 full Short", color=red, size=size.tiny)
  381. plotshape(series=long_st2, title="Sniper 2", style=shape.triangleup, location=location.belowbar, text="Sniper 2", color=lime, size=size.tiny)
  382. plotshape(series=short_st2, title="Sniper 2", style=shape.triangledown, location=location.abovebar, text="Sniper 2", color=red, size=size.tiny)
  383.  
  384. alertcondition(condition=long_st, title="Full Long Sniper ", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
  385. alertcondition(condition=short_st, title="Full Short Sniper ", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
  386. alertcondition(condition=long_st2, title="Long 100% Sniper", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
  387. alertcondition(condition=short_st2, title="Short 100% Sniper", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
  388. alertcondition(condition=longClose, title="Long Close All", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
  389. alertcondition(condition=shortClose, title="Short Close All", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
  390. alertcondition(condition=longCondition, title="Full Long", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=99% l=10")
  391. alertcondition(condition=shortCondition, title="Full Short ", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=99% l=10")
  392.  
  393. //longalert = long_st //or long_st2 or longCondition or longClose
  394. //alertcondition(condition=longalert, title="Long", message="e=bitmex s=xbtusd delay=2 b=sell t=market q=10% l=10")
  395. //shortalert = short_st or short_st2 or shortCondition or shortClose
  396. //alertcondition(condition=shortalert, title="Short", message="e=bitmex s=xbtusd delay=2 b=buy t=market q=10% l=10")
  397. ///////////////////////////////////
  398. //======[ Reset Variables ]======//
  399. ///////////////////////////////////
  400.  
  401. if longClose or not in_longCondition
  402. averageLongs := 0
  403. totalLongs := 0.0
  404. sectionLongs := 0
  405. sectionLongConditions := 0
  406.  
  407. if shortClose or not in_shortCondition
  408. averageShorts := 0
  409. totalShorts := 0.0
  410. sectionShorts := 0
  411. sectionShortConditions := 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement