{"id":534,"date":"2007-06-28T22:07:14","date_gmt":"2007-06-28T22:07:14","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/06\/28\/zoom-to-range-applications\/"},"modified":"2008-02-18T14:10:09","modified_gmt":"2008-02-18T14:10:09","slug":"zoom-to-range-applications","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/06\/28\/zoom-to-range-applications\/","title":{"rendered":"Zoom-to-Range Applications"},"content":{"rendered":"
This post offers a variety of zoom functions to help you analyze charts better. For example, you can zoom in on the cursor-selected bar, or you can step forward and backward from signal to signal or over custom price conditions. These functions are made possible by the ZoomToRange OLE function, which lets you zoom in on a specific date range. <\/p>\n
To exercise and test the functions below, copy and Insert the code to an Indicator pane, open the Param window, and click on various options to see how the functions work. For demo purposes, three zoom events are provided: Buy, Sell, and Mondays. In your own application, you would assign other conditions to the event variable, such as, for example, gaps, price patterns, etc. Stepping from Event to Event would allow you to visually analyze the chart surrounding events. The Param window looks like this: <\/p>\n
<\/p>\n
Zoom Increment is the number of bars added to the display with each zoom in\/out action. WindowWidth is the number of bars displayed when jumping from event to event.
\nThe program uses a FocusBar to which zoom dimensions are referenced. Yellow text is attached to the FocusBar on the chart to identify it. You can add other information to this PlotText(). For example, if you were analyzing buy, sell, short, and cover signals, you could add trade prices and indicator values, if you so desired. The Green histogram at the bottom of the chart indicates where the selected events occur. See an example chart below showing buy signals. The green histogram bar occurs just before the green buy arrow on the chart. <\/p>\n
<\/p>\n
Descriptions for the function below are presented ahead of the functions themselves so that you can copy all the code in one step.<\/p>\n
ZoomToIndex( FirstBarIndex, LastBarIndex)<\/strong> ZoomToCursor( ZoomWidth )<\/strong> ZoomOut( ZoomWidth )<\/strong> ZoomIn( ZoomWidth )<\/strong> ZoomToNext( Event, ZoomWidth )<\/strong> MarkFocus()<\/strong> ZoomAllOut()<\/strong> RightJustifyChart<\/strong>
\nCalling the ZoomToIndex() will zoom your chart to display a window ranging from the first argument (FirstBarIndex) to the second argument (LastBarIndex). While you cannot zoom in closer than ten bars using the Zoom In\/Out buttons on your AB Toolbar, this function lets you zoom all the way down to a two-bar window. Since most Indicators use periods expressed in bars and not in dates, the function uses the BarIndex to set the window width.<\/p>\n
\nThis function lets you zoom in to the cursor position and display the number of bars assigned to ZoomWidth. The values of important variables are displayed in the chart Title for educational purposes. <\/p>\n
\nThis function zooms out by increasing the number of bars displayed by ZoomWidth bars. <\/p>\n
\nThis function zooms in by reducing the number of bars displayed by ZoomWidth bars. <\/p>\n
\nThis function lets you jump to the next occurrence of a TRUE value in the Event Array. It also focuses on the event with a window of ZoomWidth bars. The most important application for this function would probably be to analyze trades by stepping from signal to signal and to visually inspect custom events or price patterns.
\n
\nZoomToPrev( Event, ZoomWidth )<\/strong>
\nThis function is similar to the one above, but it jumps (retreats) to the previous event. <\/p>\n
\nThis function places the Yellow text at the FocusBar. You may want to customize the text displayed. <\/p>\n
\nThis function zooms out to display all bars in the database.<\/p>\n
\nWhen trading Real-Time, your signals are generated by prices from the Last Bar whether this bar is visible or not. If, during intense trading, you miss the fact that your Chart is not fully right justified, you may see and hear orders going out while no actions show on your Chart. While such confusion usually doesn\u2019t last long, it could delay discretionary trades and even trick you into thinking there may be a coding problem, causing you to initiate debugging code. This function right-justifies your chart, and if it is triggered at the start of your session, it will prevent you from working with a shifted chart. <\/p>\nprocedure ZoomToIndex<\/span>( <\/span>FirstBarIndex<\/span>, <\/span>LastBarIndex<\/span>)\r{\r<\/span>StaticVarSet<\/span>(<\/span>"FirstBarIndex"<\/span>,<\/span>FirstBarIndex<\/span>);\r<\/span>StaticVarSet<\/span>(<\/span>"LastBarIndex"<\/span>,<\/span>LastBarIndex<\/span>);\r<\/span>DT <\/span>= <\/span>DateTime<\/span>();\r<\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r<\/span>LastDateTime <\/span>= <\/span>LastValue<\/span>(<\/span>ValueWhen<\/span>( <\/span>LastBarIndex <\/span>== <\/span>BI<\/span>, <\/span>DT <\/span>));\r<\/span>FirstDateTime <\/span>= <\/span>LastValue<\/span>(<\/span>ValueWhen<\/span>( <\/span>FirstBarIndex <\/span>== <\/span>BI<\/span>, <\/span>DT <\/span>));\r<\/span>LastDateTimestr <\/span>= <\/span>DateTimeToStr<\/span>( <\/span>LastDateTime <\/span>);\r<\/span>FirstDateTimestr <\/span>= <\/span>DateTimeToStr<\/span>( <\/span>FirstDateTime <\/span>);\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><\/pre>\n
procedure ZoomToCursor<\/span>( <\/span>ZoomWidth <\/span>)\r{\r<\/span>local ZoomWidth<\/span>;\r<\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r<\/span>SBI <\/span>= <\/span>SelectedValue<\/span>(<\/span>BI<\/span>);\r<\/span>LBI <\/span>= <\/span>LastValue<\/span>(<\/span>BI<\/span>);\r<\/span>CursorIndex<\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"CursorIndex"<\/span>));\r<\/span>FirstBarIndex <\/span>= <\/span>Max<\/span>(<\/span>0<\/span>,<\/span>SBI <\/span>- <\/span>ZoomWidth<\/span>);\r<\/span>LastBarIndex <\/span>= <\/span>Min<\/span>( <\/span>LBI<\/span>, <\/span>SBI <\/span>+ <\/span>ZoomWidth <\/span>);\r<\/span>ZoomToIndex<\/span>( <\/span>FirstBarIndex<\/span>, <\/span>LastBarIndex<\/span>);\r} <\/span><\/pre>\n
procedure ZoomOut<\/span>( <\/span>ZoomWidth <\/span>)\r{\r<\/span>local ZoomWidth<\/span>;\r<\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r<\/span>LBI <\/span>= <\/span>LastValue<\/span>(<\/span>BI<\/span>);\r<\/span>LastVisiblebar <\/span>= <\/span>Status<\/span>(<\/span>"LastVisibleBar"<\/span>);\r<\/span>FirstVisibleBar<\/span>= <\/span>Status<\/span>(<\/span>"FirstVisibleBar"<\/span>);\r<\/span>FirstBarIndex <\/span>= <\/span>Max<\/span>( <\/span>0<\/span>, <\/span>FirstVisibleBar <\/span>- <\/span>ZoomWidth<\/span>);\r<\/span>LastBarIndex <\/span>= <\/span>Min<\/span>( <\/span>LBI<\/span>, <\/span>LastVisibleBar <\/span>+ <\/span>ZoomWidth <\/span>);\r<\/span>ZoomToIndex<\/span>( <\/span>FirstBarIndex<\/span>, <\/span>LastBarIndex<\/span>);\r} <\/span><\/pre>\n
procedure ZoomIn<\/span>( <\/span>ZoomWidth <\/span>)\r{\r<\/span>local ZoomWidth<\/span>;\r<\/span>_TRACE<\/span>(<\/span>"##"<\/span>);\r<\/span>BI <\/span>= <\/span>BarIndex<\/span>();\r<\/span>LBI <\/span>= <\/span>LastValue<\/span>(<\/span>BI<\/span>);\r<\/span>LastVisiblebar <\/span>= <\/span>Status<\/span>(<\/span>"LastVisibleBar"<\/span>);\r<\/span>FirstVisibleBar<\/span>= <\/span>Status<\/span>(<\/span>"FirstVisibleBar"<\/span>);\r<\/span>FirstBarIndex <\/span>= <\/span>Max<\/span>( <\/span>0<\/span>, <\/span>FirstVisibleBar <\/span>+ <\/span>ZoomWidth<\/span>);\r<\/span>