#MTF-MACD





//+------------------------------------------------------------------+ 
//|              PowerTrend MultiTimeFrame MACD                      | 
//|                                                                  | 
//|   Copyright © 2008, Tim Hyder aka Hiachiever                     |
//|                                                                  |
//|   PO BOX 768, Hillarys, Western Australia, Australia, 6923       |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - hiachiever@gmail.com                                  |  
//+------------------------------------------------------------------+ 
#property copyright "Copyright © 2007, Tim Hyder."
#property link      "http://www.the4xtrader.com"

#define vers    "18.Feb.2008"
#define major   1
#define minor   0

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 7
#property indicator_buffers 4 
#property indicator_color1 Aqua //MACD.Above0.Up - Strong Up
#property indicator_color2 Red  //MACD.Below0.Down - Strong Down
#property indicator_color3 DodgerBlue //MACD.Above0.Dn - Weak Up
#property indicator_color4 Orange  //MACD.Below0.Up  - Weak mDn

extern string NOTE1 = " --- TimeFrame Settings ---";
extern string NOTETF = "Enter 0 to display current TF";
extern int TimeFrame = 0;
extern string NOTE2 = " --- Display Settings ---";
extern string NOTEVS = "Vertically positions indicator";
extern int VertShift = 1;

extern string NOTEMACD = " --- MACD Settings ---";
extern int MD_Fast = 12;
extern int MD_Slow = 26;
extern int MD_Sig = 9;
extern string NoteMACD1a = " -- Signal Lines = (True) || Histogram = (False) -- ";
extern bool UseSignalLines = False;

extern string NOTEDISPLAY = " --- Other Display options ---";
extern int MaxBars = 1000;
extern string DISPLAYFVS1 = "FineVertShift allows Time Frame label";
extern string DISPLAYFVS2 = "to be moved up and down";
extern double TFLabelVS = -0.6;
//colour of time frame label on far right of indicator
extern color TFTextColor = White;
extern color TFSameTFColor = Lime;
extern int TFTextFontSize = 6;

extern string NOTEALERTS = " --- Alerts ---";
extern bool AllowTextAlerts = false;
extern bool AllowSoundAlerts = false;
extern string LongSound = "alert3.wav";
extern string ShortSound = "alert4.wav";
extern bool AllowEmailAlerts = false;

//---- buffers
double bufStrongUp[], bufWeakUp[] ;
double bufStrongDn[], bufWeakDn[];

string ShortName = "";
string Prefix = "MTF-MACD";
int ArrowSize = 110;
int BarWidth = 0;
datetime LastTime = -1;
int TFrame, Window;
//Four constants used in one of the functions below
int MACD.Above0.Up = 1 ,MACD.Above0.Dn = 2 ,MACD.Below0.Dn = 3,MACD.Below0.Up = 4;

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

void init()
{  
  IndicatorBuffers(4);
  SetIndexStyle(0, DRAW_ARROW, 0, BarWidth);
  SetIndexStyle(1, DRAW_ARROW, 0, BarWidth);
  SetIndexStyle(2, DRAW_ARROW, 0, BarWidth);
  SetIndexStyle(3, DRAW_ARROW, 0, BarWidth);
  
  SetIndexArrow(0, ArrowSize);
  SetIndexArrow(1, ArrowSize);
  SetIndexArrow(2, ArrowSize);
  SetIndexArrow(3, ArrowSize);
  
  SetIndexBuffer(0, bufStrongUp);
  SetIndexBuffer(1, bufStrongDn);
  SetIndexBuffer(2, bufWeakUp);
  SetIndexBuffer(3, bufWeakDn);
    
  SetIndexEmptyValue(0, 0.0);
  SetIndexEmptyValue(1, 0.0);
  SetIndexEmptyValue(2, 0.0);
  SetIndexEmptyValue(3, 0.0);

  SetIndexLabel(0, NULL);
  SetIndexLabel(1, NULL);
  SetIndexLabel(2, NULL);
  SetIndexLabel(3, NULL);

  IndicatorDigits(0);
 
  //Verify Time Values entered by user
  TFrame = CheckTimeFrame(TimeFrame);
  
  //---- name for DataWindow and indicator subwindow label
  //VertShift = GetNextIndicatorNo(Prefix);
  ShortName = Prefix + VertShift;
  IndicatorShortName(ShortName);
}   

void deinit()
{
  int total = ObjectsTotal();  
  for (int i=total-1; i >= 0; i--) 
  {
    string name = ObjectName(i);
    if (StringFind(name, Prefix) == 0) ObjectDelete(name);
  }
}

