Advertisement
namile

Untitled

Jul 4th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. //@version=2
  2. strategy("Kalman Smoother м1",overlay=true,default_qty_type=strategy.percent_of_equity ,default_qty_value=100)
  3.  
  4. ///// Backtest Start Date /////
  5. // startDate = input(title="Start Date", type=input.integer, defval=3, minval=1, maxval=31)
  6. // startMonth = input(title="Start Month", type=input.integer, defval=3, minval=1, maxval=12)
  7. // startYear = input(title="Start Year", type=input.integer, defval=2019, minval=1800, maxval=2100)
  8. // Input
  9. fastMAlen = input(21, minval=1, title="MACD fast moving average")
  10. slowMAlen=input(100,minval=1, title="MACD slow moving average")
  11. signalMACDlen = input(21,minval=1, title="MACD signal line moving average")
  12. StochLength = input(100, minval=1, title="Stochastic Length")
  13. switch=input(true, title="Enable MACD Bar Color?")
  14.  
  15. //HISTORICAL VOLATILITY
  16. histovol_placeholder = input(true, title="==== Historical Vol ====")
  17. lengthHV = input(20, minval=1)
  18. hvValue = input(20, title="When HV is below")
  19. // annual = 252
  20. // per = timeframe.isintraday or timeframe.isdaily and timeframe.multiplier == 1 ? 1 : 7
  21. hv = 100*stdev(log(close / close[1]), lengthHV) * sqrt(252/7)
  22.  
  23.  
  24.  
  25. len = input(7, minval=1, title="Length")
  26. src = input(close, title="Source")
  27. smma = 0.0
  28. smma := na(smma[1]) ? sma(src, len) : (smma[1] * (len - 1) + src) / len
  29. // plot(sma(close,9), color=white)
  30. plot(ema(close,21), color=yellow)
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. // slow its lower
  38. Gain = input(5,type=float)
  39. // signal
  40. Gain2 = input(50,type=float)
  41.  
  42. //
  43. // src = input(close)
  44. dk = src - nz(kf[1],src)
  45. smooth = nz(kf[1],src)+dk*sqrt((Gain/10000)*2)
  46. velo = nz(velo[1],0) + ((Gain/10000)*dk)
  47. kf = smooth+velo
  48. //
  49. plot(kf,color=red,transp=0)
  50.  
  51.  
  52. //
  53.  
  54. src2 = input(close)
  55. dk2 = src2 - nz(kf2[1],src2)
  56. smooth2 = nz(kf2[1],src)+dk2*sqrt((Gain2/10000)*2)
  57. velo2 = nz(velo2[1],0) + ((Gain2/10000)*dk2)
  58. kf2 = smooth2+velo2
  59.  
  60. plot(kf2,color=purple,transp=0)
  61. // plot(kf2,color=purple,transp=0)
  62. // MACD Calculation
  63. MACD = ema(kf, fastMAlen) - ema(kf, slowMAlen)
  64. signalMACD = ema(MACD, signalMACDlen)
  65. delta = MACD - signalMACD
  66. fastMA = ema(kf,fastMAlen)
  67. slowMA = ema(kf,slowMAlen)
  68.  
  69. // ————— Williams %R —————
  70. _nWIL_Len = input(defval=14, title="    ▪ Length", minval=1)
  71. wil_up = highest(_nWIL_Len)
  72. wil_lo = lowest(_nWIL_Len)
  73. will = 100 - (100 * (close - wil_up) / (wil_up - wil_lo) * -1)
  74.  
  75. // plot(will,color=yellow,transp=0)
  76. // longCondition = crossover(ema(close,6),kf)
  77. dev = 2.5* stdev(hlc3, 10)
  78. longCondition = crossover(ema(close,6)+2*syminfo.mintick,kf)
  79. shortCondition = crossover(kf,ema(close,6)-2*syminfo.mintick)
  80. // and rsi(close,14)[1]<rsi(close,14)[0]
  81. // if (longCondition and rsi(close,14)[1]<rsi(close,14)[0] and atr(14)[1]<atr(14)[0])
  82.  
  83. stop_level_l = strategy.position_avg_price * (1 - 0.001)
  84. stop_level_s = strategy.position_avg_price * (1 + 0.001)
  85.  
  86. plot(kf2+dev , title="dev", color=green, linewidth=1)
  87. plot(kf2 -dev , title="dev", color=yellow, linewidth=1)
  88.  
  89. afterStartDate = (time >= timestamp(syminfo.timezone, 2019, 02, 30, 0, 0))
  90.  
  91. if (longCondition and afterStartDate )
  92. // strategy.entry("My Long Entry Id", strategy.long,stop=stop_level_l)
  93. strategy.entry("My Long Entry Id", strategy.long)
  94.  
  95. // shortCondition = crossunder(sma(close, 14), sma(close, 28))
  96. // shortCondition = crossunder(ema(close,6),kf)
  97. // shortCondition = crossover(kf,sma(close,9))
  98. // shortCondition = crossover(ema(close,9),sma(close,21))
  99.  
  100. // and rsi(close,14)[1]>rsi(close,14)[0]
  101. // if (shortCondition and rsi(close,14)[1]>rsi(close,14)[0] and atr(14)[1]<atr(14)[0])
  102. // if (crossover(kf2,ema(close,21)) and afterStartDate )
  103. // strategy.entry("My Short Entry Id", strategy.short,stop=stop_level_s )
  104. // strategy.entry("My Short Entry Id", strategy.short)
  105. // strategy.close_all()
  106. if (shortCondition and afterStartDate )
  107. // strategy.entry("My Short Entry Id", strategy.short,stop=stop_level_s )
  108. // strategy.entry("My Short Entry Id", strategy.short)
  109. strategy.close_all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement