Array Value Differs in different periodicity

I am running a piece of code with 1 minute and 4 minute periodicity. By periodicity i mean Analysis Settings-->Periodicity. The values that I run for these 2 runs are different whereas I expect them to be same. Below is the copy-paste runnable code. Here is the price data file that needs to be used.

_TRACE( "!CLEAR!" );
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates | chartWrapTitle );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
PlotOHLC( O, H, L, C, "Close", colorRed, styleBar , Null, Null, 0, 1, 1 );
_SECTION_END();

FwdSearchDistance = 14;

_Trace( "BarScore for FwdSearchDistance=" + FwdSearchDistance );

minSearchDistanceForBarScore = 2;

constScoreTypeLL = 1;
constScoreTypeHH = 2;

maxSearchDistanceForBarScore=50;


function calculateBarScore()
{
    if( Status( "actionex" ) != actionExAAParameters && Status( "actionex" ) != actionPortfolio )
    {
        startTime = Now( 5 );
        thisTimeframeinseconds = Interval();
        thisTimeframeinMinutes = thisTimeframeinseconds / 60;


        distCounterStartValue = minSearchDistanceForBarScore;
        InterveaningBarCounter = 0;

        for( distanceCounter = distCounterStartValue; distanceCounter <= maxSearchDistanceForBarScore; distanceCounter++ )
        {
            intersectionReachedHH = 0;
            intersectionReachedLL = 0;
            sourceBarHigh = H;
            destBarHigh = Ref( H, distanceCounter );
            sourceBarLow = L;
            destBarLow = Ref( L, distanceCounter );
            aHH = ( destBarHigh - sourceBarHigh ) / distanceCounter;
            aLL = ( destBarLow - sourceBarLow ) / distanceCounter;
            distanceIdentifier = distanceCounter; //Max(0, distanceCounter - minSearchDistanceForBarScore);
            distanceIsMoreThanBarCount = BarIndex() + distanceCounter > ( BarCount - 1 );


            for( InterveaningBarCounter = 1; InterveaningBarCounter < distanceCounter; InterveaningBarCounter++ )
            {
                lineHHAtInterveaningBar = aHH * InterveaningBarCounter  + sourceBarHigh;    //value of line_HH at bar (i+k)
                lineLLAtInterveaningBar = aLL * InterveaningBarCounter  + sourceBarLow;    //value of line_LL at bar (i+k)
                intersectionReachedHH = intersectionReachedHH || lineHHAtInterveaningBar <= Ref( H, InterveaningBarCounter );
                intersectionReachedLL = intersectionReachedLL || lineLLAtInterveaningBar >= Ref( L, InterveaningBarCounter );

                VarSet( "bsHH_" + distanceIdentifier, IIf( !distanceIsMoreThanBarCount && !intersectionReachedHH && lineHHAtInterveaningBar > Ref( H, InterveaningBarCounter ),
                        InterveaningBarCounter, IIf( distanceIdentifier == 0, 0, Nz( Varget( "bsHH_" + ( distanceIdentifier - 1 ) ) ) ) ) );
                VarSet( "bsLL_" + distanceIdentifier, IIf( !distanceIsMoreThanBarCount && !intersectionReachedLL && lineLLAtInterveaningBar < Ref( L, InterveaningBarCounter ),
                        InterveaningBarCounter, IIf( distanceIdentifier == 0, 0, Nz( Varget( "bsLL_" + ( distanceIdentifier - 1 ) ) ) ) ) );


            }

            currenIntervalInSeconds = Interval(); //_TRACE("currenIntervalInSeconds="+currenIntervalInSeconds);
            StaticVarSet( Name() + "_interval_" + currenIntervalInSeconds + "_bsHH_" + distanceIdentifier,  VarGet( "bsHH_" + distanceIdentifier ), 0, cmAlways );
            StaticVarSet( Name() + "_interval_" + currenIntervalInSeconds + "_bsLL_" + distanceIdentifier, VarGet( "bsLL_" + distanceIdentifier ), 0, cmAlways );
        }


        endTime = Now( 5 );
        runTime = round( DateTimeDiff( endTime, startTime ) ) ;


    }// end if
}//end of function

