Second low period start from the first high

hi

	n = 30;
SATHA = HHV(H,n);            
SATHB = Ref(SATHA, - 1* n);     
SATHC = Ref(SATHA, -2* n);  

if i have 3 period n1=30,n2 = 20 ,n3 = 15

the HHV of n1 found in day ( 17 ) .
how to start the n2 = 20 from day 17 to get the 2nd hi ?
and the 3ed hi period will start from the 2nd hi .

i know there is barssince and HHVBars

thank you

sorry for the title it should be

Second high period start from the first high

hi all as the below photo

hi pir

EMAQW_DAYA= 5 ;
	EMAQW_DAYB= 6 ;
	EMAQW_DAYC= 4 ;



	SATHA1 = IIf (EMAQW_DAYA > 0  , HHV(H,EMAQW_DAYA) ,0 );             
	
	
SATHB2 = IIf ( EMAQW_DAYB > 0 ,Ref(HHV(H,EMAQW_DAYA),HHV(H,EMAQW_DAYB)) ,0 );    

SATHC3 = IIf ( EMAQW_DAYC > 0 ,Ref(Ref(HHV(H,EMAQW_DAYA),HHV(H,EMAQW_DAYB)),HHV(H,EMAQW_DAYC)), 0 );  


and i try this one

test1 = HHV(H,EMAQW_DAYA)  ;
test2 = Ref(test1, -HHVBars(H,EMAQW_DAYB)- 1);

test3 = Ref(test2, -HHVBars(H,EMAQW_DAYC)- 1);

thank you

any correction for the code please

i try this way

        EMAQW_DAYA= 10 ;
	EMAQW_DAYB= 3 ;
	EMAQW_DAYC= 15 ;
	EMAQW_DAYD= 6 ;
	EMAQW_DAYE= 3 ;

test1 = HHV(H,EMAQW_DAYA)  ;
test2 = Ref(HHV(H,EMAQW_DAYA), -EMAQW_DAYB);

test3 = Ref(Ref(HHV(H,EMAQW_DAYA), -EMAQW_DAYB), -EMAQW_DAYC);

test4 = Ref(Ref(Ref(HHV(H,EMAQW_DAYA), -EMAQW_DAYB), -EMAQW_DAYC), -EMAQW_DAYD);

test5 = Ref(Ref(Ref(Ref(HHV(H,EMAQW_DAYA), -EMAQW_DAYB), -EMAQW_DAYC), -EMAQW_DAYD), -EMAQW_DAYE);


the same result with this code

        EMAQW_DAYA= 10 ;
	EMAQW_DAYB= 3 ;
	EMAQW_DAYC= 15 ;
	EMAQW_DAYD= 6 ;
	EMAQW_DAYE= 3 ;

test1 = HHV(H,EMAQW_DAYA)  ;

test2 = Ref(test1, -EMAQW_DAYB);

test3 = Ref(test2, -EMAQW_DAYC);

test4 = Ref(test3, -EMAQW_DAYD);

test5 = Ref(test4, -EMAQW_DAYE);

the chart is different then exploration

2023-01-04_5-29-45

roung

thank you

Hi needhelp,
there are several ways to achieve your task. An idea could be to move step by step redefining each time the data you are going to perform the search. Something like this:

//	consecutive_HHV.afl

n1 = 30;
n2 = 20;
n3 = 15;

bi = BarIndex();

// The concept is to search the HH position on a selection of the data.
//	Fist step: from the end back to n1 bars
n1_data = IIf(bi >= BarCount - n1 -1, H, Null);
n1_HH_pos = BarCount -  LastValue(HighestBars(n1_data)) -1;

//	Second Step: from the last HH pos back to n2 bars
n2_data = IIf( bi > n1_HH_pos - n2 - 1 AND bi < n1_HH_pos, H, Null);
n2_HH_pos = BarCount - LastValue(HighestBars(n2_data)) -1;

//	Third step: again the same...
n3_data = IIf( bi > n2_HH_pos - n3 - 1 AND bi < n2_HH_pos, H, Null);
n3_HH_pos = BarCount - LastValue(HighestBars(n3_data)) -1;

