help code identifying the accumulation phase

#1
I have made a code to identify the accumulation phase as follows:
ratio1 = 0.05;
ratio2 = 0.07;
ratio3 = 0.1;

function GetLength(r, Minp, Maxp, Length, i)
{
Minp = Min(Minp, Ref(Close, -i));
Maxp = Max(Maxp, Ref(Close, -i));
x = (Maxp/Minp) - 1;

if(x > r)
return Length;
Length++;
i++;
return GetLength(r, Minp, Maxp, Length, i);
}

len1 = 15;
len2 = 20;
Len3 = 60;
Minp = Close;
Maxp = Close;
Length = 1;
i = 1;
d1 = GetLength(ratio1, Minp, Maxp, Length, i);
d2 = GetLength(ratio2, Minp, Maxp, Length, i);
d3 = GetLength(ratio3, Minp, Maxp, Length, i);
Filter= (d1 >= Len1 OR d2 >= Len2 OR d3 >= Len3) AND MA(Volume, 20) >= 20000;
AddColumn(d1 , "5%", 1, colorWhite, IIf(d1 >= len1, colorLime, colorWhite));
AddColumn(d2 ,"7%", 1, colorWhite, IIf(d2 >= Len2, colorLime, colorWhite));
AddColumn(d3 ,"10%", 1, colorWhite, IIf(d3 >= Len3, colorLime, colorWhite));

but when you run this code, it is always an error as follows:"Condition in IF, WHILE, FOR statements has to be Numeric or Boolean type. You can not use array here, please...". This problem was identified in the code:"if(x > r)".
I do not know how correct, expect people to help.
Thanks
 

mastermind007

Well-Known Member
#2
I have made a code to identify the accumulation phase as follows:
ratio1 = 0.05;
ratio2 = 0.07;
ratio3 = 0.1;

function GetLength(r, Minp, Maxp, Length, i)
{
Minp = Min(Minp, Ref(Close, -i));
Maxp = Max(Maxp, Ref(Close, -i));
x = (Maxp/Minp) - 1;

if(x > r)
return Length;
Length++;
i++;
return GetLength(r, Minp, Maxp, Length, i);
}

len1 = 15;
len2 = 20;
Len3 = 60;
Minp = Close;
Maxp = Close;
Length = 1;
i = 1;
d1 = GetLength(ratio1, Minp, Maxp, Length, i);
d2 = GetLength(ratio2, Minp, Maxp, Length, i);
d3 = GetLength(ratio3, Minp, Maxp, Length, i);
Filter= (d1 >= Len1 OR d2 >= Len2 OR d3 >= Len3) AND MA(Volume, 20) >= 20000;
AddColumn(d1 , "5%", 1, colorWhite, IIf(d1 >= len1, colorLime, colorWhite));
AddColumn(d2 ,"7%", 1, colorWhite, IIf(d2 >= Len2, colorLime, colorWhite));
AddColumn(d3 ,"10%", 1, colorWhite, IIf(d3 >= Len3, colorLime, colorWhite));

but when you run this code, it is always an error as follows:"Condition in IF, WHILE, FOR statements has to be Numeric or Boolean type. You can not use array here, please...". This problem was identified in the code:"if(x > r)".
I do not know how correct, expect people to help.
Thanks
1) You need to replace if statement with
if (x > r)
or something else equivalent

2) Amibroker does not support recursion in user defined functions.
You will have to convert that into a loop.
 
#3
1) You need to replace if statement with
if (x > r)
or something else equivalent

2) Amibroker does not support recursion in user defined functions.
You will have to convert that into a loop.

I have corrected the code as follows:

ratio = 0.05;
Minp[0] = Close;
Maxp[0] = Close;
Length = 1;

for(i = 1; i < BarCount; i++)
{
Minp = Min(Minp[i-1], Close[ BarCount - i]);
Maxp = Max(Maxp[i-1], Close[ BarCount - i]);

if((Maxp / Minp) - 1 > ratio)
{
break;
}

Length = i;
}

ami have the following error message:"variable 'Maxp' used without having been initialized". This error occurs in the code"Maxp = Max(Maxp[i-1], Close[ BarCount - i]);". I do not understand why, obviously I have already declared variable maxp.
 

shirajroz

Active Member
#4
I have corrected the code as follows:

ratio = 0.05;
Minp[0] = Close;
Maxp[0] = Close;
Length = 1;

for(i = 1; i < BarCount; i++)
{
Minp = Min(Minp[i-1], Close[ BarCount - i]);
Maxp = Max(Maxp[i-1], Close[ BarCount - i]);

if((Maxp / Minp) - 1 > ratio)
{
break;
}

Length = i;
}

ami have the following error message:"variable 'Maxp' used without having been initialized". This error occurs in the code"Maxp = Max(Maxp[i-1], Close[ BarCount - i]);". I do not understand why, obviously I have already declared variable maxp.


Code:
ratio = 0.05;
Minp = Close;
Maxp = Close;
Length = 1;

