How do I get the bar number at the Fixed Chart Position in MT4?

 

I'm running MT4 and using MQL4. I need to access the price values at the bar thats at the Fixed Chart Position. CHART_FIXED_POSITION seems to be a place to start but thats a percentage of the screen. I need the bar offset (FixedChartBar) so that I can enter it in one of the pricing functions like

iOpen(NULL, PERIOD_D1, FixedChartBar)

 
robe070:

I'm running MT4 and using MQL4. I need to access the price values at the bar thats at the Fixed Chart Position. CHART_FIXED_POSITION seems to be a place to start but thats a percentage of the screen. I need the bar offset (FixedChartBar) so that I can enter it in one of the pricing functions like

iOpen(NULL, PERIOD_D1, FixedChartBar)

iOpen requires bar number as third parameter.

You have to know which bar you need, newest bar has index 0(zero).

If you have time of the bar, you can get it's index number using iBarShift()

iBarShift - Timeseries and Indicators Access - MQL4 Reference
iBarShift - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
Index of the bar which covers the specified time. If there is no bar for the specified time (history "gap"), the function will return -1 or the nearest bar index (depending on
 
robe070: I need to access the price values at the bar thats at the Fixed Chart Position. CHART_FIXED_POSITION seems to be a place to start but thats a percentage of the screen. I need the bar offset (FixedChartBar) 
  1. There is no such thing as a fixed position; new bars form, old bars shifts left.
  2. Shifting the chart left only effects the on screen visual. Code as no eyes; You can read any bar, on screen or not. That is not the place to start.
  3. You need the offset; yes you do. There are no mind readers here and our crystal balls are cracked. Only you know what offset you need.  Until you can state your need in concrete terms, it can not be coded. Do you really expect an answer to that vague "I need?"

  4. iOpen(NULL, PERIOD_D1, FixedChartBar)
    Don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
  5. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

 
William Roeder:
  1. There is no such thing as a fixed position; new bars form, old bars shifts left.
    There is a feature of MT4 called the Fixed Chart Position. Its a little grey triangle that by default is in the lower left corner of the chart, when auto scroll is off. You can drag that out to, say, the centre of the screen. I want to be able to derive the bar values there, which seems like I need to know the date/time at that point then I can use iBarShift(). I'm writing a tool to help with manual back testing. So the user can indicate which bar to use by dragging the Fixed Chart Position to the appropriate bar.
  2. Shifting the chart left only effects the on screen visual. Code as no eyes; You can read any bar, on screen or not. That is not the place to start.
    Yes, I understand, see (1). Its a visual tool that I'm writing. I must translate from screen to code. The screen IS the place that I need to start.
  3. You need the offset; yes you do. There are no mind readers here and our crystal balls are cracked. Only you know what offset you need.  Until you can state your need in concrete terms, it can not be coded. Do you really expect an answer to that vague "I need?"
    See (1). I informed you in the OP "I need to access the price values at the bar thats at the Fixed Chart Position."

  4. Don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
      Thanks
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
      Thanks
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
      See Fixed Chart Position
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
      I think I need to use iHigh to access the different time frames on the one chart?
  5. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4
    Are you saying I need to use the  download_history function if I need to reference other symbol or other TF? Then I need to do that. Where are the error codes defined? -  ERR_HISTORY_WILL_UPDATED and ERR_NO_HISTORY_DATA

Thank you so much for your help
 
robe070: There is a feature of MT4 called the Fixed Chart Position. Its a little grey triangle that by default is in the lower left corner of the chart, when auto scroll is off. You can drag that out to, say, the centre of the screen.
  1. FCP is usually used to scroll the chart after changing TF to keep the corresponding bar, in position.
  2. You want to complicate things. Stop autoscroll and drag the triangle. If you then press a left/right arrow, or page up/down, chart scrolls multiple bars; you are no longer pointing to the correct bar and there is no way to know that; the chart is not fixed.
  3. If you insist, read the percentage. get the chart's size in pixels. Compute the "centered" x coordinate. Convert XY to price/time. Use time, get shift.
  4. Instead, just drag a vertical line over a bar and read its time. It stays with the current bar even with shifting and TF changes. Getting the bar time is easy. It is obvious which bar has been indicated, even during dragging.
 
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
 
William Roeder:
  1. read the percentage. get the chart's size in pixels. Compute the "centered" x coordinate. Convert XY to price/time. Use time, get shift.

Thanks William. Very helpful.

I've worked out how to get the percentage and been told how to convert time to get the shift.

How do I get:

1. the chart's size in pixels.

2. Compute the "centered" x coordinate. 

3. Convert x coordinate to time

 
  1. Perhaps you should read the manual.
              ChartGetInteger - Chart Operations - MQL4 Reference
              Chart Properties - Chart Constants - Constants, Enumerations and Structures - MQL4 Reference

  2. If there are n pixels, what is the middle one? When in doubt, THINK.

  3. Perhaps you should read the manual.
    ChartXYToTimePrice - Chart Operations - MQL4 Reference
 
William Roeder:
  1. Perhaps you should read the manual.
              ChartGetInteger - Chart Operations - MQL4 Reference
              Chart Properties - Chart Constants - Constants, Enumerations and Structures - MQL4 Reference

  2. If there are n pixels, what is the middle one? When in doubt, THINK.

  3. Perhaps you should read the manual.
    ChartXYToTimePrice - Chart Operations - MQL4 Reference

Thanks WIlliam. I should be able to put it together now. You've really sped up my learning.

I do find the manuals hard to read given their translation from Russian to English, I think. I've read a lot, just not the right bits. I read time series a number of times over a number of days before it sunk in what it was on about.

Its great having somebody who is so supportive and respectful of newbies learning the ways of MQL.

<Deleted>

Reason: