Algo for finding lowest value in a lookup period that is not zero

Hi,

I know how to use LLV(volume,5) which finds the lowest volume over the past 5 periods. However how do i find the lowest volume over the past 5 periods that's not zero?

I can only think of the following ugly way which is not scalable ie if i change 5 to 50 then code gets dumb i guess a for loop would be possible in this case?

lowestVolume = Ref(Volume,-5);
IIf(lowestVolume > Ref(Volume, -4) || lowestVolume == 0, lowestVolume=Ref(Volume, -4),);
IIf(lowestVolume > Ref(Volume, -3) || lowestVolume == 0, lowestVolume=Ref(Volume, -3),);
IIf(lowestVolume > Ref(Volume, -2) || lowestVolume == 0, lowestVolume=Ref(Volume, -2),);
IIf(lowestVolume > Ref(Volume, -1) || lowestVolume == 0, lowestVolume=Ref(Volume, -1),);

Take a look at SparseCompress function
"eliminate" all bars with zero volume
https://www.amibroker.com/guide/afl/sparsecompress.html

1 Like

Possibly something like the following:

lowVOLfind=LLV(V,5) AND NOT V==0;
Filter=lowVOLfind;
AddColumn(lowVOLfind,"lowin5");AddColumn(LLV(V,5),"lowin5");

Problem with this is if i have a series of 0,100,200,300,400, I want the function to return 100. But In your function it would only return a boolean.

Problem with sparse compress is that after compressing how do I know the non 0 value is from the 5 prior bars or it could be from the 20 prior bars.

lowVOLfind=LLV(V,5 AND NOT V==0);
Filter=lowVOLfind;
AddColumn(lowVOLfind,"lowin5");

1 Like

Can I get the full working code, please?

Proof%20of%20concept

Image shows proof of concept.
I created a dummy ticker with volume of 0,200,300,400,500 and explore returned 200.

2 Likes

@Anthony, your formula is incorrect. It shows 200 only for the following reasons:

  1. because of
lowVOLfind = LLV(V, 5 AND NOT V==0);

doing the same one as

period = IIf(V > 0, 1, 0);
lowVOLfind = LLV(V, period);

So period changes between 0 and 1.

  1. Value of 200 being located at last bar of array

  2. Range being set to 1 recent bars


So your formula is the same as doing this:

lowVOLfind = V;
Filter = V > 0;
AddColumn(lowVOLfind,"lowin5");
4 Likes

Sorry I read to fast and misunderstood what you want, try this

HVolume = Highest(V);
CorectVolume = iif(Volume <= 0, HVolume, V);
lowVOL = LLV(CorectVolume, 5); 
Filter=true;
AddColumn(lowVOL,"lowin5");
2 Likes

Nice one, although I was thinking the same thing, only trying to use Previous Bar Volume when current bar is 0.

So either code would run into a problem, if all recent 5 bars are 0, then they'll be filled with Highest Vol which would then be an issue or a different Vol in my case.

so mod'ing your code,

sum5Vol = Sum( V, 5) > 0;
/// Modified original code by Anderson Wilson
HVolume = Highest(V);
CorectVolume = iif(Volume <= 0 AND sum5Vol, HVolume, V);
lowVOL = LLV(CorectVolume, 5); 
Filter=true;
AddColumn(lowVOL,"lowin5");

we try to ensure there is atleast one bar with +ve Vol else return 0.

1 Like

Well if you have 5 consecutive bars with volume equal zero, look for another data provider or another symbol to trade :sunglasses:

1 Like

hehe, you think that thought didn't cross my mind :rofl:

I think travick is on the right track.
Logic. Fill a volume array such that all the 0 volume is replaced by HVolume. Then find the smallest within 5 bars. The only issue with this is if there's an array with all zeros ie 0,0,0,0 as indicated highest volume is being returned but this can be filtered out by if sum(V,5)==0 and return lowVol = 0 when thiis is true.

Not sure if the following will compile will try later:

sum5Vol = Sum( V, 5) > 0;

if (sum5Vol){
/// Modified original code by Anderson Wilson
HVolume = Highest(V);
CorectVolume = iif(Volume <= 0 AND sum5Vol, HVolume, V);
lowVOL = LLV(CorectVolume, 5); 
} else {
lowVOL=0;
}
Filter=true;
AddColumn(lowVOL,"lowin5");

Test the code I posted, the sum5Vol is already applied in iif() Line.

if (sum5Vol){
This is wrong, you can't pass Array here. Use LastValue() which in this case isn't needed.
The whole if() is redundant.

You need to check the guide an understand AFL, it is Array processing, you need to change scalar programming thought process here.

1 Like

Herm then the code should work with all 0s then and return 0 i believe. Thank for the tip I'm very bad with what will compile and what won't Arrays confuse me... Gotta keep coding to gain experience i guess.

Thanks for all the help everyone :smiley:

Thank you @fxshrat for the explanation and correction. Appreciate the help.

2 Likes