# Guppy Count Back Line From MetaStock to Amibroker

Dear All
I found below Guppy Count Back Line formula from Metastock.

``````VCLBhi

HighDays := Input("Enter # days to cover last High for CBL calc'n:", 3, 55, 13);

If(High < HHV(High, HighDays), {then ...} PREV, {previous CBLhi, else...}
If(Ref(L,-2) < Ref(L,-1) AND Ref(L,-2) < L AND Ref(L,-1) < L, {then ...} Ref(L,-2), {2nd day back low, else...}
If((Ref(L,-3)< Ref(L,-2) AND Ref(L,-3) < Ref(L,-1) AND Ref(L,-3) < L) AND (Ref(L,-2)< L OR Ref(L,-1) < L), {then ... } Ref(L,-3), {3rd day back low, else...}
If((Ref(L,-4)< Ref(L,-3) AND Ref(L,-4) < Ref(L,-2) AND Ref(L,-4) < Ref(L,-1) AND Ref(L,-4) < L) AND (Ref(L,-3)< L OR Ref(L,-2) < L OR Ref(L,-1) < L), {then... } Ref(L,-4), {4th day back low, else...}
If((Ref(L,-5)< Ref(L,-4) AND Ref(L,-5) < Ref(L,-3) AND Ref(L,-5) < Ref(L,-2) AND Ref(L,-5) < Ref(L,-1) AND Ref(L,-5) < L) AND (Ref(L,-4)< L OR Ref(L,-3) < L OR Ref(L,-2) < L OR Ref(L,-1) < L), {then ...} Ref(L,-5), {5th day back low, else...} PREV )))))

``````

I converted to the below Amibroker afl with help of below link.

``````//Converted to AFL

HighDays= Param( " Enter # days to cover last High for CBL calc", 13, 1, 55,1 );

LowCon1 = High < HHV(High, HighDays);

LowCon2 = Ref(L,-2) < Ref(L,-1) AND Ref(L,-2) < L AND Ref(L,-1) < L;
LowCon3 = (Ref(L,-3)< Ref(L,-2) AND Ref(L,-3) < Ref(L,-1) AND Ref(L,-3) < L) AND (Ref(L,-2)< L OR Ref(L,-1) < L);
LowCon4 = (Ref(L,-4)< Ref(L,-3) AND Ref(L,-4) < Ref(L,-2) AND Ref(L,-4) < Ref(L,-1) AND Ref(L,-4) < L) AND (Ref(L,-3)< L OR Ref(L,-2) < L OR Ref(L,-1) < L);
LowCon5 = (Ref(L,-5)< Ref(L,-4) AND Ref(L,-5) < Ref(L,-3) AND Ref(L,-5) < Ref(L,-2) AND Ref(L,-5) < Ref(L,-1) AND Ref(L,-5) < L) AND (Ref(L,-4)< L OR Ref(L,-3) < L OR Ref(L,-2) < L OR Ref(L,-1) < L);

Lowresult = ValueWhen( LowCon2 OR LowCon3 OR LowCon4 OR LowCon5,
IIF( LowCon2, Ref(L,-2),
IIF( LowCon3, Ref(L,-3),
IIF( LowCon4, Ref(L,-4), Ref(L,-5)))));

Lowresult1 = ValueWhen( LowCon1, Lowresult);
Plot(Lowresult1,"\nLow",colorGreen,styleStaircase);
``````

But the both Amibroker and Metastock value is very different.

Can anyone please guide ? Is am I missing something ?

1 Like

First of all,

E.g. This long "snake"

``````x = Ref(L,-5)< Ref(L,-4) AND Ref(L,-5) < Ref(L,-3) AND Ref(L,-5) < Ref(L,-2) AND Ref(L,-5) < Ref(L,-1) AND Ref(L,-5) < L;
``````