//	plots
PlotOHLC(O, H, L, C, "", colorGrey40, styleBar);
SetChartOptions(0, chartShowDates);
PlotShapes(IIf( NOT IsNull(n1_data), shapeSmallCircle, shapeNone), colorGreen, 0, H, 5, 0);
PlotShapes(IIf( bi == n1_HH_pos, shapeUpTriangle, shapeNone), colorGreen, 0, H, 5, 0);
PlotShapes(IIf( NOT IsNull(n2_data), shapeSmallCircle, shapeNone), colorRed, 0, H, 10, 0);
PlotShapes(IIf( bi == n2_HH_pos, shapeUpTriangle, shapeNone), colorRed, 0, H, 10, 0);
PlotShapes(IIf( NOT IsNull(n3_data), shapeSmallCircle, shapeNone), colorLightBlue, 0, H, 15, 0);
PlotShapes(IIf( bi == n3_HH_pos, shapeUpTriangle, shapeNone), colorLightBlue, 0, H, 15, 0);

thank you for you reply

I used your code whit changing the first ( h)

n1_HH_pos = BarCount -  LastValue(HighestBars(n1_data)) -1;

to

n1_HH_pos = BarCount -  LastValue(HighestBars(n1_data)) ;

in the chart is ok but in exploration is giving different result

in the chart

in the exploration

the main idea as you said

also i try to use

Newday = Day()!=Ref(Day(),-1);
Total_Days = Cum(Newday);

with bi = BarIndex();

instead of BarCount

thank you

I guess you are trying to compare the value of test1 with the value of n1_HH_pos, right? You can't compare those two values because the first is referred to high price and the second of the bar index referred to that high. Is like comparing pears with apple, this has no sense. (_pos stands for "position")

To have the "highVal" value for a certain "barIndex" position you simply need to call: highVal = H[barIndex];

I've slightly modified the code to calculate those high vals and added the exploration, look at this:

//	consecutive_HHV.afl

n1 = 30;
n2 = 20;
n3 = 15;

bi = BarIndex();

// The concept is to search the HH position on a selection of the data.
//	Fist step: from the end back to n1 bars
n1_data = IIf(bi >= BarCount - n1 -1, H, Null);
n1_HH_pos = BarCount -  LastValue(HighestBars(n1_data)) -1;
n1_HH_val = H[n1_HH_pos];

//	Second Step: from the last HH pos back to n2 bars
n2_data = IIf( bi > n1_HH_pos - n2 - 1 AND bi < n1_HH_pos, H, Null);
n2_HH_pos = BarCount - LastValue(HighestBars(n2_data)) -1;
n2_HH_val = H[n2_HH_pos];

//	Third step: again the same...
n3_data = IIf( bi > n2_HH_pos - n3 - 1 AND bi < n2_HH_pos, H, Null);
n3_HH_pos = BarCount - LastValue(HighestBars(n3_data)) -1;
n3_HH_val = H[n3_HH_pos];

//	plots
PlotOHLC(O, H, L, C, "", colorGrey40, styleBar);
SetChartOptions(0, chartShowDates);
PlotShapes(IIf( NOT IsNull(n1_data), shapeSmallCircle, shapeNone), colorGreen, 0, H, 5, 0);
PlotShapes(IIf( bi == n1_HH_pos, shapeUpTriangle, shapeNone), colorGreen, 0, H, 25, 0);
PlotText(NumToStr(n1_HH_val, 1.2, False), n1_HH_pos, n1_HH_val, colorWhite, colorGreen, 40);
PlotShapes(IIf( NOT IsNull(n2_data), shapeSmallCircle, shapeNone), colorRed, 0, H, 10, 0);
PlotShapes(IIf( bi == n2_HH_pos, shapeUpTriangle, shapeNone), colorRed, 0, H, 20, 0);
PlotText(NumToStr(n2_HH_val, 1.2, False), n2_HH_pos, n2_HH_val, colorWhite, colorRed, 40);
PlotShapes(IIf( NOT IsNull(n3_data), shapeSmallCircle, shapeNone), colorLightBlue, 0, H, 15, 0);
PlotShapes(IIf( bi == n3_HH_pos, shapeUpTriangle, shapeNone), colorLightBlue, 0, H, 20, 0);
PlotText(NumToStr(n3_HH_val, 1.2, False), n3_HH_pos, n3_HH_val, colorWhite, colorLightBlue, 90);

// Exploration
SetBarsRequired(sbrAll, sbrAll);	
AddColumn(bi, "bar index", 1.0);
AddColumn(n1_HH_pos, "HH1's BI", 1.0);
AddColumn(H[n1_HH_pos], "HH1's value", 1.2);
AddColumn(n2_HH_pos, "HH2's BI", 1.0);
AddColumn(H[n2_HH_pos], "HH2's value", 1.2);
AddColumn(n3_HH_pos, "HH3's BI", 1.0);
AddColumn(H[n3_HH_pos], "HH3's value", 1.2);
Filter = Status("lastbarinrange");

