Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.73 KB | None | 0 0
  1. //@version=3
  2. //
  3.  
  4. study(title = "Open Close Cross Strategy R5.1 revised by JustUncleL", shorttitle = "OCC Strategy R5.1", overlay = true), // pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, calc_on_every_tick=false)
  5.  
  6. //
  7. // Revision: 5
  8. // Original Author: @JayRogers
  9. // Revision Author: JustUncleL revisions 3, 4, 5
  10. //
  11. // *** USE AT YOUR OWN RISK ***
  12. // - There are drawing/painting issues in pinescript when working across resolutions/timeframes that I simply
  13. // cannot fix here.. I will not be putting any further effort into developing this until such a time when
  14. // workarounds become available.
  15. // NOTE: Re-painting has been observed infrequently with default settings and seems OK up to Alternate
  16. // multiplier of 5.
  17. // Non-repainting mode is available by setting "Delay Open/Close MA" to 1 or more, but the reported
  18. // performance will drop dramatically.
  19. //
  20. // R5.1 Changes by JustUncleL
  21. // - Upgraded to Version 3 Pinescript.
  22. // - Added option to select Trade type (Long, Short, Both or None)
  23. // - Added bar colouring work around patch.
  24. // - Small code changes to improve efficiency.
  25. // - NOTE: To enable non-Repainting mode set "Delay Open/Close MA" to 1 or more.
  26. // 9-Aug-2017
  27. // - Correction on SuperSmooth MA calculation.
  28. //
  29. // R5 Changes by JustUncleL
  30. // - Corrected cross over calculations, sometimes gave false signals.
  31. // - Corrected Alternate Time calculation to allow for Daily,Weekly and Monthly charts.
  32. // - Open Public release.
  33. // R4 Changes By JustUncleL
  34. // - Change the way the Alternate resolution in selected, use a Multiplier of the base Time Frame instead,
  35. // this makes it easy to switch between base time frames.
  36. // - Added TMA and SSMA moving average options. But DEMA is still giving the best results.
  37. // - Using "calc_on_every_tick=false" ensures results between backtesting and real time are similar.
  38. // - Added Option to Disable the coloring of the bars.
  39. // - Updated default settings.
  40. //
  41. // R3 Changes by JustUncleL:
  42. // - Returned a simplified version of the open/close channel, it shows strength of current trend.
  43. // - Added Target Profit Option.
  44. // - Added option to reduce the number of historical bars, overcomes the too many trades limit error.
  45. // - Simplified the strategy code.
  46. // - Removed Trailing Stop option, not required and in my opion does not work well in Trading View,
  47. // it also gives false and unrealistic performance results in backtesting.
  48. //
  49. // R2 Changes:
  50. // - Simplified and cleaned up plotting, now just shows a Moving Average derived from the average of open/close.
  51. // - Tried very hard to alleviate painting issues caused by referencing alternate resolution..
  52. //
  53. // Description:
  54. // - Strategy based around Open-Close Crossovers.
  55. // Setup:
  56. // - I have generally found that setting the strategy resolution to 3-4x that of the chart you are viewing
  57. // tends to yield the best results, regardless of which MA option you may choose (if any) BUT can cause
  58. // a lot of false positives - be aware of this
  59. // - Don't aim for perfection. Just aim to get a reasonably snug fit with the O-C band, with good runs of
  60. // green and red.
  61. // - Option to either use basic open and close series data, or pick your poison with a wide array of MA types.
  62. // - Optional trailing stop for damage mitigation if desired (can be toggled on/off)
  63. // - Positions get taken automagically following a crossover - which is why it's better to set the resolution
  64. // of the script greater than that of your chart, so that the trades get taken sooner rather than later.
  65. // - If you make use of the stops, be sure to take your time tweaking the values. Cutting it too fine
  66. // will cost you profits but keep you safer, while letting them loose could lead to more drawdown than you
  67. // can handle.
  68. // - To enable non-Repainting mode set "Delay Open/Close MA" to 1 or more.
  69. //
  70.  
  71. // === INPUTS ===
  72. useRes = input(defval = true, title = "Use Alternate Resolution?")
  73. intRes = input(defval = 120, title = "Multiplier for Alernate Resolution")
  74. stratRes = ismonthly? tostring(interval*intRes,"###M") : isweekly? tostring(interval*intRes,"###W") : isdaily? tostring(interval*intRes,"###D") : isintraday ? tostring(interval*intRes,"####") : '60'
  75. basisType = input(defval = "SMMA", title = "MA Type: ", options=["SMA", "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMMA", "HullMA", "LSMA", "ALMA", "SSMA", "TMA"])
  76. basisLen = input(defval = 8, title = "MA Period", minval = 1)
  77. offsetSigma = input(defval = 6, title = "Offset for LSMA / Sigma for ALMA", minval = 0)
  78. offsetALMA = input(defval = 0.85, title = "Offset for ALMA", minval = 0, step = 0.01)
  79. scolor = input(false, title="Show coloured Bars to indicate Trend?")
  80. delayOffset = input(defval = 0, title = "Delay Open/Close MA (Forces Non-Repainting)", minval = 0, step = 1)
  81. tradeType = input("BOTH", title="What trades should be taken : ", options=["LONG", "SHORT", "BOTH", "NONE"])
  82. // === /INPUTS ===
  83.  
  84. // Constants colours that include fully non-transparent option.
  85. green100 = #008000FF
  86. lime100 = #00FF00FF
  87. red100 = #FF0000FF
  88. blue100 = #0000FFFF
  89. aqua100 = #00FFFFFF
  90. darkred100 = #8B0000FF
  91. gray100 = #808080FF
  92.  
  93. // === BASE FUNCTIONS ===
  94. // Returns MA input selection variant, default to SMA if blank or typo.
  95. variant(type, src, len, offSig, offALMA) =>
  96. v1 = sma(src, len) // Simple
  97. v2 = ema(src, len) // Exponential
  98. v3 = 2 * v2 - ema(v2, len) // Double Exponential
  99. v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  100. v5 = wma(src, len) // Weighted
  101. v6 = vwma(src, len) // Volume Weighted
  102. v7 = 0.0
  103. v7 := na(v7[1]) ? sma(src, len) : (v7[1] * (len - 1) + src) / len // Smoothed
  104. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  105. v9 = linreg(src, len, offSig) // Least Squares
  106. v10 = alma(src, len, offALMA, offSig) // Arnaud Legoux
  107. v11 = sma(v1,len) // Triangular (extreme smooth)
  108. // SuperSmoother filter
  109. // © 2013 John F. Ehlers
  110. a1 = exp(-1.414*3.14159 / len)
  111. b1 = 2*a1*cos(1.414*3.14159 / len)
  112. c2 = b1
  113. c3 = (-a1)*a1
  114. c1 = 1 - c2 - c3
  115. v12 = 0.0
  116. v12 := c1*(src + nz(src[1])) / 2 + c2*nz(v12[1]) + c3*nz(v12[2])
  117. 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
  118.  
  119. // security wrapper for repeat calls
  120. reso(exp, use, res) => use ? security(tickerid, res, exp, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on) : exp
  121.  
  122. // === /BASE FUNCTIONS ===
  123.  
  124. // === SERIES SETUP ===
  125. closeSeries = variant(basisType, close[delayOffset], basisLen, offsetSigma, offsetALMA)
  126. openSeries = variant(basisType, open[delayOffset], basisLen, offsetSigma, offsetALMA)
  127. // === /SERIES ===
  128.  
  129. // === PLOTTING ===
  130.  
  131. // Get Alternate resolution Series if selected.
  132. closeSeriesAlt = reso(closeSeries, useRes, stratRes)
  133. openSeriesAlt = reso(openSeries, useRes, stratRes)
  134. //
  135. trendColour = (closeSeriesAlt > openSeriesAlt) ? green : red
  136. bcolour = (closeSeries > openSeriesAlt) ? lime100 : red100
  137. barcolor(scolor?bcolour:na, title = "Bar Colours")
  138. closeP=plot(closeSeriesAlt, title = "Close Series", color = trendColour, linewidth = 2, style = line, transp = 20)
  139. openP=plot(openSeriesAlt, title = "Open Series", color = trendColour, linewidth = 2, style = line, transp = 20)
  140. fill(closeP,openP,color=trendColour,transp=80)
  141.  
  142. // === /PLOTTING ===
  143. //
  144.  
  145. //
  146. // === ALERT conditions
  147.  
  148.  
  149. // === /ALERT conditions.
  150.  
  151. // === STRATEGY ===
  152. // stop loss
  153. slPoints = input(defval = 0, title = "Initial Stop Loss Points (zero to disable)", minval = 0)
  154. tpPoints = input(defval = 0, title = "Initial Target Profit Points (zero for disable)", minval = 0)
  155. // Include bar limiting algorithm
  156. ebar = input(defval = 10000, title="Number of Bars for Back Testing", minval=0)
  157. dummy = input(false, title="- SET to ZERO for Daily or Longer Timeframes" )
  158. //
  159. // Calculate how many mars since last bar
  160. tdays = (timenow-time)/60000.0 // number of minutes since last bar
  161. 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
  162. //
  163. //set up exit parameters
  164. TP = tpPoints>0?tpPoints:na
  165. SL = slPoints>0?slPoints:na
  166.  
  167. // Make sure we are within the bar range, Set up entries and exit conditions
  168. //if ((ebar==0 or tdays<=ebar) and tradeType!="NONE")
  169. // strategy.entry("long", strategy.long, when=longCond==true and tradeType!="SHORT")
  170. // strategy.entry("short", strategy.short, when=shortCond==true and tradeType!="LONG")
  171. // strategy.close("long", when = shortCond==true and tradeType=="LONG")
  172. // strategy.close("short", when = longCond==true and tradeType=="SHORT")
  173. // strategy.exit("XL", from_entry = "long", profit = TP, loss = SL)
  174. // strategy.exit("XS", from_entry = "short", profit = TP, loss = SL)
  175. //
  176. // === /STRATEGY ===
  177. // eof
  178.  
  179. resCustom = input(title="Use Different Timeframe? Uncheck Box Above", defval="120")
  180. useCurrentRes = input(true, title="Use Current Chart Resolution?")
  181. res = resCustom
  182.  
  183. length1 = input(50, minval=1), smoothK1 = input(10, minval=1), smoothD1 = input(10, minval=1)
  184. k1 = security(tickerid, res,sma(stoch(close, close, close, length1), smoothK1))
  185. d1 = security(tickerid, res,sma(k1, smoothD1))
  186. col1 = k1 > d1
  187.  
  188. xlong = crossover(closeSeriesAlt, openSeriesAlt)
  189. xshort = crossunder(closeSeriesAlt, openSeriesAlt)
  190. longCond = xlong and tdays // alternative: longCond[1]? false : (xlong or xlong[1]) and close>closeSeriesAlt and close>=open
  191. shortCond = xshort and tdays // alternative: shortCond[1]? false : (xshort or xshort[1]) and close<closeSeriesAlt and close<=open
  192.  
  193. plot(cross(closeSeriesAlt, openSeriesAlt) ? closeSeriesAlt : na, style = circles, linewidth = 8, transp=0, color= black,title= "Alarm")
  194.  
  195.  
  196. alertcondition(longCond, title='Buy Alert', message='Buy Alert')
  197. plotshape(longCond, "Buy Alert", style = shape.triangleup, location = location.belowbar, color = blue, size = size.small,text="[Buy]",transp=0)
  198.  
  199. alertcondition(shortCond, title='Sell Alert', message='Sell Alert')
  200. plotshape(shortCond, "Sell Alert", style = shape.triangledown, location = location.abovebar, color = purple, size = size.small,text="[Sell]",transp=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement