# How to get number of bullish and bearish bars in last 10 bars

#### balasoft80

##### Active Member
Hello All,
Could anyone please help in identifying number of bullish and bearish bars in last 10 bars? The below i got from net and it calculates for whole days. I tried for last 10 bars using barindex. But no success. could you please someone help?

BarsUp=0;
BarsDown=0;
PCount = 0;
NCount = 0;
NewDay = Day() != Ref(Day(), -1);

for(i=0;i<BarCount;i++)
{
if(NewDay==True)
{
BarsUp=0;
BarsDown=0;
PCount = 0;
NCount = 0;
}
//positive count
if(C-O>0)
{
PCount++;
BarsUp=PCount;
BarsDown=NCount;
}
//negative count
if(C-O<0)
{
NCount++;
BarsUp=PCount;
BarsDown=NCount;
}

}

Thanks
Bala

#### Romeo1998

##### Well-Known Member
Sir @balasoft80
this will be helpful
good luck

Code:
``````BarsUp=0;
BarsDown=0;
PCount = 0;
NCount = 0;
NewDay = Day() != Ref(Day(), -1);

last10thbar = BarIndex() == Ref(BarIndex(),-10);

for(i=BarCount-10;i<BarCount;i++)
{
if(last10thbar[i])
{
BarsUp=0;
BarsDown=0;
PCount = 0;
NCount = 0;
}
//positive count
if(C[i]-O[i]>0)
{
PCount++;
BarsUp=PCount;
BarsDown=NCount;
}
//negative count
if(C[i]-O[i]<0)
{
NCount++;
BarsUp=PCount;
BarsDown=NCount;
}

}

Title = Title + "\nupbars in last 10 bars = " + barsup
+ "\ndownbars in last 10 bars = " + barsdown;``````

#### balasoft80

##### Active Member
Thank you Romeo1998 for the quick response. it always shows last 10 bars. I want to back test it for old data. is there it can be accomplished using barindex itself without barcount? thanks lot again

#### yusi

##### Well-Known Member
Borrowing from code by @Romeo1998 :

C:
``````_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("CountUpDownBars");
//Count of bullish and bearish bars in the last 10 bars

BarN = Param("N-bars", 10, 1, 100);
BarsUp = Sum(Close >= Open, BarN); // Open = Close is an Up Bar (assumed)
BarsDown = Sum(Close < Open, BarN);

Title = Title + "\nupbars in last " + BarN + " bars = " + BarsUp
+ "\ndownbars in last" + BarN + " bars = " + BarsDown;
_SECTION_END();``````