xmd79

PEAKS and VALLEYS

Sep 24th, 2021
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 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. // © diazfranj
  3.  
  4. //The Universe Provides.
  5. //Update added alerts***
  6. //@version=4
  7. study("PEAKS and VALLEYS",overlay=true)
  8. length = input(21)
  9. //------------------------------------------------------------------------------
  10. o = open[length],h = high[length]
  11. l = low[length],c = close[length]
  12. //------------------------------------------------------------------------------
  13. ph = pivothigh(close,length,length)
  14. pl = pivotlow(open,length,length)
  15. valH = valuewhen(ph,c,0)
  16. valL = valuewhen(pl,c,0)
  17. valpH = valuewhen(ph,c,1)
  18. valpL = valuewhen(pl,c,1)
  19. //------------------------------------------------------------------------------
  20. d = abs(c - o)
  21. hammer = pl and min(o,c) - l > d and h - max(c,o) < d
  22. ihammer = pl and h - max(c,o) > d and min(c,o) - l < d
  23. bulleng = c > o and c[1] < o[1] and c > o[1] and o < c[1]
  24. hanging = ph and min(c,o) - l > d and h - max(o,c) < d
  25. shooting = ph and h - max(o,c) > d and min(c,o) - l < d
  26. beareng = c > o and c[1] < o[1] and c > o[1] and o < c[1]
  27. //------------------------------------------------------------------------------
  28. //Descriptions
  29. //------------------------------------------------------------------------------
  30. hammer_ = "The hammer candlestick pattern is formed of a short body with a long lower wick, and is found at the bottom of a downward trend."
  31. + "\n" + "\n A hammer shows that although there were selling pressures during the day, ultimately a strong buying pressure drove the price back up."
  32. ihammer_ = "The inverted hammer is a similar pattern than the hammer pattern. The only difference being that the upper wick is long, while the lower wick is short."
  33. + "\n" + "\n It indicates a buying pressure, followed by a selling pressure that was not strong enough to drive the market price down. The inverse hammer suggests that buyers will soon have control of the market."
  34. bulleng_ = "The bullish engulfing pattern is formed of two candlesticks. The first candle is a short red body that is completely engulfed by a larger green candle"
  35. + "\n" + "\n Though the second day opens lower than the first, the bullish market pushes the price up, culminating in an obvious win for buyers"
  36. hanging_ = "The hanging man is the bearish equivalent of a hammer; it has the same shape but forms at the end of an uptrend."
  37. + "\n" + "It indicates that there was a significant sell-off during the day, but that buyers were able to push the price up again. The large sell-off is often seen as an indication that the bulls are losing control of the market."
  38. shotting_ = "The shooting star is the same shape as the inverted hammer, but is formed in an uptrend: it has a small lower body, and a long upper wick."
  39. + "\n" + "Usually, the market will gap slightly higher on opening and rally to an intra-day high before closing at a price just above the open – like a star falling to the ground."
  40. beareng_ = "A bearish engulfing pattern occurs at the end of an uptrend. The first candle has a small green body that is engulfed by a subsequent long red candle."
  41. + "\n" + "It signifies a peak or slowdown of price movement, and is a sign of an impending market downturn. The lower the second candle goes, the more significant the trend is likely to be."
  42. //------------------------------------------------------------------------------
  43. n = bar_index
  44. label lbl = na
  45. H = valH > valpH ? "STRONG SELL! HIGHEST HIGH" : valH < valpH ? " NORMAL SELL LOWER HIGH" : na
  46. L = valL < valpL ? "STRONG BUY! LOWEST LOW" : valL > valpL ? "NORMAL BUY HIGHEST LOW" : na
  47. txt = hammer ? "Hammer" : ihammer ? "Inverse Hammer" :
  48. bulleng ? "Bullish Engulfing" : hanging ? "Hanging Man" :
  49. shooting ? "Shooting Star" : beareng ? "Bearish Engulfing" : "None"
  50. des = hammer ? hammer_ : ihammer ? ihammer_ :
  51. bulleng ? bulleng_ : hanging ? hanging_ :
  52. shooting ? shotting_ : beareng ? beareng_ : ""
  53. if ph
  54. lbl := label.new(n[length],max(c,o),H + "\n" + txt,color=#ff1100,
  55. style=label.style_label_down,textcolor=color.white,tooltip=des)
  56. label.delete(lbl[1])
  57. else if pl
  58. lbl := label.new(n[length],min(c,o),L + "\n" + txt,color=#2157f3,
  59. style=label.style_label_up,textcolor=color.white,tooltip=des)
  60. label.delete(lbl[1])
  61.  
  62. var buy_in_progress = false
  63.  
  64.  
  65. //Moving Averages
  66. smaInput1 = input(title="Display SMA 9", type=input.bool, defval=true)
  67. smaInput2 = input(title="Display EMA 20", type=input.bool, defval=true)
  68. smaInput4 = input(title="Display SMA 180", type=input.bool, defval=true)
  69. colored_180 = input(false, title="Color-code 180 trend direction")
  70. vwapInput = input(title="Display VWAP", type=input.bool, defval=true)
  71.  
  72. sma9 = sma(close, 9)
  73. ema20 = ema(close, 20)
  74. sma180 = sma(close, 180)
  75.  
  76. //Plot Moving Averages
  77. plot(smaInput1 ? sma9 : na, color= color.red, title="SMA 9")
  78. plot(smaInput2 ? ema20 : na, color = color.yellow, title="EMA 20")
  79.  
  80. // Plot VWAP
  81. vwap1 = vwap(hlc3)
  82. plot(vwapInput ? vwap1 : na, color = color.blue, title="VWAP")
  83. vwaplong = vwap1 > ema20
  84. vwapshort = vwap1 < ema20
  85.  
  86. //Color SMA 180 trend direction if selected
  87. sma180_uptrend = sma(close, 180) > sma(close[2], 180)
  88. colr = sma180_uptrend == true or colored_180 == false ? color.white : colored_180 == true ? color.gray : na
  89. plot(smaInput4 ? sma180 : na, color = colr, title="SMA 180")
  90.  
  91. //Get value of lower end of candle
  92. buyLow = iff(lowest(open, 1) < lowest(close, 1), lowest(open, 1), lowest(close, 1))
  93. sellLow = lowest(close, 1)
  94.  
  95. // Find the lower MA for crossover sell condition
  96. sellma = iff((sma9<ema20), sma9, ema20)
  97.  
  98.  
  99. //SMA 9 trend direction
  100. sma9_uptrend = sma(close, 9) > sma(close[2], 9)
  101. //EMA 20 trend direction
  102. ema20_uptrend = ema(close, 20) > sma(close[2], 20)
  103.  
  104. //Buy or sell if conditions are met
  105. // Buy when the candle low is above the SMA9
  106. // Sell when the candle low is below the lower of SMA9 and EMA20
  107. Buy = iff(buy_in_progress == false and buyLow > sma9 == true, true, false)
  108. Sell = iff(buy_in_progress == true and sellLow < sellma == true, true, false)
  109.  
  110. // Determine stong buy and strong sell conditions.
  111. // If moving averages are all up, then this will qualify a buy as a strong buy.
  112. // If the moving averages are not up (ie. down) then this will qualify a sell as a strong sell
  113. StrongBuy = iff (Buy and sma9_uptrend and sma180_uptrend and ema20_uptrend and (sma9 > ema20) and (ema20 > sma180), true, false)
  114. StrongSell = iff (Sell and not sma9_uptrend and not sma180_uptrend and not ema20_uptrend and (sma9 < ema20) and (ema20 < sma180), true, false)
  115.  
  116. //Update Trading status if bought or sold
  117. if Buy
  118. buy_in_progress := true
  119. if Sell
  120. buy_in_progress := false
  121.  
  122. // Clear Buy and Sell conditions if StrongBuy or StrongSell conditions exist.
  123. // This disables plotting Buy and Sell conditions
  124. if StrongBuy
  125. Buy := true
  126. if StrongSell
  127. Sell := true
  128.  
  129.  
  130. //Display BUY/SELL indicators
  131.  
  132. plotshape(Buy,title="Buy", color=color.green, style=shape.flag,location=location.belowbar, text="BUY")
  133. plotshape(StrongBuy,title="Strong Buy", color=color.green, style=shape.flag,location=location.belowbar, text="BUY")
  134. plotshape(Sell,title="Sell", color=color.red, style=shape.flag, text="SELL")
  135. plotshape(StrongSell,title="Strong Sell", color=color.red, style=shape.flag, text="SELL")
  136.  
  137. //strategy.entry("BUY", strategy.long, 1, when=vwaplong and vwapInput)
  138. //strategy.entry("SELL", strategy.short, 1, when=vwapshort and vwapInput)
  139.  
  140. //strategy.close("BUY", when = vwapshort and vwapInput)
  141. //strategy.close("SELL", when = vwaplong and vwapInput)
  142.  
  143.  
  144. alertcondition(Buy, title="Buy Signal", message="Buy")
  145. alertcondition(Sell, title="Sell Signal", message="Sell")
  146. //------------------------------------------------------------------------------
  147. alertcondition(valH > valpH, title="STRONG SELL!", message="STRONG SELL")
  148. alertcondition(valH < valpH, title="NORMAL SELL", message="NORMAL SELL")
  149. alertcondition(valL < valpL, title="STRONG BUY!", message="STRONG BUY!")
  150. alertcondition(valL > valpL, title="NORMAL BUY", message="NORMAL BUY")
Advertisement
Add Comment
Please, Sign In to add comment