Zoom-to-Range Applications

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.

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:

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.
The 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.

Descriptions for the function below are presented ahead of the functions themselves so that you can copy all the code in one step.

ZoomToIndex( FirstBarIndex, LastBarIndex)
Calling 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.

ZoomToCursor( ZoomWidth )
This 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.

ZoomOut( ZoomWidth )
This function zooms out by increasing the number of bars displayed by ZoomWidth bars.

ZoomIn( ZoomWidth )
This function zooms in by reducing the number of bars displayed by ZoomWidth bars.

ZoomToNext( Event, ZoomWidth )
This 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.

ZoomToPrev( Event, ZoomWidth )

This function is similar to the one above, but it jumps (retreats) to the previous event.

MarkFocus()
This function places the Yellow text at the FocusBar. You may want to customize the text displayed.

ZoomAllOut()
This function zooms out to display all bars in the database.

RightJustifyChart
When 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’t 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.

procedure ZoomToIndexFirstBarIndexLastBarIndex)
{
StaticVarSet("FirstBarIndex",FirstBarIndex);
StaticVarSet("LastBarIndex",LastBarIndex);
DT DateTime();
BI BarIndex();
LastDateTime LastValue(ValueWhenLastBarIndex == BIDT ));
FirstDateTime LastValue(ValueWhenFirstBarIndex == BIDT ));
LastDateTimestr DateTimeToStrLastDateTime );
FirstDateTimestr DateTimeToStrFirstDateTime );
AB CreateObject("Broker.Application");
AW AB.ActiveWindow;
AW.ZoomToRangeFirstDateTimestrLastDateTimestr );
} 
procedure ZoomToCursorZoomWidth )
{
local ZoomWidth;
BI BarIndex();
SBI SelectedValue(BI);
LBI LastValue(BI);
CursorIndexNz(StaticVarGet("CursorIndex"));
FirstBarIndex Max(0,SBI ZoomWidth);
LastBarIndex MinLBISBI ZoomWidth );
ZoomToIndexFirstBarIndexLastBarIndex);
} 
procedure ZoomOutZoomWidth )
{
local ZoomWidth;
BI BarIndex();
LBI LastValue(BI);
LastVisiblebar Status("LastVisibleBar");
FirstVisibleBarStatus("FirstVisibleBar");
FirstBarIndex Max0FirstVisibleBar ZoomWidth);
LastBarIndex MinLBILastVisibleBar ZoomWidth );
ZoomToIndexFirstBarIndexLastBarIndex);
} 
procedure ZoomInZoomWidth )
{
local ZoomWidth;
_TRACE("##");
BI BarIndex();
LBI LastValue(BI);
LastVisiblebar Status("LastVisibleBar");
FirstVisibleBarStatus("FirstVisibleBar");
FirstBarIndex Max0FirstVisibleBar ZoomWidth);
LastBarIndex MinLBILastVisibleBar ZoomWidth );
ZoomToIndexFirstBarIndexLastBarIndex);
} 
procedure ZoomToNextEventZoomWidth )
{
local EventZoomwidth;
EventNum Cum(Event);
BI BarIndex();
LBI LastValue(BI);
DT=DateTime();
NextEventNum Min(Nz(StaticVarGet("EventNumber"))+1LastValue(EventNum));
NextEventIndex LastValue(ValueWhen(EventNum == NextEventNumBI));
StaticVarSet("EventNumber",Max(1NextEventNum));
FirstBarIndex Max0NextEventIndex ZoomWidth );
LastBarIndex MinBarCount-1NextEventIndex ZoomWidth);
ZoomToIndexFirstBarIndexLastBarIndex);
StaticVarSet("FocusIndex",NextEventIndex);
} 
procedure ZoomToPrev(  EventZoomWidth )
{
local EventZoomwidth;
EventNum Cum(Event);
BI BarIndex();
LBI LastValue(BI);
LastEventNum LastValue(EventNum);
NextEventNum Max(1,Nz(StaticVarGet("EventNumber"))-1);
NextEventIndex LastValue(ValueWhen(EventNum == NextEventNumBI,1));
StaticVarSet("EventNumber",NextEventNum);
FirstBarIndex Max0NextEventIndex ZoomWidth );
LastBarIndex MinBarCount-1NextEventIndex ZoomWidth);
ZoomToIndexFirstBarIndexLastBarIndex);
StaticVarSet("FocusIndex",NextEventIndex);
} 
procedure MarkFocus()
{
global EventNum;
DT=DateTime();
BI BarIndex();
LBI LastValue(BI);
FirstBarIndex Nz(StaticVarGet("FirstBarIndex"));
FocusIndex Min(LBI,Nz(StaticVarGet("FocusIndex"))+1);
FocusDateStr NumToStr(DT[FocusIndex],formatDateTime);
ENStr NumToStr(EventNum[FocusIndex],1.0,False);
PlotText("\n\nFocus\n"+FocusDateStr+"\n"+ENStr,FocusIndex,C[FocusIndex],colorYellow);
} 
procedure ZoomAllOut()
{
BI BarIndex();
LBI LastValue(BI);
FirstBarIndex 0;
LastBarIndex LBI;
ZoomToIndexFirstBarIndexLastBarIndex);
} 
procedure RightJustifyChartChartWidth )
{
DT DateTime();
BI BarIndex();
FirstDateTime LastValueNz(RefDT,-Max(2ChartWidth) ) ) );
FirstDateTimestr DateTimeToStrFirstDateTime );
LastDateTimestr DateTimeToStrLastValue(DT) );
AB CreateObject("Broker.Application");
AW AB.ActiveWindow;
AW.ZoomToRangeFirstDateTimestrLastDateTimestr );
} 
SetBarsRequired(1000000,1000000);
GraphXSpace 20;
_SECTION_BEGIN("ZOOM");
FocusTriggerParamTrigger("Focus on Cursor","FOCUS");// zoom out by ZoomIncrement
ZoomInTriggerParamTrigger("Zoom in","IN");// zoom in by ZoomIncrement
ZoomOutTriggerParamTrigger("Zoom out","OUT");// zoom in by ZoomIncrement
ZoomAllTrigger ParamTrigger("Zoom max. out","ALL");// zoom out to all bars
ZoomPrevTriggerParamTrigger("Go to Next Event","NEXT");// Jump to next event
ZoomNextTriggerParamTrigger("Go to Previous Event","PREVIOUS");// Jump to previous event
RJTriggerParamTrigger("Right Justify Chart","JUSTIFY");
ZoomIncrementParam("Zoom Increment",10,1,1000,2);
ZoomWidthParam("Window Width",20,2,1000,1);
_SECTION_END();
_SECTION_BEGIN("TEST SIGNALS");
ZoomEventParamList("Zoom Event","MONDAY|RSI-BUY|RSI-SELL|TARGET",1);// Test events
ProfitTargetParam("Profit Target",0.1,0,2,0.01);// Profit target
_SECTION_END(); 

