//+------------------------------------------------------------------+
//|                                                     Laguerre.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright " "
#property link      " "

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 LimeGreen
#property indicator_color2 Crimson
#property indicator_color3 LimeGreen
#property indicator_color4 Crimson
#property indicator_width3 1
#property indicator_width4 1
  //Value 4 -sell
  //Value 3 -buy

#include <WinUser32.mqh>
//---- input parameters
extern int       FastEMA=21;
extern int       SlowEMA=210;
extern int       RSIPeriod=21;
extern bool      Alerts=true;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

//---- variables
int sigCurrent=0;
int sigPrevious=0;
double pipdiffCurrent=0;
double pipdiffPrevious=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexEmptyValue(3,0.0);



//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  
datetime prevtime = 0;  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   double rsi_sig=0;
   bool entry=false;
   double entry_point=0;
   
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   //---- main loop
   for(int i=0; i<limit; i++)
   {
     //---- ma_shift set to 0 because SetIndexShift called abowe
     ExtMapBuffer1[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i);
     ExtMapBuffer2[i]=iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
     rsi_sig = iRSI(NULL, 0, RSIPeriod, PRICE_CLOSE, i);
     
     pipdiffCurrent=(ExtMapBuffer1[i]-ExtMapBuffer2[i]);

     Comment("pipdiffCurrent = "+pipdiffCurrent+" ");
     if (pipdiffCurrent>0 && rsi_sig>50) 
     {
       sigCurrent = 1;  //?????µN?N?

     }
     else if (pipdiffCurrent<0 && rsi_sig<50)
     {
       sigCurrent = 2;  //???????·

     }

     if (sigCurrent==1 && sigPrevious==2)
     {
        ExtMapBuffer4[i] = High[i]-5*Point;
        
        entry=true;
        entry_point=Ask; //?·???µN?N? ??????N??????°
        
     } 
     else if (sigCurrent==2 && sigPrevious==1)
     {
        ExtMapBuffer3[i] = Low[i]-5*Point;
        
        entry=true;
        entry_point=Bid; //?·???µN?N? ??N??????°?¶?°
        
     }

   
     sigPrevious=sigCurrent;
     pipdiffPrevious=pipdiffCurrent;
     
   }

   if (prevtime != Time[0] && Alerts) {
      prevtime = Time[0];

      if (ExtMapBuffer3[1] > 0) {
         PlaySound("expert.wav");
         MessageBox("Entry point: buy at "+entry_point+"!!", "Entry Point", MB_OK);
         Print("Laguerre: buy signal");
      } else if (ExtMapBuffer4[1] > 0) {
         PlaySound("expert.wav");
         MessageBox("Entry point: sell at "+entry_point+"!!", "Entry Point", MB_OK);
         Print("Laguerre: sell signal");
      }
   }

//----
   return(0);
  }
//+------------------------------------------------------------------+