//+------------------------------------------------------------------+
//|                                                   ChandesDMI.mq4 |
//|                                  Copyright © 2007, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers   1
#property indicator_color1    LightBlue
#property indicator_width1    2 
#property indicator_level1    70
#property indicator_level2    50
#property indicator_level3    30
//---- input parameters
extern int       Price           =  0; // Applied Price: 0-Close,1-Open,2-High,3-Low,4-Median,5-Typical,6-Weighted
extern int       StdDevPeriod    =  5; // Period of Standard Deviation
extern int       MAStdDevPeriod  = 10; // Period of smoothing
extern int       MAStdDevMode    =  0; // Mode of smoothing MA
extern int       DMIPeriod       =  5; // Dynamic Momentum Index Period
extern int       DmiLowerLimit   =  3; // Dynamic Periods Lower Bound
extern int       DmiUpperLimit   = 30; // Dynamic Periods Upper Bound
extern int       PlotMode        =  0; // Display DMI(0) or Periods Length(1) 
//---- buffers
double DMI[];
double STD[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   IndicatorBuffers(2);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,DMI);
   SetIndexBuffer(1,STD);
//---- name for DataWindow and indicator subwindow label
   string short_name="Chandes DMI ("+Price+","+StdDevPeriod+","+MAStdDevPeriod+","+MAStdDevMode+","+DMIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"DMI");
   
//----
   SetIndexDrawBegin(0,StdDevPeriod+MAStdDevPeriod+DMIPeriod);
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int      shift, limit, counted_bars=IndicatorCounted();
   
//---- 
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars ==0 ) limit=Bars-1;
   if ( counted_bars < 1 ) 
   
   for(int i=1;i<StdDevPeriod+MAStdDevPeriod+DMIPeriod;i++) 
   {
   DMI[Bars-i]=0;    
   STD[Bars-i]=0;  
   }
     
   if(counted_bars>0) limit=Bars-counted_bars;
   limit--;
   
   for( shift=limit; shift>=0; shift--)
   STD[shift] = iStdDev(NULL,0,StdDevPeriod,0,MODE_SMA,Price,shift);
   
   for( shift=limit; shift>=0; shift--)
   {
   double MASTD = iMAOnArray(STD,0,MAStdDevPeriod,0,MAStdDevMode,shift); 
   if (MASTD != 0) double VI = STD[shift]/MASTD;
   if (VI != 0)int Td = MathRound(DMIPeriod/VI); 
   
   if (Td > DmiUpperLimit ) Td = DmiUpperLimit;
   else
   if (Td < DmiLowerLimit ) Td = DmiLowerLimit;
   
   if(PlotMode == 0)
   DMI[shift] = iRSI(NULL,0,Td,PRICE_CLOSE,shift);
   else
   DMI[shift] = Td;
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+
/*

Source Code For Metastock

========================
Dynamic Mom Index - jose
========================
---8<---------------------------

{ Jose's Dynamic Momentum Index v3.1, 0~100% }
{ ©Copyright 2003-2004 Jose Silva }
{ http://www.metastocktools.com }

pds:=Input("avg DyMoIdx periods",3,252,21);
Stpds:=
 Input("Standard Deviation periods",2,252,5);
x:=Input("use Open=1 High=2 Low=3 Close=4 Volume=5 P=6",1,6,4);
plot:=Input("[1]Dynamic Momentum Idx,  [2]dynamic periods used",1,2,1);

x:=If(x=1,O,If(x=2,H,If(x=3,L,If(x=5,V,
 If(x=6,P,C)))));
Vt:=Stdev(x,Stpds)/Mov(Stdev(x,Stpds),10,S);
pds:=pds/(Vt+.14142);
pds:=If(pds>Cum(IsDefined(x))-13,
 Cum(IsDefined(x))-13,pds);
pds:=If(pds<2,2,pds);

Up:=If(x>Ref(x,-1),x-Ref(x,-1),0);
Dw:=If(x<Ref(x,-1),Ref(x,-1)-x,0);
UpAvg:=Up*2/pds+PREV*(1-2/pds);
DwAvg:=Dw*2/pds+PREV*(1-2/pds);
DyMoIdx:=
 100-(100/(1+UpAvg/If(DwAvg>0,DwAvg,.000001)));

If(plot=1,DyMoIdx,pds)

---8<---------------------------

Source Code From VTS:

Vi:= Stdev(price,stdevPeriods) / Mov(Stdev(price,stdevPeriods),stdevmaper,stdevmat) ;
Td:= Int(DmiPeriods/Vi);
Td:= if(Td>DmiUpperLimit,DmiUpperLimit,if(Td<DmiLowerLi mit,DmiLowerLimit,Td));

rsi_r:= (price - ref(price,-1));
rsi_rs:= Wilders(if(rsi_r>0,rsi_r,0),Td) / Wilders(if(rsi_r<0,Abs(rsi_r),0),Td);
DMIndex:= 100-(100/(1+rsi_rs));

IndicatorPlot:= if(plot=0,DMIndex,Td);
*/

