February 18, 2008
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\\"; fmkdir( PersistentPath ); StaticVarSetText( "~PersistentPath", PersistentPath ); } function PersistentVarGet( VarName ) { global PersistentPath; fh = fopen( PersistentPath + VarName + ".pva", "r" ); if ( fh ) { String = fgets( fh ); fclose( fh ); } else string = ""; Number = StrToNum( String ); return Number; } function PersistentVarSet( VarName, Number ) { global PersistentPath; String = NumToStr( Number, 1.3, False ); fh = fopen( PersistentPath + VarName + ".pva", "w" ); if ( fh ) { fputs( String, fh ); fclose( fh ); } return fh; } procedure ZoomToIndex( FirstBarIndex, LastBarIndex ) { DT = DateTime(); BI = BarIndex(); FirstDateTime = LastValue( ValueWhen( FirstBarIndex == BI, DT ) ); LastDateTime = LastValue( ValueWhen( LastBarIndex == BI, DT ) ); FirstDateTimestr = DateTimeToStr( FirstDateTime ); LastDateTimestr = DateTimeToStr( LastDateTime ); AB = CreateObject( "Broker.Application" ); AW = AB.ActiveWindow; AW.ZoomToRange( FirstDateTimestr, LastDateTimestr ); //_TRACE( FirstDateTimestr +", "+LastDateTimestr ); } function RestoreLastUsedRange() { if ( Status( "Action" ) == 1 ) // Only perform ranging in an indicator { // is this a "start-up execution? ChartIDStr = NumToStr( GetChartID(), 1.0, False ); PrevFirstBarIndex = PersistentVarGet( "FirstBarIndex" + ChartIDStr ); // Recall last positions PrevLastBarIndex = PersistentVarGet( "LastBarIndex" + ChartIDStr ); if ( IsNull( StaticVarGet( "StartUp" + ChartIDStr ) ) ) { ZoomToIndex( PrevFirstBarIndex, PrevLastBarIndex ); StaticVarSet( "StartUp" + ChartIDStr, True ); //_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" + ChartIDStr, FirstBarIndex ); PersistentVarSet( "LastBarIndex" + ChartIDStr, LastBarIndex ); //_TRACE( "# Update Zoom Range: CHARTID: " + ChartIDStr + ", FirstBI: " + FirstBarIndex + ", LastBI: " + LastBarIndex ); } } } _SECTION_BEGIN( "RESTORE RANGE" ); if ( ParamTrigger( "Clear all Static Variables", "CLEAR" ) ) StaticVarRemove( "*" ); Plot( C, "", 1, 128 ); SetBarsRequired( sbrAll, sbrAll ); RestoreLastUsedRange(); Title = "\n" + " ChartIDStr: " + NumToStr( GetChartID(), 1.0, False ) + "\n" + " FirstIndex: " + Status( "firstvisiblebarindex" ) + "\n" + " LastIndex: " + Status( "Lastvisiblebarindex" ) + "\n" + "Selected BI: " + BarIndex(); _SECTION_END();
Edited by Al Venosa.
Filed by Herman at 1:54 pm under AFL - Utilities and Functions
Comments Off on Restore Last Used Range v2