//+------------------------------------------------------------------+
//|                                                Arrows_Magic_Buy  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Mod Pacois"//Mod Pacois
#property link      ""//Mod Pacois

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 LimeGreen
#property indicator_color2 Gray
#property indicator_color3 Yellow
#property indicator_color4 Gray
//#property indicator_level1 0

#property indicator_style2  STYLE_DOT
#property indicator_style4  STYLE_DOT

//#property indicator_width3 2
//#property indicator_maximum 1.1
//#property indicator_minimum -1.1

extern int Current_K_period = 5;
extern int Current_D_period = 3;
extern int Current_Slowing = 3;
extern int Long_K_period = 8;
extern int Long_D_period = 4;
extern int Long_Slowing = 4;
extern int PERIOD = 0;
extern string  ______________Stoch_Mtf;
extern string TimeFrame   = 5;
extern int    StoKPeriod  = 8;
extern int    StoDPeriod  =  4;
extern int    StoSlowing  =  4;
extern int    StoPrice    =  0;
extern int    SignalMode  = MODE_SMA;
extern bool   Interpolate = true;
extern string  ______________SetAlert;
extern bool   ArrowsDn        = false;
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 = "Stochastic_Arrows";
extern double arrowsDisplaceUp = 1.0;
extern double arrowsDisplaceDn = 0.5;
extern color  arrowsUpColor    = Lime;
extern color  arrowsDnColor    = Red;
extern int    arrowsUpCode     = 233;
extern int    arrowsDnCode     = 234;
extern int    arrowsUpSize     = 1;
extern int    arrowsDnSize     = 1;


 ENUM_TIMEFRAMES    timeFrame        = 0;


string IndicatorName1 = "Stochastic_Arrows_Line_alert";



//
//
//
//
//

double rsi1[];
double rsi2[];
double rsi3[];
double rsi4[];
double trend[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   timeFrame = MathMax(timeFrame,_Period);
   IndicatorBuffers(5);
   SetIndexBuffer(0,rsi1);//if(Histo1)SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(1,rsi2);//if(Histo2)SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
   SetIndexBuffer(2,rsi3);
   SetIndexBuffer(3,rsi4);
   
   
   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]);
      double  rsi33=iRSI(NULL,0,2,0,i)*2-100;
      rsi1[i]=iStochastic(NULL, PERIOD, Long_K_period, Long_D_period, Long_Slowing, MODE_SMA, 0, MODE_MAIN, i);
      rsi2[i]=iStochastic(NULL, PERIOD, Long_K_period, Long_D_period, Long_Slowing, MODE_SMA, 0, MODE_SIGNAL, i);
      
      
   //Stochastic Oscillator > Stochastic Oscillator
      rsi3[i]= iCustom(NULL,0,"stochastic_mtf",TimeFrame ,StoKPeriod,StoDPeriod,StoSlowing,StoPrice,SignalMode,Interpolate,0,i);

      rsi4[i]=  iCustom(NULL,0,"stochastic_mtf",TimeFrame ,StoKPeriod,StoDPeriod,StoSlowing,StoPrice,SignalMode,Interpolate,1,i);
       
     // rsi4[i]=rsi33/100;
     
    
    

     
     
         trend[i] = 0;
         
         
         if ( rsi1[i]> rsi2[i]
         
        && rsi3[i]> rsi4[i]
         //&&rsi1[i]>rsi1[i+1]
         // &&rsi3[i]<GrayUp
          //&& rsi2[i]>0
         //&& rsi3[i]>0&& rsi3[i]>0
         //&& rsi3[i]> rsi3[i+1]
           )
                                            
         trend[i]= 1;
                                             
       // if(ArrowsDn)
         if (  rsi1[i]< rsi2[i]
         && rsi3[i]< rsi4[i]
         // &&rsi3[i]>GrayDn
        // && rsi3[i]<0
        //&& rsi3[i]< rsi3[i+1]
         )
            // if(ArrowsDn)
         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");
             //if(ArrowsDn)
       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),"MHL average oscillator 1.1",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ,"" +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"MHL average oscillator 1.1"),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);
}



