Afl code for different zoom type

@Tomasz
@fxshrat

Can any one write afl code for different zoom type

For Example
If I Select 1min time-frame zoom to recent 1 days or 375 bars

If I Select 5min time-frame zoom to recent 5 days or 375 bars

If I select 1hour time-frame zoom to recent 45 days or 270 bars

If I select daily time-frame zoom to recent 1year or 240 bars

If I select Weekly time-frame zoom to recent 3 year or 156 bars

If I select monthly time-frame zoom to all.

Can it be code in afl ? I did not get any built in formula for this.

Please advise ..

I have read this but unable to do as per above.
http://www.amibroker.org/userkb/2007/06/28/zoom-to-range-applications/

hi
I am pretty sure you can do it with IIF() function or switch()

hello
i am not sure if this is my code or someone else.
but you can start from here.

_SECTION_BEGIN("TIME FRAME AUTO ZOOM");
/* Below is a code which will Auto-Zoom to a pre-configured range of bars- depending on which Chart Interval I change to. 
  1 min => 390 bars, 5 => min 78 bars, 15 min => 26 bars or any other chart interval the default is 390 bars.  */

AutoZoom = ParamToggle( "TF Auto Zoom", "OFF, ON", 1 );
ChartInterval = Interval();
Plot( Close, "", colorBlack, styleCandle );

function ZoomTFChart( BeginZoom, EndZoom )
{
AB = CreateObject( "Broker.Application" );
AW = AB.ActiveWindow;
AW.ZoomToRange( BeginZoom, EndZoom );
}


if( AutoZoom )
{
    BlankBars = 10; // Enter the number of Blank Bars you have defined under Preferences- Charting.
    SetChartOptions( 0, 0, chartGridMiddle, 0, 0, BlankBars );

    ShortTFBars = 390;
    MediumTFBars = 78;
    LongTFBars = 26;
    NumberOfBars = 0;
    _Interval = Nz( StaticVarGet( "_Interval" ) );

    if( ChartInterval != _Interval )
    {
        SetBarsRequired( sbrAll, sbrAll );

        switch( ChartInterval )
        {
            case in1Minute:
                NumberOfBars = ShortTFBars;
                break;

            case in5Minute:
                NumberOfBars = MediumTFBars;
                break;

            case in15Minute:
                NumberOfBars = LongTFBars;
                break;

            default :
                NumberOfBars = ShortTFBars;
        }

        BI = BarIndex();
        DT = DateTime();

        BeginBarIndex = ValueWhen( LastValue( BarIndex() ) - BarIndex() > NumberOfBars + BlankBars, BI );
        BeginDateTime = DT[LastValue( BeginBarIndex - BI[0] + 2 )];
        BeginZoom = DateTimeToStr( BeginDateTime );
        ZoomTFChart( BeginZoom, Date() );

        WSHShell = CreateObject( "WScript.Shell" );
        WSHShell.AppActivate( "AmiBroker" );
        WSHShell.Sendkeys( "{PGDN}" );
        StaticVarSet( "_Interval", ChartInterval );
    }
}
_SECTION_END();

5 Likes

You can have it without coding in many ways:

  1. Simply save individual setups as layouts. You can switch layouts by double clicking on "Layouts" window
    ..or..
  2. Open multiple chart windows (File->New->Chart) and switch them by MDI tab.
2 Likes

Thanks for the reply. It is good idea for individual time frame. But I want to draw support and resistance line of all the timeframe in one chart with above mention interval. Layout is not fit for this.
Any other idea sir ?

Thanks . I will try and mod this code as per my requirement also I will post here if succeed.

Chart windows can SHARE support / resistance lines (studies in general), see Knowledge Base
http://www.amibroker.com/kb/2014/10/06/relationship-between-chart-panes/

Thanks for the code. I modified it as per my requirement.
Final is as below.
Used it as additional code in your afl.

_SECTION_BEGIN("Yogya TIMEFRAME ZOOM");
AutoZoom = ParamToggle( "TF Auto Zoom", "OFF, ON", 1 );
ChartInterval = Interval();

