Chandes dmi (fl)

Download script
Author: © mladen, 2019

This script is designed for the MetaTrader platform to provide traders with a specialized visual tool called "Chande's DMI with Floating Levels." It combines momentum tracking with dynamic boundaries to help identify price trends and potential overextended zones.

Core Functionality

The indicator operates by analyzing market volatility and momentum to plot a central "Chande DMI" line. Unlike standard indicators that use fixed numerical scales, this script features floating levels. This means the upper, middle, and lower boundaries are not static; they adjust automatically based on the price range of the recent historical data.

Key Components & Logic

  • Dynamic Range Analysis: The script calculates a "range" based on the highest highs and lowest lows over a specific period. It uses this range to project three floating levels:
    • Upper Level: Represents a high-intensity zone.
    • Middle Level: Serves as the equilibrium point.
    • Lower Level: Represents a low-intensity zone.
  • Momentum Calculation: The script evaluates price changes and compares them against the standard deviation of the market. It uses a custom mathematical formula to convert these raw fluctuations into a smoothed momentum value (the Chande DMI line).
  • Smart Scaling: Because it calculates a "K-factor" based on current market volatility, the indicator can adapt to both fast-moving, volatile markets and slow, sideways markets, maintaining its relevance in different trading environments.

Visual Features & Customization

The script is designed to be highly interactive and visually informative through several user-adjustable settings:

  • Color Change Modes: Users can choose how the Chande DMI line reacts to market movement. You can set it to change colors based on:
    • Slope Change: Changes color when the direction of the trend shifts up or down.
    • Outer Levels: Changes color when the line crosses the floating upper or lower boundaries (identifying overbought/oversold conditions).
    • Middle Level: Changes color specifically when the line crosses the center equilibrium.
  • Customizable Thresholds: Traders can define exactly where the "Upper" and "Lower" percentages sit relative to the current price range, allowing for a personalized sensitivity to market extremes.

Summary for the Trader

Essentially, this tool tells you where the "heartbeat" of the market is currently located relative to its recent boundaries. By watching the interaction between the shifting colored line and the floating levels, you can more easily identify when a trend is gaining strength, losing steam, or reaching a point of exhaustion.

4 Views
0 Downloads
0 Favorites
Chandes dmi (fl)
ÿþ//------------------------------------------------------------------

#property copyright   "© mladen, 2019"

#property link        "mladenfx@gmail.com"

#property description "Chande\'s DMI with floating levels"

//+------------------------------------------------------------------

#property indicator_separate_window

#property indicator_buffers 5

#property indicator_plots   4

#property indicator_label1  "Upper level"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrGray

#property indicator_style1  STYLE_DOT

#property indicator_label2  "Middle level"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrGray

#property indicator_style2  STYLE_DOT

#property indicator_label3  "Lower level"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrGray

#property indicator_style3  STYLE_DOT

#property indicator_label4  "Chande DMI"

#property indicator_type4   DRAW_COLOR_LINE

#property indicator_color4  clrGray,clrMediumSeaGreen,clrOrangeRed

#property indicator_width4  2



//

//

//





input int                 inpDMIPeriod       = 15;           // DMI period

input int                 inpDmiLowerLimit   = 10;           // DMI lower bound

input int                 inpDmiUpperLimit   = 50;           // DMI upper bound

input ENUM_APPLIED_PRICE  inpPrice           = PRICE_CLOSE;  // Price 

input int                 inpDevPeriod       = 25;           // Period of smoothing

enum enColorChangeOn

{

   cchange_onSlope,  // Change color on slope change

   cchange_onLevels, // Change color on outer levels cross

   cchange_onZero    // Change color on middle level cross

};

input int                inpFlPeriod    = 25;               // Floating levels period

input double             inpFlLevelUp   = 90;               // Floating levels up %

input double             inpFlLevelDn   = 10;               // Floating levels down %

input enColorChangeOn    inpColorChange = cchange_onLevels; // Color changing mode



//

//---

//



double val[],valc[],levu[],levd[],levm[],m_array[][13];



//------------------------------------------------------------------

//                                                                  

//------------------------------------------------------------------

//

//

//

int OnInit()

{

   //

   //--- indicator buffers mapping

   //

         SetIndexBuffer(0,levu,INDICATOR_DATA);

         SetIndexBuffer(1,levm,INDICATOR_DATA);

         SetIndexBuffer(2,levd,INDICATOR_DATA);

         SetIndexBuffer(3,val ,INDICATOR_DATA);

         SetIndexBuffer(4,valc,INDICATOR_COLOR_INDEX); 



      //

      //

      //



      iStdDeviation.init(inpDevPeriod);

      iAverage.init(inpDevPeriod);

         IndicatorSetString(INDICATOR_SHORTNAME,"Chandes DMI fl ("+(string)inpDMIPeriod+","+(string)inpFlPeriod+")");

   return(0);

}



//------------------------------------------------------------------

//                                                                  

//------------------------------------------------------------------

#define _setPrice(_priceType,_target,_index) \

   { \

   switch(_priceType) \

   { \

      case PRICE_CLOSE:    _target = close[_index];                                              break; \

      case PRICE_OPEN:     _target = open[_index];                                               break; \

      case PRICE_HIGH:     _target = high[_index];                                               break; \

      case PRICE_LOW:      _target = low[_index];                                                break; \

      case PRICE_MEDIAN:   _target = (high[_index]+low[_index])/2.0;                             break; \

      case PRICE_TYPICAL:  _target = (high[_index]+low[_index]+close[_index])/3.0;               break; \

      case PRICE_WEIGHTED: _target = (high[_index]+low[_index]+close[_index]+close[_index])/4.0; break; \

      default : _target = 0; \

   }}



//

//---

//



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[])

{

   static double prev_max,prev_min;

   static int    m_arraySize = -1,prev_i=-1;

             if (m_arraySize<rates_total) { m_arraySize = ArrayResize(m_array,rates_total+500); if (m_arraySize<rates_total) return(0); }



   //

   //---

   //

   

   int i=prev_calculated-1; if (i<0) i=0; for (; i<rates_total && !_StopFlag; i++)

   {

      double _price; _setPrice(inpPrice,_price,i);

      double deviation = iStdDeviation.calculate(_price,i);

      double deviatioa = iAverage.calculate(deviation,i,rates_total);

      double vi        = (deviatioa != 0) ? deviation/deviatioa : 0;

      int    td        = (vi != 0) ? (int)(inpDMIPeriod/vi) : 1;

             if (td>inpDmiUpperLimit) td=inpDmiUpperLimit;

             if (td<inpDmiLowerLimit) td=inpDmiLowerLimit;

      double Kg = (3.0)/(2.0+td);

      double Hg = 1.0-Kg;



      //

      //---

      //



      m_array[i][12] = _price;

         if (i>0) 

         {

            double roc = m_array[i][12]-m_array[i-1][12];

            double roa = (roc>0) ? roc : -roc;

            for (int k=0,kk=0; k<3; k++,kk=k*2)

            {

               m_array[i][kk+0] = Kg*roc              + Hg*m_array[i-1][kk+0];

               m_array[i][kk+1] = Kg*m_array[i][kk+0] + Hg*m_array[i-1][kk+1]; roc = 1.5*m_array[i][kk+0] - 0.5 * m_array[i][kk+1];

               m_array[i][kk+6] = Kg*roa              + Hg*m_array[i-1][kk+6];

               m_array[i][kk+7] = Kg*m_array[i][kk+6] + Hg*m_array[i-1][kk+7]; roa = 1.5*m_array[i][kk+6] - 0.5 * m_array[i][kk+7];

            }

            val[i] = (roa != 0) ? (roc/roa+1.0)*50.0 : 50; 

         }

         else { for (int k=0; k<12; k++) m_array[i][k] = 0; levm[i] = levu[i] = levd[i] = val[i] = 0; }

   

      //

      //---

      //

               

      if (prev_i!=i)

      {

            prev_i = i; 

            int start    = i-inpFlPeriod+1; if (start<0) start=0;

                prev_max = val[ArrayMaximum(val,start,inpFlPeriod-1)];

                prev_min = val[ArrayMinimum(val,start,inpFlPeriod-1)];

         }

         double max = (val[i] > prev_max) ? val[i] : prev_max;

         double min = (val[i] < prev_min) ? val[i]  : prev_min;

         double range = (max-min)/100.0;

            levu[i] = min+inpFlLevelUp*range;

            levd[i] = min+inpFlLevelDn*range;

            levm[i] = (levu[i]+levd[i])/2;

         

      //

      //---

      //

               

         switch (inpColorChange)

         {

            case cchange_onLevels : valc[i] = (val[i]>levu[i]) ? 1 :(val[i]<levd[i]) ? 2 : 0; break;

            case cchange_onSlope  : valc[i] = (i>0) ? (val[i]>val[i-1]) ? 1 :(val[i]<val[i-1]) ? 2 : valc[i-1] : 0; break;

            case cchange_onZero   : valc[i] = (val[i]>levm[i]) ? 1 :(val[i]<levm[i]) ? 2 : 0; break;

         }         

   }      

   return(i);

}



