Advertisement
Drzhy

OCC

Jan 5th, 2020
3,493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // === INPUTS ===
  2. useRes      = input(defval = true, title = "Use Alternate Resolution?")
  3. intRes      = input(defval = 3,    title = "Multiplier for Alernate Resolution")
  4. stratRes    = ismonthly? tostring(interval*intRes,"###M") : isweekly? tostring(interval*intRes,"###W") : isdaily?   tostring(interval*intRes,"###D") : isintraday ? tostring(interval*intRes,"####") : '60'
  5. basisType   = input(defval = "SMMA", title = "MA Type: ", options=["SMA", "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMMA", "HullMA", "LSMA", "ALMA", "SSMA", "TMA"])
  6. basisLen    = input(defval = 8, title = "MA Period", minval = 1)
  7. offsetSigma = input(defval = 6, title = "Offset for LSMA / Sigma for ALMA", minval = 0)
  8. offsetALMA  = input(defval = 0.85, title = "Offset for ALMA", minval = 0, step = 0.01)
  9. scolor      = input(false, title="Show coloured Bars to indicate Trend?")
  10. delayOffset = input(defval = 0, title = "Delay Open/Close MA (Forces Non-Repainting)", minval = 0, step = 1)
  11. tradeType   = input("BOTH", title="What trades should be taken : ", options=["LONG", "SHORT", "BOTH", "NONE"])
  12. // === /INPUTS ===
  13.  
  14. // Constants colours that include fully non-transparent option.
  15. green100 = '#008000FF'
  16. lime100  = '#00FF00FF'
  17. red100   = '#FF0000FF'
  18. blue100  = '#0000FFFF'
  19. aqua100  = '#00FFFFFF'
  20. darkred100 = '#8B0000FF'
  21. gray100 = '#808080FF'
  22.  
  23. // === BASE FUNCTIONS ===
  24. // Returns MA input selection variant, default to SMA if blank or typo.
  25. variant(type, src, len, offSig, offALMA) =
  26.     v1 = sma(src, len)                                                 // Simple
  27.     v2 = ema(src, len)                                                 // Exponential
  28.     v3 = 2 * v2 - ema(v2, len)                                         // Double Exponential
  29.     v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len)               // Triple Exponential
  30.     v5 = wma(src, len)                                                 // Weighted
  31.     v6 = vwma(src, len)                                                // Volume Weighted
  32.     v7 = 0.0
  33.     v7 = na(v7[1]) ? sma(src, len) : (v7[1] * (len - 1) + src) / len     // Smoothed
  34.     v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))    // Hull
  35.     v9 = linreg(src, len, offSig)                                      // Least Squares
  36.     v10 = alma(src, len, offALMA, offSig)                               // Arnaud Legoux
  37.     v11 = sma(v1,len)                                                  // Triangular (extreme smooth)
  38.     // SuperSmoother filter
  39.     // © 2013  John F. Ehlers
  40.     a1 = exp(-1.414*3.14159 / len)
  41.     b1 = 2*a1*cos(1.414*3.14159 / len)
  42.     c2 = b1
  43.     c3 = (-a1)*a1
  44.     c1 = 1 - c2 - c3
  45.     v12 = 0.0
  46.     v12 = c1*(src + nz(src[1])) / 2 + c2*nz(v12[1]) + c3*nz(v12[2])
  47.     type=="EMA"?v2 : type=="DEMA"?v3 : type=="TEMA"?v4 : type=="WMA"?v5 : type=="VWMA"?v6 : type=="SMMA"?v7 : type=="HullMA"?v8 : type=="LSMA"?v9 : type=="ALMA"?v10 : type=="TMA"?v11: type=="SSMA"?v12: v1
  48.  
  49. // security wrapper for repeat calls
  50. reso(exp, use, res) = use ? security(tickerid, res, exp, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on) : exp
  51.  
  52. // === /BASE FUNCTIONS ===
  53.  
  54. // === SERIES SETUP ===
  55. closeSeries     = variant(basisType, close[delayOffset], basisLen, offsetSigma, offsetALMA)
  56. openSeries      = variant(basisType, open[delayOffset], basisLen, offsetSigma, offsetALMA)
  57. // === /SERIES ===
  58.  
  59. // === PLOTTING ===
  60.  
  61. // Get Alternate resolution Series if selected.
  62. closeSeriesAlt = reso(closeSeries, useRes, stratRes)
  63. openSeriesAlt = reso(openSeries, useRes, stratRes)
  64. //
  65. trendColour = (closeSeriesAlt > openSeriesAlt) ? green : red
  66. bcolour     = (closeSeries > openSeriesAlt) ? lime100 : red100
  67. barcolor(scolor?bcolour:na, title = "Bar Colours")
  68. closeP=plot(closeSeriesAlt, title = "Close Series", color = trendColour, linewidth = 2, style = line, transp = 20)
  69. openP=plot(openSeriesAlt, title = "Open Series", color = trendColour, linewidth = 2, style = line, transp = 20)
  70. fill(closeP,openP,color=trendColour,transp=80)
  71.  
  72. // === /PLOTTING ===
  73. //
  74.  
  75. //
  76. // === ALERT conditions
  77. xlong       = crossover(closeSeriesAlt, openSeriesAlt)
  78. xshort      = crossunder(closeSeriesAlt, openSeriesAlt)
  79. longCond    = xlong   // alternative: longCond[1]? false : (xlong or xlong[1]) and close>closeSeriesAlt and close>=open
  80. shortCond   = xshort  // alternative: shortCond[1]? false : (xshort or xshort[1]) and close<closeSeriesAlt and close<=open
  81. // === /ALERT conditions.
  82.  
  83. // === STRATEGY ===
  84. // stop loss
  85. slPoints    = input(defval = 0, title = "Initial Stop Loss Points (zero to disable)", minval = 0)
  86. tpPoints    = input(defval = 0, title = "Initial Target Profit Points (zero for disable)", minval = 0)
  87. // Include bar limiting algorithm
  88. ebar            = input(defval = 10000, title="Number of Bars for Back Testing", minval=0)
  89. dummy           = input(false,         title="- SET to ZERO for Daily or Longer Timeframes" )
  90. //
  91. // Calculate how many mars since last bar
  92. tdays       = (timenow-time)/60000.0   // number of minutes since last bar
  93. tdays       = ismonthly? tdays/1440.0/5.0/4.3/interval : isweekly? tdays/1440.0/5.0/interval : isdaily? tdays/1440.0/interval : tdays/interval // number of bars since last bar
  94. //
  95. //set up exit parameters
  96. TP = tpPoints>0?tpPoints:na
  97. SL = slPoints>0?slPoints:na
  98.  
  99. // Make sure we are within the bar range, Set up entries and exit conditions
  100. if ((ebar==0 and tdays<=ebar) and tradeType!="NONE")
  101.     strategy.entry("long", strategy.long, when=longCond==true and tradeType!="SHORT")
  102.     strategy.entry("short", strategy.short, when=shortCond==true and tradeType!="LONG")
  103.     strategy.close("long", when = shortCond==true and tradeType=="LONG")
  104.     strategy.close("short", when = longCond==true and tradeType=="SHORT")
  105.     strategy.exit("XL", from_entry = "long", profit = TP, loss = SL)
  106.     strategy.exit("XS", from_entry = "short", profit = TP, loss = SL)
  107.  
  108. // === /STRATEGY ===
  109. // eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement