Create A Pine Script Strategy that Trade when Price Touch VWAP from Below and Form Shooting Star or Pin Bar (Bearish pin bar: A long upper tail that is three or more times longer than the body. It appears after a solid upward movement or at the end of an uptrend.) with Higher Volume than last 5 Candle and Place Sell Order on Next Candle of CandleStick Pattern at the low of CandleStick Pattern if low is Broken first and if High Broke first of Pattern then Trade is Invalid. When Place Order Put Stop Loss above 2% of Daily ATR Pin Bar or Shooting Star and 1:2 Risk and Reward, Also Make Sure CandleStick Pattern has High above than last 10 Candle to Consider as Trend Reversal
Assistant
pine
//@version=5
strategy("VWAP Touch and Bearish Reversal Pattern Strategy", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=1)
// Calculate VWAP
vwap = ta.vwap(hlc3)
plot(vwap, color=color.orange, linewidth=2)
// Calculate components for Bearish Pin Bar / Shooting Star
body = math.abs(close - open)
upper_shadow = high - math.max(open, close)
lower_shadow = math.min(open, close) - low
// Condition for Bearish Pin Bar
bearish_pin_bar = upper_shadow >= 3 * body
// Volume higher than last 5 candles
volume_condition = volume > ta.highest(volume[1], 5)
// High is above the last 10 candles
high_condition = high > ta.highest(high[1], 10)
// Price touches VWAP from below
vwap_touched_from_below = close[1] < vwap[1] and low <= vwap
// Entry condition
entry_condition = bearish_pin_bar and volume_condition and high_condition and vwap_touched_from_below
// Variables to manage the trade
var bool trade_in_progress = false
var bool in_position = false
var float pattern_low = na
var float pattern_high = na
var float entry_price = na
var float stop_loss_price = na
var float take_profit_price = na
var int pattern_bar_index = na
if entry_condition
pattern_bar_index := bar_index
pattern_low := low
pattern_high := high
entry_price := pattern_low
// Get daily ATR
daily_atr = request.security(syminfo.tickerid, 'D', ta.atr(14))
stop_loss_price := pattern_high + 0.02 * daily_atr
risk = stop_loss_price - entry_price
take_profit_price := entry_price - 2 * risk // 1:2 risk reward
trade_in_progress := true
// Place sell stop order
strategy.order("Short Entry", strategy.short, comment="Sell Triggered", stop=entry_price)
if trade_in_progress
if high > pattern_high
// Cancel the order
strategy.cancel("Short Entry")
trade_in_progress := false
if strategy.position_size < 0 and not in_position
in_position := true
// Set stop loss and take profit
strategy.exit("Exit Short", "Short Entry", stop=stop_loss_price, limit=take_profit_price)
if strategy.position_size == 0 and in_position
in_position := false
// Plot signals
plotshape(entry_condition, title="Bearish Pin Bar", location=location.abovebar, color=color.red, style=shape.labeldown, text="Bearish Pin Bar")
plotshape(trade_in_progress and high > pattern_high, title="Trade Invalidated", location=location.abovebar, color=color.gray, style=shape.circle, text="Trade Invalid")
// Optional: Plot entry, stop loss, and take profit levels when in position
plot(in_position ? entry_price : na, title="Entry Price", style=plot.style_linebr, color=color.red)
plot(in_position ? stop_loss_price : na, title="Stop Loss", style=plot.style_linebr, color=color.orange)
plot(in_position ? take_profit_price : na, title="Take Profit", style=plot.style_linebr, color=color.green)
This Pine Script strategy implements the trading system you've described:
Entry Conditions:
VWAP Touch from Below: Identifies when the price touches the VWAP from below.
Bearish Pin Bar / Shooting Star: Detects a candlestick pattern where the upper shadow is at least three times the size of the body.
Volume Confirmation: Ensures the volume is higher than the previous five candles.
Trend Reversal Confirmation: The high of the pattern is higher than the highs of the previous ten candles.
Execution:
Places a sell stop order at the low of the candlestick pattern on the next candle.
If the high of the pattern candle is broken before the entry, the trade is canceled.
Stop Loss and Take Profit:
Stop Loss: Set above the pattern candle's high plus 2% of the daily ATR.
Take Profit: Set at a 1:2 risk-to-reward ratio.
The script uses variables to track the state of the trade and incorporates the strategy.order and strategy.exit functions to manage orders according to your specifications. It also includes plotting statements to visualize the VWAP, entry points, and stop levels.