# how to count When A bar closed in Bollinger bands, then B is closed out Bollinger bands

31

```I want to count
assume
Give numbers as bars in sequence.
A B C D E F G H I J K
such as
When A bar closed in Bollinger bands, then B is closed out Bollinger bands are counted as 1.
When bar D is closed in Bollinger bands, the next bar is E, the closing price outside Bollinger bands is 2.
This is going on.
I can only write this, which can not be done as defined above.
//////////////////////////////////////////////////////////////////////////////////////////////////////////```
int CountBarOutInOneDay(int Num)
int CntNumBarOutInOneDay=0;
for(int i=0;i<3;i++)  //just count 3 in one day
{
double BBUper,BBLower,CloseBar;
double LBBUper,LBBLower,LCloseBar;

BBUper=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_UPPER,1);
BBLower=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_LOWER,2);
CloseBar=iClose(NULL,PERIOD_M15,1);

LBBUper=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_UPPER,2);
LBBLower=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_LOWER,2);
LCloseBar=iClose(NULL,PERIOD_M15,2);

if(LBBUper>LCloseBar && LCloseBar>LBBLower)
{
if(BBUper<CloseBar || CloseBar<BBLower)
{
CntNumBarOutInOneDay++;
}
}
}
return(CntNumBarOutInOneDay);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

How to edit this code ?

Moderator
10220

20210

1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor

2. You code has a loop, but you never use the index.
3. You code looks at constant shifts, you need variable shifts.
4. You are thinking toward the future. Think toward the past. The count starts at zero, the shift starts at one. While the close is outside the BB increment the count and shift.
1672

Boripath Boonrit:
int CountBarOutInOneDay(int Num)
int CntNumBarOutInOneDay=0;
for(int i=0;i<3;i++)  //just count 3 in one day
{
double BBUper,BBLower,CloseBar;
double LBBUper,LBBLower,LCloseBar;

BBUper=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_UPPER,1);
BBLower=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_LOWER,2);
CloseBar=iClose(NULL,PERIOD_M15,1);

LBBUper=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_UPPER,2);
LBBLower=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_LOWER,2);
LCloseBar=iClose(NULL,PERIOD_M15,2);

if(LBBUper>LCloseBar && LCloseBar>LBBLower)
{
if(BBUper<CloseBar || CloseBar<BBLower)
{
CntNumBarOutInOneDay++;
}
}
}
return(CntNumBarOutInOneDay);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

How to edit this code ?

```int CountBarOutInOneDay(int Num)
{
int CntNumBarOutInOneDay1=0;
for(int i=0;i<3;i++)  //just count 3 in one day
{
double BBUper,BBLower,CloseBar;
double LBBUper,LBBLower,LCloseBar;

double BBUper1=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_UPPER,i);
double BBUper2=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_UPPER,i+1);

double BBLower1=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_LOWER,i);
double BBLower2=iBands(NULL,0,BB_period,BB_deviation,0,PRICE_CLOSE,MODE_LOWER,i+1);
if (Open[i]>BBuper1 && Open[i+1]<BBuper2)
{
CntNumBarOutInOneDay1++;
}
if (Open[i]<BBLower1 && Open[i+1]>BBLower1)
{
//Lower Band
}

}
return(CntNumBarOutInOneDay1);
}```