//+------------------------------------------------------------------+
//|                                                     Kawasaki.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//|                                       Kawasaki_Alert_EA_V1.0.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

extern int BB_Period=20;
extern int BB_Deviation=2;
extern int BB_Shift=0;

extern int MA_Period=31;
extern int MA_Shift=0;
extern int MA_Method=0;

extern int ADX_Period=6;

extern int RSI_Period=7;
extern int MA_RSI_Period=4;
extern int MA_RSI_Shift=0;
extern int MA_RSI_Method=0;
extern double RSI_Upper_Value=70;
extern double RSI_Lower_Value=30;

extern double Minimum_Volume=10;
extern bool Show_Signal_Arrow=true;


double order_lot=0.1;
double take_profit=50;         				//Fixed takeprofit in pips (0=no takeprofit)
double stop_loss=30;           				//Fixed stoploss in pips (0=no stoploss)
int max_trades=10000;
int magic_number=92313;
double slippage=5;              			//Allowed slippage of open/close order

int bar=1;
int timeframe=0;
int number_retry_open_trade=10;

double     myPoint, mySpread, myStopLevel,myTickValue,myTickSize,myLotValue;
int        myDigits;
datetime   TradeBarTime;
bool       enableopen;
bool       enable_ea;
double     my_lots;
int        digit_lot;

int signal;
int signalclose;
int myBars;
double last_history_check;
string TradeCode="KWSKA_EA1.0";

int touchbar;
   
int err;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   SetPoint();
   if(Digits==3 || Digits==5) slippage*=10;
   if(MarketInfo(Symbol(),MODE_LOTSTEP)>=0.01) digit_lot=2;   
   if(MarketInfo(Symbol(),MODE_LOTSTEP)>=0.1) digit_lot=1;   
   if(MarketInfo(Symbol(),MODE_LOTSTEP)>=1) digit_lot=0;
   TradeBarTime=Time[0];
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

