Advertisement
xmd79

Open Close Cross Alert R6.2 by JustUncleL

Aug 7th, 2021
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. //@version=4
  2.  
  3. study(title="Open Close Cross Alert R6.2 by JustUncleL", shorttitle="OCC Alert R6.2", overlay=false)
  4.  
  5. // Revision: 6
  6. // Original Author: @JayRogers
  7. // Revisions By: R3, R4, R5, R6 by JustIncleL
  8. //
  9. //
  10. // *** USE AT YOUR OWN RISK ***
  11. // - There are drawing/painting issues in pinescript when working across resolutions/timeframes that I simply
  12. // cannot fix here.. I will not be putting any further effort into developing this until such a time when
  13. // workarounds become available.
  14. //
  15. // R6.2 By JustUncleL:
  16. // - Minor updates for Pinescript Version 3 Compliance.
  17. // - Modified Divergence to display only when in correct High/Low zone.
  18. // 9-Aug-2017
  19. // - Correction SuperSmooth MA calculation.
  20. //
  21. // R6 By JustUncleL:
  22. // - Added optional Divergence detection.
  23. // - Corrected Alternate resolution calculation to allow for Daily, Weekly and Monthly charts.
  24. // - Changed Graphic presentation.
  25. // R5 By JustUncleL:
  26. // - Change the way the Alternate resolution in selected, use a Multiplier of the base Time Frame instead,
  27. // this makes it easy to switch between base time frames.
  28. // - Added TMA and SSMA moving average options. But DEMA is still giving the best results.
  29. // - Updated Default settings.
  30. //
  31. // R4 Changes (by JustUncleL):
  32. // - Change Alert indication to coloured circle at bottom.
  33. // - Added Histogram of difference between close and open MAs as a factor of average asset MA value.
  34. //
  35. // R3 Changes (by JustUncleL):
  36. // -- Study indicator alert created for TradingView sub-system (the alertcondition() function does not work in a
  37. // "Strategy" script -- Bug).
  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 = timeframe.ismonthly ? tostring(timeframe.multiplier * intRes, "###M") :
  62. timeframe.isweekly ? tostring(timeframe.multiplier * intRes, "###W") :
  63. timeframe.isdaily ? tostring(timeframe.multiplier * intRes, "###D") :
  64. timeframe.isintraday ? tostring(timeframe.multiplier * intRes, "####") : '60'
  65. //
  66. basisType = input(defval="SMMA", title="MA Type: ", options=["SMA", "EMA", "DEMA", "TEMA", "WMA", "VWMA", "SMMA", "HullMA", "LSMA", "ALMA", "SSMA", "TMA"])
  67. basisLen = input(defval=8, title="MA Period", minval=1)
  68. offsetSigma = input(defval=6, title="Offset for LSMA / Sigma for ALMA", minval=0)
  69. offsetALMA = input(defval=0.85, title="Offset for ALMA", minval=0, step=0.01)
  70. delayOffset = input(defval=0, title="Delay Open/Close MA (Forces Non-Repainting)", minval=0, step=1)
  71. //
  72. uDiv = input(false, "Show Divergence Channel")
  73. multi = input(0.5, minval=0.0, maxval=3.0, title="Divergence Channel Width Factor (Stddev)")
  74. uHid = input(false, title="Show Hidden Divergence")
  75. uReg = input(false, title="Show Regular Divergence")
  76. uDiv := uReg or uHid ? uDiv : false
  77.  
  78. // === /INPUTS ===
  79.  
  80. // - variant(type, src, len)
  81. // Returns MA input selection variant, default to SMA if blank or typo.
  82.  
  83. // Returns MA input selection variant, default to SMA if blank or typo.
  84. variant(type, src, len, offSig, offALMA) =>
  85. v1 = sma(src, len) // Simple
  86. v2 = ema(src, len) // Exponential
  87. v3 = 2 * v2 - ema(v2, len) // Double Exponential
  88. v4 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
  89. v5 = wma(src, len) // Weighted
  90. v6 = vwma(src, len) // Volume Weighted
  91. v7 = 0.0
  92. sma_1 = sma(src, len) // Smoothed
  93. v7 := na(v7[1]) ? sma_1 : (v7[1] * (len - 1) + src) / len
  94. v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull
  95. v9 = linreg(src, len, offSig) // Least Squares
  96. v10 = alma(src, len, offALMA, offSig) // Arnaud Legoux
  97. v11 = sma(v1, len) // Triangular (extreme smooth)
  98. // SuperSmoother filter
  99. // © 2013 John F. Ehlers
  100. a1 = exp(-1.414 * 3.14159 / len)
  101. b1 = 2 * a1 * cos(1.414 * 3.14159 / len)
  102. c2 = b1
  103. c3 = -a1 * a1
  104. c1 = 1 - c2 - c3
  105. v12 = 0.0
  106. v12 := c1 * (src + nz(src[1])) / 2 + c2 * nz(v12[1]) + c3 * nz(v12[2])
  107. type == "EMA" ? v2 : type == "DEMA" ? v3 :
  108. type == "TEMA" ? v4 : type == "WMA" ? v5 : type == "VWMA" ? v6 :
  109. type == "SMMA" ? v7 : type == "HullMA" ? v8 : type == "LSMA" ? v9 :
  110. type == "ALMA" ? v10 : type == "TMA" ? v11 : type == "SSMA" ? v12 : v1
  111.  
  112. // security wrapper for repeat calls
  113. reso(exp, use, res) =>
  114. security_1 = security(syminfo.tickerid, res, exp, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
  115. use ? security_1 : exp
  116.  
  117. // === SERIES SETUP ===
  118. closeSeries = variant(basisType, close[delayOffset], basisLen, offsetSigma, offsetALMA)
  119. openSeries = variant(basisType, open[delayOffset], basisLen, offsetSigma, offsetALMA)
  120. closeOpenAvg = (closeSeries + openSeries) / 2
  121. // === /SERIES ===
  122.  
  123. // Get Alternate Open/Close Series MAs, if selected.
  124. closeSeriesAlt = reso(closeSeries, useRes, stratRes)
  125. openSeriesAlt = reso(openSeries, useRes, stratRes)
  126. //
  127. xlong = crossover(closeSeriesAlt, openSeriesAlt)
  128. xshort = crossunder(closeSeriesAlt, openSeriesAlt)
  129. longCond = xlong // alternative: longCond[1]? false : (xlong or xlong[1]) and close>closeSeriesAlt and close>=open
  130. shortCond = xshort // alternative: shortCond[1]? false : (xshort or xshort[1]) and close<closeSeriesAlt and close<=open
  131. // === /ALERT conditions
  132.  
  133. // === PLOTTING ===
  134. diff = closeSeriesAlt - openSeriesAlt
  135. pcd = 50000.0 * diff / closeOpenAvg
  136. alert = longCond or shortCond
  137. trendColour = closeSeriesAlt > openSeriesAlt ? color.green : color.red
  138. hline(0, color=color.gray, linestyle=hline.style_dashed)
  139. plot(pcd, title="OCC Difference Factor", color=trendColour, linewidth=1, style=plot.style_area, transp=80)
  140. plot(pcd, title="OCC Difference Factor", color=trendColour, linewidth=2, style=plot.style_line, transp=20)
  141. plotshape(alert, title="OCC Alert Plot", color=trendColour, style=shape.circle, location=location.bottom, transp=0, size=size.auto)
  142.  
  143. // || Functions:
  144.  
  145. f_top_fractal(_src) =>
  146. _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and
  147. _src[2] > _src[0]
  148. f_bot_fractal(_src) =>
  149. _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and
  150. _src[2] < _src[0]
  151. f_fractalize(_src) =>
  152. f_bot_fractal__1 = f_bot_fractal(_src)
  153. f_top_fractal(_src) ? 1 : f_bot_fractal__1 ? -1 : 0
  154.  
  155. // === /BASE FUNCTIONS ===
  156.  
  157. rsi_high = pcd
  158. rsi_low = pcd
  159. offset_ = multi * stdev(pcd, 20)
  160. fractal_top_rsi = f_fractalize(rsi_high) > 0 ? rsi_high[2] : na
  161. fractal_bot_rsi = f_fractalize(rsi_low) < 0 ? rsi_low[2] : na
  162.  
  163. rsi_high_prev = valuewhen(fractal_top_rsi, rsi_high[2], 1)
  164. rsi_high_price = valuewhen(fractal_top_rsi, high[2], 1)
  165. rsi_low_prev = valuewhen(fractal_bot_rsi, rsi_low[2], 1)
  166. rsi_low_price = valuewhen(fractal_bot_rsi, low[2], 1)
  167.  
  168. regular_bearish_div = fractal_top_rsi and high[2] > rsi_high_price and rsi_high[2] < rsi_high_prev and
  169. rsi_high > 0
  170. hidden_bearish_div = fractal_top_rsi and high[2] < rsi_high_price and rsi_high[2] > rsi_high_prev and
  171. rsi_high > 0
  172. regular_bullish_div = fractal_bot_rsi and low[2] < rsi_low_price and rsi_low[2] > rsi_low_prev and
  173. rsi_low < 0
  174. hidden_bullish_div = fractal_bot_rsi and low[2] > rsi_low_price and rsi_low[2] < rsi_low_prev and
  175. rsi_low < 0
  176.  
  177. plot(title='RSI High', series=uDiv ? rsi_high : na, color=color.gray)
  178. plot(title='RSI Low', series=uDiv ? rsi_low : na, color=color.gray)
  179. plot(title='RSI H F', series=uDiv ? fractal_top_rsi + offset_ : na, color=color.red, offset=-2)
  180. plot(title='RSI L F', series=uDiv ? fractal_bot_rsi - offset_ : na, color=color.green, offset=-2)
  181. plot(title='RSI H D', series=uDiv ? fractal_top_rsi + offset_ : na, style=plot.style_circles, color=regular_bearish_div and uReg or hidden_bearish_div and uHid ? color.maroon : color.gray, linewidth=3, offset=-2)
  182. plot(title='RSI L D', series=uDiv ? fractal_bot_rsi - offset_ : na, style=plot.style_circles, color=regular_bullish_div and uReg or hidden_bullish_div and uHid ? color.green : color.gray, linewidth=3, offset=-2)
  183.  
  184. plotshape(title='+RBD', series=regular_bearish_div and uReg ? rsi_high[2] + offset_ : na, text='Regular', style=shape.labeldown, location=location.absolute, color=color.maroon, textcolor=color.white, offset=-2)
  185. plotshape(title='+HBD', series=hidden_bearish_div and uHid ? rsi_high[2] + offset_ : na, text='hidden', style=shape.labeldown, location=location.absolute, color=color.maroon, textcolor=color.white, offset=-2)
  186. plotshape(title='-RBD', series=regular_bullish_div and uReg ? rsi_low[2] - offset_ : na, text='Regular', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)
  187. plotshape(title='-HBD', series=hidden_bullish_div and uHid ? rsi_low[2] - offset_ : na, text='hidden', style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white, offset=-2)
  188.  
  189. // === ALARM ALERTS for TradingView ALARM sub-system (server based) ===
  190. alertcondition(alert, message="OCC BothCond", title="OCC BothCond")
  191. alertcondition(longCond, message="OCC LongCond", title="OCC LongCond")
  192. alertcondition(shortCond, message="OCC ShortCond", title="OCC ShortCond")
  193. // === /ALARM ALERTS
  194. //eof
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement