HighestSince bars count vs Barssince count comparision

I am trying to check if number of bars are more after high was made, or if the high was made before the new candle/last candle.
The below code is not giving me desired result.
If both are same then I want to ignore my entry.

NewDay = Day()!= Ref(Day(), -1) OR BarIndex() == 0;  
BarsSince(NEWDAY)> HighestSinceBars(NewDay, H)  // not working, entry is existing in same candle, which I don't want.
1 Like

I'm checking from my mobile but did you try the == operator instead.
That's because you are saying if both are same

1 Like

Yes Clark, I tried >= but not == , as I do not want them to be equal in this case. Was trying numerous options got a solution to my problem by using a flag
SAME_CANDLE_H = IIf(H == HighestSinceBars(NewDay, H), True, False); SAME_CANDLE_H == False;

Thanks for your reply..

If you describe in detail, it will be clear what you are trying to do.

In your second post, you are comparing High and number of bars, that's like comparing apples to oranges.
HighestSince() would be more appropriate.

:slight_smile: Absolutely, apple with oranges, my bad, it was wrong while pasting, my bad, the correct code is pasted below.
I am trying to avoid same bar entry. If my buy condition is met in the bar, which made high of the day so far then I want to ignore that trade.
SAME_CANDLE_H = IIf(H == HighestSince(NewDay, H), True, False); SAME_CANDLE_H == False; // this flag I am checking for my entry criteria

1 Like

Ok, now if you are going to be returning True/False ie. 1/0 and not some custom value, then this would suffice.

SAME_CANDLE_H = H == HighestSinceBars( NewDay, H);
1 Like

Edit, it was a copy typo, it's HighestSince() and not HighestSinceBars() as already discussed above.

1 Like

Going by your original post:

// https://forum.amibroker.com/t/highestsince-bars-count-vs-barssince-count-comparision/12608?u=cougar
// Please note this code will run correctly on Interval less than inDaily only.
// Distance of High Bar from first bar of a day v/s Distance from the High Bar till the last bar of the day
// or LastValue( BarIndex() ) if the days is not finished yet
SetChartOptions( 0, chartShowDates | chartLogarithmic );
Plot( C, "Price", colorDefault, styleBar | styleThick );

bi = BarIndex();
fvb = Status( "FirstVisibleBar" );
lvb = Min( Status( "LastVisibleBar" ), BarCount - 1 );

DaysHigh = TimeFrameGetPrice( "H", inDaily, 0 );
DaysFrstBar = TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressOpen ), inDaily, expandFirst );
DaysLastBar = TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressLast ), inDaily, expandFirst );

// Using a loop in order to capture the latest BarIndex() when H == DaysHigh if in-case there
// exists multiple instances where the same Day's High is touched multiple times.
for( i = 1; i <= BarCount - 1; i++ ) {
	 if( H[ i ] == DaysHigh[ i ] ) {
		 DaysHiBar[ i ] = 0;
		 for( j = i - 1; j >= DaysFrstBar[ i ]; j-- ) if( H[ j ] == DaysHigh[ j ] ) DaysHiBar[ j ] = 1;
	 }
}
DaysHiBar = NOT DaysHiBar;
PlotShapes( IIf( DaysHiBar, shapeSmallCircle, shapeNone ), colorRose, 0, H, 12, 0 );

// Figuring out bar differences
DaysHiBarBi = ValueWhen( DaysHiBar, bi );
NumBarsBeforeDaysHi = DaysHiBarBi - DaysFrstBar;
NumBarsAfterDaysHi = DaysLastBar - DaysHiBarBi;

// Textual display
for( i = fvb; i <= lvb; i++ ) if( DaysHiBar[ i ] ) PlotTextSetFont( "Bars Before: " + NumBarsBeforeDaysHi[ i ] + "\nBars After: " + NumBarsAfterDaysHi[ i ], "Courier New", 8, i, H[ i ], colorRose, colorDefault, 40 );


(Click on the image to enlarge)

6 Likes

