Here's a summary of its main components:
Let me know your thoughts and if you know anyways for it to be improved
- VWAP and Bands Calculation:
- The script calculates the Volume Weighted Average Price (VWAP) and its standard deviation-based upper and lower bands.
- Two levels of bands are created: one set is 1 standard deviation above/below VWAP, and the other is 2 standard deviations.
- Volume Analysis:
- The script checks for high-volume breakout conditions or low-volume pullback conditions to confirm significant price moves.
- Bullish Sentiment Analysis:
- It analyzes candle patterns over a specified lookback period to confirm a bullish trend (e.g., most candles closing above their open).
- Momentum Confirmation:
- Positive and sustained momentum conditions are checked to ensure the price move has strength.
- Overextension Check:
- The Average True Range (ATR) is used to identify if the price is overextended (too far above VWAP).
- RSI Momentum:
- The Relative Strength Index (RSI) is evaluated to confirm bullish momentum (RSI above 50).
- Consolidation Detection:
- Detects periods when the price is stuck near VWAP, which might indicate consolidation and avoids generating signals during such periods.
- Breakout and Recovery Conditions:
- Signals are generated only when the price is sufficiently far from VWAP and exhibits breakout characteristics.
- Buy Signal Logic:
- Combines all the above conditions into a refined buy signal. It includes checks for:
- Price being slightly above VWAP or bands (with tolerance).
- Volume, sentiment, momentum, and RSI confirmation.
- Avoiding overextension and consolidation.
- Combines all the above conditions into a refined buy signal. It includes checks for:
Code:
# Inputs
input numDevUp1 = 1.0; # Upper Band 1 multiplier
input numDevDn1 = -1.0; # Lower Band 1 multiplier
input numDevUp2 = 2.0; # Upper Band 2 multiplier
input numDevDn2 = -2.0; # Lower Band 2 multiplier
input volumeLookback = 30; # Lookback period for average volume
input sentimentLookback = 5; # Lookback period for bullish sentiment
input tolerancePercent = 1.0; # Tolerance for recovery signals (in percentage)
input atrMultiplier = 3.0; # Threshold for overextension
input rsiLength = 14; # RSI Length
# VWAP Calculation
def totalVolume = TotalSum(volume);
def totalPriceVolume = TotalSum(volume * close);
def VWAP = totalPriceVolume / totalVolume;
# VWAP Line
plot VWAPLine = VWAP;
VWAPLine.SetDefaultColor(Color.CYAN);
# VWAP Bands
def sumSquaredDiff = TotalSum(volume * Sqr(close - VWAP));
def stdDev = Sqrt(sumSquaredDiff / totalVolume);
plot UpperBand1 = VWAP + numDevUp1 * stdDev;
UpperBand1.SetDefaultColor(Color.RED);
plot UpperBand2 = VWAP + numDevUp2 * stdDev;
UpperBand2.SetDefaultColor(Color.DARK_RED);
plot LowerBand1 = VWAP + numDevDn1 * stdDev;
LowerBand1.SetDefaultColor(Color.GREEN);
plot LowerBand2 = VWAP + numDevDn2 * stdDev;
LowerBand2.SetDefaultColor(Color.DARK_GREEN);
# Volume Confirmation
def avgVolume = Average(volume, volumeLookback);
def recentHighVolume = Sum(volume > avgVolume * 1.5, 5) > 0; # High volume breakout in last 5 bars
def isPullbackVolume = volume < avgVolume; # Pullback on low/average volume
def isVolumeConditionMet = recentHighVolume or isPullbackVolume;
# Bullish Sentiment Confirmation
def bullishSentiment = close > open and close > close[1];
def bullishCount = Sum(bullishSentiment, sentimentLookback);
def isBullishSentiment = bullishCount >= sentimentLookback * 0.6; # At least 60% bullish candles in lookback
# Momentum Confirmation
def momentum = close - close[1];
def strongMomentum = momentum > 0 and momentum > momentum[1]; # Strong positive momentum
def sustainedMomentum = close > close[1] and close[1] > close[2]; # Follow-through momentum
# ATR for Overextension
def atr = Average(TrueRange(high, low, close), 14);
def isOverextended = close > VWAP + atr * atrMultiplier; # Price too far above VWAP
# RSI Confirmation
def rsi = reference RSI(length = rsiLength);
def isRSIStrong = rsi > 50; # RSI indicates bullish momentum
# Consolidation Detection
def isConsolidating = AbsValue(close - VWAP) < (VWAP * 0.005); # Price within 0.5% of VWAP
def isStuckNearVWAP = Sum(isConsolidating, 10) > 8; # Consolidation over 8 of last 10 bars
# Breakout Momentum Detection
def breakoutMomentum = close > Max(high[1], high[2]) and volume > avgVolume * 1.5;
# Minimum Distance from VWAP
def isFarEnoughFromVWAP = AbsValue(close - VWAP) > (VWAP * 0.003); # Price at least 0.3% from VWAP
# Recovery Detection Above VWAP or Bands
def isAboveVWAP = close > VWAP;
def isAboveUpperBand1 = close > UpperBand1;
def isAboveUpperBand2 = close > UpperBand2;
def isAboveLowerBand1 = close > LowerBand1;
def isAboveLowerBand2 = close > LowerBand2;
# Slightly Above Detection with Tolerance
def tolerance = tolerancePercent / 100;
def isSlightlyAboveVWAP = close > VWAP * (1 + tolerance);
def isSlightlyAboveBands = close > VWAP and (isAboveLowerBand1 or isAboveLowerBand2 or isAboveUpperBand1 or isAboveUpperBand2);
# Refined Buy Signal Logic
def BuySignalCondition = (isSlightlyAboveVWAP or isSlightlyAboveBands)
and isVolumeConditionMet
and isBullishSentiment
and strongMomentum
and sustainedMomentum
and !isOverextended
and isRSIStrong
and !isStuckNearVWAP # Avoid consolidation
and breakoutMomentum # Require breakout
and isFarEnoughFromVWAP; # Avoid signals too close to VWAP
# Plot Buy Signal
plot BuySignalArrow = if BuySignalCondition then low - (low * 0.01) else Double.NaN;
BuySignalArrow.SetDefaultColor(Color.GREEN);
BuySignalArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
# Add Labels for Visual Feedback
AddLabel(yes, "VWAP & Bands Active", Color.YELLOW);
AddLabel(yes, "Volume Condition Met: " + isVolumeConditionMet, if isVolumeConditionMet then Color.GREEN else Color.RED);
AddLabel(yes, "Bullish Sentiment: " + isBullishSentiment, if isBullishSentiment then Color.GREEN else Color.RED);
AddLabel(yes, "Above VWAP or Bands: " + (isAboveVWAP or isSlightlyAboveBands), if isAboveVWAP or isSlightlyAboveBands then Color.GREEN else Color.RED);
AddLabel(yes, "Buy Signal: " + BuySignalCondition, if BuySignalCondition then Color.GREEN else Color.RED);
Let me know your thoughts and if you know anyways for it to be improved