How to make any function accepting variable period

As you know some functions accept variable periods
http://www.amibroker.com/guide/a_varperiods.html

If you need some function that is not on that list, you can usually easily implement variable-period version using existing functions, for example:

Variable period ROC

function VarROC( array, periods )
{
  prev = Ref( array, -periods );
  return 100*(array - prev)/prev;
}

Variable period RSI

function VarRSI( array, periods )
{
  Chg = array - Ref( array, -1 );
  pc = Max( Chg, 0 );
  nc = Max( -Chg, 0 );

  pa = AMA( pc, 1/periods );
  na = AMA( nc, 1/periods );

  return 100 * pa / ( pa + na );
} 

Variable period EMA

function VarEMA( array, period )
{
    return AMA( array, 2 / ( period + 1 ) );
}

Variable period Correlation

//Variable period correlation
function VarCorrelation( x, y, number )
{
    nom = MA( x * y, number ) - MA( x, number ) * MA( y, number );
    denom = sqrt( MA( x ^ 2, number ) - MA( x, number ) ^ 2 ) *
            sqrt( MA( y ^ 2, number ) - MA( y, number ) ^ 2 );
    return nom / denom;
}

Variable period Stochastic %K

function VarStochK( array, period1, period2 )
{
  hh = HHV( High, period1 );
  ll = LLV( Low, period1 );
  
  x = 100*(Close - ll)/(hh-ll);
  
  return MA( x, period2 );
}

Doing so typically requires knowledge of how indicator works internally. Although it is good idea to know how indicators work to build the system that is based on their inner workings, and it is really better to use above strict implementations, sometimes users urge to have "quick" solution....

So here it is brute force solution to "any indicator with variable period" problem. It allows to transform constant period functions into variable period functions. It does that by calculating constant period indicator N times where N is a number of different "period" values used in the calling code. It is not very efficient but not bad as a quick hack.

function YourConstantPeriodIndicator( constant_period )
{
  // any constant period function here
  return ROC( C, constant_period );
}

function YourVariablePeriodIndicator( period )
{
	minperiod = LastValue( Lowest( period ) );
	maxperiod = LastValue( Highest( period ) );
	
	result = Null;
	
	for( p = minperiod; p <= maxperiod; p++ )
	{	
		ind = YourConstantPeriodIndicator( p );
		
		result = IIf( period == p, ind, result );
	}
	
	return result;
}

varperiods =  Max( 1, Cum(1) - 1 );

Plot( YourVariablePeriodIndicator( varperiods ) , "Test", colorRed );
24 Likes

Hey Tomasz,

Has the code to implement this changed in latest Amibroker version 6.30.0?
I can't get the test function code for YourVariablePeriodIndicator( period ) to display at all.
Any ideas?

Cheers,

Axismagi

Nothing changed.

It works but the very last example was passing one period too much. For example for bar zero there is no ROC( C, 1 ), and for bar 1 there is no ROC( C, 2 ). The code is corrected now.

2 Likes

Aha! Thank you for explanation and the updated code.

Cheers

Excellent! Thanks Tomasz!