Restore Last Used Range v2

When starting up AmiBroker the chart range defaults to the last number of bars set in Preferences. This means that in virtually all cases you have to manually zoom and restore the chart range you were previously working with. This post presents a function that will restore the last used range before you shut down AmiBroker. If you like to use this function routinely, you can copy the functions below to your default #Include file and place this call at the top of your code:

<p>RestoreLastUsedRange();</p>

This function checks the contents of a static variable named “StartUp”+ChartIDStr. This static variable will return a NULL the first time it is called. When this happens, the ZoomToIndex() is called. This function retrieves the last used range for the current chartID from your hard disk and zooms to this range. The Plot() and Title statements are added to help you verify proper operation. You can also uncomment the two _TRACE() commands and start up DebugView for further diagnostic purposes.

If you do not have it yet, be sure to create a folder for the PersistentVariables, as shown at the top of the code. This is where the First and Last Indexes, which point to the start and end of the range being restored, are saved in a small file. You can open these with Notepad to help debugging operations.

//Pay attention to the double slashes in the path line. They need to be there.

// This creates the folder for PersistentPath variables if it doesn't exist
PersistentPath StaticVarGetText"~PersistentPath" );

if ( PersistentPath == "" // Ensures this folder exists
{
    PersistentPath "PersistentVariables\\";
    fmkdirPersistentPath );
    StaticVarSetText"~PersistentPath"PersistentPath );
}

function PersistentVarGetVarName )

{
    global PersistentPath;
    fh fopenPersistentPath VarName ".pva""r" );

    if ( fh )
    {
        String fgetsfh );
        fclosefh );
    }
    else
        string "";

    Number StrToNum( String );

    return Number;
}

function PersistentVarSetVarNameNumber )
{
    global PersistentPath;
    String NumToStrNumber1.3False );
    fh fopenPersistentPath VarName ".pva""w" );

    if ( fh )
    {
        fputsStringfh );
        fclosefh );
    }

    return fh;
}

procedure ZoomToIndexFirstBarIndexLastBarIndex )
{
    DT DateTime();
    BI BarIndex();
    FirstDateTime LastValueValueWhenFirstBarIndex == BIDT ) );
    LastDateTime LastValueValueWhenLastBarIndex == BIDT ) );

    FirstDateTimestr DateTimeToStrFirstDateTime );
    LastDateTimestr DateTimeToStrLastDateTime );

    AB CreateObject"Broker.Application" );
    AW AB.ActiveWindow;
    AW.ZoomToRangeFirstDateTimestrLastDateTimestr );
    //_TRACE( FirstDateTimestr +", "+LastDateTimestr );
}

function RestoreLastUsedRange()
{
    if ( Status"Action" ) == // Only perform ranging in an indicator
    {
        // is this a "start-up execution?
        ChartIDStr NumToStrGetChartID(), 1.0False );
        PrevFirstBarIndex PersistentVarGet"FirstBarIndex" ChartIDStr ); // Recall last positions
        PrevLastBarIndex PersistentVarGet"LastBarIndex" ChartIDStr );

        if ( IsNullStaticVarGet"StartUp" ChartIDStr ) ) )
        {
            ZoomToIndexPrevFirstBarIndexPrevLastBarIndex );
            StaticVarSet"StartUp" ChartIDStrTrue );
            //_TRACE( "# StartUp Zoom: CHARTID: " + ChartIDStr + ", PrevFirstBI: " + PrevFirstBarIndex + ", PrevLastBI: " + PrevLastBarIndex );
        }

        // Update zoom range on disk
        FirstBarIndex Status"firstvisiblebarindex" );

        LastBarIndex Status"lastvisiblebarindex" );

        if ( PrevFirstBarIndex != FirstBarIndex OR PrevLastBarIndex != LastBarIndex )
        {
            PersistentVarSet"FirstBarIndex" ChartIDStrFirstBarIndex );
            PersistentVarSet"LastBarIndex" ChartIDStrLastBarIndex );
            //_TRACE( "# Update Zoom Range: CHARTID: " + ChartIDStr + ", FirstBI: " + FirstBarIndex + ", LastBI: " + LastBarIndex );
        }
    }
}

_SECTION_BEGIN"RESTORE RANGE" );

if ( ParamTrigger"Clear all Static Variables""CLEAR" ) )
    StaticVarRemove"*" );

PlotC""1128 );

SetBarsRequiredsbrAllsbrAll );

RestoreLastUsedRange();

Title "\n" +
        " ChartIDStr: " NumToStrGetChartID(), 1.0False ) + "\n" +
        " FirstIndex: " Status"firstvisiblebarindex" ) + "\n" +
        "  LastIndex: " Status"Lastvisiblebarindex" ) + "\n" +
        "Selected BI: " BarIndex();
_SECTION_END();

Edited by Al Venosa.

Comments are closed.