Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.25 KB | None | 0 0
  1. //@version=3
  2.  
  3. // ***** Switch the "//" from study to strategy for strategy use *****
  4. //strategy(title="Ratnieks Moving Averages", shorttitle='RMA', overlay=true)
  5. study(title="Ratnieks Moving Averages", shorttitle='RMA', overlay=true)
  6.  
  7. sourcein=close
  8. sourcein2=close[1]
  9. n = input(title = "HullMA period", type=integer, defval=16)
  10. Dclose = security(tickerid,"D",close)
  11. Dopen = security(tickerid, "D",open)
  12. src = close
  13. bg = valuewhen(close,close,0)
  14. //bgcolor(bg>Dopen?lime:red)
  15.  
  16. // ==Functions==
  17.  
  18. n1 = ema(2*sma(sourcein,round(n/2))-sma(sourcein,n),round(sqrt(n)))
  19. n2 = ema(2*sma(sourcein2,round(n/2))-sma(sourcein2,n),round(sqrt(n)))
  20.  
  21. c=n1>n2?lime:red
  22.  
  23. longLogic = crossover(n1,n2) ?1:0//and bg>Dopen
  24. shortLogic = crossunder(n1,n2) ?1:0 //and bg<Dopen
  25.  
  26. long = longLogic
  27. short = shortLogic
  28.  
  29. ////////////////////////////////
  30. //======[ Signal Count ]======//
  31. ////////////////////////////////
  32.  
  33. sectionLongs = 0
  34. sectionLongs := nz(sectionLongs[1])
  35. sectionShorts = 0
  36. sectionShorts := nz(sectionShorts[1])
  37.  
  38. if long
  39. sectionLongs := sectionLongs + 1
  40. sectionShorts := 0
  41.  
  42. if short
  43. sectionLongs := 0
  44. sectionShorts := sectionShorts + 1
  45.  
  46. //////////////////////////////
  47. //======[ Pyramiding ]======//
  48. //////////////////////////////
  49.  
  50. pyrl = input(1, "Pyramiding less than") // If your count is less than this number
  51. pyre = input(0, "Pyramiding equal to") // If your count is equal to this number
  52. pyrg = input(1000000, "Pyramiding greater than") // If your count is greater than this number
  53.  
  54. longCondition = long and sectionLongs <= pyrl or long and sectionLongs >= pyrg or long and sectionLongs == pyre ? 1 : 0
  55. shortCondition = short and sectionShorts <= pyrl or short and sectionShorts >= pyrg or short and sectionShorts == pyre ? 1 : 0
  56.  
  57. ////////////////////////////////
  58. //======[ Entry Prices ]======//
  59. ////////////////////////////////
  60.  
  61. last_open_longCondition = na
  62. last_open_shortCondition = na
  63. last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
  64. last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
  65.  
  66. ////////////////////////////////////
  67. //======[ Open Order Count ]======//
  68. ////////////////////////////////////
  69.  
  70. sectionLongConditions = 0
  71. sectionLongConditions := nz(sectionLongConditions[1])
  72. sectionShortConditions = 0
  73. sectionShortConditions := nz(sectionShortConditions[1])
  74.  
  75. if longCondition
  76. sectionLongConditions := sectionLongConditions + 1
  77. sectionShortConditions := 0
  78.  
  79. if shortCondition
  80. sectionLongConditions := 0
  81. sectionShortConditions := sectionShortConditions + 1
  82.  
  83. ///////////////////////////////////////////////
  84. //======[ Position Check (long/short) ]======//
  85. ///////////////////////////////////////////////
  86.  
  87. last_longCondition = na
  88. last_shortCondition = na
  89. last_longCondition := longCondition ? time : nz(last_longCondition[1])
  90. last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
  91.  
  92. in_longCondition = last_longCondition > last_shortCondition
  93. in_shortCondition = last_shortCondition > last_longCondition
  94.  
  95. /////////////////////////////////////
  96. //======[ Position Averages ]======//
  97. /////////////////////////////////////
  98.  
  99. totalLongs = 0.0
  100. totalLongs := nz(totalLongs[1])
  101. totalShorts = 0.0
  102. totalShorts := nz(totalShorts[1])
  103. averageLongs = 0.0
  104. averageLongs := nz(averageLongs[1])
  105. averageShorts = 0.0
  106. averageShorts := nz(averageShorts[1])
  107.  
  108. if longCondition
  109. totalLongs := totalLongs + last_open_longCondition
  110. totalShorts := 0.0
  111.  
  112. if shortCondition
  113. totalLongs := 0.0
  114. totalShorts := totalShorts + last_open_shortCondition
  115.  
  116. averageLongs := totalLongs / sectionLongConditions
  117. averageShorts := totalShorts / sectionShortConditions
  118.  
  119. /////////////////////////////////
  120. //======[ Trailing Stop ]======//
  121. /////////////////////////////////
  122.  
  123. isTS = input(false, "Trailing Stop")
  124. tsi = input(1300, "Activate Trailing Stop Price (%). Divided by 100 (1 = 0.01%)") / 100
  125. ts = input(400, "Trailing Stop (%). Divided by 100 (1 = 0.01%)") / 100
  126.  
  127. last_high = na
  128. last_low = na
  129. last_high_short = na
  130. last_low_short = na
  131. last_high := not in_longCondition ? na : in_longCondition and (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1])
  132. 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])
  133. last_low := not in_shortCondition ? na : in_shortCondition and (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1])
  134. 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])
  135.  
  136. 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
  137. 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
  138.  
  139. ///////////////////////////////
  140. //======[ Take Profit ]======//
  141. ///////////////////////////////
  142.  
  143. isTP = input(false, "Take Profit")
  144. tp = input(150, "Take Profit (%). Divided by 100 (1 = 0.01%)") / 100
  145. long_tp = isTP and close > averageLongs + averageLongs / 100 * tp and not longCondition
  146. short_tp = isTP and close < averageShorts - averageShorts / 100 * tp and not shortCondition
  147.  
  148. /////////////////////////////
  149. //======[ Stop Loss ]======//
  150. /////////////////////////////
  151.  
  152. isSL = input(false, "Stop Loss")
  153. sl = input(150, "Stop Loss (%). Divided by 100 (1 = 0.01%)") / 100
  154. long_sl = isSL and close < averageLongs - averageLongs / 100 * sl and longCondition == 0
  155. short_sl = isSL and close > averageShorts + averageShorts / 100 * sl and shortCondition == 0
  156.  
  157. /////////////////////////////////
  158. //======[ Close Signals ]======//
  159. /////////////////////////////////
  160.  
  161. longCloseTP = long_tp ? 1 : 0
  162. longCloseSL = long_sl ? 1 : 0
  163. longCloseTS = long_ts ? 1 : 0
  164. shortCloseTP = short_tp ? 1 : 0
  165. shortCloseSL = short_sl ? 1 : 0
  166. shortCloseTS = short_ts ? 1 : 0
  167.  
  168. longClose = long_tp or long_sl or long_ts ? 1 : 0
  169. shortClose = short_tp or short_sl or short_ts ? 1: 0
  170.  
  171. /////////////////////////////TS
  172. //======[ Plot Colors ]======//
  173. ///////////////////////////////
  174.  
  175. longCloseCol = na
  176. shortCloseCol = na
  177. longCloseCol := long_tp ? purple : long_sl ? maroon : long_ts ? blue : longCloseCol[1]
  178. shortCloseCol := short_tp ? purple : short_sl ? maroon : short_ts ? blue : shortCloseCol[1]
  179. tpColor = isTP and in_longCondition ? purple : isTP and in_shortCondition ? purple : white
  180. slColor = isSL and in_longCondition ? red : isSL and in_shortCondition ? red : white
  181.  
  182. // === INPUT BACKTEST RANGE ===
  183. FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  184. FromMonth = input(defval = 12, title = "From Month", minval = 1, maxval = 12)
  185. FromYear = input(defval = 2018, title = "From Year", minval = 2014)
  186. ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  187. ToMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)
  188. ToYear = input(defval = 9999, title = "To Year", minval = 2014)
  189. start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
  190. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
  191. inTimeFrame() => time >= start and time <= finish ? true : false
  192.  
  193. //////////////////////////////////
  194. //======[ Strategy Plots ]======//
  195. //////////////////////////////////
  196.  
  197. plot(isTS and in_longCondition ? averageLongs + averageLongs / 100 * tsi : na, "Long Trailing Activate", aqua, style=3, linewidth=2)
  198. plot(isTS and in_longCondition and last_high >= averageLongs + averageLongs / 100 * tsi ? last_high - last_high / 100 * ts : na, "Long Trailing", fuchsia, style=2, linewidth=3)
  199. plot(isTS and in_shortCondition ? averageShorts - averageShorts/ 100 * tsi : na, "Short Trailing Activate", aqua, style=3, linewidth=2)
  200. 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)
  201. plot(isTP and in_longCondition and last_high < averageLongs + averageLongs / 100 * tp ? averageLongs + averageLongs / 100 * tp : na, "Long TP", tpColor, style=3, linewidth=2)
  202. plot(isTP and in_shortCondition and last_low > averageShorts - averageShorts / 100 * tp ? averageShorts - averageShorts / 100 * tp : na, "Short TP", tpColor, style=3, linewidth=2)
  203. plot(isSL and in_longCondition and last_low_short > averageLongs - averageLongs / 100 * sl ? averageLongs - averageLongs / 100 * sl : na, "Long SL", slColor, style=3, linewidth=2)
  204. 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)
  205.  
  206. ///////////////////////////////
  207. //======[ Alert Plots ]======//
  208. ///////////////////////////////
  209.  
  210.  
  211. // New Signal Plots
  212. plotshape(series=longCondition, title="Long", style=shape.triangleup, location=location.belowbar, color=lime, size=size.small)
  213. plotshape(series=shortCondition, title="Short", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
  214. plotshape(series=longCloseTP, title="Long Close", style=shape.xcross, location=location.belowbar, text="Take", color=lime, size=size.tiny)
  215. plotshape(series=shortCloseTP, title="Short Close", style=shape.xcross, location=location.abovebar, text="Take", color=red, size=size.tiny)
  216. plotshape(series=longCloseTS, title="Long Close", style=shape.xcross, location=location.belowbar, text="Trail", color=lime, size=size.tiny)
  217. plotshape(series=shortCloseTS, title="Short Close", style=shape.xcross, location=location.belowbar, text="Trail", color=red, size=size.tiny)
  218. plotshape(series=longCloseSL, title="Long Close", style=shape.xcross, location=location.belowbar, text="Stop", color=lime, size=size.tiny)
  219. plotshape(series=shortCloseSL, title="Short Close", style=shape.xcross, location=location.belowbar, text="Stop", color=red, size=size.tiny)
  220.  
  221. // Plots
  222. n1e=plot(n1, color=c, linewidth=1)
  223. n2e=plot(n2, color=c, linewidth=1)
  224. fill(n1e, n2e, color=c, transp=75)
  225.  
  226.  
  227. plotshape(long, style=shape.labelup, location=location.belowbar, size=size.tiny, color=lime)
  228. plotshape(short, style=shape.labeldown, location=location.abovebar, size=size.tiny, color=red)
  229. //bgcolor(in_longCondition?lime:red)
  230.  
  231. //== Alerts ==
  232. //**Change strategy to study for alerts**
  233.  
  234. alertcondition(longCondition, "LongMex", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd b=buy t=post p=-0.5 q=10% l=10 | e=bitmex s=xbtusd t=limit b=buy q=100% ts=-10 ro=1")
  235. alertcondition(shortCondition, "ShortMex", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd b=sell t=post p=0.5 q=10% l=10 | e=bitmex s=xbtusd t=limit b=sell q=100% ts=10 ro=1")
  236. //alertcondition(in_longCondition, "LongMexTS", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd t=limit b=buy q=100% ts=-10 ro=1")
  237. //alertcondition(in_shortCondition, "ShortMexTS", "e=bitmex s=xbtusd c=order t=open | e=bitmex s=xbtusd t=limit b=sell q=100% ts=10 ro=1")
  238. //== Strategy ==
  239. //** Grey these out for study use**
  240.  
  241. //***** Remove the "//" on the strategy.entry(s) for strategy use *****
  242. //strategy.entry("Long", strategy.long, limit = close, when = long)
  243. //strategy.entry("Short",strategy.short, limit = close, when = short)
  244. //strategy.close("Long", when = longCloseTS)
  245. //strategy.close("Short", when = shortCloseTS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement