Navigation´╝ÜHome > Content >

MIDAS_v2.02.mq4

Time: 2018-09-18 | Download file:MIDAS_v2.02.mq4

//+------------------------------------------------------------------+
//|                                                     MIDAS_v2.mq4 |
//|                                                       Akif TOKUZ |
//| Implementation is based on article "Introducing the MIDAS Method |
//| of Technical Analysis" by Dr. Paul Levine.                       |
//| Indicator can be started anywhere with dragging a vertical line  |
//|                                                      28.03.2011  |
//+------------------------------------------------------------------+
#property copyright "Akif TOKUZ"
#property link      "akifusenet@gmail.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Magenta
#property indicator_color2 Magenta
#property indicator_color3 Magenta
#property indicator_color4 FireBrick
#property indicator_color5 Yellow
#property indicator_width1 2
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 2
#property indicator_width5 2
#property indicator_style2 2
#property indicator_style3 2

//---- input parameters
extern int        Id=1;
extern bool       ShowVWAP=true;
extern bool       ShowTopFinder=false;
extern double     TopFinderVolumeNumBars=30; 
extern bool       ShowShiftedVWAP   =false;
extern double     shift           =20;
extern bool       ShowAveragePrice=false;
extern int        LineWidth       = 1;
extern int        LineStyle       = STYLE_DOT;
extern color      LineColor       = DeepSkyBlue;
extern datetime   MidasStartTime  = 0;



//---- indicator buffers
double VWAP[];
double TopFinder[];
double VWAPShiftPos[];
double VWAPShiftNeg[];
double AvgPrice[];
double CumVol[];//not displayed
double CumPVol[];//not displayed

//---- global variables for holding up values at fixed "start day"
double CumPVol_J=-1;
double CumVol_J=-1;




datetime TimeStart;
string NameStart;
int uninitReason;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(7);

   SetIndexBuffer(0,VWAP);
   SetIndexBuffer(1,VWAPShiftPos);   
   SetIndexBuffer(2,VWAPShiftNeg);   
   SetIndexBuffer(3,TopFinder);   
   SetIndexBuffer(4,AvgPrice);
   SetIndexBuffer(5,CumVol);
   SetIndexBuffer(6,CumPVol);   
   
   if (ShowVWAP==true)   SetIndexStyle(0,DRAW_LINE);
   else SetIndexStyle(0,DRAW_NONE);
   
   if (ShowShiftedVWAP==true)
   {
      SetIndexStyle(1,DRAW_LINE);
      SetIndexStyle(2,DRAW_LINE);
   }
   else
   {
    SetIndexStyle(1,DRAW_NONE);
    SetIndexStyle(2,DRAW_NONE);
   }

   if (ShowTopFinder==true)   SetIndexStyle(3,DRAW_LINE);
   else SetIndexStyle(3,DRAW_NONE); 
   
   if (ShowAveragePrice==true)   SetIndexStyle(4,DRAW_LINE);
   else SetIndexStyle(4,DRAW_NONE);

    
   string short_name="MIDAS";
   IndicatorShortName(short_name);
   
   SetIndexLabel(0,"VWAP");
   SetIndexLabel(1,"VWAPShiftPos");
   SetIndexLabel(2,"VWAPShiftNeg");
   SetIndexLabel(3,"TopFinder");
   SetIndexLabel(4,"AvgPrice");
   SetIndexLabel(5,"CumVol");
   SetIndexLabel(6,"CumPVol");
   
   NameStart = "StartDate"+"_"+Id;
//----
   return(0);
  }
  
  
  
