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.
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.
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 ) );