Finding the highest volume on green and red candles

Can some one please help me with my following requirement

I need to find out what is the maximum volume of all green/bull candles and of all red/bear candles in last 25 candles ?

The tricky thing here for me is how to combine the Valuewhen , HHV and may be Sum functions together to get to the answer , which I have not been able to figure out.
Any help in this would be greatly appreciated .


@richyrich I am not sure I understood what you are looking for. The highest volume for "Green candles" in last 25 bars (and same for "Red")?

But later you mention the SUM function, so are you looking to add up the Volume of Green and Red?

Below are some attempts (I use 5 bars as it was easier for debugging, so you can change it to whatever you like).

GreenCandle = Close > Open;
RedCandle = Close < Open;
// What color do you want to call the candle if Close=Open ?

RedVolume =  IIf(RedCandle, Volume, 0);
GreenVolume =  IIf(GreenCandle, Volume, 0);

TopGreenV = HHV(GreenVolume, 5);
TopRedV = HHV(RedVolume, 5);

// or are you looking for the sum total
SumGreenV = Sum( GreenVolume, 5);
SumRedV = Sum( RedVolume, 5);

Just saw your post.

RedVolume =  IIf(RedCandle, Volume, 0);
GreenVolume =  IIf(GreenCandle, Volume, 0);

This is the trick part :slight_smile: that many miss.

Creating an array with values (or bars) that you need and then work on them :+1:


@ portfoliobuilder I am sorry for the confusion. But just to be clear, you are right , I am looking for the highest volume for "Green Candles" in last 25 bars and same for "Red Candles" .

// If Close = Open I will just ignore that candle 

Thank you for your prompt reply.

1 Like

Thank you @travick , yes I missed that trick for sure :slightly_smiling_face:

1 Like

Don't forget to mark the thread as "Solved".

Sure marked it as solved. Thanks again.

Sir I want to count red and green candle, But it showing both 5 (red=5 & green=5), Please help..

g = 0;
r = 0;

for( i = 0; i<=4; i++)
Redc = Ref(C, - i) > Ref(O, - i);
Greenc = Ref(C, - i) < Ref(O, - i);

IIf(Redc == 0, r = r + 1, r = r);
IIf(Greenc == 1, g = g + 1, g = g);

printf(WriteVal(r ) + "\n");
printf(WriteVal(g ) + "\n");

Usually a red candle is defined as Close < Open and Green Candle is defined as
Close > open. Your example has the definitions reversed. This can be accompolished without

RedC=Ref(C,-1)< Ref(O,-1);
GreenC=Ref(C,-1) > Ref(O,-1);


printf(WriteVal(redCOUNT ,1)+" Red" + "\n");
printf(WriteVal(GreenCOUNT,1 )+" Green" + "\n");


Only users with License Verified Badge are allowed to post on this forum.

Also see,