Thanks Cougar for your code, its enlightened, as AFL is an ocean of functions and I am learning every moment. This code is surely helping me to learn more and go deep.

I really appreciate your effort and extending your help even though I have solved the problem with a solution. :+1:

:smiley: Yes, same I did above..

hi, i try above code, but loop continuous running and multi result showing. thanks to advise how to clear.. problem1

@Ganesan,

Do not use that loop:

bi = BarIndex();
fvb = Status( "FirstVisibleBar" );
lvb = Min( Status( "LastVisibleBar" ), BarCount - 1 );

DaysHigh = TimeFrameGetPrice( "H", inDaily, 0 );
DaysFrstBar = TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressOpen ), inDaily, expandFirst );
DaysLastBar = TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressLast ), inDaily, expandFirst );

Cond = H == DaysHigh;
DaysHiBarBi = ValueWhen( Cond, bi );

// Figuring out bar differences
NumBarsBeforeDaysHi = DaysHiBarBi - DaysFrstBar;
NumBarsAfterDaysHi = DaysLastBar - DaysHiBarBi;

// Textual display
PlotShapes( IIf( Cond, shapeSmallCircle, shapeNone ), colorRose, 0, H, 12, 0 );
for( i = fvb; i <= lvb; i++ ) if( Cond[ i ] ) PlotTextSetFont( "Bars Before: " + NumBarsBeforeDaysHi[ i ] + "\nBars After: " + NumBarsAfterDaysHi[ i ], "Courier New", 8, i, H[ i ], colorRose, colorDefault, 40 );

Plot( C, "Price", colorDefault, styleBar | styleThick );

@Cougar :slightly_smiling_face:Thank you.

please advise, what is the reason not working "for condition"?.

@Ganesan,

OP had a query in regards to counting bars before and after a condition for each day. I specifically annotated the reason for using the loop (check the commented // remarks there) - it happens so that in very shorter intraday time-periods like in 5 minutes or lesser, the exact high of the day might get touched multiple times and the for-loop was used such that the most latest bar be picked up where the condition is met out of the multiple instances (quite necessary to clearly ascertain while backtesting a system out of it).

If you read the code posted here. I annotated this:

What I meant was that, based on OP's exact requirement he could either choose between a direct array condition like H == DaysHigh (as just shown here) or use a for-loop.

First of all, "for condition" is not a "condition" but is a "loop". Condition and loop are not same!

It is not a question on working or not working. It is a question on what you want to procure and how systematically you have shown efforts to solve that problem.

Hi folks,

HHV of n bars is a very important function, but I have one requirement which does the reverse of this function. As HHV gives the highest price in the last n bars by looking to the left, I want the max consecutive bars for which today's high is the highest. So if the ticker makes a new all-time high, then this number would be total number of bars - 1 as all previous highs are below today's high. I looked through the forum but could not find any such discussion. Someone plz help on this.

Thanks in advance.

HHVBars is right below of HHV in AB help function reference

34

Plot( HHV(H,10), "HHV", colorRed );
Plot( HHVBars(H,10)+1, "HHVbars (one-based)", colorOrange, styleOwnScale );

35


Similar for Highest and HighestBars.
Latter one is one row below of Highest function in function reference guide.

Plot( Highest(H), "Highest", colorRed );
Plot( HighestBars(H)+1, "Highestbars", colorOrange, styleOwnScale );

35

Thanks @fxshrat for quick help, however, my desired result is a little bit diff. My desired output should NOT look within specific n Bars. It should quickly locate the immediate last bar when High was above today's high and then count the number of bars in between so that today's High becomes the Highest of that number of bars. Hope I was able to explain my problem. Plz, consider the chart attached below.

image

Use new function BarsSinceCompare of AB 6.40

https://www.amibroker.com/guide/afl/barssincecompare.html

Version(6.40);
bars = BarsSinceCompare(H, ">", H);
2 Likes

Thanks a lot @fxshrat, I think this is what I was looking for. Will try it after installing the newest version.