Navigation:Home > Content >

##Contrast_alert-mtf.mq4

Time: 2016-06-13 | Download file:##Contrast_alert-mtf.mq4

#property indicator_separate_window
#property indicator_buffers 8

#property indicator_color1  clrLime
#property indicator_color2  clrRed
#property indicator_color3  clrLime
#property indicator_color4  clrRed
#property indicator_color5  clrLime
#property indicator_color6  clrRed
#property indicator_color7  clrLime
#property indicator_color8  clrRed
#property indicator_minimum 0
#property indicator_maximum 5

#define UniqueID "##Contrast-mtf"

input double coeff = 0;

input string TimeFrame1            = "Current time frame";
input string TimeFrame2            = "next1";
input string TimeFrame3            = "next2";
input string TimeFrame4            = "next3";
input int    LinesWidth            =  0;
input color  LabelsColor           = DarkGray;
input int    LabelsHorizontalShift = 3;
input double LabelsVerticalShift   = 1.6;

extern bool AlertsMessage = true; 
extern bool AlertsSound   = false;
extern bool AlertsEmail   = false;
extern bool AlertsMobile  = false;
extern int  SignalBar     = 0;

datetime TimeBarup,TimeBardn;

double sto1u[];
double sto1d[];
double sto2u[];
double sto2d[];
double sto3u[];
double sto3d[];
double sto4u[];
double sto4d[];

int    timeFrames[4];
bool   returnBars;
string indicatorFileName;

int init()
{
   SetIndexBuffer(0, sto1u);
   SetIndexBuffer(1, sto1d);
   SetIndexBuffer(2, sto2u);
   SetIndexBuffer(3, sto2d);
   SetIndexBuffer(4, sto3u);
   SetIndexBuffer(5, sto3d);
   SetIndexBuffer(6, sto4u);
   SetIndexBuffer(7, sto4d);

   indicatorFileName = WindowExpertName();
   returnBars        = (TimeFrame1=="returnBars");
   if(returnBars)
      return 0;
      
   for(int i=0 ; i<8 ; i++) 
   {
      SetIndexStyle(i, DRAW_ARROW, EMPTY, LinesWidth);
      SetIndexArrow(i, 110); 
   }
   
   timeFrames[0] = stringToTimeFrame(TimeFrame4);
   timeFrames[1] = stringToTimeFrame(TimeFrame3);
   timeFrames[2] = stringToTimeFrame(TimeFrame2);
   timeFrames[3] = stringToTimeFrame(TimeFrame1);
   
   IndicatorShortName(UniqueID);
   IndicatorDigits(0);
   
   TimeBarup=Time[0];
   TimeBardn=Time[0];
   
   return 0;
}

void OnDeinit(const int reason)
{
   for(int t=0 ; t<4 ; t++)
      ObjectDelete(UniqueID+t);
}

double trend[][2];
#define _up 0
#define _dn 1