1 Like

i did this code
only the test1 and test2 working




Newday = Day()!=Ref(Day(),-1);
Total_Days = Cum(Newday);

bi = BarIndex();


n1 = 10;
n2 = 3;
n3 = 15;
n4 = 6 ;
n5 = 3 ;

test1 = HHV(H,n1)  ;

rr=  BarsSinceCompare( H, "==", test1 ); 
ss=  bi - ( rr +1 )  ; 
gff= bi< ((bi - ( (BarsSinceCompare( H, "==", test1 )) +1 ) ) - n2 ) AND bi > ( (BarsSinceCompare( H, "==", test1 ))+1) ;

test2= HHV(H, (bi< ((bi - ( (BarsSinceCompare( H, "==", test1 )) +1 ) ) - n2 ) AND bi > ( (BarsSinceCompare( H, "==", test1 ))-1))) ;





test3 = HighestBars((bi< ((bi - ( (BarsSinceCompare( H, "==", test2 )) -1 ) ) - n3 ) AND bi > ( (BarsSinceCompare( H, "==", test2 ))-1)));

test4 = HighestBars((bi< ((bi - ( (BarsSinceCompare( H, "==", test3 )) -1 ) ) - n3 ) AND bi > ( (BarsSinceCompare( H, "==", test3 ))-1)));

test5 = HighestBars((bi< ((bi - ( (BarsSinceCompare( H, "==", test4 )) -1 ) ) - n3 ) AND bi > ( (BarsSinceCompare( H, "==", test4 ))-1)));


Filter=1;

AddColumn (test1,"test1") ;
AddColumn (test2,"test2") ;
AddColumn (test3,"test3") ;
AddColumn (test4,"test4") ;
AddColumn (test5,"test5") ;

Your code is like a lesson

thank you

1 Like

i could not figure out what is wrong with my code ?

The function HighestBars() returns the number of bars that have passed since the array’s highest value. It does not return the high value (see the function reference: LINK).

Again, but in other words: once you have calculated the bar index of the array of interest - say the High array - to know the value of the array in that particular position (AKA at that BarIndex value) you should calling the array by index.
example:
highest_value_of_H_array = H[calculated_highest_BarIndex_value];

1 Like

thank you for your clear explanation

1 Like

i change it to find the low but Count forward instead of backwards .

//	consecutive_HHV.afl

n1 = 10;
n2 = 3;
n3 = 15;


bi = BarIndex();

// The concept is to search the HH position on a selection of the data.
//	Fist step: from the end back to n1 bars
n1_data = IIf(bi >= BarCount - n1 , L , Null);
n1_LL_pos = BarCount -  LastValue( LowestBars (n1_data)) -1 ; // CHANGE THE <  AND >  
n1_LL_val = L[n1_LL_pos];

//	Second Step: from the last HH pos back to n2 bars
n2_data = IIf( bi > n1_LL_pos - n2 - 1 AND bi < n1_LL_pos, L , Null);
n2_LL_pos = BarCount - LastValue( LowestBars (n2_data)) ;
n2_LL_val = L[n2_LL_pos];

//	Third step: again the same...
n3_data = IIf( bi > n2_LL_pos - n3 - 1 AND bi < n2_LL_pos, L , Null);
n3_LL_pos = BarCount - LastValue( LowestBars (n3_data)) ;
n3_LL_val = L[n3_LL_pos];


//	plots
PlotOHLC(O, H, L, C, "", colorGrey40, styleBar);
SetChartOptions(0, chartShowDates);
PlotShapes(IIf( NOT IsNull(n1_data), shapeSmallCircle, shapeNone), colorGreen, 0, H, -15, 0);
PlotShapes(IIf( bi == n1_LL_pos, shapeUpTriangle, shapeNone), colorGreen, 0, H, -15, 0);
PlotText(NumToStr(n1_LL_val, 1.2, False), n1_LL_pos, n1_LL_val, colorWhite, colorGreen, 40);
PlotShapes(IIf( NOT IsNull(n2_data), shapeSmallCircle, shapeNone), colorRed, 0, H, 15, 0);
PlotShapes(IIf( bi == n2_LL_pos, shapeUpTriangle, shapeNone), colorRed, 0, H, -15, 0);
PlotText(NumToStr(n2_LL_val, 1.2, False), n2_LL_pos, n2_LL_val, colorWhite, colorRed, 40);
PlotShapes(IIf( NOT IsNull(n3_data), shapeSmallCircle, shapeNone), colorLightBlue, 0, H, -15, 0);
PlotShapes(IIf( bi == n3_LL_pos, shapeUpTriangle, shapeNone), colorLightBlue, 0, H, -15, 0);
PlotText(NumToStr(n3_LL_val, 1.2, False), n3_LL_pos, n3_LL_val, colorWhite, colorLightBlue, 90);

