OPEN-SOURCE SCRIPT

SAHIL SAXENA 1

Sure! To add the buy and sell signals explicitly in the script, we will ensure that the strategy generates a clear "Buy" or "Sell" signal based on the conditions provided. Here's the updated Pine Script that includes the **buy** and **sell** signals along with visual plots.

### Updated Code with Buy/Sell Signal:

```pinescript
//version=5
indicator("VWAP Scalping Strategy (3m) with Signals", overlay=true)

// Input parameters
vwapLength = input.int(1, title="VWAP Length", minval=1)
smaLength = input.int(9, title="SMA Length for Trend Filter", minval=1)
rsiLength = input.int(14, title="RSI Length", minval=1)
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
stopLossPerc = input.float(0.2, title="Stop Loss (%)", minval=0.1, step=0.1)
takeProfitPerc = input.float(0.4, title="Take Profit (%)", minval=0.1, step=0.1)

// VWAP Calculation
vwap = ta.vwap(close)

// Simple Moving Average (SMA) for trend filtering
sma = ta.sma(close, smaLength)

// RSI Calculation
rsi = ta.rsi(close, rsiLength)

// Volume confirmation (optional, but can help with validating signals)
volumeSpike = volume > ta.sma(volume, 20) * 1.5

// Buy and Sell conditions
buySignal = ta.crossover(close, vwap) and close > sma and rsi < rsiOversold and volumeSpike
sellSignal = ta.crossunder(close, vwap) and close < sma and rsi > rsiOverbought and volumeSpike

// Plot VWAP and SMA
plot(vwap, color=color.blue, linewidth=2, title="VWAP")
plot(sma, color=color.orange, linewidth=2, title="SMA (Trend Filter)")

// Plot Buy and Sell signals
plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal", text="BUY")
plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal", text="SELL")

// Take Profit and Stop Loss Logic (for scalping)
var float buyPrice = na
var float stopLoss = na
var float takeProfit = na

if (buySignal)
buyPrice := close
stopLoss := buyPrice * (1 - stopLossPerc / 100)
takeProfit := buyPrice * (1 + takeProfitPerc / 100)

if (sellSignal)
buyPrice := na
stopLoss := na
takeProfit := na

// Exit conditions based on stop-loss and take-profit
longExit = (not na(buyPrice) and (close <= stopLoss or close >= takeProfit))
shortExit = (not na(buyPrice) and (close >= stopLoss or close <= takeProfit))

// Plot stop-loss and take-profit levels for reference
plot(series=longExit ? na : stopLoss, color=color.red, linewidth=1, style=plot.style_linebr)
plot(series=longExit ? na : takeProfit, color=color.green, linewidth=1, style=plot.style_linebr)

// Alert conditions for Buy and Sell signals
alertcondition(buySignal, title="Buy Signal", message="Price has crossed above VWAP, confirmed with RSI and volume spike.")
alertcondition(sellSignal, title="Sell Signal", message="Price has crossed below VWAP, confirmed with RSI and volume spike.")
```

### Breakdown of Changes:
1. **Buy and Sell Signal Logic**:
- **Buy Signal**: This occurs when:
- The price crosses above the VWAP (`ta.crossover(close, vwap)`),
- The price is above the 9-period SMA (`close > sma`), confirming the bullish trend,
- The RSI is below 30 (oversold condition), suggesting a potential reversal or a bounce,
- There’s a volume spike, confirming stronger participation.
- **Sell Signal**: This occurs when:
- The price crosses below the VWAP (`ta.crossunder(close, vwap)`),
- The price is below the 9-period SMA (`close < sma`), confirming the bearish trend,
- The RSI is above 70 (overbought condition), suggesting a potential reversal or pullback,
- There’s a volume spike, confirming stronger participation.

2. **Buy and Sell Signal Plots**:
- Green labels (`plotshape(series=buySignal, ...)`) are plotted below the bars to indicate **Buy** signals.
- Red labels (`plotshape(series=sellSignal, ...)`) are plotted above the bars to indicate **Sell** signals.

3. **Stop Loss and Take Profit**:
- For each **Buy Signal**, the script calculates the stop loss and take profit levels based on user-defined percentage inputs (0.2% for stop loss and 0.4% for take profit).
- It also visually plots these levels on the chart so you can monitor the exit points.

4. **Alert Conditions**:
- Custom alerts are included with `alertcondition()` so that you can be notified when a **Buy Signal** or **Sell Signal** occurs.

### How It Works:
- The strategy is designed for **scalping** on the **3-minute** chart. The signals are designed to capture small price movements.
- **Buy Signal**: Occurs when the price crosses above the VWAP, the price is above the trend filter (SMA), the RSI is in oversold territory, and there is a volume spike.
- **Sell Signal**: Occurs when the price crosses below the VWAP, the price is below the trend filter (SMA), the RSI is in overbought territory, and there is a volume spike.
- The script also includes **stop-loss** and **take-profit** levels to protect against larger losses and secure profits.

### Usage:
- Apply the strategy on a 3-minute chart for quick scalping trades.
- The **buy** and **sell** signals are marked with **green** and **red** labels on the chart, respectively.
- **Stop Loss** and **Take Profit** levels are calculated dynamically for each trade and plotted on the chart.
- You can enable **alerts** to notify you whenever a buy or sell signal is triggered.

### Final Note:
Make sure to backtest the strategy on historical data and fine-tune the parameters (e.g., stop loss, take profit, RSI, etc.) according to the market conditions and your risk tolerance. Scalping requires precision and careful risk management, so it’s important to adapt the strategy for your specific needs.

Feragatname