Advertisement
JustUncleL

Open Close Cross Strategy R5-BO

Jan 23rd, 2017
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. //@version=2
  2. //
  3.  
  4. strategy(title = "Open Close Cross Strategy R5-BO revised by JustUncleL", shorttitle = "OCC Strategy R5-BO", overlay = true,
  5. pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, calc_on_every_tick=false)
  6.  
  7. //
  8. // Revision: 5-BO
  9. // Original Author: @JayRogers
  10. // Revision Author: JustUncleL revisions 3, 4, 5
  11. //
  12. // *** USE AT YOUR OWN RISK ***
  13. // - There are drawing/painting issues in pinescript when working across resolutions/timeframes that I simply
  14. // cannot fix here.. I will not be putting any further effort into developing this until such a time when
  15. // workarounds become available.
  16. // NOTE: Re-painting has not been observed with default settings and seems OK up to Alternate multiplier of 5.
  17. //
  18. // R5 Changes by JustUncleL
  19. // - Corrected cross over calculations, sometimes gave false signals.
  20. // - Corrected Alternate Time calculation to allow for Daily,Weekly and Monthly charts.
  21. // - Open Public release.
  22. // R4 Changes By JustUncleL
  23. // - Change the way the Alternate resolution in selected, use a Multiplier of the base Time Frame instead,
  24. // this makes it easy to switch between base time frames.
  25. // - Added TMA and SSMA moving average options. But DEMA is still giving the best results.
  26. // - Using "calc_on_every_tick=false" ensures results between backtesting and real time are similar.
  27. // - Added Option to Disable the coloring of the bars.
  28. // - Updated default settings.
  29. //
  30. // R3 Changes by JustUncleL:
  31. // - Returned a simplified version of the open/close channel, it shows strength of current trend.
  32. // - Added Target Profit Option.
  33. // - Added option to reduce the number of historical bars, overcomes the too many trades limit error.
  34. // - Simplified the strategy code.
  35. // - Removed Trailing Stop option, not required and in my opion does not work well in Trading View,
  36. // it also gives false and unrealistic performance results in backtesting.
  37. //
  38. // R2 Changes:
  39. // - Simplified and cleaned up plotting, now just shows a Moving Average derived from the average of open/close.
  40. // - Tried very hard to alleviate painting issues caused by referencing alternate resolution..
  41. //
  42. // Description:
  43. // - Strategy based around Open-Close Crossovers.
  44. // Setup:
  45. // - I have generally found that setting the strategy resolution to 3-4x that of the chart you are viewing
  46. // tends to yield the best results, regardless of which MA option you may choose (if any) BUT can cause
  47. // a lot of false positives - be aware of this
  48. // - Don't aim for perfection. Just aim to get a reasonably snug fit with the O-C band, with good runs of
  49. // green and red.
  50. // - Option to either use basic open and close series data, or pick your poison with a wide array of MA types.
  51. // - Optional trailing stop for damage mitigation if desired (can be toggled on/off)
  52. // - Positions get taken automagically following a crossover - which is why it's better to set the resolution
  53. // of the script greater than that of your chart, so that the trades get taken sooner rather than later.
  54. // - If you make use of the stops/trailing stops, be sure to take your time tweaking the values. Cutting it too fine
  55. // will cost you profits but keep you safer, while letting them loose could lead to more drawdown than you
  56. // can handle.
  57.  
  58. // === INPUTS ===
  59. useRes = input(defval = true, title = "Use Alternate Resolution?")
  60. intRes = input(defval = 3, title = "Multiplier for Alernate Resolution")
  61. stratRes = ismonthly? tostring(interval*intRes,"###M") : isweekly? tostring(interval*intRes,"###W") : isdaily? tostring(interval*intRes,"###D") : isintraday ? tostring(interval*intRes,"####") : '60'
  62. basisType = input(defval = "SMMA", title = "MA Type: SMA, EMA, DEMA, TEMA, WMA, VWMA, SMMA, HullMA, LSMA, ALMA, SSMA, TMA (case sensitive)", type = string)
  63. basisLen = input(defval = 8, title = "MA Period", minval = 1)
  64. offsetSigma = input(defval = 6, title = "Offset for LSMA / Sigma for ALMA", minval = 0)
  65. offsetALMA = input(defval = 0.85, title = "Offset for ALMA", minval = 0, step = 0.01)
  66. scolor = input(false, title="Show coloured Bars to indicate Trend?")
  67. delayOffset = input(defval = 0, title = "Delay ( Offset Series by X Bars )", minval = 0, step = 1)
  68.  
  69. // === /INPUTS ===
  70.  
  71. // === BASE FUNCTIONS ===
  72. // Returns MA input selection variant, default to SMA if blank or typo.
  73. variant(type, src, len, offSig, offALMA) =>
  74. v1 = sma(src, len) // Simple
  75. v2 = ema(src, len) // Exponential
  76. v3 = 2 * v2 - ema(v2, len) // Double Exponential
  77. v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  78. v5 = wma(src, len) // Weighted
  79. v6 = vwma(src, len) // Volume Weighted
  80. v7 = na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
  81. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  82. v9 = linreg(src, len, offSig) // Least Squares
  83. v10 = alma(src, len, offALMA, offSig) // Arnaud Legoux
  84. v11 = sma(v1,len) // Triangular (extreme smooth)
  85. // SuperSmoother filter
  86. // © 2013 John F. Ehlers
  87. a1 = exp(-1.414*3.14159 / len)
  88. b1 = 2*a1*cos(1.414*3.14159 / len)
  89. c2 = b1
  90. c3 = (-a1)*a1
  91. c1 = 1 - c2 - c3
  92. v12 = c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
  93. 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
  94. // security wrapper for repeat calls
  95. reso(exp, use, res) => use ? security(tickerid, res, exp) : exp
  96. // === /BASE FUNCTIONS ===
  97.  
  98. // === SERIES SETUP ===
  99. closeSeries = variant(basisType, close[delayOffset], basisLen, offsetSigma, offsetALMA)
  100. openSeries = variant(basisType, open[delayOffset], basisLen, offsetSigma, offsetALMA)
  101. closeOpenAvg = (closeSeries + openSeries) / 2
  102. // === /SERIES ===
  103.  
  104. // === PLOTTING ===
  105. trendColour = reso((closeSeries > openSeries), useRes, stratRes) ? #006600 : #990000
  106. barcolor(scolor?trendColour:na, title = "Bar Colours")
  107. closeP=plot(reso(closeSeries, useRes, stratRes), title = "Close Series", color = trendColour, linewidth = 2, style = line, transp = 0)
  108. openP=plot(reso(openSeries, useRes, stratRes), title = "Open Series", color = trendColour, linewidth = 2, style = line, transp = 0)
  109. fill(closeP,openP,color=trendColour,transp=50)
  110.  
  111.  
  112. // === /PLOTTING ===
  113. //
  114. // === ALERT conditions
  115. closeSeriesAlt = reso(closeSeries, useRes, stratRes)
  116. openSeriesAlt = reso(openSeries, useRes, stratRes)
  117. //
  118. xlong = crossover(closeSeriesAlt, openSeriesAlt)
  119. xshort = crossunder(closeSeriesAlt, openSeriesAlt)
  120. longCond = xlong // alternative: longCond[1]? false : (xlong or xlong[1]) and close>closeSeriesAlt and close>=open
  121. shortCond = xshort // alternative: shortCond[1]? false : (xshort or xshort[1]) and close<closeSeriesAlt and close<=open
  122. // === /ALERT conditions.
  123.  
  124. // === STRATEGY ===
  125. boExpiryTime = input(3, "Binary Options Expiration Candles")
  126. // Include bar limiting algorithm
  127. ebar = input(defval = 5000, title="Number of Bars for Back Testing", minval=0)
  128. dummy = input(false, title="- SET to ZERO for Daily or Longer Timeframes" )
  129. //
  130. // Calculate how many mars since last bar
  131. tdays = (timenow-time)/60000.0 // number of minutes since last bar
  132. 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
  133. //
  134. //set up exit parameters
  135.  
  136. call = longCond==true
  137. put = shortCond==true
  138. ctimer = call? 0 : na(ctimer[1])? 1 : ctimer[1]+1
  139. ptimer = put? 0 : na(ptimer[1])? 1 : ptimer[1]+1
  140.  
  141. if (ebar==0 or tdays<=ebar)
  142. strategy.entry("CALL", strategy.long, when = call > 0 and put == 0 and strategy.opentrades == 0)
  143. strategy.close("CALL", when = ctimer>=boExpiryTime)
  144. strategy.entry("PUT", strategy.short, when = put > 0 and call == 0 and strategy.opentrades == 0)
  145. strategy.close("PUT", when = ptimer>=boExpiryTime)
  146.  
  147. // === /STRATEGY ===
  148. // eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement