---
description: Pine Script to Detect Buying Surges Based on 24-Hour Trading Volume
---

# Moving Average - Simple

Below is a Pine Script designed to analyze the 24-hour trading volume and detect buying surges. The script calculates the 24-hour average trading volume and identifies surges when the current volume exceeds a predefined multiplier of the average.



```java
//@version=6

indicator("Buying Surge Detector", overlay=true, shorttitle="Volume Surge") 

// Input parameters 

volumeMultiplier = input.float(1.5, title="Volume Surge Multiplier", minval=1.0) 

lookbackPeriod = input.int(1440, title="Lookback Period (Minutes)", minval=1)  // Default: 1440 minutes = 24 hours 

// Calculate average volume over the lookback period 

avgVolume = ta.sma(volume, lookbackPeriod) 


// Detect surges: Current volume > Average volume * Multiplier 

isSurge = volume > (avgVolume * volumeMultiplier) 


// Plot surge detection as a label on the chart 

plotshape(isSurge, style=shape.labelup, color=color.green, size=size.small, location=location.belowbar, title="Buying Surge") 

bgcolor(isSurge ? color.new(color.green, 90) : na, title="Background Highlight for Surge") 


// Debugging and informational plots 

plot(volume, color=color.blue, title="Current Volume") 

plot(avgVolume, color=color.orange, title="Average Volume")
```



### Script Details

{% stepper %}
{% step %}
### Inputs

* **volumeMultiplier:** defines how much higher the current volume must be in comparison to the average volume to trigger a "surge". Default is 1.5 (150% of the average volume)
* **lookbackPeriod:** this is how far the script will look back to calculate the average volume. Default is is set to 1440 minutes (24 hours of 1-minute candles)
{% endstep %}

{% step %}
### Logic&#x20;

* The script calculates the **simple moving average (SMA)** of volume over the pre-defined lookback period _(avgVolume)_
* It then checks if the current volume exceeds the average volume multiplied by the defined _volumeMultiplier._
* If the condition is met, a **buying surge** is detected.
{% endstep %}

{% step %}
### Visual Output

* A **green label** (shape.labelup) is drawn on the chart below bars where a buying surge is detected.
* The background is also highlighted faintly green during surges for additional visibility _(bgcolor)_.
* Two plots are added for reference: Current Volume (blue line) and Average Volume (orange volume)
{% endstep %}
{% endstepper %}

### Suggested Use

{% stepper %}
{% step %}
### Set the timeframe of your analysis&#x20;

Use this indicator on a lower timeframe (1 minute or 5 minute charts). This script aggregates volume based on the _lookbackPeriod_ parameter, so ensure this aligns with your strategy.
{% endstep %}

{% step %}
### Adjust the multiplier to your trading strategy

Modify the the VolumeMultiplier input to fine-tune the surge detection sensitivity. A higher multiple is better because it will result in fewer but more significant surges.
{% endstep %}

{% step %}
### Interpret the resulting signals

A green label or background indicates a potential buying surge, which points to increased liquidity and interest in the asset.
{% endstep %}
{% endstepper %}

### Optional Enhancements

{% stepper %}
{% step %}
### Add Alerts for when surges are detected

```java
alertcondition(isSurge, title="Buying Surge Alert", message="A buying surge has been detected!")
```
{% endstep %}

{% step %}
### Using exponential moving average (ta.ema) instead of SMA

```java
avgVolume = ta.ema(volume, lookbackPeriod)
```
{% endstep %}

{% step %}
### Filtering based on price action&#x20;

Filtering based on price action (only detect surges when the close price is higher than the open price)
{% endstep %}
{% endstepper %}
