HighestSince in reverse (working backwards from future to past)

I'm trying to implement a variable period variant of the future HHV found here:

Although Ref and HHV both accept variable periods it doesn't work directly. For example:

function future_HHV(array, periods) {
   return Ref(HHV(array, periods), periods);
}


after_hours_first_bar_number = ValueWhen(is_after_hours_first_bar OR is_last_bar, bar_number, 0);
bars_to_end_regular_session = after_hours_first_bar_number - bar_number;


regular_session_future_high_aux_1 = HHV(corrected_close, bars_to_end_regular_session);
regular_session_future_high = Ref(regular_session_future_high_aux_1, bars_to_end_regular_session); 

Plot (regular_session_future_high, "regular_session_future_high", colorAqua, styleLine);

Had to delete the image, beacuse of forum rules... The plot is flat, the correct that should be is further down.

It produces a flat plot, as it always getting the same value of regular_session_future_high_aux_1 which is located at the end of the regular session.

In the other hand if I try the brute force variant:

function future_HHV_static_period(array, periods) {
   return Ref(HHV(array, periods), periods);
}


function future_HHV(array, period) {
	minperiod = LastValue(Lowest(period));
	maxperiod = LastValue(Highest(period));
	
	result = Null;
	
	for(p = minperiod; p <= maxperiod; p++) {	
		ind = future_HHV_static_period(array, p);
		result = IIf(period == p, ind, result);
	}
	
	return result;
}

Then it produces what I'm looking for which is the next upcoming highest high until the end of the regular session:

The problem is the brute force variant is too slow. It looks to me I just need to calculate the variable period correctly to pass it to the Ref call but I cannot come up with the solution.

Any help, please?

Too slow means by how much? can you compare different times. Thats because its Tomasz's suggestion and not someone's random code.

Also, how many iterations do min to max go through?

0.05s to 0.40s or more. and I need to do it with the lowest low as well. There is a loop there, it's going to be slow.

It's just that I think there has to be a fully vectorized version. I calculate the HHV:

regular_session_future_high_aux_1 = HHV(C, bars_to_end_regular_session);

and the plot is there but I need to come up with the appropriate variable period for the Ref:

regular_session_future_high = Ref(regular_session_future_high_aux_1, VARIABLE_PERIOD_TO SHIFT); 

Tried different things but it wrecks my head the vectorized way. I'll try to log and analyze the brute force version to come up with a vectorized variable shift if possible.

Any ideas, please??

how many times does the FOR Loop run(iterate)?
max - min ? if these iterations are huge, then it will be slow
Give data here

Yeah, I know, that's way I'm asking for a vectorized solution.

This is for intraday one minute explorations and for all US tickers.

The regular session has 6 hours and 30 minutes, so 390 minutes. Every minute, it has to calculate the futureHHV from the current bar up to last bar that ends the session. So bars_to_end_regular_session should range between 390 and 1.

First, you should verify that your calculation of after_hours_first_bar_number is correct. You did not include all your code, but I suspect that your calculation is not producing the values that you expect.

Second, if you use an Exploration on the other intermediate values, you will quickly see why the original function does not work with variable periods. Basically, your forward Ref() is always looking at the last bar of the day, and THAT bar is always looking at HHV(1).

Third, you might consider using Reverse() with ValueWhen() to achieve your goal. For example, here is a revised (and untested) calculation of your after_hours_first_bar_number variable:

after_hours_first_bar_number = Reverse(ValueWhen(Reverse(is_after_hours_first_bar OR is_last_bar), Reverse(bar_number)));
1 Like
premarket_start_time = 040000;
premarket_end_time = 093000;

regular_session_start_time = 093000;
regular_session_end_time = 160000;

after_hours_start_time = 160000;
after_hours_end_time = 200000;

date_number = DateNum();
time_number = TimeNum();
bar_number = BarIndex();
last_bar_number = LastValue(bar_number);

is_premarket = (premarket_start_time <= time_number) AND (time_number < regular_session_start_time);
is_regular_session = (regular_session_start_time <= time_number) AND (time_number < regular_session_end_time);
is_after_hours = (after_hours_start_time <= time_number) AND (time_number < after_hours_end_time);

is_after_hours_first_bar = (is_after_hours == True) AND (Ref(is_after_hours, -1) == False);
is_last_bar = bar_number == last_bar_number; 
after_hours_first_bar_number = ValueWhen(is_after_hours_first_bar OR is_last_bar, bar_number, 0);
bars_to_end_regular_session = after_hours_first_bar_number - bar_number;
bars_to_end_regular_session = IIf(is_regular_session, bars_to_end_regular_session, 1);

I would say it's correct. And It works fine with the brute force version of futureHHV.

Yeah I see Ref is always shifting to get HHV(1) that's why the vectorized variant plots a flat line.

after_hours_first_bar_number = ValueWhen(is_after_hours_first_bar OR is_last_bar, bar_number, 0);
bars_to_end_regular_session = after_hours_first_bar_number - bar_number;
bars_to_end_regular_session = IIf(is_regular_session, bars_to_end_regular_session, 1);
//corrected_close = IIf(C > O, C, O); 
corrected_close = C;

regular_session_future_high_aux_1 = HHV(C, bars_to_end_regular_session);

// THIS IS THE WRONG VARIABLE PERIOD REF CALL
regular_session_future_high = Ref(regular_session_future_high_aux_1, bars_to_end_regular_session);   

I think something like this should be pretty close to what you're looking for. You might have to adjust it by a bar if you don't want the current bar's price to be considered a "future high".

// A function like HighestSince, but looking into the future instead of the past
function HighestUntil(event, array)
{
	revHighestSince = HighestSince(Reverse(event), Reverse(array));
	return Reverse(revHighestSince);
}

regular_session_future_high = HighestUntil(is_after_hours, corrected_close);
5 Likes

Tested and works nicely. Many thanks!

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