for(i = 1; i < BarCount; i++)
{
Minp[i] = Min(Minp[i-1], Close[ BarCount - i]);
Maxp[i] = Max(Maxp[i-1], Close[ BarCount - i]);

if((Maxp[i] / Minp[i]) - 1 > ratio)
{
break;
}

Length = i;
}
 

trash

Well-Known Member
#5
I have corrected the code as follows:

ratio = 0.05;
Minp[0] = Close;
Maxp[0] = Close;
Length = 1;

for(i = 1; i < BarCount; i++)
{
Minp = Min(Minp[i-1], Close[ BarCount - i]);
Maxp = Max(Maxp[i-1], Close[ BarCount - i]);

if((Maxp / Minp) - 1 > ratio)
{
break;
}

Length = i;
}

ami have the following error message:"variable 'Maxp' used without having been initialized". This error occurs in the code"Maxp = Max(Maxp[i-1], Close[ BarCount - i]);". I do not understand why, obviously I have already declared variable maxp.


Because you can not assign an entire array to an array element (as you did) in lines two and three.
Start reading https://www.amibroker.com/guide/h_understandafl.html


Code:
ratio = 0.05;
Minp[0] = Close[0];
Maxp[0] = Close[0];
Length = 0;

for( i = 1; i < BarCount; i++ )
{
    Minp[i] = Min( Minp[i - 1], Close[ i ] );
    Maxp[i] = Max( Maxp[i - 1], Close[ i ] );

    if( ( Maxp[i] / Minp[i] ) - 1 > ratio )
    {	  
        break;
    } 

    Length = i;
}

bi = BarIndex();
Minp = ValueWhen( bi <= length, minp );
Maxp = ValueWhen( bi <= length, maxp );

Plot( C, "Price", colorDefault, styleCandle );
Plot( maxp, "Maxp", colorGreen, styleline );
Plot( minp, "Minp", colorRed, styleline );
Title = StrFormat( "Length: %g, MinP: %g, MaxP: %g", Length, Minp, Maxp );

SetBarsRequired( -2, -2 );
 

mastermind007

Well-Known Member
#6
IMHO, the loop needs to be inverted. It starts with the nearest price first ...
In essence, you are solving the formula that can be somewhat expressed as

HHV(Close, ?) / LLV(Close, ?) >= desiredRatio;

You can further shrink the code size if you get rid of all of the extra variables used only once

Code:
ratio1 = 0.05;
ratio2 = 0.07;
ratio3 = 0.1;

len1 = 15;
len2 = 20;
Len3 = 60;

function GetLength(r)
{
	MaxP = MinP = Close[BarCount - 1];
	[B][U]for (i = BarCount - 1; i > 1; i--)[/U][/B]
	{
		MinP = Min(Minp, Close[i - 1]);
		MinP = Max(Minp, Close[i - 1]);
		x = (Maxp/Minp) - 1;
	
		if (x > r)
			break;
	}

	return BarCount - i;
}

d1 = GetLength(ratio1);
d2 = GetLength(ratio2);
d3 = GetLength(ratio3);

Filter= 
(d1 >= Len1) OR (d2 >= Len2) OR (d3 >= Len3)) 
AND MA(Volume, 20) >= 20000;
AddColumn(d1 , "5%", 1, colorWhite, IIf(d1 >= len1, colorLime, colorWhite));
AddColumn(d2 ,"7%", 1, colorWhite, IIf(d2 >= Len2, colorLime, colorWhite));
AddColumn(d3 ,"10%", 1, colorWhite, IIf(d3 >= Len3, colorLime, colorWhite));
 
#7
Hi MasterMind007, can you explain how to interpret this afl. Identifying accumulation prior to a move can be a good strategy.

I get some nos on exploration, but don't know how to interpret?

A big thanks to OP and MasterMind007 for sharing and making it work.
 

mastermind007

Well-Known Member
#8
Hi MasterMind007, can you explain how to interpret this afl. Identifying accumulation prior to a move can be a good strategy.

I get some nos on exploration, but don't know how to interpret?

A big thanks to OP and MasterMind007 for sharing and making it work.
As I've already mentioned earlier, the above AFL simply does equivalent of Excel's What if analysis and solves for a formula (by brute force approach) with a single-unknown
and returns the value of x.

The Formula to be solved is:
HHV(C, x) / LLV(C, x) > ratio

ratio is the input parameter
The numbers you see on screen are simply the values of x for 5%, 7% and 10% ratio values. They are seed values for further technical analysis.

As an example scrip, exploaration for CEATLTD, values returned for 5%, 7% and 10% are 8, 9 and 198 respectively for last day.

To find lower ratios, the afl did not have to go too far back but for the higher ratio, it had to go back for about 6 months ...

This numbers, now form the basis of your further AFLs and you can plot more lines using this numbers as a sources for parameters.

Since length for the 5% and the 7% is too similar, I choose to use only the 5% length. 10% length is too big so I need to scale it down harmonically.
For CEATLTD, 198 divided by 8 gives you 24 as quotient and 6 as remainder, so MACD(8, 24, 6) plots a MACD that fits nicely with the CEATLTD chart.
 
Last edited:

Similar threads