Navigation:Home > Content >

LabTrend1a.mq4

Time: 2016-09-24 | Download file:LabTrend1a.mq4

//+------------------------------------------------------------------+
//|                                                 LabTrend1_v4.mq4 |
//|                           Copyright © 2008, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"



#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 LimeGreen
#property indicator_color4 Red
#property indicator_color5 LimeGreen
#property indicator_color6 Red
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2





extern double  Risk           = 3;       //Price Channel narrowing factor (1..10)  
extern int     Signal         = 1;       //Display signals mode
extern int     ColorBar       = 1;       //Display color bars mode: 0-no,1-yes 
extern int     SoundAlertMode = 1;       //Sound Alert switch 
extern string  timeFrame      = "Current time frame";
extern string  atrtimeFrame   = "Current time frame"; 






double UpTrendSignal[];
double DownTrendSignal[];
double UpTrendBar[];
double DownTrendBar[];
double bsmax[];
double bsmin[];
double time[];
double trend[];



int    Length;
bool   UpTrendAlert=false;
bool   DownTrendAlert=false;
int    TimeFrame;
int    atrTimeFrame;





int init()
{
   

   IndicatorBuffers(8); 
   SetIndexBuffer(0,UpTrendSignal);   SetIndexStyle(0,DRAW_ARROW);    SetIndexArrow(0,108); SetIndexLabel(0,"UpTrend Signal");
   SetIndexBuffer(1,DownTrendSignal); SetIndexStyle(1,DRAW_ARROW);    SetIndexArrow(1,108); SetIndexLabel(1,"DownTrend Signal");
   SetIndexBuffer(2,UpTrendBar);      SetIndexStyle(2,DRAW_HISTOGRAM);                      SetIndexLabel(2,"UpTrend Bar");
   SetIndexBuffer(3,DownTrendBar);    SetIndexStyle(3,DRAW_HISTOGRAM);                      SetIndexLabel(3,"DownTrend Bar");
   SetIndexBuffer(4,bsmax);
   SetIndexBuffer(5,bsmin);
   SetIndexBuffer(6,time);
   SetIndexBuffer(7,trend);
   
   IndicatorShortName(stringToTimeFrame(timeFrame)+" LabTrend1 ");
   
   TimeFrame    = stringToTimeFrame(timeFrame);
   atrTimeFrame = stringToTimeFrame(atrtimeFrame);
   Length       = 3 + 2*Risk;  
   
   return(0);
}

int deinit() { return(0); }



int start()
{
   
  
   double high, low, sum, UpBar,DnBar;
   int counted_bars=IndicatorCounted();
   int limit,i;
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
      limit=MathMax(MathMin(Bars-counted_bars,Bars-1),TimeFrame/Period());   
      

   
   
      for (i=limit; i>=0; i--)
      {
      int y                     = iBarShift(NULL,TimeFrame   ,Time[i]);
      int r                     = iBarShift(NULL,atrTimeFrame,Time[i]);
      double smin               = iLow(NULL, TimeFrame,iLowest(NULL, TimeFrame,MODE_LOW, Length,y));         
      double smax               = iHigh(NULL,TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH,Length,y)); 
             time[i]            = (iTime(NULL,TimeFrame,y)+TimeFrame*60);            
             bsmax[i]           = smax-(smax-smin)*(33.0-Risk)/100.0;	
             bsmin[i]           = smin+(smax-smin)*(33.0-Risk)/100.0;
             UpTrendSignal[i]   = iLow(NULL,TimeFrame,y)  - 0.5*iATR(NULL,atrTimeFrame,10,r);
             DownTrendSignal[i] = iHigh(NULL,TimeFrame,y) + 0.5*iATR(NULL,atrTimeFrame,10,r);
      
   
      trend[i] = trend[i+1];  
      if(trend[i+1]<=0 && Close[i]>bsmax[i] && time[i]==(Time[i]+Period()*60))  trend[i] = 1; 
      if(trend[i+1]>=0 && Close[i]0) 
      {
         if (Signal>0)
         {
         if(trend[i+1]<0)
         {
	      if (SoundAlertMode>0 && i==0) PlaySound("alert2.wav");      	        
	      }
	      else
	      UpTrendSignal[i]=EMPTY_VALUE;
	      }
	      	  
	    if(ColorBar>0)
	    {
            if(Close[i]>UpBar)
	         {
	         UpTrendBar[i]  = High[i];
	         DownTrendBar[i]= Low[i];
	         }
	         else
	         {
	         UpTrendBar[i]  = EMPTY_VALUE;
	         DownTrendBar[i]= EMPTY_VALUE;
	         }
         }   
	   }
      else
      UpTrendSignal[i]=EMPTY_VALUE;
      	  
      if (trend[i]<0) 
	   {
         if (Signal > 0) 
         {   
            if(trend[i+1]>0)
	         {
	         if (SoundAlertMode>0 && i==0) PlaySound("alert2.wav");
	         }
	         else
	         DownTrendSignal[i]=EMPTY_VALUE;
	      }
	              
         if(ColorBar>0)
	      {
            if(Close[i]0 && Volume[0]>1 && !UpTrendAlert)
	{
	Message = " "+Symbol()+" M"+Period()+": Signal for BUY";
	if ( SoundAlertMode>0 ) Alert (Message); 
	UpTrendAlert=true; DownTrendAlert=false;
	} 
	 	  
	if ( trend[2]>0 && trend[1]<0 && Volume[0]>1 && !DownTrendAlert)
	{
	Message = " "+Symbol()+" M"+Period()+": Signal for SELL";
	if ( SoundAlertMode>0 ) Alert (Message); 
	DownTrendAlert=true; UpTrendAlert=false;
	} 	         

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



int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf= 0)
      {
         char = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                  s = StringSetChar(s, lenght, char - 32);
          else 
              if(char > -33 && char < 0)
                  s = StringSetChar(s, lenght, char + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}

Recommend