# RELATIVE VOLUME OF THE FIRST 5 MIN CANDLE

#### mike_usa

##### New member
This my first attempt to write a code , but it seems not working . Could anyone from the expert coder help me to correct whatever is missing
@barbaros ;
what I'm trying to do a RVOL scan for first 5 mins candle to the sum of the average volume of the same candle from previous last 5 days
Code:
``````# Define the start and end times for the time range
def startTime = 930;
def endTime = 935;

# Calculate the average volume for the first 5-minute bar after market open for the last numDays days
def numDays = 5;
DEF A= fold i = 1 to 5 with avgvol = 0 do avgvol +volume [i];
DEF AVGVOL5 = A / 5;
# Check if the current 5-minute bar volume is greater than the average volume
def condition = volume > avgVol5;
DEF RVOL = CONDITION >1;
# Plot the scan results on the chart
plot Scan = RVOL;``````

#### barbaros

Staff member
Try this. I'm not the original coder of this but tweaked it to give you the result that you are looking for. You will need to set your chart to 5m timeframe. I didn't test it too much other than just looking at values on the chart.

Code:
``````## RVol Dynamic
## Free for use. Header credits must be included when any form of the code included in this package is used.
## Any indicator built on this indicator needs to attribute the original author's work

input numDays = 5;
input startTime = 0930;

def BarToBeAveraged = (secondsFromTime(startTime)==0) and GetDay() <> GetLastDay();
def SignalBar = (secondsFromTime(startTime)==0);
def TotalSignalCount = if BarToBeAveraged then TotalSignalCount + 1 else TotalSignalCount;

def bar = BarNumber();
def BarNum = if !IsNaN(close) and BarNumber()>0 then bar else BarNum;
def VBar = HighestAll(BarNum);

def SignalRangeTotal;
if BarNumber() == 0 {
SignalRangeTotal = 0;
} else if BarToBeAveraged {
SignalRangeTotal = SignalRangeTotal + volume;
} else {
SignalRangeTotal = SignalRangeTotal;
}

def TotalToBeRemoved = fold i = 0 to VBar while ((TotalSignalCount - GetValue(TotalSignalCount,i)) <= numDays) and TotalSignalCount > numDays do
if BarToBeAveraged then TotalToBeRemoved + GetValue(volume,i)
else TotalToBeRemoved;
def CumVol = SignalRangeTotal - TotalToBeRemoved;
def RVol = CumVol / numDays;

AddLabel(yes, "Cum Volume:" + CumVol, Color.WHITE);
AddLabel(yes, "Avg Volume:" + RVol, Color.WHITE);``````