Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. //@version=3
  2. strategy(title="SFP 30m", shorttitle="SFP 30m", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.15)
  3.  
  4. FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
  5. FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  6. FromYear = input(defval = 2019, title = "From Year", minval = 2015)
  7. ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
  8. ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  9. ToYear = input(defval = 9999, title = "To Year", minval = 2017)
  10.  
  11. start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
  12. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
  13. testPeriod() => time >= start and time <= finish ? true : false
  14.  
  15.  
  16. //HA plot
  17. handleHA = heikinashi(tickerid)
  18. openHA = security(handleHA, period, open)
  19. highHA = security(handleHA, period, high)
  20. lowHA = security(handleHA, period, low)
  21. closeHA = security(handleHA, period, close)
  22. src = openHA
  23.  
  24. //EMA trend
  25. exponential = input(true, title="Exponential MA")
  26. MA1 = input(33, title="MA 1")
  27. MA2 = input(67, title="MA 2")
  28. ma21 = exponential ? ema(src, MA1) : sma(src, MA1)
  29. ma55 = exponential ? ema(src, MA2) : sma(src, MA2)
  30. fastemalength = input(20, title="fastema") //----------------------------------------------------------
  31. fastema = ema(src, fastemalength)//-------------------------------------------------
  32.  
  33. //BANDS---------------------------------------------------------------------------------------------------------------------------------
  34. mult1 = input(50, minval=0.001, maxval=50, step=1, title="Trailing stop band length" )
  35. mult2 = input(3, minval=0.001, maxval=50, title="2nd Stdev Mult")
  36. mult3 = input(1, minval=0.001, maxval=50, title="3rd Stdev Mult")
  37. basis = fastema
  38. dev1 = mult1 * stdev(src, fastemalength)
  39. dev2 = mult2 * stdev(src, fastemalength)
  40. dev3 = mult3 * stdev(src, fastemalength)
  41. upper1 = basis + dev1
  42. lower1 = basis - dev1
  43. upper2 = basis + dev2
  44. lower2 = basis - dev2
  45. upper3 = basis + dev3
  46. lower3 = basis - dev3
  47.  
  48. ////
  49. //RSI
  50. rsiLength = input(2, minval=1, title="RSILength")
  51. up = rma(max(change(src), 0), rsiLength)
  52. down = rma(-min(change(src), 0), rsiLength)
  53. rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
  54. rsin= input(13)
  55. sn = 100 - rsin
  56. ln = 0 + rsin
  57.  
  58. /////trendmaster
  59. Factor=input(16, minval=1,maxval = 140)
  60. Pd=input(1, minval=1,maxval = 140)
  61. Up=(closeHA)-(Factor*atr(Pd))
  62. Dn=(closeHA)+(Factor*atr(Pd))
  63. TrendUp=na
  64. TrendUp:= closeHA[1]>TrendUp[1]? max(Up,TrendUp[1]) : Up
  65. TrendDown=na
  66. TrendDown:= closeHA[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
  67. Trend=na
  68. Trend := closeHA > TrendDown[1] ? 1: closeHA< TrendUp[1]? -1: nz(Trend[1],1)
  69. Tsl = Trend==1? TrendUp: TrendDown
  70. linecolor = Trend == 1 ? green : red
  71. plot(Tsl, color = linecolor , style = line , linewidth = 2,title = "SuperTrend")
  72.  
  73. //Stop runs
  74. lookbacklength = input(20, minval=1, step=5, title="stop run lookback length")
  75.  
  76. //Volumethreshold
  77. volumethreshold = input(1, minval=1, maxval=3, step=0.05, title="candle vol multiplier")
  78.  
  79. //
  80. //Long wick
  81. redcandle = closeHA<openHA
  82. greencandle = closeHA>openHA
  83. topWickLen = redcandle ? highHA-openHA:highHA-closeHA
  84. botWickLen = greencandle ? openHA-lowHA:closeHA-lowHA
  85. bodyLen = redcandle ? openHA-closeHA:closeHA-openHA
  86.  
  87. //HMA
  88. f_hma(_src, _length)=>
  89. //HMA = WMA(2*WMA(n/2) − WMA(n)),sqrt(n))
  90. _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
  91. f_hma2(_src, _length)=>
  92. //HMA = WMA(2*WMA(n/2) − WMA(n)),sqrt(n))
  93. _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))
  94.  
  95. hma1length = input(14)
  96. hma2length = input(27)
  97.  
  98. hma1 = f_hma(close, hma1length)
  99. hma2 = f_hma2(close, hma2length)
  100.  
  101. // Put your long and short rules here
  102. longCondition = close>Tsl
  103. //low<lowest(low, lookbacklength)[1] and close>lowest(close[1], lookbacklength) and not na(close[lookbacklength]) and close>Tsl
  104. //close>lowest(low, lookbacklength) and low<lowest(low, lookbacklength)[1] and volume>volumethreshold*max(volume[1], volume[lookbacklength]) and close>lowest(close[1], lookbacklength) and not na(close[lookbacklength])
  105. //Trend == 1 and closeHA>openHA and closeHA>highHA[1]
  106. //and Trend[1] == -1 ? Trend: na
  107. //botWickLen>bodyLen and closeHA>openHA and rsi > sn
  108. //and closeHA>openHA and closeHA>highHA[1]
  109. //closeHA>openHA and closeHA>highHA[1] and volema1>volema2 and ((fastema>ma21 and ma21>ma55) or (fastema<ma21 and ma21<ma55))
  110. shortCondition = close<Tsl
  111. //high>highest(high, lookbacklength)[1] and close<highest(close[1], lookbacklength) and not na(close[lookbacklength]) and close<Tsl
  112. //close<highest(high, lookbacklength) and high>highest(high, lookbacklength)[1] and volume>volumethreshold*max(volume[1], volume[lookbacklength]) and close<highest(close[1], lookbacklength) and not na(close[lookbacklength])
  113. //Trend == -1 and closeHA<openHA and closeHA<lowHA[1]
  114. //and Trend[1] == 1 ? Trend: na
  115. //topWickLen>bodyLen and closeHA<openHA and rsi < ln
  116. //and closeHA<openHA and closeHA<lowHA[1]
  117. //closeHA<openHA and closeHA<lowHA[1] and volema1<volema2 and ((fastema<ma21 and ma21<ma55) or (fastema>ma21 and ma21>ma55))
  118.  
  119. //hold time (in bars)
  120. holdbarslen = input(40, minval=1, step=5, title="Hold time in number of bars: ")
  121. holdbars = barssince(longCondition or shortCondition)>holdbarslen
  122.  
  123.  
  124. longClose = crossunder(close, Tsl)
  125. //crossover(src, upper2) or closeHA<lower1 or crossunder(closeHA, Tsl) or holdbars
  126. shortClose = crossover(close, Tsl)
  127. //crossunder(src, lower2) or closeHA>upper1 or crossover(closeHA, Tsl) or holdbars
  128.  
  129. if testPeriod()
  130. strategy.entry("Long", 1, when=longCondition)
  131. strategy.entry("Short", 0, when=shortCondition)
  132. strategy.close("Long", when=longClose)
  133. strategy.close("Short", when=shortClose)
  134.  
  135. //SL
  136.  
  137. stopLow = na
  138. stopHigh = na
  139. if (longCondition and strategy.position_size <= 0) or (shortCondition and strategy.position_size >= 0)
  140. stopLow := low
  141. stopHigh := high
  142. else
  143. if strategy.position_size == 0
  144. stopLow := na
  145. stopHigh := na
  146. else
  147. stopLow := stopLow[1]
  148. stopHigh := stopHigh[1]
  149.  
  150. slPercent = input(0, title='Stop Loss %', type=float, step=0.1) / 100
  151. tsPercent = input(0, title='Trailing Stop Loss %', type=float, step=0.1) / 100
  152.  
  153. stopLevelLong = strategy.position_size > 0 ? strategy.position_avg_price * (1 - slPercent) : na
  154. stopLevelShort = strategy.position_size < 0 ? strategy.position_avg_price * (1 + slPercent) : na
  155.  
  156. trailingStopLevelLong = na
  157. trailingStopLevelShort = na
  158. trailingStopLevelLong := strategy.position_size > 0 ? (na(trailingStopLevelLong[1]) ? high * (1 - tsPercent) : max(high * (1 - tsPercent), trailingStopLevelLong[1])) : na
  159. trailingStopLevelShort := strategy.position_size < 0 ? (na(trailingStopLevelShort[1]) ? low * (1 + tsPercent) : min(low * (1 + tsPercent), trailingStopLevelShort[1])) : na
  160.  
  161. plotshape(slPercent > 0 ? stopLevelLong : na, location=location.absolute, style=shape.xcross, color=red)
  162. plotshape(slPercent > 0 ? stopLevelShort : na, location=location.absolute, style=shape.xcross, color=red)
  163. //plotshape(tsPercent > 0 ? trailingStopLevelLong : na, location=location.absolute, style=shape.xcross, color=blue)
  164. //plotshape(tsPercent > 0 ? trailingStopLevelShort : na, location=location.absolute, style=shape.xcross, color=blue)
  165. //plotshape(strategy.position_avg_price, location=location.absolute, style=shape.circle, color=white)
  166.  
  167. dynStop = input(false, title='Dynamic stop', type=bool)
  168. if dynStop and not (longCondition or shortCondition or longClose or shortClose)
  169. strategy.exit("Dynamic Stop Loss (Long)", "Long", stop=stopLow)
  170. strategy.exit("Dynamic Stop Loss (Short)", "Short", stop=stopHigh)
  171. else
  172. strategy.cancel("Dynamic Stop Loss (Long)")
  173. strategy.cancel("Dynamic Stop Loss (Short)")
  174.  
  175. if slPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
  176. strategy.exit("Stop Loss (Long)", "Long", stop=stopLevelLong)
  177. strategy.exit("Stop Loss (Short)", "Short", stop=stopLevelShort)
  178. else
  179. strategy.cancel("Stop Loss (Long)")
  180. strategy.cancel("Stop Loss (Short)")
  181.  
  182. if tsPercent > 0 and not (longCondition or shortCondition or longClose or shortClose)
  183. strategy.exit("Trailing Stop Loss (Long)", "Long", stop=trailingStopLevelLong)
  184. strategy.exit("Trailing Stop Loss (Short)", "Short", stop=trailingStopLevelShort)
  185. else
  186. strategy.cancel("Trailing Stop Loss (Long)")
  187. strategy.cancel("Trailing Stop Loss (Short)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement