Use the GUI controls to go date on a daily chart?

Q1) I want to use the GUI controls to go date on a daily chart,
and my code below was checked with "Vertify syntax" but no error,
but it just cannot work, please help!
//
Q2) it seems cannot debug the interactivity with the GUI control,
so is there another way to debug it?
Thank you very much!
//
my full code is as below:

idDateTime = 1; 
idGoDate = 2;
////////////////////////////////////////////////////////////////////////////////
function goDate()
{
	dt = DateTime();
	from_date = GuiGetText(idDateTime); 
	from_date = StrToDateTime(from_date);
	from_date = DateTimeConvert( 2, from_date );
	to_date = DateTimeAdd(from_date, 10, inDaily);

	Lookedup_startDate = Lookup(dt, from_date, -2); 
	Lookedup_endDate = Lookup(dt, to_date, -1); 

	beg_dt = DateTimeToStr( Lookedup_startDate ); 
	end_dt = DateTimeToStr( Lookedup_endDate );   

	zoom_trigger = ParamTrigger( "Zoom to Range", "CLICK HERE" );
	if ( zoom_trigger ) {		
		AB = CreateObject( "Broker.Application" );
		AW = AB.ActiveWindow;
		AW.ZoomToRange( beg_dt , end_dt );
	}
} 

function CreateGUI()
{
	// Create a DateTime GUI control
	GuiDateTime(idDateTime, 5, 25, 140, 30, notifyEditChange);
	//
	// Create a Button GUI control
	GuiButton("&Go", idGoDate, 150, 25, 40, 30, notifyClicked);
}

function HandleEvents()
{   
	for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {   
		switch ( id ) 
		{
			case idGoDate:  goDate();  break;
		}
    }
}
////////////////////////////////////////////////////////////////////////////////
CreateGUI(); 
HandleEvents();

@alexlin, the simplest way to "debug" your code is to use a few extra lines of code that display the flow of the code and some intermediate values ​​to verify that they are what you expect.

In Amibroker, some functions can be used for this purpose. My preferred ones are _Trace () and _Tracef (), which I use with an external log visualizer like this one (but you can also use the AB internal one).

Here, for example, is how I would have modified your code to understand what happens (there may be more changes and improvements).

SetBarsRequired( -2, -2 ); // require ALL bars  - to ensure that Lookup() will work properly

idDateTime = 1;
idDaysSpan = 2;
idGoDate   = 3;

daysSpanDefault = 10;

////////////////////////////////////////////////////////////////////////////////
function goDate()
{
    _TRACE( "goDate() - called from Handle Events" );
    dt = DateTime();
    from_GUIStr = GuiGetText( idDateTime );
    daysSpan = StrToNum( GuiGetText( idDaysSpan ) ); // added a control to get a variable range of days

    // What is the value of daysSpan if the content of the edit is NOT a valid number? it return zero...
    if( daysSpan <= 0 )
        daysSpan = daysSpanDefault;

    _TRACEF( "Type of: daysspan %s - Value %g", typeof( daysSpan ), daysSpan );
    from_dateDt = StrToDateTime( from_GUIStr );
    from_dateDtStr = DateTimetoStr( from_dateDt, 3 ); // ISO FORMAT
    _TRACEF( "GUI text: %s - from_date (as string): %s", from_GUIStr, from_dateDtStr );
    to_dateDt = DateTimeAdd( from_dateDt, daysSpan, inDaily );
    to_dateDtStr = DateTimeToStr( to_dateDt, 3 );
    _TRACEF( "to_dateDT (as string): %s", to_dateDtStr );
    _TRACEF( "Type of: from_dateDT %s - to_dateDT %s", typeof( from_dateDt ), typeof( to_dateDt ) );

    // Lookup using all bars....
    Lookedup_startDate = Lookup( dt, from_dateDt, -1 );
    Lookedup_endDate = Lookup( dt, to_dateDt, 1 );
    // alternative...
    // beg_dt = DateTimeFormat( "%Y-%m-%d", Lookedup_startDate );
    // end_dt = DateTimeFormat( "%Y-%m-%d", Lookedup_endDate );
    beg_dt = DateTimeToStr( Lookedup_startDate, 3 );
    end_dt = DateTimeToStr( Lookedup_endDate, 3 );
    _TRACEF( "beg_Dt (String):  %s - end_dt (String): %s", beg_dt, end_dt );

	// As part of this function this is triggered each time the Go button is clicked
    AB = CreateObject( "Broker.Application" );
    AW = AB.ActiveWindow;
    AW.ZoomToRange( beg_dt , end_dt );
}

function CreateGUI()
{
    // Create a DateTime GUI control
    GuiDateTime( idDateTime, 5, 25, 140, 25, 0 ); // you do not handle any change here
    // Added and edit to choose a days span
    rc = GuiEdit( idDaysSpan, 150, 25, 50, 25, 0 );

    if( rc == guiNew )
        GuiSetText( NumToStr( daysSpanDefault ), idDaysSpan );

    // Create a Button GUI control
    GuiButton( "&Go", idGoDate, 205, 25, 40, 25, notifyClicked );
}

function HandleEvents()
{
    // use a loop to clear all the queed events
    for( n = 0; id = GuiGetEvent( n, 0 ); n++ )
    {
        code = GuiGetEvent( n, 1 );
        text = GuiGetEvent( n, 2 ); // optional used only as an example
        _TRACEF( "n %g - ID: %g - Code: %g - Event: \"%s\" - NotifyClicked value: %g", n, id, code, text, notifyClicked );

        switch( id )
        {
            case idGoDate:
                if( code == notifyClicked )
                {
                    goDate();
                }
                break;

            default:
                break;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
CreateGUI();
HandleEvents();

Please, note that I have deleted the manual trigger and added the SetBarsRequired() function at the top.

Some of the code is or seems redundant, but I hope it gives an idea of ​​how to proceed.

Once you have verified that everything is as desired, the debug code can be commented out (usually, I hardly ever remove it).

2 Likes