Search code examples
pine-scripttradingpine-script-v4pine-script-v5

How do I get Average Volume of 10 days without using security function in 15 min chart?


I want to build a pine script which will give me average volume of 10 Days from 09:30 to 16:00 without using the security function. I want to calculate 10 days average volume using the 15 min chart. Help me to code this pine script. I am new to the pine script.


Solution

  • //@version=5
    indicator("10 Day volume average", overlay = false)
    
    len = input(10)
    
    var float todays_volume = na
    var float[] daily_volume_array = array.new_float()
    
    within_time_range = time >= timestamp(year, month, dayofmonth, 9, 30) and time < timestamp(year, month, dayofmonth, 16, 0)
    
    // Or if you need to account for timezone
    //within_time_range = time >= timestamp("GMT+3", year, month, dayofmonth, 9, 30) and time < timestamp("GMT+3", year, month, dayofmonth, 16, 0)
    
    if within_time_range and not within_time_range[1]
        array.unshift(daily_volume_array, todays_volume)
        if array.size(daily_volume_array) > len
            array.pop(daily_volume_array)
        todays_volume := volume
    else if within_time_range
        todays_volume += volume
    
    avg_vol = array.avg(daily_volume_array)
    
    plot(avg_vol)