April 22, 2009
IMPORTANT: This article applies ONLY to AmiBroker version 5.24 and earlier. Version 5.25 includes native support for log scale in Study() function and this workaround is no longer needed.
Some of you may have tried using Study() function in logarithmic scale charts and noticed that the output of Study() function becomes curved line (not straight) as soon as logarithmic scale is used.
Before giving you solution, I would like to state some obvious things:
A straight line in log scale is NOT straight line in linear scale and vice versa. Trendlines drawn in log scale do NOT cross at the same points (except beginning and ending) as same trendline drawn in linear scale. This is pretty much the same in all charting programs.
As for the Study() function – it always uses LINEAR equation y = a*x + b regardless of particular chart scale. So, Study() always produces straight line in the linear price domain, so “a” coefficient is constant and represents the slope in price terms (dollar per bar)
This is done so, because Automatic Analysis does not have a concept of “scale” (linear vs logarithmic), therefore if Study() function was dependent on given pane setting it would not produce the same result, if the same formula was used in automatic analysis.
If you want to have “straight” line in the logarithmic price domain you need to convert to log domain in the formula, as shown in the code below
function StudyInLogScale( Studyid, Chartid )
if( Version() < 5.25 )
SetBarsRequired( sbrAll, sbrAll );
temp = Study( Studyid, Chartid );
bi = BarIndex();
beg = LastValue( ValueWhen( ExRem( temp, 0 ), bi ) );
end = LastValue( ValueWhen( temp, bi ) );
result = Null;
if( beg < BarCount AND end < BarCount )
begval = temp[ beg ];
endval = temp[ end ];
factor = endval/begval;
for( i = beg; i <= end; i++ )
result[ i ] = begval * factor ^ ( ( i - beg )/( end - beg ) );
result = Study( Studyid, Chartid );
logscale = ParamToggle("Log Scale", "Off|On", 0 );
SetChartOptions( IIf( logScale, 2, 3 ), chartLogarithmic );
Plot( C, "Price", colorBlack, styleCandle );
if( logscale AND Version() <= 5.24 )
ss = StudyInLogScale("RE", GetChartID() );
ss = Study("RE", GetChartID() );
Plot( ss, "Study", colorRed );