Anchoring Chart on the left side

hello all,

I would like to , anchor chart view on th left side, so that when new price data is updated the left side stays at the same date/time position. currently if i want to view the whole chart from point a to point b
while market is live , i would need to scroll back to when market is open. hence my wish to anchor the chart on the left side.

if anyone don't mind pointing the concepts or guide for me to get started coding by myself.

apologize if my question comes off as parasitic , no effort chump.

Thank You

You can start here

XShift = 0

XShift - allows to visually shift the chart past the last bar.

-ve values move the plot Left, +1 to the right.

1 Like

much appreciated , will experiment.

@fourier this KB article may be useful:

How to browse charts in selected date range

Use once F12 to mark the start bar of the day, then, each time that you want to see all the bars accumulated during the live trading period, select the last visible bar (most recently added to the chart) and use SHIFT-F12 to mark the new end of the range.

As the article suggests, create a shortcut to Zoom to Range to make the Zoom operation more accessible.


@fourier it can be done. I'm not at home today, but when I get back - tomorrow I will reply with the example code...

1 Like

i apologize for not explaining my question thoroughly let me clarify.
current setup:

  1. 1-minute time frame
  2. start of day the right most edge of
    chart pane with price plot updates every
  3. at every update , the chart moves to the left as
    new price is plotted.

what i aim to do programatically, i.e. with out constantly pressing a chart button zoom out
re adjust chart postion, is to have the left side anchored , and whilst new data is being plotted
to keep the chart stationary and not moving.


i hopy my reply convey my idea clearly

Thank you again for your time and suggestions.

Yes, I understand. You want to see the whole session all the time (no matter if there are 100, 200 or 500 bars - so you would like to automatically "squeeze" the chart (not scroll) as new bars appear during the session... Yes it can be done. As I wrote above - I will post example code tomorrow.

1 Like

yes sir @Milosz , that is correct, if you could be so kind as to pointing out the concepts / guide in the help contents

i would like to code it myself.

Thank you again.

@Milosz apoligies, i posted my reply with out refeshing the topic page, hence did not see your reply prior.

Thanks again sir.

You would have to use OLE.

dt = DateTime();
bi = BarIndex();
dn = DateNum();

SetBarsRequired( 86400 / Max(1, Interval()) );

procedure ZoomToBarIndex( fbi, lbi ) {
    /// posted to discussion thread here   
    /// @link
    gid = StrFormat( "%g_%s", GetChartID(), Name() );
    static_fbi = Nz( StaticVarGet( "ZOOM_FIRST_BI_" + gid ) );
    static_lbi = Nz( StaticVarGet( "ZOOM_LAST_BI_" + gid ) );

    if ( static_fbi != fbi || static_lbi != lbi ) {
        StaticVarSet( "ZOOM_FIRST_BI_" + gid, fbi );
        StaticVarSet( "ZOOM_LAST_BI_" + gid, lbi );
        tmfrm = Max(1, Interval());

        fdt = LastValue( ValueWhen( bi == fbi, DateTimeAdd( dt, 0, tmfrm ) ) );
        fdt_str = DateTimeToStr( fdt, 3 );
        ldt = LastValue( ValueWhen( bi == lbi, DateTimeAdd( dt, 1, tmfrm ) ) );
        ldt_str = DateTimeToStr( ldt, 3 );                

        AB = CreateObject( "Broker.Application" );
        AW = AB.ActiveWindow;
        AW.ZoomToRange( fdt_str, ldt_str );      

newday = dn != Ref( dn, -1 );
fbi = LastValue( ValueWhen( newday, bi ) ); 
lbi = LastValue( bi );

if ( ParamToggle( "Chart Anchoring", "OFF|ON", 1 ) )
	ZoomToBarIndex( fbi, lbi );

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Price", colorDefault, styleBar );

@fxshrat - thanks for the interesting code. Yes, exactly - I was thinking about a similar solution making use of ZoomToRange() each time a new bar appears or on demand. I will post my version tomorrow.

1 Like

Would it be possible to use SetChartOptions to adjust the number of blankbars as the session progresses?

The number of remaining bars can be calculated using the session close time, last bar time and bar interval. The session would start with a full session’s worth of blank bars and the number of blank bars would decrease as each new bar arrives.

@fourier if you like the zoom approach via OLE, many related functions were posted years ago on the user KB:

No need to reinvent the wheel!


I just tried my approach of adjusting blank bars using SetChartOptions.

It appears that we can not reduce the number of blank bars. The first bar of the day sets a day’s worth of blank bars. Once that is set, subsequent calls to request fewer bars do not work. Changing to another symbol and back sets the expected number of blank bars. I can always increase the number, but not reduce it.


@fxshrat i am just amazed at the code right now, and have to say bit speechless, I am also studying the code to understand what you did . but you have my utmost gratitude.

Initially I was going to try another (less strict) approach comparing to @fxshrat 's solution, but after giving it some additional thoughts, I came to the conclusion, that his already presented version is better. Because my code became so similar to the above one, posting it makes no sense.

@bigalgator when using Fxshrat's code, you can change the number of blank bars in the right margin replacing 1 with 5, 10, 20 or other number in the below line.

ldt = LastValue( ValueWhen( bi == lbi, DateTimeAdd( dt, 1, tmfrm ) ) );

You might also need to increase the number of blank bars (either by pressing End key or changing the default number of Blank bars in the right margin via Parameters --> Charting

Frankly, I don't like this idea, because I wouldn't like to see mostly empty chart during the session :wink:

1 Like

@beppe While in general I agree with this statement, in my opinion it is not the case here because:

  1. In those examples you won't find a code meeting the criteria of OP request.
  2. Tomasz always warns users against using OLE from AFL, because when used improperly (especially by inexperienced people), they might impose serious problems. Because I remember some of Fourier's past codes ( an example ) I can savely assume, that he is not an experienced AFL coder. For this reason I thought it would be better if some more experienced user shows him how it should be done properly and safely.
  3. @fxshrat 's code can be treated as addition to those mentioned examples - also because he wrapped the whole code in a function (procedure to be exact) in a similar way.
  4. I also wrote that I was going to post an example code, because in this way, I could not only help Fourier, but also practice my coding skills and possibly learn something new. For example I like the way Fxshrat used Interval() in conjunction with SetBarsrequired() and DateTimeAdd() to show n blank bars. Personally I find replying to such posts much more interesting than explaining someone for the n'th time what every user can easily find in AB documentation or what has been already written on this forum.

For the reasons above (especially point 1 and 2) and taking into accout that the proposed code solves Fourier's request (he is very satisfied with it) and preparing the code took some time and effort, in my opinion (you know @beppe that I like you very much :slight_smile: ) the Solution this time should go to Fxshrat.

It is not a matter of liking OLE approach or not. I might not like OLE (because of it's disadvantages mentioned many times by Tomasz in his posts and AB documentation) but there is no other option if we want to achieve this effect.

1 Like

@Milosz - it is not @beppe who marks things as solutions. I marked it.

1 Like

Yes, of course I'm aware of that.