//@version=5
strategy("VWAP Crossover Strategy with Entry, Stop Loss, Target, and Exit Logic",
overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// VWAP calculation
vwapValue = ta.vwap(close)
// Plot VWAP
plot(vwapValue, title="VWAP", color=color.blue)
// ATR settings for stop loss and target levels
atrMultiplierSL = input.float(1.5, title="ATR Multiplier for Stop Loss")
atrMultiplierTP = input.float(2.0, title="ATR Multiplier for Target")
atrLength = input.int(14, title="ATR Length")
atrValue = ta.atr(atrLength)
// Trading direction selection
tradeDirection = input.string("Both", title="Trading Direction", options=["Both",
"Buy Only", "Sell Only"])
// Define buy and sell conditions
longCondition = ta.crossover(close, vwapValue)
shortCondition = ta.crossunder(close, vwapValue)
// Variables to store entry, stop loss, and target levels
var float entryPrice = na
var float stopLossPrice = na
var float targetPrice = na
// Buy entry logic
if (tradeDirection == "Both" or tradeDirection == "Buy Only") and longCondition
entryPrice := high // Buy entry at the high of the signal candle
stopLossPrice := entryPrice - atrMultiplierSL * atrValue // Stop loss below
entry
targetPrice := entryPrice + atrMultiplierTP * atrValue // Target above entry
strategy.entry("Buy", strategy.long)
// Sell entry logic
if (tradeDirection == "Both" or tradeDirection == "Sell Only") and shortCondition
entryPrice := low // Sell entry at the low of the signal candle
stopLossPrice := entryPrice + atrMultiplierSL * atrValue // Stop loss above
entry
targetPrice := entryPrice - atrMultiplierTP * atrValue // Target below entry
strategy.entry("Sell", strategy.short)
// Exit logic based on stop loss or target
if strategy.position_size > 0 // If in a long position
if low <= stopLossPrice // Exit if price hits stop loss
strategy.close("Buy", comment="Long Stop Loss")
if high >= targetPrice // Exit if price hits target
strategy.close("Buy", comment="Long Target")
if strategy.position_size < 0 // If in a short position
if high >= stopLossPrice // Exit if price hits stop loss
strategy.close("Sell", comment="Short Stop Loss")
if low <= targetPrice // Exit if price hits target
strategy.close("Sell", comment="Short Target")
// Plot entry, stop loss, and target lines
plot(entryPrice, title="Entry Price", color=color.blue, style=plot.style_linebr,
linewidth=2)
plot(stopLossPrice, title="Stop Loss", color=color.red, style=plot.style_linebr,
linewidth=2)
plot(targetPrice, title="Target Price", color=color.green, style=plot.style_linebr,
linewidth=2)
// Fill background between entry and stop loss (red) and between entry and target
(green)
fill(plot(entryPrice, "Entry Line", color=color.blue), plot(stopLossPrice, "Stop
Loss Line", color=color.red), color=color.new(color.red, 85))
fill(plot(entryPrice, "Entry Line", color=color.blue), plot(targetPrice, "Target
Line", color=color.green), color=color.new(color.green, 85))
// Optional plot shapes for visual cues
plotshape(series=longCondition and (tradeDirection == "Both" or tradeDirection ==
"Buy Only"), location=location.belowbar, color=color.green, style=shape.labelup,
title="Buy Signal")
plotshape(series=shortCondition and (tradeDirection == "Both" or tradeDirection ==
"Sell Only"), location=location.abovebar, color=color.red, style=shape.labeldown,
title="Sell Signal")