//+------------------------------------------------------------------+
//|                              Indicator: Kelechi Stoch Alert2.mq4 |
//|                                       Created with EABuilder.com |
//|                                             http://eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "http://eabuilder.com"
#property version   "1.00"
#property description "coded jimtang"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 5
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 5
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

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 = PERIOD_H1;
extern double Current_Stoch_Low = 20;
extern double Current_Stoch_High = 80;
extern bool use_Main = true;
extern bool use_Signal = true;
datetime time_alert; //used when sending alert
extern bool Send_Email = true;
extern bool Audible_Alerts = true;
extern bool Push_Notifications = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   int handle;
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Kelechi Stoch Alert2 @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | Kelechi Stoch Alert2 @ "+Symbol()+","+Period()+" | "+message);
      if(Audible_Alerts) Alert(type+" | Kelechi Stoch Alert2 @ "+Symbol()+","+Period()+" | "+message);
      if(Send_Email) SendMail("Kelechi Stoch Alert2", type+" | Kelechi Stoch Alert2 @ "+Symbol()+","+Period()+" | "+message);
      handle = FileOpen("Kelechi Stoch Alert2.txt", FILE_TXT|FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE, ';');
      if(handle != INVALID_HANDLE)
        {
         FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, type+" | Kelechi Stoch Alert2 @ "+Symbol()+","+Period()+" | "+message);
         FileClose(handle);
        }
      if(Push_Notifications) SendNotification(type+" | Kelechi Stoch Alert2 @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 233);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 234);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      

if(use_Main)
{
bool MainBuy,MainSell;
double main = iStochastic(NULL, PERIOD_CURRENT, Current_K_period, Current_D_period, Current_Slowing, MODE_SMA, 0, MODE_MAIN, i);


if(main< Current_Stoch_Low)
MainBuy=true;
else
MainBuy=false;
if(main>Current_Stoch_High)
MainSell=true;
else
MainSell=false;
      } else {
         MainBuy=true;
         MainSell=true; 
       }
      
if(use_Signal)
{
bool SignalBuy,SignalSell;
double signal = iStochastic(NULL, PERIOD_CURRENT, Current_K_period, Current_D_period, Current_Slowing, MODE_SMA, 0, MODE_SIGNAL, i);


if(signal< Current_Stoch_Low)
SignalBuy=true;
else
SignalBuy=false;

if(signal>Current_Stoch_High)
SignalSell=true;
else
SignalSell=false;
      } else {
         SignalBuy=true;
         SignalSell=true; 
       }
       
             
      //Indicator Buffer 1
      if(iStochastic(NULL, PERIOD, Long_K_period, Long_D_period, Long_Slowing, MODE_SMA, 0, MODE_MAIN, i) > iStochastic(NULL, PERIOD_H1, Long_K_period, Long_D_period, Long_Slowing, MODE_SMA, 0, MODE_SIGNAL, i) //Stochastic Oscillator > Stochastic Oscillator
      &&SignalBuy
      &&MainBuy

      )
        {
         Buffer1[i] = Low[i]; //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(iStochastic(NULL, PERIOD, Long_K_period, Long_D_period, Long_Slowing, MODE_SMA, 0, MODE_MAIN, i) < iStochastic(NULL, PERIOD_H1, Long_K_period, Long_D_period, Long_Slowing, MODE_SMA, 0, MODE_SIGNAL, i) //Stochastic Oscillator < Stochastic Oscillator
      &&SignalSell
      &&MainSell
      )
        {
         Buffer2[i] = High[i]; //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+