function ZoomTFChart( BeginZoom, EndZoom )
{
AB = CreateObject( "Broker.Application" );
AW = AB.ActiveWindow;
AW.ZoomToRange( BeginZoom, EndZoom );
}

if( AutoZoom )
{
    BlankBars = 10; // Enter the number of Blank Bars you have defined under Preferences- Charting.
    NumberOfBars = 0;
   _Interval = Nz( StaticVarGet( "_Interval" ) );

   if( ChartInterval != _Interval )
  {
        SetBarsRequired( sbrAll, sbrAll );

        switch( ChartInterval )
        {
            case in1Minute:
                NumberOfBars = 375;
                break;
            case in5Minute:
                NumberOfBars = 375;
                break;

            case inHourly:
                NumberOfBars = 270;
                break;
              case inDaily:
                NumberOfBars = 270;
                break;
                
               case inWeekly:
                NumberOfBars = 156;
                break;
                
                case inMonthly:
                NumberOfBars = 120;
                break;

            default :
                NumberOfBars = 100;
        }

        BI = BarIndex();
        DT = DateTime();

        BeginBarIndex = ValueWhen( LastValue( BarIndex() ) - BarIndex() > NumberOfBars + BlankBars, BI );
        BeginDateTime = DT[LastValue( BeginBarIndex - BI[0] + 2 )];
        BeginZoom = DateTimeToStr( BeginDateTime );
        ZoomTFChart( BeginZoom, Date() );

        WSHShell = CreateObject( "WScript.Shell" );
        WSHShell.AppActivate( "AmiBroker" );
        WSHShell.Sendkeys( "{PGDN}" );
        StaticVarSet( "_Interval", ChartInterval );
    }
}
_SECTION_END();
3 Likes

Hi @Yogyatrader I am using your code in my existing Price Chart AFL.

I have three chart windows in layout one for daily,weekly and monthly.

When I got to each chart and Turn on the TF Auto Zoom from Pram then I get Error as in screenshot. Any idea why is it causing and how to fix it?

_SECTION_BEGIN("Yogya TIMEFRAME ZOOM");
AutoZoom = ParamToggle( "TF Auto Zoom", "OFF, ON", 1 );
ChartInterval = Interval();

function ZoomTFChart( BeginZoom, EndZoom )
{
AB = CreateObject( "Broker.Application" );
AW = AB.ActiveWindow;
AW.ZoomToRange( BeginZoom, EndZoom );
}

if( AutoZoom )
{
    BlankBars = 10; // Enter the number of Blank Bars you have defined under Preferences- Charting.
    NumberOfBars = 0;
   _Interval = Nz( StaticVarGet( "_Interval" ) );

   if( ChartInterval != _Interval )
  {
        SetBarsRequired( sbrAll, sbrAll );

        switch( ChartInterval )
        {
            case in1Minute:
                NumberOfBars = 375;
                break;
            case in5Minute:
                NumberOfBars = 375;
                break;

            case inHourly:
                NumberOfBars = 270;
                break;
              case inDaily:
                NumberOfBars = 270;
                break;
                
               case inWeekly:
                NumberOfBars = 156;
                break;
                
                case inMonthly:
                NumberOfBars = 120;
                break;

            default :
                NumberOfBars = 100;
        }

        BI = BarIndex();
        DT = DateTime();

        BeginBarIndex = ValueWhen( LastValue( BarIndex() ) - BarIndex() > NumberOfBars + BlankBars, BI );
        BeginDateTime = DT[LastValue( BeginBarIndex - BI[0] + 2 )];
        BeginZoom = DateTimeToStr( BeginDateTime );
        ZoomTFChart( BeginZoom, Date() );

        WSHShell = CreateObject( "WScript.Shell" );
        WSHShell.AppActivate( "AmiBroker" );
        WSHShell.Sendkeys( "{PGDN}" );
        StaticVarSet( "_Interval", ChartInterval );
    }
}
_SECTION_END();