int signal()
{ 
   int i;
   
   double ma1,ma2;
   double di_p1,di_m1,di_p2,di_m2;
   double bb_up1,bb_dn1;

   double ha11=iCustom(Symbol(),timeframe,"Heiken Ashi",0,bar);
   double ha21=iCustom(Symbol(),timeframe,"Heiken Ashi",1,bar);
   double ha31=iCustom(Symbol(),timeframe,"Heiken Ashi",2,bar);
   double ha41=iCustom(Symbol(),timeframe,"Heiken Ashi",3,bar);

   double ha12=iCustom(Symbol(),timeframe,"Heiken Ashi",0,bar+1);
   double ha22=iCustom(Symbol(),timeframe,"Heiken Ashi",1,bar+1);
   double ha32=iCustom(Symbol(),timeframe,"Heiken Ashi",2,bar+1);
   double ha42=iCustom(Symbol(),timeframe,"Heiken Ashi",3,bar+1);
   
   bool lastdi_buy=false;
   bool lastdi_sell=false;
   
   for(i=1;i<=10;i++) // for(i=bar;i<=Bars;i++)
   {
      di_p1=iADX(Symbol(),timeframe,ADX_Period,0,MODE_PLUSDI,i);
      di_m1=iADX(Symbol(),timeframe,ADX_Period,0,MODE_MINUSDI,i);
      di_p2=iADX(Symbol(),timeframe,ADX_Period,0,MODE_PLUSDI,i+1);
      di_m2=iADX(Symbol(),timeframe,ADX_Period,0,MODE_MINUSDI,i+1);
      if(di_p1>di_m1 && di_p2<=di_m2) { lastdi_buy=true; break; }
      else if(di_p1<di_m1 && di_p2>=di_m2) { lastdi_sell=true; break; }
   }
   
   bool lastha_buy=false;
   bool lastha_sell=false;
   double ha_11,ha_21;
   double haup,hadn;
    for(i=0;i<=10;i++) // for(i=bar;i<=Bars;i++)
   {
      ha_11=iCustom(Symbol(),timeframe,"Heiken Ashi",0,i);   
      ha_21=iCustom(Symbol(),timeframe,"Heiken Ashi",1,i);   
      bb_up1=iBands(Symbol(),timeframe,BB_Period,BB_Deviation,BB_Shift,0,MODE_UPPER,i);
      bb_dn1=iBands(Symbol(),timeframe,BB_Period,BB_Deviation,BB_Shift,0,MODE_LOWER,i);
      if(ha_11>=ha_21) { haup=ha_11; hadn=ha_21; }
      else { haup=ha_21; hadn=ha_11; }
      if(haup>=bb_dn1 && hadn<=bb_dn1) lastha_buy=true;
      if(haup>=bb_up1 && hadn<=bb_up1) lastha_sell=true;
      if(lastha_buy || lastha_sell) 
      {
         touchbar=i;
         break;
      }
   }
   


   bool lastma_buy=false;
   bool lastma_sell=false;

   double arr[];
   ArrayResize(arr,Bars);
   ArraySetAsSeries(arr,true);
  for(i=0;i<=10;i++)//  for(i=0;i<=Bars;i++)
   {
      arr[i]=iRSI(Symbol(),timeframe,RSI_Period,0,i);
   }
   
   for(i=0;i<=10;i++)// for(i=bar;i<=Bars;i++)
   {
      ma1=iMAOnArray(arr,0,MA_RSI_Period,MA_RSI_Shift,MA_RSI_Method,i);    
      ma2=iMAOnArray(arr,0,MA_RSI_Period,MA_RSI_Shift,MA_RSI_Method,i+1);
      if(ma1>RSI_Lower_Value && ma2<=RSI_Lower_Value) { lastma_buy=true; break; }
      else if(ma1<RSI_Upper_Value && ma2>=RSI_Upper_Value) { lastma_sell=true; break; }    
   }

   double open0=iOpen(Symbol(),PERIOD_D1,1);
   double close0=iClose(Symbol(),PERIOD_D1,1);
   double deltaopen=MathAbs(Bid-open0);
   double deltaclose=MathAbs(Bid-close0);
   if(iVolume(Symbol(),timeframe,bar)<Minimum_Volume 
      //|| deltaopen<=myPoint 
      //|| deltaclose<=myPoint
     )
   { 
      return(30);   
   }
   
   if( 
           ha11<ha21 && ha12<ha22
        && lastma_buy
        && lastdi_buy
        && lastha_buy   
      )   
   {
      return(10);
   }

   else if( 
           ha11>=ha21 && ha12>=ha22
        && lastma_sell
        && lastdi_sell
        && lastha_sell
      )   
   {
      return(20);
   }


   return(30);
}



//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   double sl,tp;   
   int openticket1;
   int openticket2;
   double openprice1;
   double openprice2;
   double stop;
   int i;
   int x;
   int status;

   if(isNewCandle())
   {
      signal=signal();
   }

   bool enableopen = true; 
   
   if(enableopen)
      if(TradeBarTime!=Time[0])
         if(signal!=30 )
            if(CountOpenOrders(OP_BUY)+CountOpenOrders(OP_SELL)<max_trades)
            {
               if(signal==10 && GetHLineValue("last_touch_"+Symbol()+strtf(Period())+TradeCode)!=iTime(Symbol(),timeframe,touchbar))
               {
                  Alert("Buy Alert "+ Symbol()+ " " + strtf(Period()) + " Date & Time: " + TimeToStr(TimeCurrent(),TIME_DATE)+" "+TimeToStr(TimeCurrent(),TIME_MINUTES) + " - " + WindowExpertName());
                  DrawLine("last_touch_"+Symbol()+strtf(Period())+TradeCode,iTime(Symbol(),timeframe,touchbar));
                  if(Show_Signal_Arrow) DrawArrow("arrow_up_"+iTime(Symbol(),timeframe,bar), iLow(Symbol(),timeframe,bar)-2*iATR(Symbol(),timeframe,14,bar), iTime(Symbol(),timeframe,bar) , Lime, 1, 233);
               }
               else if(signal==20 && GetHLineValue("last_touch_"+Symbol()+strtf(Period())+TradeCode)!=iTime(Symbol(),timeframe,touchbar))
               {      
                  Alert("Sell Alert "+ Symbol()+ " " + strtf(Period()) + " Date & Time: " + TimeToStr(TimeCurrent(),TIME_DATE)+" "+TimeToStr(TimeCurrent(),TIME_MINUTES) + " - " + WindowExpertName());
                  DrawLine("last_touch_"+Symbol()+strtf(Period())+TradeCode,iTime(Symbol(),timeframe,touchbar));
                  if(Show_Signal_Arrow) DrawArrow("arrow_dn_"+iTime(Symbol(),timeframe,bar), iHigh(Symbol(),timeframe,bar)+2*iATR(Symbol(),timeframe,14,bar), iTime(Symbol(),timeframe,bar) , Red, 1, 234);
               }
            }


 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+-------------------------General Functions------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+


