Chaikin Money Flow for MT5? - page 2

 
Igor Faggiano #:
Thank you good sir.
 
less iterations better performances:
#property copyright "Copyright 2024, Paul"
#property link      "https://www.mql5.com"
#property version   "1.03"
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "CMF"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrMaroon
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

// Paramètres d'entrée
input int                 InpCMFPeriod    = 20;           // Période CMF
input ENUM_APPLIED_VOLUME InpVolumeType   = VOLUME_TICK;  // Type de volume

// Buffer
double         CMFBuffer[];

// Variables globales
int            CMFPeriod;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   // Validation des paramètres
   if(InpCMFPeriod <= 0)
   {
      Print("La période CMF doit être supérieure à 0");
      return INIT_PARAMETERS_INCORRECT;
   }
   
   CMFPeriod = InpCMFPeriod;
   
   // Mapping du buffer
   SetIndexBuffer(0, CMFBuffer, INDICATOR_DATA);
   
   // Propriétés de l'indicateur
   IndicatorSetInteger(INDICATOR_DIGITS, 4);
   IndicatorSetString(INDICATOR_SHORTNAME, "CMF(" + IntegerToString(CMFPeriod) + ")");
   
   // Définir la ligne zéro
   IndicatorSetInteger(INDICATOR_LEVELS, 1);
   IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, 0.0);
   IndicatorSetString(INDICATOR_LEVELTEXT, 0, "0");
   IndicatorSetInteger(INDICATOR_LEVELCOLOR, 0, clrGray);
   IndicatorSetInteger(INDICATOR_LEVELSTYLE, 0, STYLE_SOLID);
   
   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[])
{
   if(rates_total < CMFPeriod)
      return 0;
   
   int start = prev_calculated - 1;
   if(start < CMFPeriod) start = CMFPeriod;
   
   for(int i = start; i < rates_total && !IsStopped(); i++)
   {
      double sumAD = 0;
      double sumVolume = 0;
      
      for(int j = 0; j < CMFPeriod; j++)
      {
         int index = i - j;
         double highLowRange = high[index] - low[index];
         
         if(highLowRange > 0)
         {
            double moneyFlowMultiplier = ((close[index] - low[index]) - (high[index] - close[index])) / highLowRange;
            double moneyFlowVolume = moneyFlowMultiplier * (InpVolumeType == VOLUME_TICK ? (double)tick_volume[index] : (double)volume[index]);
            
            sumAD += moneyFlowVolume;
            sumVolume += (InpVolumeType == VOLUME_TICK ? (double)tick_volume[index] : (double)volume[index]);
         }
      }
      
      CMFBuffer[i] = (sumVolume > 0) ? sumAD / sumVolume : 0;
   }
   
   return(rates_total);
}

Découvrez de nouvelles opportunités MetaTrader 5 avec la communauté et les services MQL5
Découvrez de nouvelles opportunités MetaTrader 5 avec la communauté et les services MQL5
  • 2024.08.16
  • www.mql5.com
MQL5 : langage de stratégies de trading intégré à la plateforme de trading MetaTrader 5, permet d'écrire vos propres robots de trading, indicateurs techniques, scripts et bibliothèques de fonctions