# Pls help re Barsince Barcount? - count number of green bars for say the past nine bars (minute bars)

Hi all,

I am a rookie of AFL. I managed to develop a couple of basic analysis and explorations… I researched this site and a number of forums re AFL but I couldn’t figure out how to count the number of green bars say for the past nine bars (minute bars).

I want to “Buy” if and only if at least seven of the past nine bars are green bars. Can any expert here help me with the AFL? Please forgive my ignorance. Many thanks. I can then leverage on the input and explore.

Regards
KSY

@SKm The members of this forum come from all over the world and we use different terminology to describe the charts and methods we use. So I would suggest being specific with your request.

If by "Green Bar" you mean a bar where the Close is greater than the Open, then try this.

``````GreenBar = C > O;
Condition7 = Sum( GreenBar, 9 ) > 6;

// Explore //
Filter = 1;
g_bar = IIf( C > O, colorLime, colorDefault );
SevenOfNine = IIf( Condition7, colorLightBlue, colorDefault );
AddColumn( GreenBar, "GreenBar", 1.0, colorDefault, g_bar );
AddColumn( Condition7, "Condition7", 1.0 , colorBlack, SevenOfNine );
``````

Produces this type of Exploration

1 Like

Many thanks portfoliobuilder. It helps big time. I think I am getting there.

(1) I have a couple of follow on questions hope you can help again! In Amibroker chart, for bars when the O==C, bar color will sometimes be green and sometime be red. Does the color of the most recent green/red bar dictate the color of the following O==C bars?

(2) Based on your input, I inserted two columns (pls see below afl) to show the Red bar and the Condition (if met) when 8 of the past 10 bars were Red... but I found that the Condition8Red column simply shows what is shown under the Condition8Green column (pls see 939am row). May I know if I did anything wrong here?

GreenBar = C > O;
Condition8Green = Sum( GreenBar, 10 ) > 7;

RedBar = O < C;
Condition8Red = Sum( RedBar, 10 ) > 7;

// Explore //
Filter = 1;
g_bar = IIf( C > O, colorLime, colorDefault );
EightOfTenGreen = IIf( Condition8Green, colorLightBlue, colorDefault );
AddColumn( GreenBar, "GreenBar", 1.0, colorDefault, g_bar );
AddColumn( Condition8Green, "Condition8Green", 1.0 , colorBlack, EightOfTenGreen );

r_bar = IIf( C < O, colorRed, colorDefault );
EightOfTenRed = IIf( Condition8Red, colorLightBlue, colorDefault );
AddColumn( RedBar, "RedBar", 1.0, colorDefault, r_bar );
AddColumn( Condition8Red, "Condition8Red", 1.0 , colorBlack, EightOfTenRed );

Thanks again.
K

@SKim,

I think you have a simple typo…

For RedBar, try C<O …

Also, if you want to specifically handle when O==C, have one of the two conditions be C>=O or C<=O. I suppose you could do both, and that way either sequence would be able to count a matched bar.

Hope that helps.
Snoopy

2 Likes

Stupid me. thanks a lot.

Hello, I try to establish something similar but tell me in the indicator itself. I want to know how many bars of more than 1 standard deviation there have been in the last 10 days and 5 days.
regards

@lol111 there are many many examples of "counting a condition" on this forum. Search the forum and adapt one of those solutions to your needs.

Alternatively, post your code and your attempted solution instead of a chart. It is more likely you will get help with an example of the code and attempted solution because your question may be unclear to many readers. I for example have no idea what this means

One standard deviation of what? What are you measuring? Show your code.

Good luck.

Please use code tags if inserting code, see here

``````_SECTION_BEGIN("Desviaciones estándar");

// OSCAR G. CAGIGAS
// 20 SEPTIEMBRE 2019

En "The volatility edge in options trading" Jeff Augen propone un indicador que consiste en pintar en el número de desviaciones estándar que se ha movido
un mercado de un cierre al siguiente. Este indicador toma la forma de un histograma debajo del precio y se utiliza como una alerta para abrir posiciones en
opciones cuando el mercado en cuestión supera las 2 desviaciones estándar.
*/
//DESVIACION STANDARD
desv_est = sqrt( EMA((C-Ref(C,-1))^2, 36 ) ) ;
//sqrt(252)*StDev(ln(C/Ref(C,-1)),21);

