Detecting volume spikes in Pine Script is a useful way to identify sudden increases in trading activity, which can signal potential trend reversals, breakouts, or increased market interest. Below is a Pine Script code snippet that detects and highlights volume spikes based on a threshold (e.g., volume being significantly higher than the average volume over a specific period).
//@version=5
indicator("Volume Spikes Detection", overlay=true)
// Input parameters
volumeLength = input.int(20, title="Volume Average Length")
spikeThreshold = input.float(2.0, title="Spike Threshold Multiplier")
// Calculate average volume
avgVolume = ta.sma(volume, volumeLength)
// Detect volume spikes
volumeSpike = volume > (avgVolume * spikeThreshold)
// Plot volume spikes on the chart
plotshape(
series=volumeSpike,
location=location.belowbar,
color=color.red,
style=shape.triangleup,
size=size.small,
title="Volume Spike"
)
// Plot average volume line
hline(avgVolume, "Average Volume", color=color.blue)
// Highlight volume bars during spikes
plot(volumeSpike ? volume : na, color=color.red, title="Volume Spike Bars")
// Optional: Alert for volume spikes
alertcondition(volumeSpike, title="Volume Spike Alert", message="Volume Spike Detected!")
How It Works:
-
Average Volume Calculation:
- The script calculates the Simple Moving Average (SMA) of volume over a specified period (
volumeLength).
-
Volume Spike Detection:
- A volume spike is detected when the current volume exceeds the average volume multiplied by a threshold (
spikeThreshold).
-
Visualization:
- Volume spikes are highlighted with red triangles below the price chart.
- The average volume is plotted as a horizontal blue line.
- Volume bars during spikes are colored red for emphasis.
-
Alert:
- An optional alert is triggered when a volume spike is detected.
Customization:
- Adjust
volumeLength to change the period for average volume calculation.
- Modify
spikeThreshold to define how much higher the volume must be to qualify as a spike.
Example Use Case:
- Use volume spikes to confirm breakouts or identify potential trend reversals.
- Combine with price action analysis for stronger signals.
Let me know if you'd like further modifications or explanations!