void SetPoint()
{

   myPoint     =  MarketInfo(Symbol(),MODE_POINT);
   mySpread    =  MarketInfo(Symbol(),MODE_SPREAD);
   myDigits    =  MarketInfo(Symbol(),MODE_DIGITS);
   myStopLevel =  MarketInfo(Symbol(),MODE_STOPLEVEL);
   myTickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
   myTickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   myLotValue = myTickValue/myTickSize;

   if(
         myDigits==3    ||  
         myDigits==5
     )
      {
         myPoint     =  myPoint  *  10;
         mySpread    =  mySpread /  10;
         myStopLevel =  myStopLevel / 10;
         myDigits    =  myDigits -1;
      }    

}



//+------------------------------------------------------------------+
//+------------------Count Number of Open Orders---------------------+

int CountOpenOrders(int direction)
{
int i,j;
   

   
   j  =  0;
   
   for(i=OrdersTotal()-1;i>=0;i--)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==True)
            {
               if(   OrderType()          == direction   &&
                     OrderSymbol()        == Symbol()          &&
                     OrderMagicNumber()   == magic_number     
                 )
                     j++;
            }
         else
            Print("Could not SELECT trade");
      }
   
   return(j);
}

bool isNewCandle()  
{
   bool res=false;
   if (myBars!=Bars)
   {
      myBars=Bars;
      res=true;
   }

return(res);   

}

string strtf(int tf)
{
   switch(tf)
   {
      case PERIOD_M1: return("M1");
      case PERIOD_M5: return("M5");
      case PERIOD_M15: return("M15");
      case PERIOD_M30: return("M30");
      case PERIOD_H1: return("H1");
      case PERIOD_H4: return("H4");
      case PERIOD_D1: return("D1");
      case PERIOD_W1: return("W1");
      case PERIOD_MN1: return("MN1");
      default:return("Unknown timeframe");
   }
}

void DrawArrow(string Name, double price, datetime time , color myColor, int size, int arrowcode)
{
   int win_number=0;
   ObjectCreate(Name,OBJ_ARROW,win_number,0,0);
   ObjectSet(Name,OBJPROP_ARROWCODE,arrowcode);
   ObjectSet(Name,OBJPROP_PRICE1,price);
   ObjectSet(Name,OBJPROP_TIME1,time);
   ObjectSet(Name,OBJPROP_COLOR, myColor);
   ObjectSet(Name,OBJPROP_WIDTH, size);
   return;
}


//+------------------------------------------------------------------+
//+-----------------------------Draw Line----------------------------+


void DrawLine(string sName, double dPrice,color cLineClr=CLR_NONE)
{
    int iWidth=1;
    string sObjName = sName;

    if(ObjectFind(sObjName) == -1){
        // create object 
        ObjectCreate(sObjName,OBJ_HLINE, 0, 0,0);
    }

    ObjectSet(sObjName,OBJPROP_PRICE1,dPrice);
    ObjectSet(sObjName, OBJPROP_COLOR, cLineClr);
    ObjectSet(sObjName, OBJPROP_WIDTH, iWidth);
}

//+------------------------------------------------------------------+
//+---------------------------Get Line Price-------------------------+


double GetHLineValue(string name)
{

   if (ObjectFind(name) == -1)
      return(-1);
   else
      return(ObjectGet(name,OBJPROP_PRICE1));
}







