Custom indicator: Auto Scale window, with 0-center

 

Hi, got this indicator code working somewhat, it is run even when mouse scrolling the chart.

The indicator data has a wide range, something like +/- 0.1...+/-5.0, depending of chart scale, TF and instrument.

The goal is to improve readability by:

A) "0" in the center of the indicator window at all times

B) Auto scale the indicator window (the visible part of it) so that

the largest indicator line peak, either pos or neg, fills the window without clipping.

//---- indicator buffers
double Linebuffer[];//Visible indicator line
double Scale[];//Working copy of line buffer

//-------------------------------------------------------------------
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
 
        ArrayCopy(Scale, Linebuffer, 0, WHOLE_ARRAY);//Copy Linebuffer to a temporary array
        //ArraySetAsSeries(Scale, true);// ?
   double max = ArrayMaximum(Linebuffer,WHOLE_ARRAY, 0);//Find data range or amplitude
        IndicatorSetDouble(INDICATOR_MAXIMUM,max); 
   IndicatorSetDouble(INDICATOR_MINIMUM,-max);

  }
//-------------------------------------------------------------------

See picture for current problem, the 0 is all over the scale



Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
  • www.mql5.com
Some technical indicators have several buffers drawn in the chart. Numbering of indicator buffers starts with 0. When copying indicator values using the CopyBuffer() function into an array of the double type, for some indicators one may indicate the identifier of a copied buffer instead of its number.
 

You don't need to use the "OnChartEvent". You can just draw the invisible "reverse line" for it.

#property indicator_type2   DRAW_LINE
#property indicator_color2  clrNONE

double         Linebuffer[];  //Indicator buffer
double         ReverseLine[];

SetIndexBuffer(0, Linebuffer);
SetIndexBuffer(1, ReverseLine);
:
:

for (i = limit; i >= 0; i--)
{
  Linebuffer[i]  = ...................;
  ReverseLine[i] = -Linebuffer[i];
}

By the way, is this an MQL4 code? It looks different from MQL5.

 
Nagisa Unada:

You don't need to use the "OnChartEvent". You can just draw the invisible "reverse line" for it.

By the way, is this an MQL4 code? It looks different from MQL5.

Nagisa, So simple, so obvious, and it works perfectly. Just hide the line with color None. Yes mql4.


 
Pilot65:

 Yes mql4.

In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

Reason: