//+------------------------------------------------------------------+
//|                                                Arrows_Magic_Buy  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Mod Pacois"//Mod Pacois
#property link      ""//Mod Pacois

#property indicator_separate_window
#property indicator_buffers 4 
#property indicator_color1  Lime
#property indicator_color2  Red
#property indicator_color3  Gray
#property indicator_color4  clrNONE
#property indicator_level1 0
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
//#property indicator_maximum 1.1
//#property indicator_minimum -1.1

 int key = 8763765;
 double Smooth = 1.0;
int bars = 300;
 int X_Filtr = 20;
int Progression = 10;
 int Len = 12;
 extern int BarBollinger=5;
 int SensitivityUp = 0;
 int SensitivityDn = 0;
 bool tma2_nrp=false; 
extern string  ______________SetAlert;
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = false;
extern bool   alertsSound      = true;
extern bool   alertsNotify     = false;
extern bool   alertsEmail      = false;
extern string soundFile        = "alert2.wav";

extern string  _____________SetArrows;
extern bool   arrowsVisible    = true;
extern bool   LineVisible      = false;
extern string arrowsIdentifier = "Arrows_Magic2";
extern double arrowsDisplaceUp = 1.0;
extern double arrowsDisplaceDn = 0.5;
extern color  arrowsUpColor    = Magenta;
extern color  arrowsDnColor    = Yellow;
extern int    arrowsUpCode     = 233;
extern int    arrowsDnCode     = 234;
extern int    arrowsUpSize     = 1;
extern int    arrowsDnSize     = 1;

extern bool   HistoPrice          = false;
extern bool   HistoRsi           = false;
extern bool   HistoTma           = false;
extern bool   HistoPrice2        = false;



 ENUM_TIMEFRAMES    timeFrame        = 0;

/*
extern int    SSAPrice                =  PRICE_CLOSE;
extern int    SSAPrice2               =  PRICE_OPEN;
extern int    SSALag                  = 20;
extern int    SSANumberOfComputations = 2;
extern int    SSAPeriodNormalization  = 20;
extern int    SSANumberOfBars         = 1000;
extern int    DeviationsPeriod        = 20;
extern double DeviationsMultiplier    = 500.0;
extern double DeviationsTreshold      = 0.009;
extern double             Level1      = 0.5;         
extern double             Level2      = 0.25;  
extern double             Level3      = -0.5;         
extern double             Level4      = -0.25; 
extern string ___________ArrowUp;

extern string ___________Arrows_alert_set;


extern bool   Histo1            = false;
extern bool   Histo2            = false;
extern bool   Histo3            = false;
extern bool   Histo4           = false;
extern bool   Smoth            = true;
extern bool   TmaGhost         = false;
extern bool   TmaFast          = true;
extern bool   TmaFastLow       = false;

extern bool   TmaFastLow_Low      = false;
extern bool   UltimateMa        = false;
extern bool   TmaFastLowYellow      = false;
extern bool   CloseEmaOpen     = false;
extern bool   CloseEmaLow      = false;

extern bool   AdxOn_Off        = false;
extern bool   CCI    = false;
extern bool   RSI    = false;
extern bool   PipsOn_Off    = false;
extern bool   SureRange_On_Off    = false;
extern bool   Sure_On_Off    = false;
extern bool   Sure2_On_Off    = false;
extern bool   VQ7    = false;
extern bool   Detrend   = false;
extern bool   Extreme2    = false;
extern bool   Noise_filter   =false;
extern bool   Noise_filter2   =false;
extern bool   AngleTma   = false;
extern bool   Ema  = false;
extern bool   Ema2  = true;
extern bool   Ema2_2  = FALSE;
extern bool   atr  = FALSE;
extern bool   Ema3  = false;
extern bool   Kalman  = false;
extern bool   Kalman2  = false;
extern bool   TRENDSENTRY = false;
extern bool   InstantaneousTrendline= false;
extern bool   ZigZagOsc= false;
extern bool   BBTrend= false;
extern bool   BBTrend2= true;
extern bool   BBTrend3= false;
extern bool   BBTrendM= false;
extern int    BarZiZag=   400;
extern bool   PipsOpen= false;
extern int    BarPips1   = 1;
extern int    BarPips2   = 10;
extern bool    AdvStep  = false;
extern bool    Takbir  = false;
extern bool    obv  = false;
extern int    BarCloseT  = 0;
extern int    BarTma   = 1;

extern int    Molt   = 10;
/*/
string IndicatorName1 = "Arrows_Magic_Buy";