//NUMERO DE DESV ESTÁNDAR
numdesv = (C - Ref(C,-1)) / Ref(desv_est,-1);

//PINTAR LAS DESVIACIONES Y LOS LÍMITES DE 2 DESV
GraphXSpace = 10; //añade espacio para ver los letreros
Plot(numdesv,"NumDesv",colorBlueGrey,styleHistogram|styleThick,Null,Null,0,0,6);
Plot(1,"",colorGreen,styleLine);
Plot(-1,"",colorGreen,styleLine);
Plot(2,"",colorGreen,styleLine);
Plot(-2,"",colorGreen,styleLine);
//SI PASA DE 2 DESV ANOTAMOS EL VALOR

bi = Barindex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
for( i = fvb; i <= lvb; i++ )
{
if( numdesv[i] > 1 )
{
PlotText( NumToStr( numdesv[i], 1.2 ), i - 1, numdesv[ i ] + 0.25, colorBlue );
}
else
if( numdesv[i] < -1 )
{
PlotText( NumToStr( numdesv[i], 1.2 ), i - 2, numdesv[ i ] - 0.75, colorblue );
}

}

/// Added by fxshrat@gmail.com for user @LOL111
/// https://forum.amibroker.com/t/pls-help-re-barsince-barcount-count-number-of-green-bars-for-say-the-past-nine-bars-minute-bars/2212/9
cond = numdesv > 1;
//cond = abs(numdesv) > 1;// uncomment if needed
cs5 = Sum(cond, 5);
cs10 = Sum(cond, 10);
Title = StrFormat("{{NAME}} {{DATE}} | %s NUM. > 1 SD EN ULTIMOS 10 DIAS: %g, NUM. > 1 SD EN ULTIMOS 5 DIAS: %g %s | {{VALUES}}",
EncodeColor(colorRed), SelectedValue(cs10), SelectedValue(cs5), EncodeColor(-1));
_SECTION_END();
``````

If you need absolute value then change from

``````cond = numdesv > 1;
``````

to (by uncommenting it in upper code)

``````cond = abs(numdesv) > 1;
``````

2 Likes

sorry. It was a mistake because I was putting another comment and it seems that I got confused. I upload it as is. Thank you

``````_SECTION_BEGIN("Desviaciones estándar");

// OSCAR G. CAGIGAS
// 20 SEPTIEMBRE 2019

En "The volatility edge in options trading" Jeff Augen propone un indicador que consiste en pintar en el número de desviaciones estándar que se ha movido
un mercado de un cierre al siguiente. Este indicador toma la forma de un histograma debajo del precio y se utiliza como una alerta para abrir posiciones en
opciones cuando el mercado en cuestión supera las 2 desviaciones estándar.
*/
//DESVIACION STANDARD
desv_est = sqrt( EMA( ( C-Ref(C,-1) )^2, 36 ) ) ;
//sqrt(252)*StDev(ln(C/Ref(C,-1)),21);

//NUMERO DE DESV ESTÁNDAR
numdesv = (C - Ref(C,-1)) / Ref(desv_est,-1);

//PINTAR LAS DESVIACIONES Y LOS LÍMITES DE 2 DESV
GraphXSpace = 10;  //añade espacio para ver los letreros
Plot(numdesv,"NumDesv",colorRed,styleHistogram|styleThick,Null,Null,0,0,6);
Plot(1,"",colorBlack,styleLine);
Plot(-1,"",colorBlack,styleLine);
Plot(2,"",colorBlack,styleLine);
Plot(-2,"",colorBlack,styleLine);
//SI PASA DE 2 DESV ANOTAMOS EL VALOR
for( i = 0; i < BarCount; i++ )
{
if( numdesv[i] > 1 )
{
PlotText( NumToStr(numdesv[i],1.2), i-1, numdesv[ i ] + 0.25, colorRed);
}
else if( numdesv[i] < -1 )
{
PlotText( NumToStr(numdesv[i],1.2), i-2, numdesv[ i ] - 0.75, colorGreen );
}

}
_SECTION_END();

``````

And now tell you thank you very much. It is exactly what I wanted. It is a market filter for a type of strategy with options.
Greetings