Navigation´╝ÜHome > Content >

macd_osmax_mtf.mq4

Time: 2016-12-15 | Download file:macd_osmax_mtf.mq4

#property indicator_separate_window
#property indicator_buffers    6
#property indicator_color1     Magenta
#property indicator_width1     2
#property indicator_color2     Aqua
#property indicator_width2     2
#property indicator_color3     Yellow
#property indicator_width3     2
#property indicator_color4     Silver 
#property indicator_width4     1
#property indicator_color5     Red
#property indicator_width5     1
#property indicator_color6     DodgerBlue
#property indicator_width6     2
#property indicator_level1     0
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor MediumOrchid

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    FastMa          = 12;
extern int    FastMaMode      = MODE_EMA;
extern int    SlowMa          = 26;
extern int    SlowMaMode      = MODE_EMA;
extern int    SignalMa        = 9;
extern int    SignalMaMode    = MODE_SMA;
extern int    OsmaX           = 2;

extern string _               = "alerts settings";
extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double osmaNu[];
double osmaDn[]; 
double osmaUp[]; 
double osma[];
double signal[];
double macd[];
double trend[];
double slope[];

//
//
//
//
//

string indicatorFileName;
int    timeFrame;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{

   IndicatorBuffers(8);
   SetIndexBuffer(0,osmaDn); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,osmaUp); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,osmaNu); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,osma);
   SetIndexBuffer(4,signal);
   SetIndexBuffer(5,macd);
   SetIndexBuffer(6,trend);
   SetIndexBuffer(7,slope);
      FastMa   = MathMax(FastMa,1);
      SlowMa   = MathMax(SlowMa,1);
      SignalMa = MathMax(SignalMa,1);

      //
      //
      //
      //
      //
      //
   
      indicatorFileName = WindowExpertName();
      returnBars        = (TimeFrame == "returnBars");     if (returnBars)     return(0);
      calculateValue    = (TimeFrame == "calculateValue"); if (calculateValue) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);   

      //
      //
      //
      //
      //
   
  IndicatorShortName(timeFrameToString(timeFrame)+"  osma macd("+FastMa+","+SlowMa+","+SignalMa+")");
return(0);
}

int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { osmaDn[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame == Period())
   {
        for(int i=limit; i>=0; i--) macd[i]   = iMA(NULL,0,FastMa,0,FastMaMode,PRICE_CLOSE,i) - iMA(NULL,0,SlowMa,0,SlowMaMode,PRICE_CLOSE,i);
        for(    i=limit; i>=0; i--) signal[i] = iMAOnArray(macd,Bars,SignalMa,0,SignalMaMode,i);
        for(    i=limit; i>=0; i--) 
        {
             osma[i]   = (macd[i] - signal[i]) * OsmaX;
             osmaDn[i] = EMPTY_VALUE;
             osmaUp[i] = EMPTY_VALUE;
             osmaNu[i] = EMPTY_VALUE;
             trend[i]  = trend[i+1];
             slope[i]  = slope[i+1];
             
             //
             //
             //
             //
             //
             
             if (macd[i] > macd[i+1]) trend[i] =  1;
             if (macd[i] < macd[i+1]) trend[i] = -1;
             if (osma[i] > osma[i+1]) slope[i] =  1;
             if (osma[i] < osma[i+1]) slope[i] = -1;
                
             if (trend[i]== 1 && slope[i] == 1) osmaUp[i] = osma[i];
             if (trend[i]== 1 && slope[i] ==-1) osmaNu[i] = osma[i];
             if (trend[i]==-1 && slope[i] ==-1) osmaDn[i] = osma[i];
             if (trend[i]==-1 && slope[i] == 1) osmaNu[i] = osma[i];
               
      }
      manageAlerts();
      return(0);
      } 
      
      //
      //
      //
      //
      //
      
      limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
         for(i=limit; i>=0; i--)
         {
            int y = iBarShift(NULL,timeFrame,Time[i]);  
             osma[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,3,y);
             signal[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,4,y);
             macd[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,5,y);
             trend[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,6,y);
             slope[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,FastMaMode,SlowMa,SlowMaMode,SignalMa,SignalMaMode,7,y);
             osmaDn[i] = EMPTY_VALUE;
             osmaUp[i] = EMPTY_VALUE;
             osmaNu[i] = EMPTY_VALUE;
      
         }
         
         for(i=limit;i>=0;i--)
         {
             if (trend[i]== 1 && slope[i] == 1) osmaUp[i] = osma[i];
             if (trend[i]== 1 && slope[i] ==-1) osmaNu[i] = osma[i];
             if (trend[i]==-1 && slope[i] ==-1) osmaDn[i] = osma[i];
             if (trend[i]==-1 && slope[i] == 1) osmaNu[i] = osma[i];   
                               
   }
   
   //
   //
   //
   //
   //
   
   manageAlerts();
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

//
//
//
//
//

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      
      //
      //
      //
      //
      //
      
      
      if (slope[whichBar] != slope[whichBar+1] && trend[whichBar] != trend[whichBar+1])
      {
      if (slope[whichBar] == 1 && trend[whichBar] == 1) doAlert(whichBar,"buy");
      if (slope[whichBar] ==-1 && trend[whichBar] ==-1) doAlert(whichBar,"sell");
      }         
     
    } 
            
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," osmax ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," osmax"),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

Recommend