can be simplified to this short "salamander" (note: LLV* include current bar, that's why 6 but not 5 below)

``````x = Ref(L,-5) == Ref(L, -LLVbars(L,6));
``````

Same for the other ones

And as for MS PREV I used loop

So overall

``````/// CBLhi conversion from https://www.meta-formula.com/Metastock-Formulas-C.html
/// by fxshrat@gmail.com
HighDays = 3;

cond1 = High < Ref(HHV(High, HighDays),-0);

cond2_1 = Ref(L,-2) == Ref(L, -LLVbars(L,3)) AND L > Ref(L,-1);// Ref(L,-2) < Ref(L,-1) AND Ref(L,-2) < L /*AND Ref(L,-1) < L*/;

cond3_1 = Ref(L,-3) == Ref(L, -LLVbars(L,4));//Ref(L,-3)< Ref(L,-2) AND Ref(L,-3) < Ref(L,-1) AND Ref(L,-3) < L;
cond3_2 = L > Ref(L,-2) OR L > Ref(L,-1);

cond4_1 = Ref(L,-4) == Ref(L, -LLVbars(L,5));//Ref(L,-4)< Ref(L,-3) AND Ref(L,-4) < Ref(L,-2) AND Ref(L,-4) < Ref(L,-1) AND Ref(L,-4) < L;
cond4_2 = L > Ref(L,-3) OR L > Ref(L,-2) OR L > Ref(L,-1);

cond5_1 = Ref(L,-5) == Ref(L, -LLVbars(L,6));//Ref(L,-5) < Ref(L,-4) AND Ref(L,-5) < Ref(L,-3) AND Ref(L,-5) < Ref(L,-2) AND Ref(L,-5) < Ref(L,-1) AND Ref(L,-5) < L;
cond5_2 = L > Ref(L,-4) OR L > Ref(L,-3) OR L > Ref(L,-2) OR L > Ref(L,-1);

CBLhi = Null;
CBLhi[ 4 ] = L[ 4 ];
for ( i = 5; i < BarCount; i++ ) {
prev = CBLhi[ i - 1 ];
if (cond1[ i ])
CBLhi[ i ] = prev; //{previous CBLhi, else...}
else if(cond2_1[ i ])
CBLhi[ i ] = L[ i - 2 ]; //{2nd day back low, else...}
else if(cond3_1[ i ] AND cond3_2[ i ])
CBLhi[ i ] = L[ i - 3 ]; //{3rd day back low, else...}
else if(cond4_1[ i ] AND cond4_2[ i ])
CBLhi[ i ] = L[ i - 4 ];//{4th day back low, else...}
else if(cond5_1[ i ] AND cond5_2[ i ])
CBLhi[ i ] = L[ i - 5 ];//{5th day back low, else...}
else
CBLhi[ i ] = prev;
}

Plot( C, "Price", colorDefault, styleBar );
Plot( CBLhi, "CBLhi", colorRed, styleStaircase );
``````

And we can further simplify to

``````/// CBLhi conversion from https://www.meta-formula.com/Metastock-Formulas-C.html
/// by fxshrat@gmail.com
HighDays = 3;

cond1 = High < Ref(HHV(High, HighDays),-0);

for( i = 2; i <= 5; i++ )
VarSet("cond"+i+"_1", Ref(L,-i) == Ref(L, -LLVbars(L,i+1)));

cond2_1 = cond2_1 AND L > Ref(L,-1);
cond3_2 = L > Ref(L,-2) OR L > Ref(L,-1);
cond4_2 = L > Ref(L,-3) OR L > Ref(L,-2) OR L > Ref(L,-1);
cond5_2 = L > Ref(L,-4) OR L > Ref(L,-3) OR L > Ref(L,-2) OR L > Ref(L,-1);

CBLhi = Null;
CBLhi[ 4 ] = L[ 4 ];
for ( i = 5; i < BarCount; i++ ) {
prev = CBLhi[ i - 1 ];
if (cond1[ i ])
CBLhi[ i ] = prev; //{previous CBLhi, else...}
else if(cond2_1[ i ])
CBLhi[ i ] = L[ i - 2 ]; //{2nd day back low, else...}
else if(cond3_1[ i ] AND cond3_2[ i ])
CBLhi[ i ] = L[ i - 3 ]; //{3rd day back low, else...}
else if(cond4_1[ i ] AND cond4_2[ i ])
CBLhi[ i ] = L[ i - 4 ];//{4th day back low, else...}
else if(cond5_1[ i ] AND cond5_2[ i ])
CBLhi[ i ] = L[ i - 5 ];//{5th day back low, else...}
else
CBLhi[ i ] = prev;
}

Plot( C, "Price", colorDefault, styleBar );
Plot( CBLhi, "CBLhi", colorRed, styleStaircase );
`````` 3 Likes

And that one (and the other ones) can be simplified to

``````cond5_2 = L > Ref(LLV(L, 4), -1);
``````

etc.

So just

``````/// CBLhi conversion from https://www.meta-formula.com/Metastock-Formulas-C.html
/// by fxshrat@gmail.com
HighDays = 3;

cond1 = High < Ref(HHV(High, HighDays),-0);

for( i = 2; i <= 5; i++ )
VarSet("cond"+i, Ref(L,-i) == Ref(L,-LLVbars(L,i+1)) AND L > Ref(LLV(L,i-1),-1));

CBLhi = Null;
CBLhi[ 4 ] = L[ 4 ];
for ( i = 5; i < BarCount; i++ ) {
prev = CBLhi[ i - 1 ];
CBLhi[ i ] = IIf(cond1[ i ], prev,
IIf(cond2[ i ], L[ i - 2 ],
IIf(cond3[ i ], L[ i - 3 ],
IIf(cond4[ i ], L[ i - 4 ],
IIf(cond5[ i ], L[ i - 5 ], prev)))));
}

Plot( C, "Price", colorDefault, styleBar );
Plot( CBLhi, "CBLhi", colorRed, styleStaircase );
``````
3 Likes

With this loopless version you can get almost the same result as with the loop version(s). But there are slight differences here and there.

``````/// CBLhi conversion from https://www.meta-formula.com/Metastock-Formulas-C.html
/// by fxshrat@gmail.com
HighDays = 3;

cond1 = High < Ref(HHV(High, HighDays),-0);

for( i = 2; i <= 5; i++ )
VarSet("cond"+i, Ref(L,-i) == Ref(L,-LLVbars(L,i+1)) AND L > Ref(LLV(L,i-1),-1));

cond6 = cond2 OR cond3 OR cond4 OR cond5;
array = IIF(cond2, Ref(L,-2),
IIF(cond3, Ref(L,-3),
IIF(cond4, Ref(L,-4),
IIf(cond5, Ref(L,-5), Ref(L,-1)))));

CBLhi = ValueWhen(/*cond1 OR*/ cond6, array);
CBLhi = ValueWhen(NOT cond1, CBLhi);

Plot( CBLhi, "CBLhi", colorGreen, styleStaircase );
`````` 3 Likes

Thanks for the solution. I am overwhelm by your expertise in Metastock and Amibroker. This indicator look very nice.

Thanks again for the code.

1 Like

I had a 2nd look at my last post's code (which was showing differences to the other versions) and...

It seems now I have found fast(er) solution not requiring `BarCount` loop.
I tested several `HighDays` settings and thousands of symbols and results of below update have been equal to BarCount loop version. (If you still get major differences then report back.)

Here there is comparison between BarCount loop version and below update (run of thousands of symbols): ``````/// CBLhi conversion from https://www.meta-formula.com/Metastock-Formulas-C.html
/// by fxshrat@gmail.com
HighDays = 3;

cond1 = High < Ref(HHV(High, HighDays),-0);

for ( i = 2; i <= 5; i++ )
VarSet("cond"+i, Ref(L,-i) == Ref(L, -LLVbars(L,i+1)) AND L > Ref(LLV(L, i-1), -1));

bi = BarIndex();
start_cond = bi == 4;
cond6 = cond2 OR cond3 OR cond4 OR cond5;
CBLhi = IIf(cond2, Ref(L,-2), IIf(cond3, Ref(L,-3),
IIf(cond4, Ref(L,-4), IIf(cond5, Ref(L,-5), Ref(L,-1)))));
CBLhi = IIf(bi>3, CBLhi, Null);
//
for ( i = 1; i <= 10; i++ ) {
CBLhi = ValueWhen(NOT cond1, CBLhi);
CBLhi = ValueWhen(cond6 OR start_cond, IIf(start_cond, L, CBLhi));
}

Plot( C, "Price", colorDefault, styleBar );
Plot( CBLhi, "CBLhi", colorGreen, styleStaircase );
//
``````
4 Likes

Actually I compared to this version (using if-else not IIf):

``````Plot( C, "Price", colorDefault, styleBar );

/// CBLhi conversion from https://www.meta-formula.com/Metastock-Formulas-C.html
/// by fxshrat@gmail.com
HighDays = 3;

cond1 = High < Ref(HHV(High, HighDays),-0);

for ( i = 2; i <= 5; i++ )
VarSet("cond"+i, Ref(L,-i) == Ref(L, -LLVbars(L,i+1)) AND L > Ref(LLV(L, i-1), -1));

CBLhi = Null;
CBLhi[ 4 ] = L[ 4 ];
for ( i = 5; i < BarCount; i++ ) {
prev = CBLhi[ i - 1 ];
if (cond1[ i ])      CBLhi[ i ] = prev;
else if (cond2[ i ]) CBLhi[ i ] = L[ i - 2 ];
else if (cond3[ i ]) CBLhi[ i ] = L[ i - 3 ];
else if (cond4[ i ]) CBLhi[ i ] = L[ i - 4 ];
else if (cond5[ i ]) CBLhi[ i ] = L[ i - 5 ];
else                 CBLhi[ i ] = prev;
}

Plot( C, "Price", colorDefault, styleBar );
Plot( CBLhi, "CBLhi", colorRed, styleStaircase, null, null, 0, 2 );
``````

Thanks for this version. I guess you love this indicator. That's why you provided these may version of this.
I also find it very useful for taking profit with this indicator.

No. It got nothing to do with any indicator.
It's just that if something bugs me or something is fishy then I won't let go and then I am digging and digging and digging... (like Inspector Columbo) til a case is solved. Maybe I am just bit of a perfectionist.

8 Likes

And we (less skilled coders) appreciate it, as you help show us how things can be done better.

If only I (we) could remember them all. 2 Likes

Hi fxshrat

You (and a few others members) amaze me with your knowledge of Amibroker.

Can i ask what your background is (if its not a personal question) ?

Regards

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.