Can I count all bar

I try to count all bar green bar and red bar after cross macd but it seem didn't work
it should be reset each time in a new condition. Can anyone point out my error?

Code button (use to enter/paste AFL formula)
Plot( C, "Price", colorDefault, styleBar );

UpEma = Cross( MACD(), Signal() );
UpBar = Close > Open;

DowmEma = Cross(Signal(), MACD());
Lowbar = Close < Open;

CountUp = UpEma AND UpBar;
CountDown = DowmEma AND Lowbar;

countallbar = BarsSince(CountUp OR DowmEma);
CountUpbar = BarsSince(countUp);
CountLowbar = BarsSince(CountDown);

Plot( countallbar,"countallbar",colorWhite,styleOwnScale );
Plot( CountUpbar, "CountUpbar", colorGreen, styleOwnScale );
Plot( CountLowbar, "CountLowbar", colorRed, styleOwnScale );

You should just use

CrossUp = Cross( MACD(), Signal() );
CrossDn = Cross( Signal(), MACD() );

CountAllBar = BarsSince( CrossUp or CrossDn );

BTW: Next time please use CODE TAGS

1 Like

Thank you for your kindness and sorry for I miss code tags

but I want them would be reset condition and count amount of green bar and red bar in condition.

I get this code form the old topic. I try to modify to get new condition by myself
but it isn't work.


  1. count all green bar (close > open) in signal cross(macd(),signal());
  2. count all red bar (close < openl) in signal cross(signal(),macd());

help me please

Buy = Cross( MACD(), Signal() );
UpBar = Close > Open;

/// Similar to
/// @link
Sell = 0;
cnt_array = Null;
bprice = cnt = 0; 
for ( i = 0; i < BarCount; i++ )
	if ( Buy[ i ] && bprice == 0 ) {
		bprice = BuyPrice[ i ];
	} else Buy[ i ] = 0;
	if ( bprice > 0  ) {	
		if ( UpBar[i] ) 
		// exit if count of UpBar > n 
		if (cnt > 5) // I want they continue count in condition 2 
			Sell[ i ] = 1;    
			bprice = cnt = 0;
		cnt_array[ i ] = cnt;	// store cnt to array

Plot( C, "Price", colorDefault, styleBar );
PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow,
            IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, L, H ) );
Plot( cnt_array, "UpBarCount", colorYellow, styleOwnScale );

The problem is that you are not describing precisely what you want. Describing the GOAL, not the code is important when asking questions: How to ask a good question

You don't need loops. I gave you working code (for counting from EITHER cross up or cross down). If you want for cross up alone you just use

CrossUp = Cross( MACD(), Signal() );
CountSinceCrossUP = BarsSince( CrossUp );

It continues counting even if it crosses down, because you count from cross UP.
Logically it does not matter if cross down occured when you are really counting bars since cross UP.
If you want something else, like counting only when cross down DID NOT occur then you have to SAY SO, in your question, PRECISELY.

CrossUp = Cross( MACD(), Signal() );
CountSinceCrossUP = BarsSince( CrossUp );
IsUp = Flip( MACD(), Signal() );
CountSinceCrossUpOtherwiseNull = IIF( IsUp, CountSinceCrossUP , Null ); 

Thank you for your help and your recommend.

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