1
votes

I'm creating an MT4 EA in mql4, and would like to automatically adjust the price scale (y-axis) shown, based on some indicator results.

enter image description here

I.e. I have (y-max, y-min) values from a pivot indicator, and would like to re-scale my chart to center on the pivot line, regardless of where the current price is. For example, in the figure shown, I'd like to see the scale between the orange and yellow lines. Perhaps adding a few pixels above and below for visibility.

Unfortunately the MT4 forums are very lacking in the info on this, and I cannot even find a starting point. Possibly related info here:

How can I have my EA to resize/re-scale the price range for the current chart window?
(Possibly as a percentage of the total available size.)

1

1 Answers

1
votes

After collecting some bits and pieces from various forums and sources, I managed to patch together a beautiful solution.

What does it do?

  1. Adds a bool setting to enable automatic scaling, otherwise only do it once upon OnInit().
  2. Uses the OnChartEvent() event handler to detect CHARTEVENT_CHART_CHANGE, which is a Windows originated event of re-sizing the chart window, and scale the contents accordingly.
...
extern bool  autoChartScaling = true;         // Enable Automatic chart scaling

...
void AdjustChartPrice() {
    ...
    ChartSetInteger(cid,CHART_SCALEFIX, 0, true);    // Set the MODE for using a fixed chart scale ([x] Fixed MT4 option)
    ChartSetDouble(cid,CHART_FIXED_MAX, ymax);       // Maximum chart price (height) in [points]
    ChartSetDouble(cid,CHART_FIXED_MIN, ymin);       // Minimum chart price (height) in [points]
}

...
int OnInit() {
   ...
   OnChartEvent();
}

void OnChartEvent(const int id, const long& lparam, const double& dparam,  const string& sparam) {
    // Adjust the chart price Max/Min if chart window changed
    if (autoChartScaling && id == CHARTEVENT_CHART_CHANGE) AdjustChartPrice();  
}