Which code replicates the double-click over the Y axis in fix-scale mode?

 

Hi,

I'd like to reproduce as code the following steps:

  1. Scale to max zoom out
  2. Increase the value of the min and maximum Y axis (so, if it's plotting $10 - $20, the scales will be like $5 - $25).
  3. Set scales to fix mode
  4. Scale to "normal", original zoom
  5. Double-click over the Y axis
The result will be an axis bar like this:


I already know how to do most of those steps; only the 5th item, the double-click, I don't know how to do it by code. I thought it would be using CHART_PRICE_MAX and CHART_PRICE_MIN, but those seems to be read-only values. So, how can I do this double-click by code? I couldn't find any suitable canditate in all the variables regarding ChartSetDouble (or ChartSetInteger).


 

This should get you started:

//+------------------------------------------------------------------+
//| Gets chart scale (from 0 to 5)                                   |
//+------------------------------------------------------------------+
int ChartScaleGet(const long chart_ID=0)
  {
//--- prepare the variable to get the property value
   long result=-1;
//--- reset the error value
   ResetLastError();
//--- receive the property value
   if(!ChartGetInteger(chart_ID,CHART_SCALE,0,result))
     {
      //--- display the error message in Experts journal
      Print(__FUNCTION__+", Error Code = ",GetLastError());
     }
//--- return the value of the chart property
   return((int)result);
  }
//+------------------------------------------------------------------+
//| Sets chart scale (from 0 to 5)                                   |
//+------------------------------------------------------------------+
bool ChartScaleSet(const long value,const long chart_ID=0)
  {
//--- reset the error value
   ResetLastError();
//--- set property value
   if(!ChartSetInteger(chart_ID,CHART_SCALE,0,value))
     {
      //--- display the error message in Experts journal
      Print(__FUNCTION__+", Error Code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
Documentation on MQL5: Examples of Working with the Chart / Constants, Enumerations and Structures
Documentation on MQL5: Examples of Working with the Chart / Constants, Enumerations and Structures
  • www.mql5.com
This section contains examples of working with chart properties. One or two complete functions are displayed for each property. These functions...
 

If you want to capture double clicks, you'll need to time 2 clicks of CHARTEVENT_CLICK or CHARTEVENT_OBJECT_CLICK:

Documentation on MQL5: OnChartEvent / Event Handling
Documentation on MQL5: OnChartEvent / Event Handling
  • www.mql5.com
The function is called in indicators and EAs when the ChartEvent event occurs. The function is meant for handling chart changes made by a user or...
 
MT5 auto reset Fixed Price Min/Max  
 
Ryan L Johnson #:
This should get you started:

Thanks for the help, but handling chart scale I alredy know and it is implemented. My problem is just replicating the chart behaviour of double-clicking the Y axis at the right.

Ryan L Johnson #:
If you want to capture double clicks

No, not capture; I want to replicate with code the chart's behaviour when the Y axis is double-clicked.

Luu Tuan Trung #:
MT5 auto reset Fixed Price Min/Max

Thanks, but that doesn't work: if I set any value to FIXED max/min, my goal is lost. I need the maximum and minimum price values to be set without any change to the FIXED values.

 
Martin Bittencourt #:
I want to replicate with code the chart's behaviour when the Y axis is double-clicked.
Martin Bittencourt #:
[I]f I set any value to FIXED max/min, my goal is lost. I need the maximum and minimum price values to be set without any change to the FIXED values.
Given the fact that when fixed max/min is disabled in chart properties, double clicking on the price scale does nothing in the terminal, we might be investigating the impossible.
 
Ryan L Johnson #:
Given the fact that when fixed max/min is disabled in chart properties, double clicking on the price scale does nothing in the terminal, we might be investigating the impossible.

Maybe a video will help explaining my goal: .

 
Martin Bittencourt #:

Maybe a video will help explaining my goal: .

If a picture is worth a thousand words, a video is worth a million. I now see that you're making an MT5 chart operate like a TradingView chart─without using a chart template.

I had another look at the MQL5 chart events and I see what you mean. There is no double click/snap chart event. For whatever it's worth, I have noticed in the past that ChartRedraw() seemed to "inadvertently" snap the chart to the height of visible bars when called. That might be a workaround, albeit a rather blunt one.

 
Martin Bittencourt:

Hi,

I'd like to reproduce as code the following steps:

  1. Scale to max zoom out
  2. Increase the value of the min and maximum Y axis (so, if it's plotting $10 - $20, the scales will be like $5 - $25).
  3. Set scales to fix mode
  4. Scale to "normal", original zoom
  5. Double-click over the Y axis
The result will be an axis bar like this:


I already know how to do most of those steps; only the 5th item, the double-click, I don't know how to do it by code. I thought it would be using CHART_PRICE_MAX and CHART_PRICE_MIN, but those seems to be read-only values. So, how can I do this double-click by code? I couldn't find any suitable canditate in all the variables regarding ChartSetDouble (or ChartSetInteger).


The "double-click" behavior on the Y-axis is essentially a shortcut to toggle off Scale Fixed and return to Auto Scaling. When you do this, MT5 recalculates the visible range automatically.

To replicate this via code without losing your fixed bounds permanently, you should toggle the CHART_SCALEFIX property. Try this sequence:

// 1. Disable Fixed Scale to trigger MT5's auto-calculation
ChartSetInteger(0, CHART_SCALEFIX, false);

// 2. Force a redraw to let the terminal recalculate min/max
ChartRedraw(0);

// 3. (Optional) If you need to return to fixed mode with the new "snapped" values:
double autoMax = ChartGetDouble(0, CHART_PRICE_MAX);
double autoMin = ChartGetDouble(0, CHART_PRICE_MIN);

ChartSetInteger(0, CHART_SCALEFIX, true);
ChartSetDouble(0, CHART_FIXED_MAX, autoMax);
ChartSetDouble(0, CHART_FIXED_MIN, autoMin);

This approach forces the terminal to perform the "snap" you see in the video by momentarily letting the built-in auto-scale engine take over.

 
Mostafa Ghanbari #:

The "double-click" behavior on the Y-axis is essentially a shortcut to toggle off Scale Fixed and return to Auto Scaling. When you do this, MT5 recalculates the visible range automatically.

To replicate this via code without losing your fixed bounds permanently, you should toggle the CHART_SCALEFIX property. Try this sequence:

This approach forces the terminal to perform the "snap" you see in the video by momentarily letting the built-in auto-scale engine take over.

I didn't check the execution of your code, though I can say mixing ChartSetxxx and ChartGetxxx, with a ChartRedraw in the middle isn't something reliable. ChartSetxxx and ChartRedraw are asynchronous functions, and even if your code works sometimes, it will not be reliable.
 
Mostafa Ghanbari #:
This approach forces the terminal to perform the "snap" you see in the video by momentarily letting the built-in auto-scale engine take over.
Thanks for the code, but setting the fixed max+min with the values gotten after ChartRedraw is the opposite I want to do: doing so will give a fixed max/min with the values that I get after the double-click, when I want these values to be the ones from before the double-click.