function getBarScore( arrayFwdSearchDistance, barScoreType )
{
    retval = Null;
    currenIntervalInSeconds = Interval();

    for( searchDistanceCounter = minSearchDistanceForBarScore; searchDistanceCounter <= maxSearchDistanceForBarScore; searchDistanceCounter++ )
    {
        distanceIdentifier = searchDistanceCounter;

        retval =  IIf( arrayFwdSearchDistance == searchDistanceCounter, iif( barScoreType == constScoreTypeHH, StaticVarGet( Name() + "_interval_" + currenIntervalInSeconds + "_bsHH_" + distanceIdentifier ), StaticVarGet( Name() + "_interval_" + currenIntervalInSeconds + "_bsLL_" + distanceIdentifier ) ), retval );
    }


    return retval;
}

PatternTimeFrame=in1Minute * 13;
TimeFrameSet(PatternTimeFrame);
calculateBarScore();

bsHH = getBarScore( maxSearchDistanceForBarScore, constScoreTypeHH );
//bsLL = getBarScore( maxSearchDistanceForBarScore, constScoreTypeLL );
TimeFrameRestore();

bsHH=TimeFrameExpand(bsHH, PatternTimeFrame);
//bsLL=TimeFrameExpand(bsLL, PatternTimeFrame);

atr1 = ATR( 1 );

for( i = 1; i < BarCount; i++ )
{
    PlotTextSetFont( "" + bsHH[i], "windings", 7, i , H[i] + atr1[i] / 3 , colorYellow, -100 );//
    //PlotTextSetFont( "" + bsLL[i], "windings", 7, i , L[i] - atr1[i] / 3 , colorYellow, -100 );//

}

_TRACE( "bsHH=" + bsHH );
_TRACE( "barindex()=" + barindex() );

Filter=1;
AddColumn(bsHH, "bsHH");
//AddColumn(bsLL, "bsLL");


See the Exploration screenshot below. First one is for periodicity 1 minute and the next one is for periodicity 4 minutes.


I don't know what this code is supposed to do (you did not describe it), but one thing caught my attention, you are doing this:

So you are calculating something in 13 minute interval. It works well when you select periodicity of 1-minute. But when you select periodicity of 4-minute (so input bars are 4-minute), you can't really switch to 13-minute interval because 13 is NOT divisible by 4. TimeFrameSet compresses current data to desired interval. For compression to work the desired interval must be integer multiple of input interval.

1 Like

Thankyou for responding so quickly.

My main program is this

  1. Identify a pattern in PatternTimeFrame (Lets say 4 Hours interval).

  2. Imagine this pattern has 7 legs. Leg-A, Leg-B, Leg-C, Leg-D, Leg-E, Leg-F, Leg-G. Each of this leg is a pattern in itself. So each leg will have 7 sub legs. Now in order to confirm the pattern i need to ensure that each of these legs have the necessary 7 legs. And for that I need to go to lower time frame. This lower time frame could be 1-Hour, 10-mins, 35-Mins anything etc. I must iterate one by one. I check upto 1/60th of the patterntimeframe. That means if the main pattern was in 4 hours I check all the way down till 4 minutes.

  3. I set the Analysis Window-->Periodicity to the lowest time frame I need. In this example it is 4 minutes (1/60th of 4 Hours). Why don't i set it to 1 minute? Because if i set it to 1 minute the EXECUTION TIME INCREASES BY 100 TIMES.

  4. The 13-minute time frame is sub-leg verification timeframe.

This is the whole story.

Now I was not aware of that for compression to work desired interval must be integer multiple of input interval. It would be great if you can put in this line in the mutiple timeframe tutorial that is part of amibroker user guide, it may save people time.

Can you think of any workaround to this?