November 4, 2014
Indicators based on user values rather than standard OHLC prices
Sometimes we may want to calculate indicators based not only on standard OHLC prices but on some other user-definable values. Some functions like RSI or CSI have additional versions (RSIa, CCIa respectively) that accept custom input array. In this case it is very easy to calculate the indicator based on user defined value. For example RSI from average of High and Low prices could be written as follows:
customArray = ( High + Low ) / 2;
Plot( RSIa( customArray, 14 ), "RSI from (H+L)/2", colorRed )
But many of the built-in indicators available in AFL as functions refer indirectly to standard OHLC arrays and their parameters do not offer array argument as one of inputs.
Fortunatelly there is an easy way to provide custom array as input for any other built-in functions. For this purpose, it is enough to override OHLC arrays (or just Close if the indicator only uses Close as input) within the code before calling given function and assign our custom array. As a simple example, let us consider calculating MACD indicator out of average of High and Low prices as input.
procedure SaveRestorePrices( DoSave )
{
global SaveO, SaveH, SaveL, SaveC, SaveV;
if( DoSave )
{
SaveO = Open;
SaveH = High;
SaveL = Low;
SaveC = Close;
SaveV = Volume;
}
else
{
Open = SaveO;
High = SaveH;
Low = SaveL;
Close = SaveC;
Volume = SaveV;
}
}
// save OHLCV arrays
SaveRestorePrices( True );
// calculate our array
customArray = ( High + Low ) / 2;
// override built-in array(s)
Close = customArray;
// calculate our function, MACD and Signal in this case
Plot( MACD( 12, 26 ), "MACD", colorRed );
Plot( Signal( 12, 26, 9 ), "Signal", colorBlue );
// restore OHLCV arrays
SaveRestorePrices( False )
The code first calculates the custom array (we use just use average of High and Low prices in this example, but of course the calculations may be more complex), then assigns the result of these calculations to Close overriding the regular values stored in close array. Then – when we call MACD() function which uses Close as input – it will be based on the modified values.
The above operations do not affect the underlying database at all – the prices are overridden only for the purpose of calculation of this particular formula and other charts / indicators are not affected at all.
Filed by Tomasz Janeczko at 5:15 am under Indicators
Comments Off on Indicators based on user values rather than standard OHLC prices