SHARE
TWEET

Untitled

a guest Oct 18th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@version=4
  2. strategy("3 Candle Compression Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
  3.  
  4. // Pull prices from 4h timeframe
  5.  
  6. highprice = security("BITMEX:XBTUSD", '240', high)
  7. lowprice = security("BITMEX:XBTUSD", '240', low)
  8. openprice = security("BITMEX:XBTUSD", '240', open)
  9. closeprice = security("BITMEX:XBTUSD", '240', close)
  10.  
  11. highpricep = security("BITMEX:XBTUSD", '240', high[1])
  12. lowpricep = security("BITMEX:XBTUSD", '240', low[1])
  13. openpricep = security("BITMEX:XBTUSD", '240', open[1])
  14. closepricep = security("BITMEX:XBTUSD", '240', close[1])
  15.  
  16. // Define range
  17.  
  18. insidecandle = highprice < highprice[1] and lowprice > lowprice[1]
  19.  
  20. insidecandlep = highpricep < highpricep[1] and lowpricep > lowpricep[1]
  21.  
  22. rangecompression = insidecandle and insidecandlep
  23.  
  24. //Time condition
  25.  
  26. isMon = dayofweek(time('D')) == dayofweek.monday
  27. isTue = dayofweek(time('D')) == dayofweek.tuesday
  28. isWed = dayofweek(time('D')) == dayofweek.wednesday
  29. isThu = dayofweek(time('D')) == dayofweek.thursday
  30. isFri = dayofweek(time('D')) == dayofweek.friday
  31.  
  32. tradingsession = isMon or isTue or isWed or isThu or isFri
  33.  
  34. // Long and Short structure
  35.  
  36. longhigh_cond = highprice
  37.  
  38. longhighprice = 0.0
  39. longhighprice := rangecompression ? longhigh_cond : longhighprice[1]
  40.  
  41. shortlow_cond = lowprice
  42.  
  43. shortlowprice = 0.0
  44. shortlowprice := rangecompression ? shortlow_cond : shortlowprice[1]
  45.  
  46. // Long/Short conditions
  47.  
  48. breakout = crossover(close, longhighprice)
  49. breakdown = crossunder(close, shortlowprice)
  50.  
  51. bullprice = close > longhighprice
  52. bearprice = close < shortlowprice
  53.  
  54. paintgreen = bullprice
  55. paintyellow = longhighprice > close and close > shortlowprice
  56. paintred = bearprice
  57.  
  58. plot(longhighprice, color = color.green, linewidth=3)
  59. plot(shortlowprice, color = color.red, linewidth=3)
  60.  
  61. barcolor(paintgreen ? color.green : na)
  62. barcolor(paintyellow ? color.yellow : na)
  63. barcolor(paintred ? color.red : na)
  64.  
  65. ///////// STOP LOSS /////////
  66.  
  67. // Pull prices from 1D timeframe
  68.  
  69. highpricesl = security("BITMEX:XBTUSD", '1440', high)
  70. lowpricesl = security("BITMEX:XBTUSD", '1440', low)
  71. openpricesl = security("BITMEX:XBTUSD", '1440', open)
  72. closepricesl = security("BITMEX:XBTUSD", '1440', close)
  73.  
  74. highpricepsl = security("BITMEX:XBTUSD", '1440', high[1])
  75. lowpricepsl = security("BITMEX:XBTUSD", '1440', low[1])
  76. openpricepsl = security("BITMEX:XBTUSD", '1440', open[1])
  77. closepricepsl = security("BITMEX:XBTUSD", '1440', close[1])
  78.  
  79. // Define range for stoploss
  80.  
  81. insidecandlesl = highpricesl < highpricesl[1] and lowpricesl > lowpricesl[1]
  82.  
  83. insidecandlepsl = highpricepsl < highpricepsl[1] and lowpricepsl > lowpricepsl[1]
  84.  
  85. rangecompressionsl = insidecandlesl and insidecandlepsl
  86.  
  87. // Long stoploss and short stoploss
  88.  
  89. longhigh_condsl = highpricesl
  90.  
  91. longhighpricesl = 0.0
  92. longhighpricesl := insidecandlesl ? longhigh_condsl : longhighpricesl[1]
  93.  
  94. shortlow_condsl = lowpricesl
  95.  
  96. shortlowpricesl = 0.0
  97. shortlowpricesl := insidecandlesl ? shortlow_condsl : shortlowpricesl[1]
  98.  
  99. //plotshape(bullpricesl, color=color.red, style=shape.xcross, location=location.abovebar, size=size.small)
  100. //plotshape(bearpricesl, color=color.green, style=shape.arrowup, location=location.belowbar, size=size.large)
  101.  
  102. // CREATE ORDERS
  103.  
  104. // Long order
  105.  
  106. strategy.entry("long", strategy.long, when = breakout and tradingsession)
  107.  
  108. // Stoploss Long
  109.  
  110. breakoutsl = crossover(close, longhighpricesl)
  111. bullpricesl = close > longhighprice and crossunder(close, shortlowpricesl) or close < shortlowprice
  112.  
  113. strategy.close("long", when = bullpricesl)
  114.  
  115. // Short order
  116.    
  117. strategy.entry("short", strategy.short, when = breakdown and tradingsession)
  118.  
  119. // Stoploss short
  120.  
  121. breakdownsl = crossover(close, shortlowpricesl)
  122. bearpricesl = close < shortlowprice and crossover(close, longhighpricesl) or close > longhighprice
  123.    
  124. strategy.close("short", when = bearpricesl)
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top