variable plots

 

Hi

Is it possible in a custom indicator to change the number of plots on chart based on user inputs?

 
Of course. Don't process some buffers.
 
whroeder1:
Of course. Don't process some buffers.

Thank you,

What if there are so many plots, e.g. 50?

 
Seyyed Mohammad: What if there are so many plots, e.g. 50?

Then the indicator is likely useless.

 
whroeder1:

Then the indicator is likely useless.

So the only solution is to define max number of buffers and use only the numbers required? 
 
Seyyed Mohammad:
So the only solution is to define max number of buffers and use only the numbers required? 

Yup. Although, once you set the max number in the properties the rest you can setup dynamically. Take this example:

//+------------------------------------------------------------------+
//|                                                      MaWaves.mq4 |
//|                                                      nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 100
//--- input parameters
input int      inpPlots=50; //Number of plots
input int      inpMultiplier=2;//Multiplier

#include <indicators/indicators.mqh>

class Ma : public CiMA {
public:
   double      buffer[];
   bool        refresh(const int &rates_total, const int &prev_calculated);
   bool        init(int buffer_index, int ma_period, color col);
};

class MaArray : public CIndicators {
public: 
   bool        refresh(const int &rates_total, const int &prev_calculated);
};

MaArray g_mas;
//+------------------------------------------------------------------+
int OnInit()
{
   color cols[6] = {
      clrRed, clrOrange, clrYellow, 
      clrLimeGreen, clrViolet, clrDodgerBlue
   };
   for (int i=0; i<inpPlots; i++) {
      Ma *ma = new Ma();
      g_mas.Add(ma);
      if (!ma.init(i, (i + 1) * inpMultiplier, cols[i % 6])) 
         return INIT_FAILED;
   }
   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   if (!g_mas.refresh(rates_total, prev_calculated))
      return 0;
   return(rates_total);
}
//+------------------------------------------------------------------+

bool MaArray::refresh(const int &rates_total, const int &prev_calculated)
{
   for (int i=this.Total()-1; i>=0; --i) {
      Ma *ma = this.At(i);
      if (!ma.refresh(rates_total, prev_calculated))
         return false;
   }
   return true;
}

bool Ma::refresh(const int &rates_total, const int &prev_calculated)
{
   int limit = rates_total - this.MaPeriod() - prev_calculated - 1;
   if (limit < 0)
      limit = 0;
   for (int i=limit; i>=0; --i) {
      double ma = this.Main(i);
      if (ma == 0.0 || ma == EMPTY_VALUE)
         return false;
      this.buffer[i] = ma;
   }
   return true;
}

bool Ma::init(int buffer_index, int ma_period, color col)
{
   if (!this.Create(_Symbol, PERIOD_CURRENT, ma_period, 0, MODE_EMA, PRICE_CLOSE))
      return false;
   ArraySetAsSeries(this.buffer, true);
   bool set = SetIndexBuffer(buffer_index, this.buffer);
   SetIndexStyle(buffer_index, DRAW_LINE, STYLE_SOLID, 1, col);
   return true; 
}


 
nicholi shen:

Yup. Although, once you set the max number in the properties the rest you can setup dynamically. Take this example:

Thank you very much.

I never used the mql oop features and started to try it.

It seems to be a little complicated.

 
nicholi shen:

Yup. Although, once you set the max number in the properties the rest you can setup dynamically. Take this example:

Dear nicholi shen,

The code you posted here is not working for me

I copied it to my editor, changed the "SetIndexStyle" to some "PlotIndexSetInteger" and compiled it. There is only one warning that "no indicator plot defined for indicator" and no errors. but nothing is shown on the chart when testing the indicator.

Have you tested it yourself?

Reason: