Certainly! Below is the Pine Script strategy that implements your requirements:
//@version=5
strategy("Shooting Star/Pin Bar Reversal Strategy", overlay=true, margin_long=100, margin_short=1000)
// Parameters
shootingStarBodyRatio = 0.33
shootingStarWickMultiplier = 3
showShootingStar = true
// Calculate candle properties
bodySize = math.abs(open - close)
upperWick = high - math.max(open, close)
lowerWick = math.min(open, close) - low
range = high - low
// Volume condition: Current volume greater than previous 5 candles' volume
maxVolume = ta.highest(volume[1], 5)
// High of current candle higher than previous 10 candles
prevHigh10 = ta.highest(high[1], 10)
// Check for Shooting Star/Bearish Pin Bar pattern
isShootingStar = showShootingStar and
bodySize < range * shootingStarBodyRatio and
upperWick > lowerWick * shootingStarWickMultiplier and
volume > maxVolume and
high > prevHigh10
// Identify uptrend: Price above 50-period SMA (simple uptrend definition)
ma = ta.sma(close, 50)
trendUp = close > ma
// Compute Daily ATR
dailyATR = request.security(syminfo.tickerid, "D", ta.atr(14))
// Variables to track order levels
var float entryPrice = na
var float stopLoss = na
var float takeProfit = na
var float patternHigh = na
var float patternLow = na
var bool waitingForEntry = false
// When pattern is detected, set up pending order levels
if isShootingStar and trendUp
entryPrice := low // Sell stop at low of pattern candle
stopLoss := entryPrice + dailyATR * 0.02 // Stop loss 2% of Daily ATR above entry
risk = stopLoss - entryPrice
takeProfit := entryPrice - risk * 2 // Take profit at 1:2 risk/reward
waitingForEntry := true
patternHigh := high
patternLow := low
// Order execution logic
if waitingForEntry
patternHighBroken = high >= patternHigh
patternLowBroken = low <= patternLow
if patternHighBroken and not patternLowBroken
// High of pattern candle broke first - setup invalid
waitingForEntry := false
strategy.cancel_all()
else if patternLowBroken and not patternHighBroken
// Low of pattern candle broke first - enter short position
strategy.entry("Short", strategy.short, qty=1, stop=entryPrice)
strategy.exit("Short Exit", "Short", stop=stopLoss, limit=takeProfit)
waitingForEntry := false
else if patternHighBroken and patternLowBroken
// Both levels broken in same bar - cannot determine which broke first, cancel setup
waitingForEntry := false
strategy.cancel_all()
// Reset variables if not waiting for entry
if not waitingForEntry
entryPrice := na
stopLoss := na
takeProfit := na
patternHigh := na
patternLow := na
// Plot signals on chart
plotshape(isShootingStar, title="Shooting Star", location=location.abovebar, color=color.red, style=shape.triangleup, size=size.tiny)
plotshape(strategy.position_size < 0, title="Sell", location=location.belowbar, color=color.red, style=shape.triangledown, size=size.small)
Explanation of the Strategy:
-
Pattern Detection:
- Body Size: The body of the candle is less than 33% of the total candle range.
- Upper Wick: The upper wick is more than three times the lower wick.
- Volume Condition: Current volume is higher than the maximum volume of the previous 5 candles.
- Trend Reversal Check: The high of the candle is higher than the highs of the previous 10 candles.
-
Trend Confirmation:
- The price is above the 50-period Simple Moving Average (SMA), indicating an uptrend.
-
Order Execution Logic:
- Entry: Place a sell stop order at the low of the Shooting Star/Pin Bar candle.
- Invalidation: If the high of the pattern candle is breached before the low, the trade is invalidated.
- Stop Loss: Set 2% of the Daily ATR above the entry price.
- Take Profit: Set at a level where the risk-to-reward ratio is 1:2.
-
Limitations:
- Due to the limitations of bar resolution in Pine Script, if both the high and low of the pattern candle are breached in the same bar, the strategy cannot determine which occurred first, and the trade is canceled.
- For more precise execution, consider running this strategy on lower timeframes to better capture the sequence of price movements within each candle.
Important Notes:
-
Timeframe Consideration: The strategy uses the request.security function to fetch the Daily ATR regardless of the chart timeframe. Ensure that the chart timeframe aligns with your trading practices.
-
Risk Management: Always adjust the position sizing (qty) and ensure it aligns with your risk management rules and account size.
-
Optimization: You may adjust the shootingStarBodyRatio, shootingStarWickMultiplier, and moving average period (ma) to better fit the specific characteristics of the instrument you're trading.
Feel free to test this strategy in TradingView and adjust parameters as needed. Remember to thoroughly backtest any strategy before applying it to live trading to ensure it performs as expected.