//
//
//
//
//

double rsi1[];
double rsi2[];
double rsi3[];
double rsi4[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   timeFrame = MathMax(timeFrame,_Period);
   IndicatorBuffers(5);
   SetIndexBuffer(0,rsi1);if(HistoPrice)SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(1,rsi2);if(HistoRsi)SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(2,rsi3);if(HistoTma)SetIndexStyle(2,DRAW_HISTOGRAM,0,2);
  SetIndexBuffer(3,rsi4);if(HistoPrice2)SetIndexStyle(3,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(4,trend);
        
  // SetLevelValue(1,  Level1  );
  // SetLevelValue(2,  Level2  );   
 //  SetLevelValue(3,  Level3  );
  // SetLevelValue(4,  Level4  );   
         
  
   return(0);
}
int deinit()
{
   deleteArrows(); 
   
return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit = MathMin(MathMax(Bars-counted_bars,3*timeFrame/_Period),Bars-1);

   //
   //
   //
   //
   //
   //
   

   

   for (i=limit; i >= 0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      
      
      rsi2[i]=    iBands ( NULL , 0,20,2,0, PRICE_CLOSE , MODE_LOWER , i)/Point-iBands ( NULL , 0,20,2,0, PRICE_CLOSE , MODE_LOWER , i+BarBollinger)/Point;
       
     // rsi1[i]=  Low[i]/Point-Low[i+5]/Point;

    // rsi2[i]=  High[i]/Point-High[i+5]/Point;
    rsi1[i]=    iBands ( NULL , 0,20,2,0, PRICE_CLOSE , MODE_UPPER , i)/Point-iBands ( NULL , 0,20,2,0, PRICE_CLOSE , MODE_UPPER , i+BarBollinger)/Point;
     
    
      
      
      
      
      
       
      
      
      
      
      
    

     
     
         trend[i] = 0;
         
         
         if ( Close[i]-Open[i]>0
         
         &&  rsi1[i]>SensitivityUp 
         && rsi3[i+1]<-SensitivityUp 
        && Open[i]-iMA(NULL, 0, 20, 0,3, PRICE_LOW, i)>0
         
         // && rsi4[i]>0 
          )                                
         trend[i]= 1;
                                             
        
         if (  Close[i]-Open[i]<0
         
        && rsi1[i]<-SensitivityDn 
        && rsi3[i+1]>SensitivityDn 
        && Open[i]-iMA(NULL, 0, 20, 0,3, PRICE_HIGH, i)<0
        // && rsi1[i+1]>0  
         )
            
         trend[i]=-1;
                                                                                
                                                                                                                                    
            
         manageArrow(i); 
            
            
       
            
            
            
            
            
            
            
    }
    
    //
    //
    //
    //
    //
      
    if (alertsOn)
    {
       if (alertsOnCurrent)
            int whichBar = 0;
       else     whichBar = 1;
       if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("Buy");
       else  doAlert("Sell");       
    }

return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message =  StringConcatenate(timeFrame+" "+Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS),"SSA normalized with flat  ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," SSA normalized with flat " +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"SSA normalized with flat "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (arrowsVisible)
   {
     if (trend[i] != trend[i+1])
     {
       deleteArrow(Time[i]);
       if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
       if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize,true);
     }                                            
      
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,int theSize, bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      if(LineVisible)
         ObjectCreate(name,OBJ_VLINE,0,Time[i],0);
         ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode );
         ObjectSet(name,OBJPROP_COLOR,    theColor);
         ObjectSet(name,OBJPROP_WIDTH,    theSize );      
         
         if (up)
              ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsDisplaceDn * gap);      
         else ObjectSet(name,OBJPROP_PRICE1, Low[i] - arrowsDisplaceUp * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}



