Limit OP For A Cycle Of Trend

 

Hi,


This is my first EA, and i would like to put a limit of number of continueos OP for a cycle of trend, i have crack my head and search for the code for almost two days returned with no result.


eg, when EMA 20 cross over EMA 40, this EA will OP with TP 15pips continueously without stop until the trend change reversely and hit my SL, so any one can help me to put the code to limit the number of continueous OP, eg every new trend only allow for 2 continueous OP, then wait for a reversal trend.


Thanks a lot


#property copyright ""

#property link ""

#include <stdlib.mqh>

#include <stderror.mqh>


extern bool    TimeFilter = false;

extern int     HoursStart = 13;

extern int     HoursEnd = 7;

extern int     HoursClose = 7;

extern int     MinuteClose = 00;

extern int     TakeProfit =15;

extern int     StopLoss =100;

extern bool    MoneyMangement = false;

extern double  MaximumRisk    = 0.1;

extern bool    MicroAccount   = false;

extern double  Maxlot = 100;

extern double  FixLots=0.1;

extern int     Slippage =3;

extern int    MagicNumber   = 2277227;

int tf=0;


string EA_name     = "Merdekarama V1.1 by Yast77";

string TradeComment;

int    lotPrecision;

int    total=0,ticket;

//datetime Bar;

bool permit=false;

int cnt=0;

static int prevtime = 0;


//+------------------------------------------------------------------+

//| expert initialization function                                   |

//+------------------------------------------------------------------+

int init()

  {

//----

   

  

   

   if (Symbol() == "AUDCADm" || Symbol() == "AUDCAD") MagicNumber = MagicNumber + 1;

   if (Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") MagicNumber = MagicNumber + 2;

   if (Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") MagicNumber = MagicNumber + 3;

   if (Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") MagicNumber = MagicNumber + 4;

   if (Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") MagicNumber = MagicNumber + 5;

   if (Symbol() == "EURAUDm" || Symbol() == "EURAUD") MagicNumber = MagicNumber + 6;

   if (Symbol() == "EURCADm" || Symbol() == "EURCAD") MagicNumber = MagicNumber + 7;

   if (Symbol() == "EURCHFm" || Symbol() == "EURCHF") MagicNumber = MagicNumber + 8;

   if (Symbol() == "EURGBPm" || Symbol() == "EURGBP") MagicNumber = MagicNumber + 9;

   if (Symbol() == "EURJPYm" || Symbol() == "EURJPY") MagicNumber = MagicNumber + 10;

   if (Symbol() == "EURUSDm" || Symbol() == "EURUSD") MagicNumber = MagicNumber + 11;

   if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") MagicNumber = MagicNumber + 12;

   if (Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") MagicNumber = MagicNumber + 13;

   if (Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") MagicNumber = MagicNumber + 14;

   if (Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") MagicNumber = MagicNumber + 15;

   if (Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") MagicNumber = MagicNumber + 16;

   if (Symbol() == "USDCHFm" || Symbol() == "USDCHF") MagicNumber = MagicNumber + 17;

   if (Symbol() == "USDJPYm" || Symbol() == "USDJPY") MagicNumber = MagicNumber + 18;

   if (Symbol() == "USDCADm" || Symbol() == "USDCAD") MagicNumber = MagicNumber + 19;

   if (MagicNumber == 0) MagicNumber = MagicNumber + 20;

   TradeComment  = StringConcatenate(Symbol()," ",EA_name);

   CalculateLotPrecision();

   

   

//----

   return(0);

  }

//+------------------------------------------------------------------+

//| expert deinitialization function                                 |

//+------------------------------------------------------------------+

int deinit()

  {

//----


//----

   return(0);

  }

  

  

  void CalculateLotPrecision(){

   double lotstep=MarketInfo(Symbol(),MODE_LOTSTEP);

   if(lotstep==1) lotPrecision=0;

   if(lotstep==0.1) lotPrecision=1;

   if(lotstep==0.01) lotPrecision=2;

   if(lotstep==0.001) lotPrecision=3;

}


double GetLots()

{

   double lot;

   if(MoneyMangement)

   {

   lot=NormalizeDouble(AccountBalance()/10000,lotPrecision)*MaximumRisk;

   if(MicroAccount)lot=NormalizeDouble(lot/10,lotPrecision);

   if(lot>Maxlot)lot=Maxlot;

   }

   else

   {

      lot=FixLots;

   }

   return(lot);

}

  

  

//+------------------------------------------------------------------+

//| expert start function                                            |

//+------------------------------------------------------------------+

int start()

  {

//----

  Comment("Yast77");

  

     

      //----------------------- CHECK CHART NEED MORE THAN 100 BARS

   if(Bars<100)

   {

      Print("bars less than 100");

      return(0);  

   }

   

   

   //orderclose time 

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) // check for symbol

{


if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?

if(TimeFilter)

{

if(Hour()== HoursClose && Minute()>MinuteClose)

{

OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue); // close position


return(0); // exit

}

}

}


if(OrderType()==OP_SELL) // short position is opened

{

// should it be closed?

if(TimeFilter)

{

if(Hour()== HoursClose && Minute()>MinuteClose)

{

OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); // close position


return(0); // exit

}

}

}

}

}


total=0;


for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) // check for symbol

{

   total++;

}

}


   if(prevtime!=Time[0])

   {

        //Bar=iTime(NULL,0,0);

            

if(TimeFilter)

{

   //Check Trading Hours

   permit=false;

  if(HoursStart<HoursEnd) if(Hour()>=HoursStart && Hour()<HoursEnd) permit=true;

  if(HoursStart>HoursEnd)

    {

    if(Hour()>=HoursStart) permit=true;

    if(Hour()>=0 && Hour()<HoursEnd) permit=true;

    }

  if(HoursStart==HoursEnd)  permit=true;

}

else

permit=true;

         

   // total=OrdersTotal();

   if(total<1 && permit==true)

   {

       //open buy

       if(GetSignal()==1)

        {

         ticket=OrderSend(Symbol(),OP_BUY,GetLots(),Ask,Slippage,Ask - (StopLoss * Point),Ask+TakeProfit*Point,TradeComment,MagicNumber,0,Green);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

            prevtime=Time[0];

            

           }

         else Print("Error opening BUY order : ",GetLastError()); 

         return(0); 

        }

      // open sell

      if(GetSignal()==-1)

        {

         ticket=OrderSend(Symbol(),OP_SELL,GetLots(),Bid,Slippage,Bid + (StopLoss * Point),Bid-TakeProfit*Point,TradeComment,MagicNumber,0,Red);

         if(ticket>0)

           {

            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

            prevtime=Time[0];

           }

         else Print("Error opening SELL order : ",GetLastError()); 

         return(0); 

        }

      

     }

      

      }

    

    if(total>0)  

    CheckClose();


      

      

      

 

///----

   return(0);

  }




