Volume condition in loop?

I am creating an AFL code which includes loop to check if the each of the previous bars have volume greater than moving average of last 20 candles. I am just attaching a small portion of the loop but it is giving me an error. Please help.

for(i = 1;i < BarCount; i++)
   {
     if(Volume[i] > 3* ma(volume,20)

I am not getting any errors if I check this code for errors. But when I scan the code for finding real stocks I am getting error that - condition in IF statement has to be numeric or Boolean type. You can not use array here.

After this error, just to check I changed the code to if(Volume[i] > 100000) and then its working perfectly fine.
Kindly assist me what changes I need to make if I want to find candles which has volume greater than 3 times of moving average of of last 20 candles volume.

Please read here about "How AFL works" and here about coding mistakes.

  1. First thing you are doing wrong is using array (see "your" MA function) within loop as well as within if statement. No, no, no... Do not do that (see links above).
  2. Second thing you do wrong is using loop instead of array processing. Use latter one but not looping.
  3. Third thing is that you added incomplete example.

Anyway here is solution using array processing and IIf function (BTW, also read here about difference between "IIf" and "if").

VolumeCondition = Volume > 3* MA(V, 20);

// replace 'True' with array or other variable to be returned.
Return_Something_If_True = IIf( VolumeCondition, True, False );

or use ValueWhen to keep same output until next true occurrence of condition.

VolumeCondition = Volume > 3* MA(V, 20);

array_returned = Close;// replace 'Close' by array of your choice 
Return_Something_If_True = ValueWhen( VolumeCondition, array_returned );
1 Like

Thanks for the replay. Actually I have made the afl coded through some coder on payment. The code is all about breakout by a long size candle (where candle size is more than 10% on daily basis) . He coded it through looping. He has used this condition here which is below.

jsbarsize = Param("barsize percentage",10,5,20,0.2,0);
js1[i] = (C[i]  - O[i] ) *100 /C[i] ;   //calculates the size of the candle
if(js1[i]> jsbarsize)     //compares if the current size is greater than js1 or not

Here on every candle loop is checking if the candle is more than 10% or not. Then the code does the remaining things. Here I just want to add one more condition of volume that the long candle also has high volume.

I hope you understood what I am trying to do. Please help fxshrat

someone please help me with this