Calcul de la période des cycles selon Ehlers

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #6787 quote
    zilliq
    Participant
    Master

    Bonjour à tous,

    Je cherche à coder cet indicateur d’Ehlers qui détermine la péridoe du cycle dominant.

    Je bute sur la conversion de ce code MT5 en PRT car MT5 n’est vraiment pas ma tasse de thé

    Alors si quelqu’un a plus de facilité pour faire la conversion

    Merci d’avance

    Zilliq

     


    //+------------------------------------------------------------------+
    //|                                                  CyclePeriod.mq5 |
    //|                                      Copyright 2011, Investeo.pl |
    //|                                               http://Investeo.pl |
    //+------------------------------------------------------------------+
    #property copyright "Copyright 2011, Investeo.pl"
    #property link      "http://Investeo.pl"
    #property version   "1.00"
    #property indicator_separate_window
    
    #property description "CyclePeriod indicator - described by John F. Ehlers"
    #property description "in \"Cybernetic Analysis for Stocks and Futures\""
    
    #property indicator_buffers 2
    #property indicator_plots 2
    #property indicator_width1 1
    #property indicator_width2 1
    #property indicator_type1   DRAW_LINE
    #property indicator_type2   DRAW_LINE
    #property indicator_color1  Green
    #property indicator_color2  Red
    #property indicator_label1  "Cycle"
    #property indicator_label2  "Trigger Line"
    
    #define Price(i) ((high[i]+low[i])/2.0)
    
    double Smooth[];
    double Cycle[];
    double Trigger[];
    //double Price[];
    double Q1[]; // Quadrature component
    double I1[]; // InPhase component
    double DeltaPhase[];
    double InstPeriod[];
    double CyclePeriod[];
    
    
    input double InpAlpha=0.07; // alpha
    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+
    int OnInit()
      {
    //--- indicator buffers mapping 
       ArraySetAsSeries(Cycle,true);
       ArraySetAsSeries(CyclePeriod,true);
       ArraySetAsSeries(Trigger,true); 
       ArraySetAsSeries(Smooth,true);
       //ArraySetAsSeries(Price,true);
       
       SetIndexBuffer(0,CyclePeriod,INDICATOR_DATA);
       SetIndexBuffer(1,Trigger,INDICATOR_DATA);
       
       PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
       PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);
    
       return(0);
      }
    //+------------------------------------------------------------------+
    //| 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[])
      {
    //---   
       long tickCnt[1];
       int i;
       int ticks=CopyTickVolume(Symbol(), 0, 0, 1, tickCnt);
       if(ticks!=1) return(rates_total);
       double DC, MedianDelta;
    
       Comment(tickCnt[0]);
    
       if(prev_calculated==0 || tickCnt[0]==1)
         {
          //--- last counted bar will be recounted
          int nLimit=rates_total-prev_calculated-1; // start index for calculations
    
          ArraySetAsSeries(high,true);
          ArraySetAsSeries(low,true);
          
          ArrayResize(Smooth,Bars(_Symbol,_Period));
          ArrayResize(Cycle,Bars(_Symbol,_Period));
          //ArrayResize(Price,Bars(_Symbol,_Period));
          ArrayResize(CyclePeriod,Bars(_Symbol,_Period));
          ArrayResize(InstPeriod,Bars(_Symbol,_Period));
          ArrayResize(Q1,Bars(_Symbol,_Period));
          ArrayResize(I1,Bars(_Symbol,_Period));
          ArrayResize(DeltaPhase,Bars(_Symbol,_Period));
          
          if (nLimit>rates_total-7) // adjust for last bars
             nLimit=rates_total-7;   
          
          for(i=nLimit;i>=0 && !IsStopped();i--)   
          {
             Smooth[i] = (Price(i)+2*Price(i+1)+2*Price(i+2)+Price(i+3))/6.0;
       
             if (i<rates_total-7)
             {
                Cycle[i] = (1.0-0.5*InpAlpha) * (1.0-0.5*InpAlpha) * (Smooth[i]-2.0*Smooth[i+1]+Smooth[i+2])
                          +2.0*(1.0-InpAlpha)*Cycle[i+1]-(1.0-InpAlpha)*(1.0-InpAlpha)*Cycle[i+2];
                       
             } else         
             {
                Cycle[i]=(Price(i)-2.0*Price(i+1)+Price(i+2))/4.0;
             }
             
             Q1[i] = (0.0962*Cycle[i]+0.5769*Cycle[i+2]-0.5769*Cycle[i+4]-0.0962*Cycle[i+6])*(0.5+0.08*InstPeriod[i+1]);
             I1[i] = Cycle[i+3];
             
             if (Q1[i]!=0.0 && Q1[i+1]!=0.0) 
                DeltaPhase[i] = (I1[i]/Q1[i]-I1[i+1]/Q1[i+1])/(1.0+I1[i]*I1[i+1]/(Q1[i]*Q1[i+1]));
             if (DeltaPhase[i] < 0.1)
                DeltaPhase[i] = 0.1;
             if (DeltaPhase[i] > 0.9)
                DeltaPhase[i] = 0.9;
            
             MedianDelta = Median(DeltaPhase, i, 5);
             
             if (MedianDelta == 0.0)
                DC = 15.0;
             else
                DC = (6.28318/MedianDelta) + 0.5;
            
             InstPeriod[i] = 0.33 * DC + 0.67 * InstPeriod[i+1];
             CyclePeriod[i] = 0.15 * InstPeriod[i] + 0.85 * CyclePeriod[i+1];
             Trigger[i] = CyclePeriod[i+1];
          }
         }
    //--- return value of prev_calculated for next call
       return(rates_total);
      }
    //+------------------------------------------------------------------+
    
    double Median(double& arr[], int idx, int m_len)
    {
       double MedianArr[];
       int copied;
       double result = 0.0;
       
       ArraySetAsSeries(MedianArr, true);
       ArrayResize(MedianArr, m_len);
       
       copied = ArrayCopy(MedianArr, arr, 0, idx, m_len);
       if (copied == m_len)
       {
          ArraySort(MedianArr);
          if (m_len %2 == 0) 
                result = (MedianArr[m_len/2] + MedianArr[(m_len/2)+1])/2.0;
          else
                result = MedianArr[m_len / 2];
          
       }
       else Print(__FILE__+__FUNCTION__+"median error - wrong number of elements copied."); 
       return result; 
    }
    #6802 quote
    Nicolas
    Keymaster
    Master

    J’ai recodé l’indicateur pour PRT, mais je ne suis pas certain pour le calcul de la fonction Median.

    Regarde ce que ça donne:

    Price = medianprice
    InpAlpha=0.07
    once MedianDelta = 0
    
    if barindex>20 then
     Smooth = (Price[2]*Price[1]+2*Price[2]+Price[3])/6.0
    
      if barindex<27 then
       CycleP = (1-0.5*InpAlpha) * (1-0.5*InpAlpha) * (Smooth-2.0*Smooth[1]+Smooth[2])+2.0*(1.0*InpAlpha)*CycleP[1]-(1.0-InpAlpha)*(1.0-InpAlpha)*CycleP[2]
       else
      CycleP = (Price-2.0*Price[1]+Price[2])/4.0
     endif
    
    Q1 = (0.0962*CycleP+0.5769*CycleP[2]-0.5769*CycleP[4]-0.0962*CycleP[6])*(0.5+0.08*InstPeriod[1])
    
    I1 = CycleP[3]
    
     if Q1<>0 AND Q1[1]<>0 then
      DeltaPhase = (I1/Q1-I1[1]/Q1[1])/(1.0+I1*I1[1]/(Q1*Q1[1]))
     elsif DeltaPhase<0.1 then
      DeltaPhase = 0.1
     elsif DeltaPhase>0.9 then 
      DeltaPhase = 0.9
    endif
    
     array = 0
      for i = 0 to 5 do
       array = array + DeltaPhase[i]
      next
     MedianDelta = array/2
    
     if MedianDelta = 0 then 
      DC=15
     else
      DC=(6.28318/MedianDelta) + 0.5
     endif
    
     InstPeriod = 0.33*DC+0.67*InstPeriod
     CyclePeriod = 0.15*InstPeriod+0.85*CyclePeriod[1]
    
    endif
    
    RETURN CyclePeriod,CyclePeriod[1]
    #7956 quote
    Nicolas
    Keymaster
    Master

    Cela a été utile?

    #8035 quote
    zilliq
    Participant
    Master

    Merci Nicolas,

    Désolé complètement débordé en ce moment

    D’accord avec toi, le median je ne l’avais pas compris

    Je vais tester et comparer avec ce que ca donne sur MT5

    Je te dis ca asap

    En tout cas merci

    Zilliq

    #8037 quote
    zilliq
    Participant
    Master

    Il y a des valeurs négatives

    Cela ne doit pas être ca

    Perso je ne comprends pas cette fonction median. Le problème vient peut être de là

    Zilliq

    #8040 quote
    zilliq
    Participant
    Master

    Voici le code MT4, peut être plus “simple”

    //+------------------------------------------------------------------+
    //|                                                  CyclePeriod.mq4 |
    //|                                                                  |
    //| Cycle Period                                                     |
    //|                                                                  |
    //| Algorithm taken from book                                        |
    //|     "Cybernetics Analysis for Stock and Futures"                 |
    //| by John F. Ehlers                                                |
    //|                                                                  |
    //|                                              contact@mqlsoft.com |
    //|                                          http://www.mqlsoft.com/ |
    //+------------------------------------------------------------------+
    #property copyright "Coded by Witold Wozniak (www.mqlsoft.com)"
    #property link      "http://www.mqlsoft.com"
    #property version "2.0"
    #property description "The algorithm was taken from the book "
    #property description "\"Cybernetics Analysis for Stock and Futures\" "
    #property description "by John F. Ehlers"
    #property strict
    
    #property indicator_separate_window
    #property indicator_buffers 1
    #property indicator_color1 clrRed
    
    #property indicator_minimum 0
    
    double DeltaPhase[];
    double CPeriod[];
    double Smooth[];
    double Cycle[];
    double Q1[];
    double I1[];
    double InstPeriod[];
    
    input double Alpha = 0.07;
    
    int buffers = 0;
    int drawBegin = 8;
    int median = 5;
    
    int init() {
        IndicatorBuffers(7);
        initBuffer(CPeriod, "Cycle Period", DRAW_LINE);
        initBuffer(Smooth);
        initBuffer(Cycle);
        initBuffer(Q1);
        initBuffer(I1);
        initBuffer(DeltaPhase);
        initBuffer(InstPeriod);    
        IndicatorShortName("Cycle Period [" + DoubleToStr(Alpha, 2) + "]");
        return (0);
    }
      
    int start() {
        if (Bars <= drawBegin) return (0);
        int countedBars = IndicatorCounted();
        if (countedBars < 0) return (-1);
        if (countedBars > 0) countedBars--;
        int s, limit = MathMin(Bars - countedBars - 1, Bars - drawBegin);
        for (s = limit; s >= 0; s--) {    
            Smooth[s] = (P(s) + 2.0 * P(s + 1) + 2.0 * P(s + 2) + P(s + 3)) / 6.0;
            Cycle[s] = (1.0 - 0.5 * Alpha) * (1.0 - 0.5 * Alpha) * (Smooth[s] - 2 * Smooth[s + 1] + Smooth[s + 2]) 
                + 2.0 * (1.0 - Alpha) * Cycle[s + 1]
                - (1.0 - Alpha) * (1.0 - Alpha) * Cycle[s + 2];
            if (s > Bars - 8) {
                Cycle[s] = (P(s) - 2.0 * P(s + 1) + P(s + 2)) / 4.0;           
            }
            Q1[s] = (0.0962 * Cycle[s] + 0.5769 * Cycle[s + 2] - 0.5769 * Cycle[s + 4] - 0.0962 * Cycle[s + 6])
                * (0.5 + 0.08 * InstPeriod[s + 1]);
            I1[s] = Cycle[s + 3];
            if (Q1[s] != 0.0 && Q1[s + 1] != 0.0) {
                DeltaPhase[s] = (I1[s] / Q1[s] - I1[s + 1] / Q1[s + 1]) 
                    / (1.0 + I1[s] * I1[s + 1] / (Q1[s] * Q1[s + 1]));
            }
            DeltaPhase[s] = MathMax(0.1, DeltaPhase[s]);
            DeltaPhase[s] = MathMin(1.1, DeltaPhase[s]);
            double MedianDelta, DC;
            double M[];
            ArrayResize(M, median);
            ArrayCopy(M, DeltaPhase, 0, s, median);
            ArraySort(M);
            if (median % 2 == 0) {
                MedianDelta = (M[median / 2] + M[(median / 2) + 1]) / 2.0;
            } else {
                MedianDelta = M[median / 2];
            }
            if (MedianDelta == 0.0) {
                DC = 15.0;
            } else {
                DC = 6.28318 / MedianDelta + 0.5;
            }
            InstPeriod[s] = 0.33 * DC + 0.67 * InstPeriod[s + 1];
            CPeriod[s] = 0.15 * InstPeriod[s] + 0.85 * CPeriod[s + 1];
        }
        return (0);
    }
    
    double P(int index) {
        if (index >= Bars) {
            return ((High[Bars - 1] + Low[Bars - 1]) / 2.0);
        }
        return ((High[index] + Low[index]) / 2.0);
    }
    
    void initBuffer(double& array[], string label = "", int type = DRAW_NONE, int arrow = 0, int style = EMPTY, int width = EMPTY, color clr = CLR_NONE) {
        SetIndexBuffer(buffers, array);
        SetIndexLabel(buffers, label);    
        SetIndexEmptyValue(buffers, 0);
        SetIndexDrawBegin(buffers, drawBegin);
        SetIndexShift(buffers, 0);
        SetIndexStyle(buffers, type, style, width);
        SetIndexArrow(buffers, arrow);    
        buffers++;
    }
    
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Calcul de la période des cycles selon Ehlers


ProBuilder : Indicateurs & Outils Personnalisés

New Reply
Author
author-avatar
zilliq @zilliq Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by zilliq
9 years, 8 months ago.

Topic Details
Forum: ProBuilder : Indicateurs & Outils Personnalisés
Language: French
Started: 05/11/2016
Status: Active
Attachments: No files
Logo Logo
Loading...