int GetSignal()

{

      int TradeDirection =0;

     RefreshRates();

   

      double Var1 = iCustom(NULL, PERIOD_M15, "TE", 2, 3, 1, 1);

      double Var2 = iCustom(NULL, PERIOD_M15, "TE", 2, 3, 0, 1);     


 

      double Var3 =iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,0);

      double Var4 =iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);     

      

      double Var5 =iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);

      double Var6 =iMA(NULL,0,40,0,MODE_EMA,PRICE_CLOSE,0); 


      

      double value6=iCustom(Symbol(),tf,"Trendy_MTF",30,0,0);   

      double value7=iCustom(Symbol(),tf,"Trendy_MTF",60,0,0);

     

      

      


      //buy signal

      if((Var1 > Var2) && (Var3 > Var4) && (Var5 > Var6) && value6>50 && value7>50)

      {

         TradeDirection = 1;

      }

      

      if((Var1 < Var2) && (Var3 < Var4) && (Var5 < Var6) && value6<50 && value7<50)

      {

         TradeDirection = -1;

      }

      

      return(TradeDirection);

      

 }


 int CheckClose()

   {

   RefreshRates();

      double value11 =iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0);

      double value12 =iMA(NULL,0,8,0,MODE_EMA,PRICE_CLOSE,0);  

      

      

      for (int cnt = OrdersTotal()-1 ; cnt >= 0; cnt--) 

   { 

      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES); 

      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderCloseTime()==0) 

      { 

        

            if(OrderType()==OP_BUY)  

            {

               if((value11 < value12))

            OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue); 

            }

            

            if(OrderType()==OP_SELL)

            {

               if((value11 > value12)) 

            OrderClose(OrderTicket(),OrderLots(),Ask,3,Red); 

            }

         

        

      } 

   }

   return(0); 

   

 }