Icustom - Display or not display ?

 

Bonjour
Pas de repos pour les guerriers même le dimanche matin :-)


Question
j'ai un code pour afficher une moyenne mobile, rien de sorcier

//+------------------------------------------------------------------+
//|                                                          MMA.mq5 |
//|                                             Copyright 2023, GwDs |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, GwDs"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

//--- input parameters
input int inpPeriod = 20; // Period of SMA
//--- indicator buffers
double SMABuffer[];
//--- global variables
int SMA_Handle;
//--- plot SMA
#property indicator_label1  "SMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAliceBlue
#property indicator_style1  STYLE_DOT
#property indicator_width1  1
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- create the SMA indicator with the desired parameters
   SMA_Handle = iMA( _Symbol, _Period, inpPeriod, 0, MODE_SMA, PRICE_CLOSE);
   //--- check for errors
   if( SMA_Handle == INVALID_HANDLE)
     {
      Print( "Error creating SMA indicator");
      return( INIT_FAILED);
     }
        
   //--- set the indicator buffer for SMA values
   SetIndexBuffer( 0,SMABuffer,INDICATOR_DATA);
   //--- set the graphical properties of the SMA plot
   PlotIndexSetInteger( 0, PLOT_DRAW_BEGIN, inpPeriod-1);
   PlotIndexSetString( 0, PLOT_LABEL, "SMA( " + string(inpPeriod) + ")");
   
   string name = "Mon indicateur (" + string(inpPeriod) + ")";
   IndicatorSetString(INDICATOR_SHORTNAME, name);
   
   PlotIndexSetDouble( 0,PLOT_EMPTY_VALUE,0.0);
   
   //--- succeed
   ArrayInitialize( SMABuffer, EMPTY_VALUE);
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Fonction d'itération d'indicateur personnalisé                   |
//+------------------------------------------------------------------+
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[])
        {
//+------------------------------------------------------------------+
//| OnCalculate - Calculs préliminaires                              |
//+------------------------------------------------------------------+
      int limit = prev_calculated;
                if( prev_calculated == 0) 
                {
                        limit = inpPeriod;
                } 
                else 
                {
                        limit = prev_calculated;
                }  

//+------------------------------------------------------------------+
//| OnCalculate - Nouvelle barres ou lancement indicateur            |
//+------------------------------------------------------------------+
      /* Si le nombre de barres calculées est inférieur au nombre total de barres*/
                if ( prev_calculated < rates_total)
                {
                        /* Toutes les barres sont retournées dans le buffer */
                        if( CopyBuffer( SMA_Handle, 0, 0, rates_total, SMABuffer) <=0)
                        {
                                Print("Error copying SMA values");
                                return(0);
                        }
                }
 
           //--- return value of prev_calculated for next call
           return(rates_total);
        }
//+------------------------------------------------------------------+

Comme on peut le voir, ce code gère son affichage pour "voir" la MMA. C'est un peu le but

Mais j'ai besoin de cette MMA dans un autre code et je compte bien utiliser icustom qui va me donner accès au buffer de ce code.

C'est le code appelant qui va gérer l'affichage de la MMA.

Icustom sait libérer les ressources allouées à l'affichage alors qu'elles ne seront pas utilisées ?
Doit on faire un fichier spécifique de la MMA qui ne gérera aucun affichage ?

Voici le code sans l'affichage 


//+------------------------------------------------------------------+
//|                                                MMA_NoDisplay.mq5 |
//|                                             Copyright 2023, GwDs |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, GwDs"
#property link      "https://www.mql5.com"

//--- input parameters
input int inpPeriod = 20; // Period of SMA
//--- indicator buffers
double SMABuffer[];
//--- global variables
int SMA_Handle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   //--- create the SMA indicator with the desired parameters
   SMA_Handle = iMA( _Symbol, _Period, inpPeriod, 0, MODE_SMA, PRICE_CLOSE);
   //--- check for errors
   if( SMA_Handle == INVALID_HANDLE)
     {
      Print( "Error creating SMA indicator");
      return( INIT_FAILED);
     }
        
   //--- set the indicator buffer for SMA values
   SetIndexBuffer( 0,SMABuffer,INDICATOR_DATA);
   
   //--- succeed
   ArrayInitialize( SMABuffer, EMPTY_VALUE);
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
        {
//+------------------------------------------------------------------+
//| OnCalculate - Calculs préliminaires                              |
//+------------------------------------------------------------------+
      int limit = prev_calculated;
                if( prev_calculated == 0) 
                {
                        limit = inpPeriod;
                } 
                else 
                {
                        limit = prev_calculated;
                }  

//+------------------------------------------------------------------+
//| OnCalculate - Nouvelle barres ou lancement indicateur            |
//+------------------------------------------------------------------+
      /* Si le nombre de barres calculées est inférieur au nombre total de barres*/
                if ( prev_calculated < rates_total)
                {
                        /* Toutes les barres sont retournées dans le buffer */
                        if( CopyBuffer( SMA_Handle, 0, 0, rates_total, SMABuffer) <=0)
                        {
                                Print("Error copying SMA values");
                                return(0);
                        }
                }


           //--- return value of prev_calculated for next call
           return(rates_total);
        }
//+------------------------------------------------------------------+
Bon weekend