Archivio blog

sabato 15 marzo 2014

CrossOverAdvisor_Mql4


//+------------------------------------------------------------------+
//|                                                 CrossAdvisor.mq4 |
//|                              Copyright 2013, Riccardo Placitelli |
//|                            http://forexsmartsharing.blogspot.it/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Riccardo Placitelli"
#property link      "http://forexsmartsharing.blogspot.it/"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrGreen
#property indicator_color2 clrRed
#property description "Questo indicatore fornisce alert grafici in corrispondenza"
#property description "rispettivamente di un incrocio positivo/negativo tra 2 Medie."
#property description "Per un corretto funzionamento si consiglia di settare i parametri"
#property description "dell'indicatore in modo coerente con le Medie applicate al grafico."
//---
input bool SoundON=true;
input bool MailON=false;
//---
input int FastMA_Mode=1;
input int FastMA_Period=21;
input int FastMA_Price=0;
input int SlowMA_Mode=1;
input int SlowMA_Period=100;
input int SlowMA_Price=0;
//---
double CrossUp[];
double CrossDown[];
//---
int flagval1=0;
int flagval2=0;
//---
datetime CurTime=TimeCurrent();
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexStyle(0,DRAW_ARROW,EMPTY,3);
   SetIndexArrow(0,233);
   SetIndexBuffer(0,CrossUp);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,3);
   SetIndexArrow(1,234);
   SetIndexBuffer(1,CrossDown);
//---
   GlobalVariableSet("AlertTime"+Symbol()+Period(),CurTime);
   GlobalVariableSet("SignalType"+Symbol()+Period(),OP_SELLSTOP);
//---   
   return(INIT_SUCCEEDED);
   }
   
//---- Deinit
int deinit()
  {
//---- 
   GlobalVariableDel("AlertTime"+Symbol()+Period());
   GlobalVariableDel("SignalType"+Symbol()+Period());
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//| LSMA with PriceMode                                              |
//| PrMode  0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2,    |
//| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4  |
//+------------------------------------------------------------------+
double LSMA(int RPeriod, int PrMode, int shift)
   {
   int i;
   double sum,pr;
   int length;
   double lengthvar;
   double tmp;
   double wt;
   //--- 
   length=RPeriod;
   //---
   sum=0;
   for(i=length; i>=1;i--)
     {
      lengthvar=length+1;
      lengthvar/=3;
      tmp=0;
      switch(PrMode)
        {
         case 0: pr=Close[length-i+shift];break;
         case 1: pr=Open[length-i+shift];break;
         case 2: pr=High[length-i+shift];break;
         case 3: pr=Low[length-i+shift];break;
         case 4: pr=(High[length-i+shift] + Low[length-i+shift])/2;break;
         case 5: pr=(High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift])/3;break;
         case 6: pr=(High[length-i+shift] + Low[length-i+shift] + Close[length-i+shift] + Close[length-i+shift])/4;break;
        }
      tmp =(i - lengthvar)*pr;
      sum+=tmp;
     }
   wt=sum*6/(length*(length+1));
   //---
   return(wt);
   }
  
//---
//+------------------------------------------------------------------+
//| Custom indicator Start function                                  |
//+------------------------------------------------------------------+
int OnStart()
   {
   int i, counter;
   double tmp=0;
   double FastMANow,SlowMANow,FastMAPrevious,SlowMAPrevious;
   double Range,AvgRange;
   //---
   int counted_bars=IndicatorCounted();
   if(counted_bars<0)return(-1);
   if(counted_bars>0)counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0)limit-=1+9;
   //---
   for(i=0; i<=limit;i++)
      {
      counter=i;
      Range=0;
      AvgRange=0;
         for(counter=i; counter<=1+9; counter++)
            {
            AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
            }
      Range=AvgRange/10;
      //---
      if(FastMA_Mode==4)
         {
         FastMANow=LSMA(FastMA_Period,FastMA_Price,i);
         FastMAPrevious=LSMA(FastMA_Period,FastMA_Price,i+1);
         }
         else 
         {
         FastMANow=iMA(NULL,0,FastMA_Period,0,FastMA_Mode,FastMA_Price,i);
         FastMAPrevious=iMA(NULL,0,FastMA_Period,0,FastMA_Mode,FastMA_Price,i+1);
         }
      if(SlowMA_Mode==4)
         {
         SlowMANow=LSMA(SlowMA_Period,SlowMA_Price,i);
         SlowMAPrevious=LSMA(SlowMA_Period,SlowMA_Price,i+1);
         }
         else 
         {
         SlowMANow=iMA(NULL,0,SlowMA_Period,0,SlowMA_Mode,SlowMA_Price,i);
         SlowMAPrevious=iMA(NULL,0,SlowMA_Period,0,SlowMA_Mode,SlowMA_Price,i+1);
         }
         //--- condition to alert
         if(FastMANow>SlowMANow && FastMAPrevious<SlowMAPrevious)
            {
               if(i==1 && flagval1==0) {flagval1=1;flagval2=0;}
               CrossUp[i]=Low[i]-Range*0.75;
            }
            else
         if(FastMANow<SlowMANow && FastMAPrevious>SlowMAPrevious)
            {
               if(i==1 && flagval2==0) {flagval2=1;flagval1=0;}
               CrossDown[i]=High[i]+Range*0.75;
            }         
      }
      if(flagval1==1 && CurTime>GlobalVariableGet("AlertTime"+Symbol()+Period()) && GlobalVariableGet("SignalType"+Symbol()+Period())!=OP_BUY)
         {
         if(SoundON) Alert("BuySignal @ Ask=",Ask,"\n Bid=",Bid,"\n Time",TimeToStr(CurTime,TIME_DATE)," ",TimeHour(CurTime),":",TimeMinute(CurTime),"\n Symbol=",Symbol()," Period=",Period());        
         if(MailON) SendMail("Buy Signal Alert","BuySignal @ Ask= "+DoubleToStr(Ask,4)+"\n Bid= "+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime,TIME_DATE)+" "+TimeHour(CurTime)+":"+TimeMinute(CurTime)+" Symbol="+Symbol()+" Period="+Period());
         //---
         tmp=CurTime+(Period()-MathMod(Minute(),Period()))*60;
         GlobalVariableSet("AlertTime"+Symbol()+Period(),tmp);
         GlobalVariableSet("SignalType"+Symbol()+Period(),OP_SELL);
         }
      if(flagval2==1 && CurTime>GlobalVariableGet("AlertTime"+Symbol()+Period()) && GlobalVariableGet("SignalType"+Symbol()+Period())!=OP_SELL) 
         {
         if(SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime,TIME_DATE)," ",TimeHour(CurTime),":",TimeMinute(CurTime),"\n Symbol=",Symbol()," Period=",Period());
         if(MailON) SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime,TIME_DATE)+" "+TimeHour(CurTime)+":"+TimeMinute(CurTime)+" Symbol="+Symbol()+" Period="+Period());
         //---
         tmp=CurTime+(Period()-MathMod(Minute(),Period()))*60;
         GlobalVariableSet("AlertTime"+Symbol()+Period(),tmp);
         GlobalVariableSet("SignalType"+Symbol()+Period(),OP_BUY);
         }
      return(0);
   }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
   {
//--- Get the number of bars available for the current symbol and chart period
   int bars=Bars(Symbol(),0);
   Print("Bars = ",bars,", rates_total = ",rates_total,",  prev_calculated = ",prev_calculated);
   Print("time[0] = ",time[0]," time[rates_total-1] = ",time[rates_total-1]);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Nessun commento:

Posta un commento

condividi la tua opinione...