//+------------------------------------------------------------------+
//| Delete all objects with given prefix                             |
//+------------------------------------------------------------------+
void ObDeleteObjectsByPrefix(string Prefix)
{
   int L = StringLen(Prefix);
   int i = 0; 
   while(i < ObjectsTotal())
   {
       string ObjName = ObjectName(i);
       if(StringSubstr(ObjName, 0, L) != Prefix) 
       { 
           i++; 
           continue;
       }
       ObjectDelete(ObjName);
   }
}
  
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

  int deinit()
    {
     switch(UninitializeReason())
       {
        case REASON_CHARTCLOSE:  Print("REASON_CHARTCLOSE:",NameStart);break;
        case REASON_REMOVE:      Print("REASON_REMOVE:",NameStart);ObDeleteObjectsByPrefix("StartDate");break;
        case REASON_RECOMPILE:   Print("REASON_RECOMPILE:",NameStart);break;
        case REASON_CHARTCHANGE: Print("REASON_CHARTCHANGE:",NameStart);break;
        case REASON_PARAMETERS:  Print("REASON_PARAMETERS:",NameStart); if (MidasStartTime>0) ObDeleteObjectsByPrefix("StartDate"); break;
        case REASON_ACCOUNT:     Print("REASON_ACCOUNT:",NameStart);break;
       }
       
     //...
   return(0);
    }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   int counted_bars=IndicatorCounted();
   double d;
   double e;
   double result;
   double x;
   double D;
  
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;


  if (ObjectFind(NameStart) == -1)
   {
      if (MidasStartTime==0)   
            TimeStart = iTime(NULL, 0, 20);
      else  TimeStart = MidasStartTime;            
    ObjectCreate(NameStart, OBJ_VLINE, 0, 0, 0);
  }
  if (ObjectGet(NameStart, OBJPROP_TIME1) != TimeStart) {
    ObjectSet(NameStart, OBJPROP_COLOR, LineColor);
    ObjectSet(NameStart, OBJPROP_BACK, false);
    ObjectSet(NameStart, OBJPROP_WIDTH, LineWidth);
    ObjectSet(NameStart, OBJPROP_STYLE, LineStyle);
    if (ObjectGet(NameStart, OBJPROP_TIME1) == 0) ObjectSet(NameStart, OBJPROP_TIME1, TimeStart);
    else TimeStart = ObjectGet(NameStart, OBJPROP_TIME1);

    ArrayInitialize(VWAP,EMPTY_VALUE);
    ArrayInitialize(TopFinder,EMPTY_VALUE);
    ArrayInitialize(VWAPShiftPos,EMPTY_VALUE);
    ArrayInitialize(VWAPShiftNeg,EMPTY_VALUE);
    ArrayInitialize(AvgPrice,EMPTY_VALUE);
    ArrayInitialize(CumVol,EMPTY_VALUE);
    ArrayInitialize(CumPVol,EMPTY_VALUE);
    

  }

   CumPVol_J = -1;
   CumVol_J = -1;
   int limit = iBarShift(NULL,0,ObjectGet(NameStart, OBJPROP_TIME1));
   for (i=limit; i>=0; i--)
   {
      AvgPrice[i]=(High[i]+Low[i])/2;
      CumVol[i]=CumVol[i+1]+Volume[i];
      CumPVol[i]=CumPVol[i+1]+Volume[i]*AvgPrice[i];
      
      
      if (TimeStart <= Time[i])
      {
          if ((CumPVol_J==-1)&&(CumVol_J==-1))        // save starting day values
          {
            CumPVol_J = CumPVol[i];
            CumVol_J = CumVol[i];          
            VWAP[i]=AvgPrice[i];                      // at the start day we need to put initial values                     
            VWAPShiftPos[i]=VWAP[i]+VWAP[i]*shift/10000; // which are calculated differently             
            VWAPShiftNeg[i]=VWAP[i]-VWAP[i]*shift/10000; // which are calculated differently             
            TopFinder[i]=AvgPrice[i];                 // (check Dr. Levine's lectures)
          }else
          {      
            VWAP[i]=( CumPVol[i] - CumPVol_J ) / ( CumVol[i] - CumVol_J );             
            VWAPShiftPos[i]=VWAP[i]+VWAP[i]*shift/10000;
            VWAPShiftNeg[i]=VWAP[i]-VWAP[i]*shift/10000;
                         
            d=( CumVol[i] - CumVol_J );                  // calculate TopFinder
            D=TopFinderVolumeNumBars*(CumVol[i]/Bars);   // multiply TopFinderVolumeNumBars input with average volume per bar
            e=d*(1-d/D);                                 // when d/D gets close to 1, e will be zero.
            
            x=CumVol[i]-e;
            if (d/D<0.9999)                              // protection from division by zero
            {
                  for (int j=0;j        

Recommend