February 18, 2008
Restore Last Used Range
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:
1 | RestoreLastUsedRange(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | \\Pay attention to the double slashes in the path line. They need to be there. PersistentPath = "C:\\Program Files\\AmiBroker\\PersistentVariables\\"; 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 ) { StaticVarSet( "FirstBarIndex", FirstBarIndex ); StaticVarSet( "LastBarIndex", LastBarIndex ); DT = DateTime(); BI = BarIndex(); LastDateTime = LastValue( ValueWhen( LastBarIndex == BI, DT ) ); FirstDateTime = LastValue( ValueWhen( FirstBarIndex == BI, DT ) ); LastDateTimestr = DateTimeToStr( LastDateTime ); FirstDateTimestr = DateTimeToStr( FirstDateTime ); AB = CreateObject( "Broker.Application" ); AW = AB.ActiveWindow; AW.ZoomToRange( FirstDateTimestr, LastDateTimestr ); } function RestoreLastUsedRange() { if ( Status( "Action" ) == 1 ) { ChartIDStr = NumToStr( GetChartID(), 1.0, False ); PrevFirstBarIndex = PersistentVarGet( "FirstBarIndex" + ChartIDStr ); PrevLastBarIndex = PersistentVarGet( "LastBarIndex" + ChartIDStr ); if ( IsNull( StaticVarGet( "StartUp" + ChartIDStr ) ) ) { ZoomToIndex( PrevFirstBarIndex, PrevLastBarIndex ); StaticVarSet( "StartUp" + ChartIDStr, True ); //_TRACE( "# StartUp: CID: " + ChartIDStr + ", PFBI: " + PrevFirstBarIndex + ", PLBI: " + PrevLastBarIndex ); } FirstBarIndex = Status( "firstvisiblebarindex" ); LastBarIndex = Status( "lastvisiblebarindex" ); if ( PrevFirstBarIndex != FirstBarIndex OR PrevLastBarIndex != LastBarIndex ) { PersistentVarSet( "FirstBarIndex" + ChartIDStr, FirstBarIndex ); PersistentVarSet( "LastBarIndex" + ChartIDStr, LastBarIndex ); //_TRACE( "# UpdateRange: CID: " + ChartIDStr + ", FBI: " + FirstBarIndex + ", LBI: " + LastBarIndex ); } } } |
///// All code above this line can be placed in an Include file /////
1 2 3 4 5 6 7 | Plot( C, "", 1, 128 ); RestoreLastUsedRange(); Title = "\n" + "ChartIDStr: " + NumToStr( GetChartID(), 1.0, False ) + "\n" + "FirstIndex: " + Status( "firstvisiblebarindex" ) + "\n" + " LastIndex: " + Status( "Lastvisiblebarindex" ); |
Edited by Al Venosa.
Filed by Herman at 1:54 pm under AFL - Utilities and Functions
No Comments


6 Comments


(7 votes, average: 4.29 out of 5)
