Using range selector and BeginValue/EndValue

Hello,

When I do not select the Range begin (F12) and Range End (Shift + F12) on chart explicitly, in such a case the first bar date of the Symbol is set as Range begin and the last bar date as the Range End dates.

RangeFromDate = DateTimeToStr( BeginValue(dt), mode);
RangeToDate = DateTimeToStr( EndValue(dt), mode);

The same way, when I have not explocitly selected a particular date on chart still I get the last bar date of the symbol.

bardate = DateTimeToStr( SelectedValue(DateTime()), mode = 4) ;

Appears like that how it has been designed to work for.

But what I intend to do is, if I do not select any Begin or End Range explicitly then such dates should be empty when assigned to a variable. The same for Date also, If i have not selected a particular date on chart then the bardate variable (as above) should be Null or empty.

How Can I do that?

You rather should have opened a new topic since your question has got nothing to do with “escaping double quotes”.

But anyway…

/// source @link http://forum.amibroker.com/t/using-range-selector-and-beginvalue-endvalue/772/2
EnableTextOutput( 0 );

mode = 4;

dt = DateTime();

bgdt = BeginValue(dt);
enddt = EndValue(dt);

if( bgdt == dt[0] || enddt == LastValue(dt) ) {
//if( bgdt == dt[0] && enddt == LastValue(dt) ) { // alternatively
	bgdt = enddt = NULL;
}

RangeFromDate = DateTimeToStr( bgdt, mode);
RangeToDate = DateTimeToStr( enddt, mode);

printf( "Begin date: %s\nEnd date: %s", RangeFromDate, RangeToDate );

1 Like

As for selected date you could do like so:

/// source @link http://forum.amibroker.com/t/using-range-selector-and-beginvalue-endvalue/772/3

EnableTextOutput( 0 );

mode = 4;

dt = DateTime();
selectdt = SelectedValue(dt);

gcmb = GetCursorMouseButtons(); /// @link https://www.amibroker.com/guide/afl/getcursormousebuttons.html
gcx_dt = GetCursorXPosition( 0 );/// @link https://www.amibroker.com/guide/afl/getcursorxposition.html
gcx_px = GetCursorXPosition( 1 );
lmbdown = gcmb & 1;

if( lmbdown && gcx_dt <= LastValue(dt ) && gcx_px < Status( "pxchartwidth" ) ) {
	StaticVarSet( "SelectedDate", selectdt );
} else
	StaticVarRemove( "SelectedDate" ); 	

printf( "\nSelected date: %s", DateTimeToStr( StaticVarGet( "SelectedDate" ), mode ) );

Plot( C, "Price", colorDefault, styleCandle );
2 Likes

@fxshrat I will make a note to create new Topic next time.

You code works. I did some modification to achieve below, but my modified code is not giving intended results.

Conditions:

  1. When Range Being date is not explicitly selected then make it Null. If it’s explicitly selected then use the Selected being Value.

  2. When Range End date is not explicitly selected then make it Null. If it’s explicitly selected then use the Selected End Value.

  3. if Only Begin date is selected and No End Date is Selected then make End date as Null and use the Beging Selected date for “RangeFromDate”.

  4. When No Single date is selected on Chart then make the bardate variable as NULL.

my modified version:

EnableTextOutput( 0 );

mode = 4;

dt = DateTime();

bgdt = BeginValue(dt);
enddt = EndValue(dt);
seldate = SelectedValue(dt);


if( bgdt == dt[0] || enddt == LastValue(dt)  ) {
//if( bgdt == dt[0] && enddt == LastValue(dt) ) { // alternatively
	bgdt = enddt = NULL;
}

if( bgdt != dt[0] || enddt == LastValue(dt)  ) {
//if( bgdt == dt[0] && enddt == LastValue(dt) ) { // alternatively
	enddt = NULL;
}

if( seldate == LastValue(dt)  ) {
//if( bgdt == dt[0] && enddt == LastValue(dt) ) { // alternatively
	seldate = NULL;
}


RangeFromDate = DateTimeToStr( bgdt, mode);
RangeToDate = DateTimeToStr( enddt, mode);
bardate = DateTimeToStr( seldate, mode);

printf( "Begin date: %s\nEnd date: %s\n Selected Date: %s", RangeFromDate, RangeToDate, bardate );


First of all, Thanks to @fxshrat for helping me think the right way. He did most of the code, I did few changes. All credit to him.

Below code satisfy all conditions as mentioned in me immediate above post.

Please read the purpose of the code in below comments before using it.

//Source @link http://forum.amibroker.com/t/using-range-selector-and-beginvalue-endvalue/772/4
//Credit - fxshrat
/*
Purpose:

1) When Range Being date is not explicitly selected then make it same as Visible begin date. If it's explicitly selected then use the Selected being Value.

2) When Range End date is not explicitly selected then make it as Visible Last date. If it's explicitly selected then use the Selected End Value.

3) if Only Begin date is selected and No End Date is Selected then make End date as Last Visible Date and use the Beging Selected date for "RangeFromDate".

4) When No Single date is selected on Chart then make the bardate variable as NULL.
*/


EnableTextOutput( 0 );

mode = 4;

dt = DateTime();

bgdt = BeginValue(dt);
enddt = EndValue(dt);
visbgdt = FirstVisibleValue(dt);
visenddt = LastVisibleValue(dt);


if( bgdt == dt[0] ) 
	{

	bgdt = FirstVisibleValue(dt);
		
	}
	
if( enddt == LastValue(dt) ) 
	{

	enddt= LastVisibleValue(dt);
	
	}

RangeFromDate = DateTimeToStr( bgdt, mode);
RangeToDate = DateTimeToStr( enddt, mode);

printf( "Begin date: %s\nEnd date: %s\n", RangeFromDate, RangeToDate);


selectdt = SelectedValue(dt);

gcmb = GetCursorMouseButtons(); /// @link https://www.amibroker.com/guide/afl/getcursormousebuttons.html
gcx_dt = GetCursorXPosition( 0 );/// @link https://www.amibroker.com/guide/afl/getcursorxposition.html
gcx_px = GetCursorXPosition( 1 );
lmbdown = gcmb & 1;

if( lmbdown && gcx_dt <= LastValue(dt ) && gcx_px < Status( "pxchartwidth" ) ) {
	StaticVarSet( "SelectedDate", selectdt );
} else
	StaticVarRemove( "SelectedDate" ); 	

printf( "\nSelected date: %s", DateTimeToStr( StaticVarGet( "SelectedDate" ), mode ) );

1 Like