Filter = 1 ;
// Exploration
SetBarsRequired(sbrAll, sbrAll);	
AddColumn(bi, "bar index", 1.0);
AddColumn(n1_LL_pos, "LL1's BI", 1.0);
AddColumn(L[n1_LL_pos], "LL1's value", 1.2);
AddColumn(n2_LL_pos, "LL2's BI", 1.0);
AddColumn(L[n2_LL_pos], "LL2's value", 1.2);
AddColumn(n3_LL_pos, "LL3's BI", 1.0);
AddColumn(L[n3_LL_pos], "LL3's value", 1.2);

thank you

For a reason unknown to me, it seem that the LowestBars() function behave in a different manner that the HighestBars(). This causes that after the first call it returns always the count from the bar after the not null last one.

Seeing that the HighestBars() function works as expected, a workaround to this could be to write a function that uses HighestBars() to find the position of the lowest one. Something like this:

function alternative_LowestBars(array)
{
	data = -1 * array;
	lastLowestBars = HighestBars(data);
	return(lastLowestBars);
}


bi = BarIndex();
AddColumn(bi, "BarIndex", 1.0);

values_1 = IIf(bi >= BarCount - 10 , L, Null);
LLpos = BarCount - LastValue(LowestBars(values_1)) - 1;
AddColumn(values_1, "values1", 1.0, colorDefault, IIf(bi == LLpos, colorGold, colorDefault));
AddColumn(LLpos, "LLpos", 1.0);

values_2 = IIf(bi >= LLpos - 5 AND bi < LLpos , L, Null);
LLpos2 = BarCount - LastValue(LowestBars(values_2)) - 1;
AddColumn(values_2, "values2 ", 1.0, colorDefault, IIf(bi == LLpos, colorGold, colorDefault));
AddColumn(LLpos2, "LLpos2 (uses LowestBars()", 1.0);

LLpos3 = BarCount - LastValue(alternative_LowestBars(values_2)) - 1;
AddColumn(values_2, "values2", 1.0, colorDefault, IIf(bi == LLpos3, colorGold, colorDefault));
AddColumn(LLpos3, "LLpos3 (uses alternative_LowestBars()", 1.0);

I think that there is something that I'm missing regarding the LowestBars() use, so I'm really curious to know why the LowestBars() function behaves so differently that HighestBars(). Any idea?

HighestBars and LowestBars are THE SAME FUNCTION. Yes physically ONE function, not two. ONE code.
Internally there is SINGLE function that handles BOTH Highest and Lowest.
Inside the function there is single if() that changes from highest to lowest like this

if( Highest ? ( table[ i ] > val ) : ( table[ i ] < val ) )

So your assumptions are incorrect.

I noticed that you removed the -1 offset from the n2_data and n3_data definitions. This is an error because in this way you consider the actual low as part of the data for the next search, hence if before that data the trend was bearish all then the lowest values "collapse" on the first lowest bar found.

Beside this you should adapt the code using the alternative_LowestBars() function instead of the standard LowestBars().

The code should be something like this:

//	consecutive_LLV - using custom LowestBars function.afl

function alternative_LowestBars(array)
{
	data = -1 * array;
	lastLowestBars = HighestBars(data);
	return(lastLowestBars);
}


n1 = 10;
n2 = 3;
n3 = 15;


bi = BarIndex();

// The concept is to search the LL position on a selection of the data.
//	Fist step: from the end back to n1 bars
n1_data = IIf(bi >= BarCount - n1 , L , Null);
n1_LL_pos = BarCount -  LastValue( alternative_LowestBars (n1_data)) -1 ;
n1_LL_val = L[n1_LL_pos];