PrevZoomWidthNz(StaticVarGet("ZoomWidth"));
ZoomChangePrevZoomWidth != ZoomWidthStaticVarSet("ZoomWidth",ZoomWidth); 

ZoomAction FocusTrigger OR ZoomOutTrigger OR ZoomAllTrigger OR ZoomNextTrigger OR ZoomPrevTrigger OR ZoomChange OR ZoomInTrigger OR RJTrigger; 

if( ZoomEvent == "MONDAY" Event DayOfWeek() == 1;
else if( ZoomEvent == "RSI-BUY" ) 
{ 
Event Cross(30,RSI());// Buy rule to investigate
PlotShapes(IIf(Event,shapeSmallUpTriangleshapeNone),5,0,C,0);
Sell 0; 
}
else if( ZoomEvent == "RSI-SELL" ) 
{ 
Event Cross(RSI(),70);// sell rule to investigate 
PlotShapes(IIf(Event,shapeHollowDownTriangleshapeNone),4,0,C,0);
Buy 0; 
}
else if( Zoomevent == "TARGET" ) 
{
Target Ref(C,1) - C;// Profit target to analyze
Event Target ProfitTarget;
PlotShapes(IIf(Event,shapeSmallUpTriangleshapeNone),5,0,C,0);
}
else Event 0Plot(C,"",1,128);
EventNum Cum(Event);
FVBI Status("FirstVisibleBarIndex");
LVBI Status("LastVisibleBarIndex");
BI BarIndex();
SBI SelectedValue(BI);
LBI LastValue(BI); 

FirstVisibleBar Status"FirstVisibleBar" );
Lastvisiblebar Status("LastVisibleBar");
for( FirstvisiblebarLastvisiblebar AND BarCountb++)
{
if( Event[b] ) PlotText("\n\n\n\nEvent\n"+NumToStr(EventNum[b],1.0,False),b,L[b],1);
} 

if( GetCursorMouseButtons() == ) 
{
StaticVarSet("CursorIndex",SBI);
EventNum Cum(Event);
StaticVarSet("EventNumber",SelectedValue(EventNum));
StaticVarSet("FocusIndex",SBI-1);
}
CursorIndexNz(StaticVarGet("CursorIndex")); 

if( ZoomAction )
{
CurrentRangeLastVisiblebar-FirstVisibleBar;
VisibleCenterint((LastVisibleBar FirstVisibleBar)/2);
PrevEventIndex LastValue(ValueWhen(Event,BI,1));
NextEventIndex LastValue(ValueWhen(Event,BI,-1));
FirstBarIndex Nz(StaticVarGet("FirstBarIndex"));
LastBarIndex Nz(StaticVarGet("LastBarIndex")); 

if( FocusTriggerZoomToCursorZoomWidth );
else if( ZoomInTrigger ZoomInZoomWidth );
else if( ZoomOutTrigger ZoomOutZoomWidth );
else if( ZoomNextTrigger ZoomToPrevEventZoomWidth );
else if( ZoomPrevTrigger ZoomToNextEventZoomWidth );
else if( ZoomAllTrigger ZoomAllOut();
else if( RJTrigger RightJustifyChartZoomWidth );
} 

MArkFocus(); 

Plot(Event,"",5,styleArea|styleOwnScale|styleNoLabel,0,10);
CursorIndex StaticVarGet("CursorIndex");
EventNumberStaticVarGet("EventNumber");
Title "\n"+
"Cursor Index: "+NumToStr(CursorIndex,1.0,False)+"\n"+
"EventNumber: "+NumToStr(EventNumber,1.0,False)+"\n";

Edited by Al Venosa

Comments are closed.