amibroker

More elegant way to convert this function to return array instead of single value in Amibroker


I have the following function which returns a single value.

function getVolumeHigh_excludeUpBars(period)
{
    volume_exclude_up = IIf( ROC(CLOSE,1) < 0, Volume, 0);

    SELECTED_BAR = SelectedValue( BarIndex() );
    volume_exclude_up[SELECTED_BAR] = Volume[SELECTED_BAR]; 

    volume_High = hhv(volume_exclude_up, period);
    return volume_High;
}

I want to convert the above function to return an array instead of a single value. I rewrote the function. Here it is;

function getArray_VolumeHigh_excludeUpBars(period)
{
    volume_exclude_up = IIf( ROC(CLOSE,1) < 0, Volume, 0);

    for (i=(BAR_COUNT-1);i>=0;i--)
    {
        volume_exclude_up[i] = Volume[i];   
        volume_High[i] = hhv(volume_exclude_up, period);
    }   

    return volume_High;
}

The rewritten function is inefficient as it uses for-do loop to assign value individually into the array. Is there a more efficient and elegant way to rewrite the function?


Solution

  • For what you're trying to achieve, this looks like it should be fine

    volume_exclude_up = IIf( ROC(CLOSE,1) < 0, Volume, 0);
    volume_High = hhv(volume_exclude_up, period);
    

    With

    volume_exclude_up[SELECTED_BAR] = Volume[SELECTED_BAR]; 
    

    and

    volume_exclude_up[i] = Volume[i]; 
    

    you are changing the original volume_exclude_up variable from the conditional to volume.

    Either you want volume_exclude_up to equal volume all the time, or equal it conditionally, your code first says equal it conditionally, then changes it's mind and says, equal it all the time. So in effect, decide whether you want

    volume_high = hhv(volume_exclude_up, period)
    

    or

    volume_high = hhv(v, period)