I want to divide the period into three phases,
Each stage contains 300 days
I want to find the highest for each stage
The second stage begins after obtaining the highest of the first stage
I mean, the highest happened on day 124 in the first stage
The second stage begins after 124 days
The third stage is the same way

i try this but not working

n=300;
SATA=HHV(H,n);
SATAa=HHV(H,(BarsSince( H == HHV( H, n ) )+n));
SATAaa=HHV(H,((BarsSince(HHV(H,(BarsSince( H == HHV( H, n ) )+n))))+n));
SATB1=BarsSince( H == HHV( H, n ) );
SATB2=(BarsSince( H == HHV( H, n ) )+n);
SATB3= (BarsSince(HHV(H,((BarsSince(HHV(H,(BarsSince( H == HHV( H, n ) )+n))))+n))))+n;
filter=1;
AddColumn( SATA,"SATA",1.2,colorDefault,colorDefault);
AddColumn( SATB1,"SATB1",1.2,colorDefault,colorDefault);
AddColumn( SATAa,"SATAa",1.2,colorDefault,colorDefault);
AddColumn( SATB2,"SATB2",1.2,colorDefault,colorDefault);
AddColumn( SATAaa,"SATAaa",1.2,colorDefault,colorDefault);
AddColumn( SATB3,"SATB3",1.2,colorDefault,colorDefault);

You need to begin by clearly defining the problem to be solved. In your initial post you said:

But then in a later post you said:

If the three stages are all of fixed length, the problem is much easier to solve. However, if the start of the second stage is dependent on a condition within the first stage (for example when the highest high occurs on bar 124), then a different approach is needed.

One challenge with the "conditional start" version of the problem is that I don't believe you can look backwards from a particular date to accurately determine SATHA, SATHB ,and SATHC. With the "fixed length" version of the problem, it's straightforward to find the high price in each of the three stages:

n = 300;
HH = HHV(H,n);
SATHA = Ref(HH, -2*n); // Highest price between 600 and 900 bars ago
SATHB = Ref(HH, -1*n); // Highest price between 300 and 600 bars ago
SATHC = HH; // Highest price of the most recent 300 bars

However, using a conditional start, all you know for sure is that the earliest start bar for SATHA is 900 bars before the current bar. But that would only happen if the highest price in each stage occurs on the final (300th) bar of the stage, which is highly unlikely.

Before a solution can be coded, you (the coder) need to understand exactly how to solve the problem manually. I think you would find it helpful to work with a small data set (100 bars) and a small value of N (20). Now start at bar 0 and identify SATHA, SATHB, and SATHC. Repeat the exercise starting from bar 1, 2, 3... (N-20). See if you can determine a way to reliably determine the start bar for the entire pattern for any given end bar. I suspect the answer is "no", but perhaps I'm just not clever enough to see an elegant solution.

Your question implies that you want SATHA, SATHB, and SATHC to be arrays. If this is the case, and each value can only be determined by looping (forward or backward) through the data once for each element of SATHA, SATHB, and SATHC, then your code is likely to be extremely slow because of the amount of processing you will need to do for each symbol. That in turn may make you reconsider your entire approach.

One last hint: you might want to look at the functions HHVBars() and LLVBars() which would help determine how long ago the HHV and LLV occurred.

n = 300;
SATHA = HHV(H,n); // Highest price of the most recent 300 bars
SATHB = Ref(SATHA, -1*n); // Highest price between 300 and 600 bars ago
SATHC = Ref(SATHA, -2*n); // Highest price between 600 and 900 bars ago
MAXH= Max(SATHA , Max(SATHB ,SATHC )); // Highest high
SATLA = LLV(L,n); // lowest price of the most recent 300 bars
SATLB = Ref(SATLA, -1*n); // lowest price between 300 and 600 bars ago
SATLC = Ref(SATLA, -2*n); // lowest price between 600 and 900 bars ago
MINL= Min(SATLA , Min(SATLB ,SATLC )); // lowest low
BARH= BarsSince(H==MAXH) ; // number of bar to event hi
dtH = Ref( DateTime(), -BARH ); // date of event hi ( **** but it give me string ***)
BARL= BarsSince(L==MINL) ; // number of bar to event lo ( it is not correct )
dtL = Ref( DateTime(), -BARL ); // date of event lo ( **** but it give me string ***)

last 4 lines I think it is not correct.
this one give me 3358
BARL= BarsSince(L==MINL) ;