void start()
{ 
   int i,tf,x,y=0;
   datetime TimeArray[];
   int MD;

   Window = WindowFind(ShortName);
   //This indicator is designed to display mutliple TFs in 1 indicator Window
   //When suqsequent indicators are dropped on the same Window
   //as a previous one WindowFind returns -1 ie can't find it.
   //This loop is to loop through the windows and find the window reference
   //The purpose behind all of this is to fix the problem of the TF labels
   //not being displayed for indicator 2,3,4 etc because of the Window reference
   if (Window == -1) 
   {
      for (i=0; i<20; i++)
      {
         x = WindowFind(Prefix + i);  
         if (x > 0)
         {
            Window = x;
            break;
         }
      }
   }   

   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if (counted_bars < 0) return(-1);
   //---- the last counted bar will be recounted
   if (counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
   limit = MathMin(limit, MaxBars);

  //-------------------------------1----------------------------------------   
  
   // Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
   for(i=0,y=0;i<limit;i++)
   {
      if (Time[i]<TimeArray[y]) y++;

      /***********************************************************   
         Add your main indicator loop below.  You can reference an existing
         function with its name or an indicator with its iName  or iCustom.
         Rule 1:  Add extern inputs above for all neccesary values   
         Rule 2:  Use 'TFrame' for the indicator time frame
         Rule 3:  Use 'y' for your indicator's shift value
       **********************************************************/  
      MD = GetMACD(TFrame,y);

      bufStrongUp[i] = 0;
      bufWeakUp[i] = 0;
      bufStrongDn[i] = 0;
      bufWeakDn[i] = 0;

      if (MD == MACD.Above0.Up) bufStrongUp[i] = VertShift;
      else if (MD == MACD.Above0.Dn) bufWeakUp[i] = VertShift;
      else if (MD == MACD.Below0.Dn) bufStrongDn[i] = VertShift;
      else if (MD == MACD.Below0.Up) bufWeakDn[i] = VertShift;
  }//End For Loop
  
  CreateTFLabel(TFrame);
 
  string MailSubject = "MTF MACD";
  string msg = "MTF MACD on " +Symbol()+ " " +(TimeFrame);
  
  int bar = 1;
  
  if (LastTime != Time[0])
  {
    //-> Lime
    if (bufStrongUp[bar] != 0 && bufStrongUp[bar+1] == 0)
    {
      msg = msg + " has changed to Strong Up Trend";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(LongSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }

    //<- Lime
    if (bufStrongUp[bar] == 0 && bufStrongUp[bar+1] != 0)
    {
      msg = msg + " has changed from Strong Up Trend";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(LongSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }

    //-> Red
    if (bufStrongDn[bar] != 0 && bufStrongDn[bar+1] == 0)
    {
      msg = msg + " has changed to Strong Down Trend";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(ShortSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }

    //<- Red
    if (bufStrongDn[bar] == 0 && bufStrongDn[bar+1] != 0)
    {
      msg = msg +  " has changed from Strong Down Trend";
      if (AllowTextAlerts) Alert(msg);
      if (AllowSoundAlerts) PlaySound(ShortSound);
      if (AllowEmailAlerts) SendMail(MailSubject, msg);
    }
    
    LastTime = Time[0];
  }
   

  return(0);
}
  
  
void CreateTFLabel(int TF)
{
   string txt = TF2Str(TF);
   if(UseSignalLines) txt = txt;
   double TimeDiff = Time[0]-Time[1];  
   string name = Prefix+VertShift+"_TF_"+txt;
   color labelcolor = TFTextColor;
   string fontname = "Arial";

   //If Tf = Chart Period change TF Label colour so that it can be easily identified
   if (TF == Period())
   {
      labelcolor = TFSameTFColor;
      fontname = "Arial Bold";
      TFLabelVS = -0.7;
   }
         
   if(VertShift<TFLabelVS) TFLabelVS = 0; //TFLabelVS = TF Label Vertical Shift
   
   if (ObjectFind(name) == -1)
   {  
     ObjectCreate(name, OBJ_TEXT, Window, iTime(NULL,0,0)+2*TimeDiff, VertShift-TFLabelVS);
     ObjectSetText(name, txt,TFTextFontSize,fontname, labelcolor);
   }
   else
     ObjectSet(name, OBJPROP_TIME1, iTime(NULL,0,0)+2*TimeDiff);
}

string TF2Str(int period) 
{
  switch (period) 
  {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN");
  }
  return (Period());
} 

int CheckTimeFrame(int TimeFrame)
{
   int result;

   if (TimeFrame == 0) 
      result = Period();   
   else
   {
      switch(TimeFrame) 
      {
         case 1    : result = PERIOD_M1;  break; 
         case 5    : result = PERIOD_M5;  break;
         case 15   : result = PERIOD_M15; break;
         case 30   : result = PERIOD_M30; break;
         case 60   : result = PERIOD_H1;  break;
         case 240  : result = PERIOD_H4;  break;
         case 1440 : result = PERIOD_D1;  break;
         case 7200 : result = PERIOD_W1;  break;
         case 28800: result = PERIOD_MN1; break;
         default  : result = Period(); break; //Error so return current TF
      }
   }
   return(result);
}

int GetMACD(int TF, int shift)
{
   if (UseSignalLines)
   {
      double MACDM=iMACD(NULL,TF,MD_Fast,MD_Slow,MD_Sig,PRICE_CLOSE,MODE_MAIN,shift); 
      double MACDSIG=iMACD(NULL,TF,MD_Fast,MD_Slow,MD_Sig,PRICE_CLOSE,MODE_SIGNAL,shift);    
      
      if (MACDM>0)
      {
         if (MACDM>MACDSIG) return(MACD.Above0.Up); 
         else return(MACD.Above0.Dn); //this is weak up default = Dodger Blue 
      }
      else //MACD < 0
      {
         if (MACDM<MACDSIG) return(MACD.Below0.Dn);
         else return(MACD.Below0.Up);
      }
   }
   else
   {
      double MDNow=iOsMA(NULL,TF,MD_Fast,MD_Slow,MD_Sig,PRICE_CLOSE,shift); 
      double MDPrev=iOsMA(NULL,TF,MD_Fast,MD_Slow,MD_Sig,PRICE_CLOSE,shift+1); 
      if (MDNow> 0)
      {
         if (MDNow>= MDPrev) return(MACD.Above0.Up);
         else return(MACD.Above0.Dn);
      }
      else
      {
         if (MDNow<= MDPrev) return(MACD.Below0.Dn);
         else return(MACD.Below0.Up);
      }
   }
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:

Implements a curve of type DRAW_ARROW


Indicators Used:

MACD Histogram
Moving Average of Oscillator


Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen
It plays sound alerts
It sends emails