xmd79

hack3

Oct 2nd, 2021
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // https://www.investopedia.com/
  3. // There are a number of very clever people I have taken bits of code and ideas, thanks to you all :)
  4. // © raul3429
  5.  
  6. //@version=4
  7. study(title="MACD+RSI+Flag v3.3 by RM", shorttitle="MACD+RSI+Flg v3.3 by RM", resolution="", overlay = false )
  8.  
  9. // This indicator has line and the band changing color during oversold -green- /overbought -red- with Flags for first candle after direction change shown
  10. // shown as triangles also text is available with the flag. The MACD 12 and MACD 26 are shown with line values and their histogram differences. The crossings marked as diamonds.
  11. // RSI has been scaled down by the "scaleRSI" parameter to enable plotting alongside MACD.
  12. // User may need to adjust this scaleRSI as required to shown RSI & MACD in the proper combination.
  13. // RSI: The relative strength index (RSI) is a momentum indicator measures recent price changes to evaluate overbought or oversold conditions.
  14. // MACD: Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows the relationship between
  15. // two moving averages of a security’s price. The MACD is = 12EMA - 26EMA, The histogram represent this difference.
  16. // Added divergence indicator for Price-RSI slope
  17. // Revised plots' scaleRSI input
  18. // Disclaimer: This is not a Financial advisory tool. For education purposes only. Use at your own risk.
  19.  
  20. //Source
  21. src = input(title="Source", type=input.source, defval=close)
  22.  
  23. // MACD inputs
  24. fast_length = input(title="MACD Fast Length", type=input.integer, defval=12)
  25. slow_length = input(title="MACD Slow Length", type=input.integer, defval=26)
  26. signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
  27. sma_source = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
  28. sma_signal = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
  29.  
  30. // RSI inputs
  31. len = input(14, minval=1, title="RSI Length")
  32. osRSI = input(title="OverSold RSI", defval=30, minval = 1, maxval =100)
  33. obRSI = input(title="OverBought RSI", defval=70, minval = 1, maxval =100)
  34. scaleRSI = input(title="RSI Plot Scale Factor [%]", defval=10, minval = 1, maxval =100) / 100
  35. flagRSI = input(title="Flag and Text os/ob RSI", defval=true, type= input.bool)
  36. txtRSI = input(title="Show Text os/ob RSI", defval=false, type= input.bool)
  37.  
  38.  
  39. // Plot color inputs
  40. col_macd = #2962FF
  41. col_signal = #FF6D00
  42. col_grow_above = #26A69A
  43. col_fall_above = #B2DFDB
  44. col_grow_below = #FFCDD2
  45. col_fall_below = #FF5252
  46. //col_macd = input(#2962FF, "MACD Line  ", input.color, group="Color Settings", inline="MACD")
  47. //col_signal = input(#FF6D00, "Signal Line  ", input.color, group="Color Settings", inline="Signal")
  48. //col_grow_above = input(#26A69A, "Above   Grow", input.color, group="Histogram", inline="Above")
  49. //col_fall_above = input(#B2DFDB, "Fall", input.color, group="Histogram", inline="Above")
  50. //col_grow_below = input(#FFCDD2, "Below Grow", input.color, group="Histogram", inline="Below")
  51. //col_fall_below = input(#FF5252, "Fall", input.color, group="Histogram", inline="Below")
  52.  
  53. // colors
  54. c_white = #ffffff //White
  55. c_green = #006400 //Green
  56. c_green100 = #006400FF//Green 100%
  57. c_green90 = #00640090//Green 90%
  58. c_greenLight = #388e3c //Green Light
  59. c_red = #8B0000 //Red
  60. c_red100 = #8B0000FF//Red 100%
  61. c_red90 = #8B000090//Red 90%
  62. c_redLight = #b71c1c //Red Light
  63. c_orange = #f57f17 //Orange
  64. c_orange90 = #f57f1790 //Orange90%
  65.  
  66. // MACD
  67. fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
  68. slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
  69. macd = fast_ma - slow_ma
  70. signal = sma_signal == "SMA" ? sma(macd, signal_length) : ema(macd, signal_length)
  71. hist = macd - signal
  72.  
  73. plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
  74. plot(macd, title="MACD", color=col_macd)
  75. plot(signal, title="Signal", color=col_signal)
  76. plotshape(crossover (signal, macd) , title="McD CrossOver", style=shape.diamond, location=location.top, color=c_orange90, size=size.tiny)
  77. plotshape(crossunder (signal, macd), title="McD CrossUnder", style=shape.diamond, location=location.bottom, color=c_orange90, size=size.tiny)
  78.  
  79. //RSI
  80. uprsi = rma(max(change(src), 0), len)
  81. downrsi = rma(-min(change(src), 0), len)
  82. rsi = downrsi == 0 ? 100 : uprsi == 0 ? 0 : 100 - (100 / (1 + uprsi / downrsi))
  83.  
  84. f_rsiCond = rsi >= obRSI ? 1 : rsi <= osRSI ? -1 : 0
  85.  
  86. f_rsiCol= f_rsiCond == -1 ? c_red :
  87. f_rsiCond == 1 ? c_green : c_white
  88.  
  89. p2 =plot(rsi * scaleRSI , "RSI Scaled", color= f_rsiCol , linewidth= 1)
  90. pm =plot(50 * scaleRSI , "RSI Mid", color= f_rsiCol , linewidth= 1)
  91. fill(p2, pm, color=color.new(f_rsiCol, 85) , title="Background")
  92.  
  93. // Trade signals per RSI
  94. pine_rsi_prev(x, y) =>
  95. u = max(x[1] - x[2], 0)
  96. d = max(x[2] - x[1], 0)
  97. rs = rma(u, y) / rma(d, y)
  98. res = 100 - 100 / (1 + rs)
  99.  
  100. isRedCandle = close < open
  101. isGreenCandle = close > open
  102.  
  103. prevCandleIsRed = close[1] < open[1]
  104. prevCandleIsGreen = open[1] < close[1]
  105.  
  106. isRsiUnder_osRSI= pine_rsi_prev(close, 14) < 30
  107. isRsiOver_obRSI = pine_rsi_prev(close, 14) > 70
  108.  
  109. enterCallTrade = isGreenCandle and prevCandleIsRed and isRsiUnder_osRSI and flagRSI
  110. enterPutTrade = isRedCandle and prevCandleIsGreen and isRsiOver_obRSI and flagRSI
  111.  
  112. plotshape(enterCallTrade, title = "Buy Flag", text = " ", textcolor = c_white, color = c_green90, style = shape.triangleup, size = size.tiny, location = location.bottom , editable =false)
  113. plotshape(enterPutTrade, title= "Sell Flag", text = " ", textcolor = c_white, color = c_red90, style = shape.triangledown, size = size.tiny, location = location.top , editable =false)
  114. plotshape(enterCallTrade and txtRSI, title = "Buy Text", text = "OS Call" , textcolor = c_white, color = c_green90, style = shape.triangleup, size = size.tiny, location = location.bottom, editable =false)
  115. plotshape(enterPutTrade and txtRSI, title= "Sell Text", text = "OB Put" , textcolor = c_white, color = c_red90, style = shape.triangledown, size = size.tiny, location = location.top, editable =false)
  116.  
  117. // Divergence Indicator
  118.  
  119. //@version=4
  120. //study(title="Divergence Indicator", format=format.price, resolution="")
  121.  
  122. lbR = input(title="Pivot Lookback Right", defval=5)
  123. lbL = input(title="Pivot Lookback Left", defval=5)
  124. rangeUpper = input(title="Max of Lookback Range", defval=60)
  125. rangeLower = input(title="Min of Lookback Range", defval=5)
  126. plotBull = input(title="Plot Bullish", defval=true)
  127. plotHiddenBull = input(title="Plot Hidden Bullish", defval=false)
  128. plotBear = input(title="Plot Bearish", defval=true)
  129. plotHiddenBear = input(title="Plot Hidden Bearish", defval=false)
  130.  
  131. bearColor = c_red
  132. bullColor = c_green
  133. hiddenBullColor = color.new(c_green, 80)
  134. hiddenBearColor = color.new(c_red, 80)
  135. textColor = c_white
  136. noneColor = color.new(c_white, 100)
  137.  
  138. osc = rsi(src, len)
  139. plot(osc * scaleRSI , title="RSI Do not show", linewidth=2, color=#2962FF, display = display.none)
  140.  
  141. plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
  142. phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
  143. _inRange(cond) =>
  144. bars = barssince(cond == true)
  145. rangeLower <= bars and bars <= rangeUpper
  146.  
  147. //------------------------------------------------------------------------------
  148. // Regular Bullish
  149. // Osc: Higher Low
  150.  
  151. oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  152.  
  153. // Price: Lower Low
  154.  
  155. priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
  156. bullCond = plotBull and priceLL and oscHL and plFound
  157.  
  158. plot(
  159. plFound ? osc[lbR] * scaleRSI : na,
  160. offset=-lbR,
  161. title="Regular Bullish",
  162. linewidth=2,
  163. color=(bullCond ? bullColor : noneColor)
  164. )
  165.  
  166. plotshape(
  167. bullCond ? osc[lbR] * scaleRSI : na,
  168. offset=-lbR,
  169. title="Regular Bullish Label",
  170. text=" Bull ",
  171. style=shape.labelup,
  172. location=location.absolute,
  173. color=bullColor,
  174. textcolor=textColor
  175. )
  176.  
  177. //------------------------------------------------------------------------------
  178. // Hidden Bullish
  179. // Osc: Lower Low
  180.  
  181. oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
  182.  
  183. // Price: Higher Low
  184.  
  185. priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
  186. hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
  187.  
  188. plot(
  189. plFound ? osc[lbR] * scaleRSI : na,
  190. offset=-lbR,
  191. title="Hidden Bullish",
  192. linewidth=2,
  193. color=(hiddenBullCond ? hiddenBullColor : noneColor)
  194. )
  195.  
  196. plotshape(
  197. hiddenBullCond ? osc[lbR] * scaleRSI : na,
  198. offset=-lbR,
  199. title="Hidden Bullish Label",
  200. text=" H Bull ",
  201. style=shape.labelup,
  202. location=location.absolute,
  203. color=bullColor,
  204. textcolor=textColor
  205. )
  206.  
  207. //------------------------------------------------------------------------------
  208. // Regular Bearish
  209. // Osc: Lower High
  210.  
  211. oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  212.  
  213. // Price: Higher High
  214.  
  215. priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
  216.  
  217. bearCond = plotBear and priceHH and oscLH and phFound
  218.  
  219. plot(
  220. phFound ? osc[lbR] * scaleRSI : na,
  221. offset=-lbR,
  222. title="Regular Bearish",
  223. linewidth=2,
  224. color=(bearCond ? bearColor : noneColor)
  225. )
  226.  
  227. plotshape(
  228. bearCond ? osc[lbR] * scaleRSI : na,
  229. offset=-lbR,
  230. title="Regular Bearish Label",
  231. text=" Bear ",
  232. style=shape.labeldown,
  233. location=location.absolute,
  234. color=bearColor,
  235. textcolor=textColor
  236. )
  237.  
  238. //------------------------------------------------------------------------------
  239. // Hidden Bearish
  240. // Osc: Higher High
  241.  
  242. oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
  243.  
  244. // Price: Lower High
  245.  
  246. priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
  247.  
  248. hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
  249.  
  250. plot(
  251. phFound ? osc[lbR] * scaleRSI : na,
  252. offset=-lbR,
  253. title="Hidden Bearish",
  254. linewidth=2,
  255. color=(hiddenBearCond ? hiddenBearColor : noneColor)
  256. )
  257.  
  258. plotshape(
  259. hiddenBearCond ? osc[lbR] * scaleRSI : na,
  260. offset=-lbR,
  261. title="Hidden Bearish Label",
  262. text=" H Bear ",
  263. style=shape.labeldown,
  264. location=location.absolute,
  265. color=bearColor,
  266. textcolor=textColor
  267. )
  268.  
  269.  
Advertisement
Add Comment
Please, Sign In to add comment