Let MT5 remember chart position

 

When OnDeInit() and OnInit() is called or when timeframe is changed, MT5 forgets about the last datetime / XY Location of chart where user was browsing history of bars,

How it is possible that i can save these data and call it later? Any functions for that?

I want to save this before OnDeinit() is called : 

1. Number of bars available in window

2. Chart scale status

to make sure i can load this setting later

 
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Properties
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Properties
  • www.mql5.com
Chart Properties - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Positioning Constants
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Positioning Constants
  • www.mql5.com
Positioning Constants - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Arpit T: I want to save this before OnDeinit() is called :

Just save them when OnDeinit is called.

 
William Roeder #:

Just save them when OnDeinit is called.

I want to know the position of chart in respect to datetime, for example i have been browsing the chart from date : 31dec2022 2:37pm , and i want to save this info using ChartGetInteger  

and today is 2feb2023 so mt5 open charts for today by default

so next time i open chart, i want to load this info (  31dec2022 2:37pm  ) using ChartSetInteger, so Chart starts from  31dec2022 2:37pm so please inform which function to use for this case?

I can get first visible bar info from  

ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);

but how to set?

 
Arpit T #: I want to know the position of chart in respect to datetime, for example i have been browsing the chart from date : 31dec2022 2:37pm , and i want to save this info using ChartGetInteger and today is 2feb2023 so mt5 open charts for today by default so next time i open chart, i want to load this info (  31dec2022 2:37pm  ) using ChartSetInteger, so Chart starts from  31dec2022 2:37pm so please inform which function to use for this case? I can get first visible bar info from but how to set?
Use ChartNavigate to re-position the chart's display based on the state you saved.
Documentation on MQL5: Chart Operations / ChartNavigate
Documentation on MQL5: Chart Operations / ChartNavigate
  • www.mql5.com
ChartNavigate - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
Use ChartNavigate to re-position the chart's display based on the state you saved.
Thanks, CHART_BEGIN can be set from the value i saved using 
ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR,0);

but what about CHART_END how do i get its value to save on current chart?

 
Arpit T #: Thanks, CHART_BEGIN can be set from the value i saved using but what about CHART_END how do i get its value to save on current chart?

If you know which is the first bar visible, and how wide it is then, then you can calculate the last bar.

CHART_FIRST_VISIBLE_BAR

Number of the first visible bar in the chart. Indexing of bars is the same as for timeseries.

int r/o

CHART_WIDTH_IN_BARS

Chart width in bars

int r/o

 
Fernando Carreiro #:

If you know which is the first bar visible, and how wide it is then, then you can calculate the last bar.

CHART_FIRST_VISIBLE_BAR

Number of the first visible bar in the chart. Indexing of bars is the same as for timeseries.

int r/o

CHART_WIDTH_IN_BARS

Chart width in bars

int r/o

CHART_WIDTH_IN_BARS returning wrong info, as you can see in chart i measure there are 35 bars but it shows 45

May I know whats the logic for this? 

I am also getting when using  ChartNavigate on OnInit

ERR_CHART_NAVIGATE_FAILED

4111

Error navigating through chart

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Examples of Working with the Chart
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Examples of Working with the Chart
  • www.mql5.com
Examples of Working with the Chart - Chart Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Untitled.png  45 kb
 
Arpit T #: CHART_WIDTH_IN_BARS returning wrong info, as you can see in chart i measure there are 35 bars but it shows 45. May I know whats the logic for this?

No, it is NOT incorrect! You are applying a shift to your chart, which is not measured in bars, but in percentage of the chart.

CHART_SHIFT_SIZE

The size of the zero bar indent from the right border in percents

double  (from 10 to 50 percents)

As a programmer you need to start trying to "debug" your own thoughts and logic.

When in doubt, try thinking of possible reasons why and then research the documentation to find the possible causes and solutions.

Programming is about "logic thought", and you can only develop that skill by trying to resolve things yourself.

If every time you hit a "bump" you come running to the forum for an answer, you will never develop that skill in you.

It is like developing your muscles — if every time you need to lift something, you ask someone else to do it, your muscles don't grow.

 
Fernando Carreiro #:

No, it is NOT incorrect! You are applying a shift to your chart, which is not measured in bars, but in percentage of the chart.

CHART_SHIFT_SIZE

The size of the zero bar indent from the right border in percents

double  (from 10 to 50 percents)

As a programmer you need to start trying to "debug" your own thoughts and logic.

When in doubt, try thinking of possible reasons why and then research the documentation to find the possible causes and solutions.

Programming is about "logic thought", and you can only develop that skill by trying to resolve things yourself.

If every time you hit a "bump" you come running to the forum for an answer, you will never develop that skill in you.

It is like developing your muscles — if every time you need to lift something, you ask someone else to do it, your muscles don't grow.

ok thanks for suggestion