Sure! Below is a Pine Script Strategy that implements your trading requirements:
- Entry Condition: Sell when the price touches the VWAP from below and forms a Shooting Star (or Pin Bar) with higher volume than the last 5 candles. The next candle must break the low of the Shooting Star before breaking its high.
- Stop Loss: Set at 2% of the daily ATR above the high of the Shooting Star.
- Take Profit: Set at a 1:2 risk to reward ratio.
Here is the code:
//@version=5
strategy("VWAP Touch and Shooting Star Strategy", overlay=true, initial_capital=100000)
// Calculate VWAP
vwap = ta.vwap
// Detect when price touches VWAP from below
fromBelowTouch = (low <= vwap) and (close[1] < vwap[1])
// Function to identify a Shooting Star
isShootingStar() =>
realBody = math.abs(close - open)
upperShadow = high - math.max(open, close)
lowerShadow = math.min(open, close) - low
totalRange = high - low
// Conditions for Shooting Star
condition1 = upperShadow > (realBody * 2) // Upper shadow at least twice the real body
condition2 = lowerShadow <= (realBody * 0.5) // Small or no lower shadow
condition3 = realBody < (totalRange * 0.5) // Real body less than 50% of the total range
condition1 and condition2 and condition3
// Check if current candle is a Shooting Star
shootingStar = isShootingStar()
// Check if volume is higher than the previous 5 candles
volumeHigher = volume > ta.max(volume[1], 5)
// Initialize variables
var float patternHigh = na
var float patternLow = na
var bool entryOrderPlaced = false
var float stopLossPrice = na
var float takeProfitPrice = na
// Get daily ATR for Stop Loss calculation
dailyATR = request.security(syminfo.tickerid, "D", ta.atr(14))
// Entry Signal: When all conditions are met
isEntrySignal = fromBelowTouch and shootingStar and volumeHigher
// Place Entry and Exit Orders
if isEntrySignal
patternHigh := high // High of the Shooting Star
patternLow := low // Low of the Shooting Star
// Calculate Stop Loss and Take Profit
stopLossPrice := patternHigh + (dailyATR * 0.02)
riskPerUnit = stopLossPrice - patternLow
takeProfitPrice := patternLow - (2 * riskPerUnit)
// Place a Sell Stop order at the low of the Shooting Star
strategy.entry("Short", strategy.short, stop=patternLow)
// Place Exit orders for Stop Loss and Take Profit
strategy.exit("Exit", from_entry="Short", stop=stopLossPrice, limit=takeProfitPrice)
entryOrderPlaced := true
// Monitor for High being broken before Entry
if entryOrderPlaced and strategy.position_size == 0
if high >= patternHigh
// Cancel Entry and Exit orders if high is broken first
strategy.cancel(id="Short")
strategy.cancel(id="Exit")
entryOrderPlaced := false
patternHigh := na
patternLow := na
stopLossPrice := na
takeProfitPrice := na
// Reset variables when position is entered or canceled
if strategy.position_size < 0
entryOrderPlaced := false // Entry order executed
if not entryOrderPlaced and strategy.position_size == 0
patternHigh := na
patternLow := na
stopLossPrice := na
takeProfitPrice := na
// Plot VWAP and markers for visualization
plot(vwap, color=color.orange, title="VWAP")
plotshape(isEntrySignal, title="Entry Signal", location=location.abovebar, color=color.green, style=shape.arrowdown, text="Sell Signal")
Explanation:
- VWAP Calculation: We use the built-in
ta.vwap function to calculate the Volume Weighted Average Price.
- Entry Conditions:
- Price Touches VWAP from Below: We check if the current low is less than or equal to the VWAP and the previous close was below the previous VWAP.
- Shooting Star Formation: We define a function
isShootingStar() that returns true if the candle meets the criteria of a Shooting Star.
- Higher Volume than Last 5 Candles: We compare the current volume to the maximum volume of the previous 5 candles.
- Order Placement:
- When all entry conditions are met, we store the high and low of the Shooting Star.
- We calculate the Stop Loss price by adding 2% of the daily ATR to the high of the Shooting Star.
- The Take Profit is set to achieve a 1:2 risk to reward ratio.
- We place a Sell Stop order at the low of the Shooting Star using
strategy.entry() with the stop parameter.
- We set the exit conditions using
strategy.exit() with stop and limit parameters for Stop Loss and Take Profit, respectively.
- Order Cancellation:
- If the high of the Shooting Star is broken before the entry order is filled, we cancel both the entry and exit orders using
strategy.cancel().
- Variable Reset:
- We reset the variables once the position is entered or if the entry order is canceled.
- Visualization:
- We plot the VWAP on the chart for reference.
- We use
plotshape() to mark the entry signals on the chart.
Note: This strategy assumes that you are trading on a timeframe lower than the daily chart since it uses the daily ATR for Stop Loss calculations. Also, due to limitations in Pine Script, we cannot determine the exact sequence of price movements within a candle. Therefore, we simulate the condition of the next candle breaking the low before the high by monitoring the price levels after the Shooting Star formation.
Important: Always test any trading strategy thoroughly before using it in live trading. Past performance is not indicative of future results.