//------------------------------------------------------------------

//                                                                  

//------------------------------------------------------------------

//

//

//



class cStdDeviation

{

   private :

      int m_period;

      int m_arraySize;

         class cStdStruct

         {

            public :

               double price;

               double price2;

               double sum;

               double sum2;

               cStdStruct() : price(0),price2(0),sum(0),sum2(0) { }

         };

      cStdStruct m_array[];

   public:

      cStdDeviation() { init(1); }

     ~cStdDeviation() { ArrayFree(m_array); }



      //

      //---

      //



      void init(int period)

      {

         m_period    = (period>1) ? period : 1;

         m_arraySize = m_period+32;

            ArrayResize(m_array,m_arraySize);

      }

      

      double calculate(double price, int i)

      {

         int _ic = (i)%m_arraySize;

            m_array[_ic].price =price;

            m_array[_ic].price2=price*price;

            

            //

            //---

            //

            

            if (i>m_period)

            {

               int _ip = (i-1)        % m_arraySize;

               int _if = (i-m_period) % m_arraySize;

                  m_array[_ic].sum  = m_array[_ip].sum +m_array[_ic].price -m_array[_if].price;

                  m_array[_ic].sum2 = m_array[_ip].sum2+m_array[_ic].price2-m_array[_if].price2;

            }

            else  

            {

                  m_array[_ic].sum  = m_array[_ic].price;

                  m_array[_ic].sum2 = m_array[_ic].price2; 

                  for(int k=1, _ip=_ic-1; k<m_period && i>=k; k++, _ip--) 

                  {

                     if (_ip<0) _ip += m_arraySize;

                        m_array[_ic].sum  += m_array[_ip].price; 

                        m_array[_ic].sum2 += m_array[_ip].price2; 

                  }                  

            }         

            return (MathSqrt((m_array[_ic].sum2-m_array[_ic].sum*m_array[_ic].sum/(double)m_period)/(double)m_period));

      }

};

cStdDeviation iStdDeviation;



//

//---

//



class CSma

{

   private :

      struct scSmaArrayStruct

      {

         double value;

         double summ;

      };

      scSmaArrayStruct m_array[];

      int              m_arraySize;

      int              m_period;

   public :

      CSma() : m_arraySize(-1) {                     return; }

     ~CSma()                   { ArrayFree(m_array); return; }

     

     //

     //---

     //

      

     void   init(int period) { m_period = (period>1) ? period : 1; }

     double calculate(double value, int i, int bars)

         {

            if (m_arraySize<bars) { m_arraySize = ArrayResize(m_array,bars+500); if (m_arraySize<bars) return(0); }

                m_array[i].value=value;

                if (i>m_period)

                     m_array[i].summ = m_array[i-1].summ+value-m_array[i-m_period].value;

               else

               {

                     m_array[i].summ = 0;

                     int k=0; for(; k<m_period && i>=k; k++)

                            m_array[i].summ += m_array[i-k].value;

                     return(m_array[i].summ/(double)k);

               }         

               return(m_array[i].summ/(double)m_period);

      }   

};

CSma iAverage;

Comments