//+------------------------------------------------------------------+
//|                                                      MTF_RSX.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright ""
#property link      "http://www.forex-tsd.com/indicators-metatrader-4/18585-2-qqe-combined-anchored-need-help.html#post264993"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color2 Yellow
#property indicator_style2 STYLE_DOT

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
**************************************************************************/
extern int       TimeFrame      = 0;
extern int       SF             = 5;     // original 5
extern int       RSI_Period     = 14;    // original 14
extern double    DARFACTOR      = 4.236;   //original 4.236
extern int       mamode         = 3;     // 0=sma,1=ema,2=ssma,3=lwma
extern int       maxBars        = 1500;

extern string    _arrows        = "arrows if true"; 
extern bool      UseARR         = true;
extern color     arrUPclr       = Lime;
extern color     arrDNclr       = Red;
extern int       UpArrowSize    = 2;
extern int       DnArrowSize    = 2;

extern string    note17         = "turn on Alert = true; turn off = false";
extern bool      alertOn        = true;
extern string    SoundFileName  = "alert.wav";


extern string    note13         = "turn on soundAlert = true; turn off = false";
extern bool      SoundON        = true;

extern string    note15         = "send Email Alert = true; turn off = false";
extern bool      EmailON        = false;


double ExtMapBuffer1[];
double ExtMapBuffer2[];
int    flagval1 = 0;
int    flagval2 = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {

//---- indicator line
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(0,RSI_Period);
   SetIndexDrawBegin(1,RSI_Period);

  // SetIndexArrow(0,159);
//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN"; break;
      default : TimeFrameStr="TF0";
   }
   IndicatorShortName("QQE ["+TimeFrameStr+"] ("+SF+","+RSI_Period+","+DARFACTOR+") ");   
  }
//----  
  return(0);
//----
int deinit()
  {
int obj=ObjectsTotal(); 
for(int i=obj-1; i>=0; i--) 
{ 
   string name=ObjectName(i);
   if(StringFind(name,"qqeDn_")!=-1) 
   ObjectDelete(name);
   if(StringFind(name,"qqeUp_")!=-1) 
   ObjectDelete(name); 
}  
//----
   return(0);
  } 
//+------------------------------------------------------------------+
//| MTF rsx                                         |
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,limit,PrevBar,bloks,blokb,y=0,counted_bars=IndicatorCounted();
 
// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
   limit=Bars-counted_bars+TimeFrame/Period();
   limit= MathMin(limit,maxBars);
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++;

/***********************************************************   
   Add your main indicator loop below.  You can add the full
      indicator code or you can just reference an existing
      indicator with its iValue or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator time frame
   Rule 3:  Use 'y' for your indicator's shift value
 **********************************************************/ 
 
   ExtMapBuffer1[i]=iCustom(NULL,TimeFrame,"QQEA",SF,RSI_Period,DARFACTOR,mamode,0,y) ; 
   ExtMapBuffer2[i]=iCustom(NULL,TimeFrame,"QQEA",SF,RSI_Period,DARFACTOR,mamode,1,y) ;
   }  
if (TimeFrame>Period()) 
   {
     int PerINT=TimeFrame/Period()+1;
     datetime TimeArr[]; 
     ArrayResize(TimeArr,PerINT);
     ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period()); 
     for(i=0;i<PerINT+1;i++) 
     {
     if (TimeArr[i]>=TimeArray[0]) 
     {
      ExtMapBuffer1[i]=ExtMapBuffer1[0]; 
      ExtMapBuffer2[i]=ExtMapBuffer2[0]; 
   } } }
   RefreshRates();
   for(i=limit; i>=0; i--)
   {
   if (alertOn)
    {
    if (ExtMapBuffer1[0]<ExtMapBuffer2[0] && ExtMapBuffer1[1]>ExtMapBuffer2[1])
    {   
    if (i == 1 && flagval1==0)
    {
    flagval1=1;
    flagval2=0;
    Alert("QQE SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",TimeFrame);
    if (EmailON) SendMail("QQE 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="+TimeFrame);
    if (SoundON) PlaySound(SoundFileName);
    
    }
    }
    else if (ExtMapBuffer1[0]>ExtMapBuffer2[0] && ExtMapBuffer1[1]<ExtMapBuffer2[1])
    {  
    if (i == 1 && flagval2==0)
    {
    flagval2=1;
    flagval1=0;
    Alert("QQE BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",TimeFrame);
    if (EmailON) SendMail("QQE BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+TimeFrame);
    if (SoundON) PlaySound(SoundFileName);
   
    }}} } 
   
     RefreshRates();
    for(i=limit; i>=0; i--)
    {
    if(UseARR && PrevBar!=Bars)
    {
    if (ExtMapBuffer1[i]<ExtMapBuffer2[i] && bloks==0)
    {   bloks=1; blokb=0;
        string ArrowDown="qqeDn_"+i;
        ObjectCreate(ArrowDown,OBJ_ARROW,0,Time[i],High[i]+iATR(NULL,0,5,i));
        ObjectSet(ArrowDown,OBJPROP_ARROWCODE, 242); 
        ObjectSet(ArrowDown,OBJPROP_WIDTH,DnArrowSize);
        ObjectSet(ArrowDown,OBJPROP_COLOR,arrDNclr);
    }
    if (ExtMapBuffer1[i]>ExtMapBuffer2[i] && blokb==0)
    {   bloks=0; blokb=1;
        string ArrowUp="qqeUp_"+i;
        ObjectCreate(ArrowUp,OBJ_ARROW,0,Time[i],Low[i]-iATR(NULL,0,5,i));
        ObjectSet(ArrowUp,OBJPROP_ARROWCODE,241);
        ObjectSet(ArrowDown,OBJPROP_WIDTH,UpArrowSize);
        ObjectSet(ArrowUp,OBJPROP_COLOR,arrUPclr);
    }
    }
    
    }
    
   return(0);
  }
//+------------------------------------------------------------------+