Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © diazfranj
- //The Universe Provides.
- //Update added alerts***
- //@version=4
- study("PEAKS and VALLEYS",overlay=true)
- length = input(21)
- //------------------------------------------------------------------------------
- o = open[length],h = high[length]
- l = low[length],c = close[length]
- //------------------------------------------------------------------------------
- ph = pivothigh(close,length,length)
- pl = pivotlow(open,length,length)
- valH = valuewhen(ph,c,0)
- valL = valuewhen(pl,c,0)
- valpH = valuewhen(ph,c,1)
- valpL = valuewhen(pl,c,1)
- //------------------------------------------------------------------------------
- d = abs(c - o)
- hammer = pl and min(o,c) - l > d and h - max(c,o) < d
- ihammer = pl and h - max(c,o) > d and min(c,o) - l < d
- bulleng = c > o and c[1] < o[1] and c > o[1] and o < c[1]
- hanging = ph and min(c,o) - l > d and h - max(o,c) < d
- shooting = ph and h - max(o,c) > d and min(c,o) - l < d
- beareng = c > o and c[1] < o[1] and c > o[1] and o < c[1]
- //------------------------------------------------------------------------------
- //Descriptions
- //------------------------------------------------------------------------------
- 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."
- + "\n" + "\n A hammer shows that although there were selling pressures during the day, ultimately a strong buying pressure drove the price back up."
- 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."
- + "\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."
- 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"
- + "\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"
- hanging_ = "The hanging man is the bearish equivalent of a hammer; it has the same shape but forms at the end of an uptrend."
- + "\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."
- 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."
- + "\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."
- 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."
- + "\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."
- //------------------------------------------------------------------------------
- n = bar_index
- label lbl = na
- H = valH > valpH ? "STRONG SELL! HIGHEST HIGH" : valH < valpH ? " NORMAL SELL LOWER HIGH" : na
- L = valL < valpL ? "STRONG BUY! LOWEST LOW" : valL > valpL ? "NORMAL BUY HIGHEST LOW" : na
- txt = hammer ? "Hammer" : ihammer ? "Inverse Hammer" :
- bulleng ? "Bullish Engulfing" : hanging ? "Hanging Man" :
- shooting ? "Shooting Star" : beareng ? "Bearish Engulfing" : "None"
- des = hammer ? hammer_ : ihammer ? ihammer_ :
- bulleng ? bulleng_ : hanging ? hanging_ :
- shooting ? shotting_ : beareng ? beareng_ : ""
- if ph
- lbl := label.new(n[length],max(c,o),H + "\n" + txt,color=#ff1100,
- style=label.style_label_down,textcolor=color.white,tooltip=des)
- label.delete(lbl[1])
- else if pl
- lbl := label.new(n[length],min(c,o),L + "\n" + txt,color=#2157f3,
- style=label.style_label_up,textcolor=color.white,tooltip=des)
- label.delete(lbl[1])
- var buy_in_progress = false
- //Moving Averages
- smaInput1 = input(title="Display SMA 9", type=input.bool, defval=true)
- smaInput2 = input(title="Display EMA 20", type=input.bool, defval=true)
- smaInput4 = input(title="Display SMA 180", type=input.bool, defval=true)
- colored_180 = input(false, title="Color-code 180 trend direction")
- vwapInput = input(title="Display VWAP", type=input.bool, defval=true)
- sma9 = sma(close, 9)
- ema20 = ema(close, 20)
- sma180 = sma(close, 180)
- //Plot Moving Averages
- plot(smaInput1 ? sma9 : na, color= color.red, title="SMA 9")
- plot(smaInput2 ? ema20 : na, color = color.yellow, title="EMA 20")
- // Plot VWAP
- vwap1 = vwap(hlc3)
- plot(vwapInput ? vwap1 : na, color = color.blue, title="VWAP")
- vwaplong = vwap1 > ema20
- vwapshort = vwap1 < ema20
- //Color SMA 180 trend direction if selected
- sma180_uptrend = sma(close, 180) > sma(close[2], 180)
- colr = sma180_uptrend == true or colored_180 == false ? color.white : colored_180 == true ? color.gray : na
- plot(smaInput4 ? sma180 : na, color = colr, title="SMA 180")
- //Get value of lower end of candle
- buyLow = iff(lowest(open, 1) < lowest(close, 1), lowest(open, 1), lowest(close, 1))
- sellLow = lowest(close, 1)
- // Find the lower MA for crossover sell condition
- sellma = iff((sma9<ema20), sma9, ema20)
- //SMA 9 trend direction
- sma9_uptrend = sma(close, 9) > sma(close[2], 9)
- //EMA 20 trend direction
- ema20_uptrend = ema(close, 20) > sma(close[2], 20)
- //Buy or sell if conditions are met
- // Buy when the candle low is above the SMA9
- // Sell when the candle low is below the lower of SMA9 and EMA20
- Buy = iff(buy_in_progress == false and buyLow > sma9 == true, true, false)
- Sell = iff(buy_in_progress == true and sellLow < sellma == true, true, false)
- // Determine stong buy and strong sell conditions.
- // If moving averages are all up, then this will qualify a buy as a strong buy.
- // If the moving averages are not up (ie. down) then this will qualify a sell as a strong sell
- StrongBuy = iff (Buy and sma9_uptrend and sma180_uptrend and ema20_uptrend and (sma9 > ema20) and (ema20 > sma180), true, false)
- StrongSell = iff (Sell and not sma9_uptrend and not sma180_uptrend and not ema20_uptrend and (sma9 < ema20) and (ema20 < sma180), true, false)
- //Update Trading status if bought or sold
- if Buy
- buy_in_progress := true
- if Sell
- buy_in_progress := false
- // Clear Buy and Sell conditions if StrongBuy or StrongSell conditions exist.
- // This disables plotting Buy and Sell conditions
- if StrongBuy
- Buy := true
- if StrongSell
- Sell := true
- //Display BUY/SELL indicators
- plotshape(Buy,title="Buy", color=color.green, style=shape.flag,location=location.belowbar, text="BUY")
- plotshape(StrongBuy,title="Strong Buy", color=color.green, style=shape.flag,location=location.belowbar, text="BUY")
- plotshape(Sell,title="Sell", color=color.red, style=shape.flag, text="SELL")
- plotshape(StrongSell,title="Strong Sell", color=color.red, style=shape.flag, text="SELL")
- //strategy.entry("BUY", strategy.long, 1, when=vwaplong and vwapInput)
- //strategy.entry("SELL", strategy.short, 1, when=vwapshort and vwapInput)
- //strategy.close("BUY", when = vwapshort and vwapInput)
- //strategy.close("SELL", when = vwaplong and vwapInput)
- alertcondition(Buy, title="Buy Signal", message="Buy")
- alertcondition(Sell, title="Sell Signal", message="Sell")
- //------------------------------------------------------------------------------
- alertcondition(valH > valpH, title="STRONG SELL!", message="STRONG SELL")
- alertcondition(valH < valpH, title="NORMAL SELL", message="NORMAL SELL")
- alertcondition(valL < valpL, title="STRONG BUY!", message="STRONG BUY!")
- alertcondition(valL > valpL, title="NORMAL BUY", message="NORMAL BUY")
Advertisement
Add Comment
Please, Sign In to add comment