How to get H and L the last of prev month

Dear AFL Experts,

How are you today!

I am currently exploring and learning to program. I have also searched the forum for fxshrat's code, which I am attaching below. However, I am struggling to determine the highest and lowest prices of the last day of the month.

I would greatly appreciate any help from you.

Thank you very much!

SetOption("WarningLevel", 1);
_N( Formula_Name = "" );
_N( System_Ver = "" + Name() );
Day_OfWeek = DayOfWeek();
No_Month = Month();
SetBarsRequired( sbrAll, 0 );
RequestTimedRefresh( 1, False );
_N( Day_Name = WriteIf( Day_OfWeek == 0, "Sun", WriteIf( Day_OfWeek == 1, "Mon", WriteIf( Day_OfWeek == 2, "Tue", WriteIf( Day_OfWeek == 3, "Wed",	WriteIf( Day_OfWeek == 4, "Thu",	WriteIf( Day_OfWeek == 5, "Fri", WriteIf( Day_OfWeek == 6, "Sat",	"Unknown" ) ) ) ) ) ) ) );
_N( Month_Name = Writeif( No_Month == 1, "Jan", Writeif( No_Month == 2, "Feb", Writeif( No_Month == 3, "Mar", Writeif( No_Month == 4, "Apr", Writeif( No_Month == 5, "May",
                          Writeif( No_Month == 6, "Jun", Writeif( No_Month == 7, "Jul", Writeif( No_Month == 8, "Aug", Writeif( No_Month == 9, "Sept", Writeif( No_Month == 10, "Oct", Writeif( No_Month == 11, "Nov", Writeif( No_Month == 12, "Dec", "Unknown" ) ) ) ) ) ) ) ) ) ) ) ) );
_N(Hour_Min_Sec = WriteIf( Hour() > 9, "" + Hour(), "0" + Hour() ) + ":" + WriteIf( Minute() > 9, "" + Minute(), "0" + Minute() ) + ":" + WriteIf( Second() > 9, "" + Second(), "0" + Second() ));
_N( CustomDateFormat = Day_Name + " " + NumToStr( Day(), 1.0 ) + " " + Month_Name + " " + NumToStr( Year(), 1.0, False ) + "  " + WriteIf( Interval() < inDaily, Hour_Min_Sec, "" ) );
Yogya_Upcolor = ColorRGB( 0, 177, 88 );
Yogya_DownColor = ColorRGB( 246, 70, 65 );
Setchartoptions( 0, Chartshowarrows | chartShowDates );
SetbarfillColor( IIf( C > O,  Yogya_Upcolor, Yogya_DownColor ) );
Plot( C, "Price", IIf( C > O,  Yogya_Upcolor, Yogya_DownColor ), styleCandle | styleNoTitle );
SetFormulaName( Formula_Name + "" + System_Ver );
_N( Title = EncodeColor( ColorRGB( 255, 150, 150 ) ) + StrFormat( Formula_Name + "" + System_Ver + "  {{INTERVAL}}  " ) + CustomDateFormat + "" + StrFormat( "  Open = %g, Hi = %g, Lo = %g, Close = %g, Volume = %g Change = (%.1f%%) {{VALUES}}", O, H, L, C, V, SelectedValue( ROC( C, 1 ) ) ) );

SetTradeDelays(0,0,0,0);


function ValueAtPrevRangeHighest(start_of_range, array1, array2) {
	/// code source:
	/// @link https://forum.amibroker.com/t/day-of-high-and-low-within-a-monthly-bar/14175/2	
	local hh_bars, ref_array2;
	// bars that have passed since highestsince()
	hh_bars = HighestSinceBars(start_of_range, array1, 1);
	// get array2 at highestsince() by "looking" backwards hh_bars
	ref_array2 = Ref(array2, -hh_bars);
	// only get prev. array2 at start of new time range
	result = ValueWhen(start_of_range, Ref(ref_array2, -1));
	return result;
}
function ValueAtPrevRangeLowest(start_of_range, array1, array2) {
	/// code source:
	/// @link https://forum.amibroker.com/t/day-of-high-and-low-within-a-monthly-bar/14175/2
	local ll_bars, ref_array2;
	// bars that have passed since lowestsince()
	ll_bars = LowestSinceBars(start_of_range, array1, 1);
	// get array2 at lowestsince() by "looking" backwards ll_bars
	ref_array2 = Ref(array2, -ll_bars);
	// only get prev. array2 at start of new time range
	result = ValueWhen(start_of_range, Ref(ref_array2, -1));
	return result;
}

dt = DateTime();
mth = Month();
new_mth = mth != Ref(mth, -1 );

prev_mth_high_date = ValueAtPrevRangeHighest(new_mth, H, dt);
prev_mth_low_date = ValueAtPrevRangeLowest(new_mth, L, dt);

Plot( C, "Price", colorDefault, styleBar );
Plot( TimeFrameGetPrice("H", inMonthly, -1), "Previous Month's High", colorGreen, styleDots );
Plot( TimeFrameGetPrice("L", inMonthly, -1), "Previous Month's Low", colorRed, styleDots );

printf( "Previous month's HHV's date %s\n", DateTimeToStr(SelectedValue(prev_mth_high_date)));
printf( "Previous month's LLV's date %s", DateTimeToStr(SelectedValue(prev_mth_low_date)));

@Huynq99 , try this exploration (apply to a single instrument):

mth = Month();
new_mth = mth != Ref( mth, -1 );

prev_mth_last_high = ValueWhen( new_mth, Ref( H, -1 ) );
prev_mth_last_low = ValueWhen( new_mth, Ref( L, -1 ) );

// This little utility function is only used to help clearly identify non-null values in a column.
function nil( a )
{
    return ( iif( a, a, Null ) );
}

Filter = 1;
AddColumn( H, "High" );
AddColumn( prev_mth_last_high, "Last Day of Prev. Month High" );
AddColumn( L, "Low" );
AddColumn( prev_mth_last_low, "Last Day of Prev. Month Low" );
AddColumn( Nil( new_mth ), "New Month", 1 );
SetSortColumns( -2 );

Try to fully understand what the ValueWhen() function is used for, examining also the way it is used in the two excellent code functions you found in this forum.

2 Likes

Dear Peppe,

I sincerely appreciate your kind assistance.
Wishing you happiness and joy.

Best regards,

1 Like