int start()
{
   int i, r, 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)
   {
      sto1u[0] = limit+1;
      return 0;
   }

   if(timeFrames[0] != Period())
      limit = MathMax(limit, MathMin(Bars-1, iCustom(NULL, timeFrames[0], indicatorFileName, coeff, "returnBars", 0, 0) * timeFrames[0] / Period()));

   if(timeFrames[1] != Period())
      limit = MathMax(limit, MathMin(Bars-1, iCustom(NULL, timeFrames[1], indicatorFileName, coeff, "returnBars", 0, 0) * timeFrames[1] / Period()));

   if(timeFrames[2] != Period())
      limit = MathMax(limit, MathMin(Bars-1, iCustom(NULL, timeFrames[2], indicatorFileName, coeff, "returnBars", 0, 0) * timeFrames[2] / Period()));

   if(timeFrames[3] != Period())
      limit = MathMax(limit, MathMin(Bars-1, iCustom(NULL, timeFrames[3], indicatorFileName, coeff, "returnBars", 0, 0) * timeFrames[3] / Period()));

   if(ArrayRange(trend, 0)!=Bars)
      ArrayResize(trend, Bars);

   int window = WindowFind(UniqueID);
   for(int t=0 ; t<4 ; t++)
   {
      string label = timeFrameToString(timeFrames[t]);
      ObjectCreate(UniqueID+t, OBJ_TEXT, window, 0, 0);
      ObjectSet(UniqueID+t, OBJPROP_COLOR, LabelsColor);
      ObjectSet(UniqueID+t, OBJPROP_PRICE1, t+LabelsVerticalShift);
      ObjectSetText(UniqueID+t, label, 7, "Arial");
   }               
   
   for(t=0 ; t<4 ; t++)
      ObjectSet(UniqueID+t, OBJPROP_TIME1, Time[0]+Period()*LabelsHorizontalShift*60);

   for(i=limit, r=Bars-i-1 ; i>=0 ; i--,r++)
   {
      trend[r][_up] = 0;
      trend[r][_dn] = 0;
      for(int k=0 ; k<4 ; k++)
      {
         int y = iBarShift(NULL, timeFrames[k], Time[i]);
         double stochm = iCustom(NULL, timeFrames[k], "##Contrast", coeff, 1, y);
         double stochs = iCustom(NULL, timeFrames[k], "##Contrast", coeff, 0, y);

         bool isUp = (stochm>stochs);
         switch(k)
         {
            case 0: if(isUp) { sto1u[i] = k+1; sto1d[i] = EMPTY_VALUE;}  else { sto1d[i] = k+1; sto1u[i] = EMPTY_VALUE; } break;
            case 1: if(isUp) { sto2u[i] = k+1; sto2d[i] = EMPTY_VALUE;}  else { sto2d[i] = k+1; sto2u[i] = EMPTY_VALUE; } break;
            case 2: if(isUp) { sto3u[i] = k+1; sto3d[i] = EMPTY_VALUE;}  else { sto3d[i] = k+1; sto3u[i] = EMPTY_VALUE; } break;
            case 3: if(isUp) { sto4u[i] = k+1; sto4d[i] = EMPTY_VALUE;}  else { sto4d[i] = k+1; sto4u[i] = EMPTY_VALUE; } break;
         }

         if (isUp)
            trend[r][_up] += 1;
         else
            trend[r][_dn] += 1;
      }
   }
//-------------------------------------------------------------------+   
 if(AlertsMessage || AlertsSound || AlertsEmail || AlertsMobile)
  { 
   bool sigup=false,sigdn=false; 
   
   string message1 = (WindowExpertName()+" - "+Symbol()+"  "+timeFrameToString(Period())+" - Signal Up");
   string message2 = (WindowExpertName()+" - "+Symbol()+"  "+timeFrameToString(Period())+" - Signal Dn");
       
    if(TimeBarup!=Time[0] && sto3u[SignalBar]==3 && sto4u[SignalBar]==4 && !sigup)
     { 
        if (AlertsMessage) Alert(message1);
        if (AlertsSound)   PlaySound("alert2.wav");
        if (AlertsEmail)   SendMail(Symbol()+" - "+WindowExpertName()+" - ",message1);
        if (AlertsMobile)  SendNotification(message1);
        TimeBarup=Time[0];
        sigup = true;
     }
    if(TimeBardn!=Time[0] && sto3d[SignalBar]==3 && sto4d[SignalBar]==4 && !sigdn)
     { 
        if (AlertsMessage) Alert(message2);
        if (AlertsSound)   PlaySound("alert2.wav");
        if (AlertsEmail)   SendMail(Symbol()+" - "+WindowExpertName()+" - ",message2);
        if (AlertsMobile)  SendNotification(message2);
        TimeBardn=Time[0];
        sigdn = true;
    }
   if(sigup && (sto3u[SignalBar]!=3 || sto4u[SignalBar]!=4)) sigup = false;
   if(sigdn && (sto3d[SignalBar]!=3 || sto4d[SignalBar]!=4)) sigdn = false;
  }
//-------------------------------------------------------------------+
   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 toInt(double value)
{
   return(value);
}

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);

   int max = ArraySize(iTfTable)-1;

   int add = 0;

   int nxt;

   nxt = (StringFind(tfs, "NEXT1")>-1);
   if(nxt>0)
   {
      tfs = ""+Period();
      add = 1;
   }

   nxt = (StringFind(tfs, "NEXT2")>-1);
   if(nxt>0)
   {
      tfs = ""+Period();
      add=2;
   }
   
   nxt = (StringFind(tfs, "NEXT3")>-1);
   if(nxt>0)
   {
      tfs = ""+Period();
      add=3;
   }

   for(int i=max ; i>=0 ; i--)
      if(tfs==sTfTable[i] || tfs==""+iTfTable[i])
         return MathMax(iTfTable[toInt(MathMin(max, i+add))], 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 chr = StringGetChar(s, length);

      if((chr>96 && chr<123) || (chr>223 && chr<256))
         s = StringSetChar(s, length, chr-32);
      else
         if(chr>-33 && chr<0)
            s = StringSetChar(s, length, chr+224);
   }

   return s;
}

Recommend