{"id":1537,"date":"2008-02-18T13:54:59","date_gmt":"2008-02-18T13:54:59","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/02\/18\/restore-last-used-range\/"},"modified":"2011-03-04T15:13:34","modified_gmt":"2011-03-04T15:13:34","slug":"restore-last-used-range","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/02\/18\/restore-last-used-range\/","title":{"rendered":"Restore Last Used Range v2"},"content":{"rendered":"
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>\n
<\/span><<\/span>p<\/span>><\/span>RestoreLastUsedRange<\/span>();<\/<\/span>p<\/span>><\/span><\/pre>\nThis 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.<\/p>\n
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.<\/p>\n
<\/span>\/\/Pay attention to the double slashes in the path line. They need to be there.\r\r\/\/ This creates the folder for PersistentPath variables if it doesn't exist\r<\/span>PersistentPath <\/span>= <\/span>StaticVarGetText<\/span>( <\/span>"~PersistentPath" <\/span>);\r\rif ( <\/span>PersistentPath <\/span>== <\/span>"" <\/span>) <\/span>\/\/ Ensures this folder exists\r<\/span>{\r <\/span>PersistentPath <\/span>= <\/span>"PersistentVariables\\\\"<\/span>;\r <\/span>fmkdir<\/span>( <\/span>PersistentPath <\/span>);\r <\/span>StaticVarSetText<\/span>( <\/span>"~PersistentPath"<\/span>, <\/span>PersistentPath <\/span>);\r}\r\rfunction <\/span>PersistentVarGet<\/span>( <\/span>VarName <\/span>)\r\r{\r global <\/span>PersistentPath<\/span>;\r <\/span>fh <\/span>= <\/span>fopen<\/span>( <\/span>PersistentPath <\/span>+ <\/span>VarName <\/span>+ <\/span>".pva"<\/span>, <\/span>"r" <\/span>);\r\r if ( <\/span>fh <\/span>)\r {\r <\/span>String <\/span>= <\/span>fgets<\/span>( <\/span>fh <\/span>);\r <\/span>fclose<\/span>( <\/span>fh <\/span>);\r }\r else\r <\/span>string <\/span>= <\/span>""<\/span>;\r\r <\/span>Number <\/span>= <\/span>StrToNum<\/span>( String );\r\r return <\/span>Number<\/span>;\r}\r\rfunction <\/span>PersistentVarSet<\/span>( <\/span>VarName<\/span>, <\/span>Number <\/span>)\r{\r global <\/span>PersistentPath<\/span>;\r <\/span>String <\/span>= <\/span>NumToStr<\/span>( <\/span>Number<\/span>, <\/span>1.3<\/span>, <\/span>False <\/span>);\r <\/span>fh <\/span>= <\/span>fopen<\/span>( <\/span>PersistentPath <\/span>+ <\/span>VarName <\/span>+ <\/span>".pva"<\/span>, <\/span>"w" <\/span>);\r\r if ( <\/span>fh <\/span>)\r {\r <\/span>fputs<\/span>( <\/span>String<\/span>, <\/span>fh <\/span>);\r <\/span>fclose<\/span>( <\/span>fh <\/span>);\r }\r\r return <\/span>fh<\/span>;\r}\r\r<\/span>procedure ZoomToIndex<\/span>( <\/span>FirstBarIndex<\/span>, <\/span>LastBarIndex <\/span>)\r{\r <\/span>DT <\/span>= <\/span>DateTime<\/span>();\r <\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r <\/span>FirstDateTime <\/span>= <\/span>LastValue<\/span>( <\/span>ValueWhen<\/span>( <\/span>FirstBarIndex <\/span>== <\/span>BI<\/span>, <\/span>DT <\/span>) );\r <\/span>LastDateTime <\/span>= <\/span>LastValue<\/span>( <\/span>ValueWhen<\/span>( <\/span>LastBarIndex <\/span>== <\/span>BI<\/span>, <\/span>DT <\/span>) );\r\r <\/span>FirstDateTimestr <\/span>= <\/span>DateTimeToStr<\/span>( <\/span>FirstDateTime <\/span>);\r <\/span>LastDateTimestr <\/span>= <\/span>DateTimeToStr<\/span>( <\/span>LastDateTime <\/span>);\r\r <\/span>AB <\/span>= <\/span>CreateObject<\/span>( <\/span>"Broker.Application" <\/span>);\r <\/span>AW <\/span>= <\/span>AB<\/span>.<\/span>ActiveWindow<\/span>;\r <\/span>AW<\/span>.<\/span>ZoomToRange<\/span>( <\/span>FirstDateTimestr<\/span>, <\/span>LastDateTimestr <\/span>);\r <\/span>\/\/_TRACE( FirstDateTimestr +\", \"+LastDateTimestr );\r<\/span>}\r\rfunction <\/span>RestoreLastUsedRange<\/span>()\r{\r if ( <\/span>Status<\/span>( <\/span>"Action" <\/span>) == <\/span>1 <\/span>) <\/span>\/\/ Only perform ranging in an indicator\r <\/span>{\r <\/span>\/\/ is this a \"start-up execution?\r <\/span>ChartIDStr <\/span>= <\/span>NumToStr<\/span>( <\/span>GetChartID<\/span>(), <\/span>1.0<\/span>, <\/span>False <\/span>);\r <\/span>PrevFirstBarIndex <\/span>= <\/span>PersistentVarGet<\/span>( <\/span>"FirstBarIndex" <\/span>+ <\/span>ChartIDStr <\/span>); <\/span>\/\/ Recall last positions\r <\/span>PrevLastBarIndex <\/span>= <\/span>PersistentVarGet<\/span>( <\/span>"LastBarIndex" <\/span>+ <\/span>ChartIDStr <\/span>);\r\r if ( <\/span>IsNull<\/span>( <\/span>StaticVarGet<\/span>( <\/span>"StartUp" <\/span>+ <\/span>ChartIDStr <\/span>) ) )\r {\r <\/span>ZoomToIndex<\/span>( <\/span>PrevFirstBarIndex<\/span>, <\/span>PrevLastBarIndex <\/span>);\r <\/span>StaticVarSet<\/span>( <\/span>"StartUp" <\/span>+ <\/span>ChartIDStr<\/span>, <\/span>True <\/span>);\r <\/span>\/\/_TRACE( \"# StartUp Zoom: CHARTID: " + ChartIDStr + \", PrevFirstBI: " + PrevFirstBarIndex + \", PrevLastBI: " + PrevLastBarIndex );\r <\/span>