//	Second Step: from the last LL pos back to n2 bars
n2_data = IIf( bi > n1_LL_pos - n2 - 1 AND bi < n1_LL_pos, L , Null);
n2_LL_pos = BarCount - LastValue( alternative_LowestBars (n2_data)) -1 ;  // ! corrected the missing -1 offset
n2_LL_val = L[n2_LL_pos];

//	Third step: again the same...
n3_data = IIf( bi > n2_LL_pos - n3 - 1 AND bi < n2_LL_pos, L , Null);
n3_LL_pos = BarCount - LastValue( alternative_LowestBars (n3_data)) -1;  // ! corrected the missing -1 offset
n3_LL_val = L[n3_LL_pos];


//	plots
PlotOHLC(O, H, L, C, "", colorGrey40, styleBar);
SetChartOptions(0, chartShowDates);
PlotShapes(IIf( NOT IsNull(n1_data), shapeSmallCircle, shapeNone), colorGreen, 0, L, -15, 0);
PlotShapes(IIf( bi == n1_LL_pos, shapeDownTriangle, shapeNone), colorGreen, 0, L, 15, 0);
PlotText(NumToStr(n1_LL_val, 1.2, False), n1_LL_pos, n1_LL_val, colorWhite, colorGreen, 40);
PlotShapes(IIf( NOT IsNull(n2_data), shapeSmallCircle, shapeNone), colorRed, 0, L, 15, 0);
PlotShapes(IIf( bi == n2_LL_pos, shapeDownTriangle, shapeNone), colorRed, 0, L, 15, 0);
PlotText(NumToStr(n2_LL_val, 1.2, False), n2_LL_pos, n2_LL_val, colorWhite, colorRed, 40);
PlotShapes(IIf( NOT IsNull(n3_data), shapeSmallCircle, shapeNone), colorLightBlue, 0, L, -15, 0);
PlotShapes(IIf( bi == n3_LL_pos, shapeDownTriangle, shapeNone), colorLightBlue, 0, L, 15, 0);
PlotText(NumToStr(n3_LL_val, 1.2, False), n3_LL_pos, n3_LL_val, colorWhite, colorLightBlue, 90);

Filter = 1 ;
// Exploration
SetBarsRequired(sbrAll, sbrAll);	
AddColumn(bi, "bar index", 1.0);
AddColumn(n1_LL_pos, "LL1's BI", 1.0);
AddColumn(L[n1_LL_pos], "LL1's value", 1.2);
AddColumn(n2_LL_pos, "LL2's BI", 1.0);
AddColumn(L[n2_LL_pos], "LL2's value", 1.2);
AddColumn(n3_LL_pos, "LL3's BI", 1.0);
AddColumn(L[n3_LL_pos], "LL3's value", 1.2);

1 Like

Thank you Thomasz for your reply but I'm sorry I don't understand, could you elaborate please?

The thing that's not clear to me is why if I write two versions of the same routine, one using HighestBars() to find the tops and the other using LowestBars() to find the bottoms, one gives me a sounding answer and the other not.

My simple reasoning is: if the two functions are physically the same, both should work or none of them should work...

I agree with you . even I am not that expert but Logic

your code working perfectly

my idea is to keep it simple as possible. as flow :-

1- HHV or LLV is clear

n1 = 10 ;
// for hi
h1 = HHV (H,n1 ) ;
// for low
l1 = LLV ( L, n1 )

now the 2nd hi or 2nd low but with condition ( it should start from the bar that event happened )

my idea is

1- find the total of data using

Newday = Day()!=Ref(Day(),-1);
Total_Days = Cum(Newday);

2- find the total bar to the event for both hi or low as ( example )

total_bar_to_1st_hi = BarsSinceCompare( H, "==", h1 ) ;

h1 happened in bar number 6 from total period of ( n1 = 10 )

3- now find out how many data left of after we subtract the total bar event for ( h1 ) the new data will be used for 2nd hi

data_for_2nd_hi = Total_Days - ( total_bar_to_1st_hi ) ;

total data - 6
when we have ( the left of data ) we should use it to find hi for ( n2 = 3 )
n2 = 3 ;
now we have the left of data ( but how to code the left of the data )
h2 = in the left of data use HHV ( H , n2 )

I hope my idea is clear ( as simple subtract the event from the total and use the rest as new data )

thank you ( 2DD ) for you great work

by the way such issue like this is not in the forum and you are the first one solve it .

in the forum is constant one period .

thank you for your time and your effort .

1 Like

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