Need help with fractal breakout EA. Each fractal to be used only once

 

Hi. I need help modifying an EA that I paid to have coded. It is a basic fractal breakout EA with trailing stop and breakeven, TP and SL. I want to modify it such that, once a fractal is used as a signal to open a trade, another trade will not be opened based on the same fractal. This is to prevent the instance when, a trade is profitably closed and theprice comes back towards the fractal to head in the opposite direction. This is the original code.

 

extern int myMagic = 11;

extern int TakeProfit = 2000;
extern int StopLoss = 1000;  
extern int BreakEvenStop = 500;  
extern int TrailingStop = 500;  
extern int TrailingStep = 10;  

extern int BreakoutPips=30;

extern double Lots = 0.1;      //fixed lot size
extern double MaximumRisk = 5; //% risk. lot size will be calculated so that stoploss was equal to risk% of balance
extern bool   FixedLot = true; //trigger to use MM

extern int    slippage=30;      //slippage for market order processing
extern bool   ECN_SLTP=false;    //true - send market order without sl/tp and modify it later
extern int    OrderTriesNumber=5; //to repeate sending orders when got some error

extern string    EAName="FR"; 

bool buysig,sellsig,closebuy,closesell; //flags for signals
int lastsig,tries,at,at2,co;


void start()  {

   if(IsTradeAllowed()==false) return;

   co=CalculateCurrentOrders();
   
   CheckForSignals();

   if (co==0)     //---- calculate open orders by current symbol
      CheckForOpen();
   else {
      CheckForClose();
      BreakEvenStop();
      TrailStop();
   }
        
}

double LotsRisk(int StopLoss)  { 
   double lot=Lots,risk=MaximumRisk;
   int SL=StopLoss;
   if (SL<=0) {
      SL=100;
      if (Digits==5 || Digits==3) SL=1000;
   }
   double minlot=MarketInfo(Symbol(),MODE_MINLOT);
   int nl=0;
   if (minlot<1) nl=1;
   if (minlot<0.1) nl=2;
   if (minlot<0.01) nl=3;
//---- select lot size
   if (!FixedLot)
      lot=NormalizeDouble(AccountBalance()*risk*0.001/SL,nl);
//---- return lot size
   lot=MathMax(lot,minlot);
   return(lot);
}

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()  {
   int ord;
//----
   for(int i=0;i<OrdersTotal();i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==myMagic) ord++;
   }
//---- return orders volume
   return(ord);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForSignals() {

      //indicators variables
      double fru1,frd1;
      for (int i=3;i<Bars;i++) {
         if (fru1<Point || fru1>999999)
            fru1 = iFractals(NULL,0,MODE_UPPER,i); 
         if (frd1<Point || frd1>999999)
            frd1 = iFractals(NULL,0,MODE_LOWER,i); 
         if (fru1>Point && fru1<999999 && frd1>Point && frd1<999999)
            break;
      }
      
      buysig=false;
      sellsig=false;
      //long entry signal condition
      if (fru1>Point && fru1<999999 && Bid>=fru1+BreakoutPips*Point && Low[0]<fru1+BreakoutPips*Point) {
          buysig=true;
      }
      //short entry signal
      if (frd1>Point && frd1<999999 && Bid<=frd1-BreakoutPips*Point && High[0]>frd1+BreakoutPips*Point) {
          sellsig=true;
      }

      closebuy=false;
      closesell=false;
}

void CheckForOpen() {
   int    res,tr;
   
//---- sell conditions
   if(sellsig && lastsig!=Time[0])  {
      res=OpenOrder(OP_SELL,LotsRisk(StopLoss),0,StopLoss,TakeProfit);
      if (res>0) lastsig=Time[0];
      return;
   }
//---- buy conditions
   if(buysig && lastsig!=Time[0])  {
      res=OpenOrder(OP_BUY,LotsRisk(StopLoss),0,StopLoss,TakeProfit);
      if (res>0) lastsig=Time[0];
      return;
   }
}
  
int OpenOrder(int type,double lot,double openpr,int SL,int TP) {
   int    res,tr,col,kk;
   double openprice,sl,tp;
   tries=0;
   while (res<=0 && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      if (type==OP_SELL) {
         openprice=Bid; 
         if (SL>0) sl=openprice+SL*Point;
         if (TP>0) tp=openprice-TP*Point;
         col=Red;
      } else if (type==OP_BUY) {
         openprice=Ask;
         if (SL>0) sl=openprice-SL*Point;
         if (TP>0) tp=openprice+TP*Point;
         col=Blue;
      } else if (type==OP_SELLSTOP || type==OP_SELLLIMIT) {
         openprice=openpr;
         if (SL>0) sl=openprice+SL*Point;
         if (TP>0) tp=openprice-TP*Point;
         col=Red;
      } else {
         openprice=openpr;
         if (SL>0) sl=openprice-SL*Point;
         if (TP>0) tp=openprice+TP*Point;
         col=Blue;
      }
      if (type!=OP_BUY && type!=OP_SELL) {
         res=OrderSend(Symbol(),type,lot,openprice,slippage,sl,tp,EAName+"_"+myMagic,myMagic,0,col);
      } else {
         if (!ECN_SLTP)
            res=OrderSend(Symbol(),type,lot,openprice,slippage,sl,tp,EAName+"_"+myMagic,myMagic,0,col);
         else {
            res=OrderSend(Symbol(),type,lot,openprice,slippage,0,0,EAName+"_"+myMagic,myMagic,0,col);
            if (res>0) {
               kk=0;
               while (kk<7) {
                  kk++;
                  if (OrderSelect(res,SELECT_BY_TICKET)) {
                     if (OrderModify(res,OrderOpenPrice(),sl,tp,OrderExpiration(),col)) break;
                     else Sleep(1000);
                  } else Sleep(1000);
               }
            }
         }
      }
      tries++;
   }
   if (res<=0) Print("Error opening order : ",ErrorDescription(GetLastError()));
   return(res);
}
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()  {
   bool bres;
   for(int i=OrdersTotal()-1;i>=0;i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  continue;
      if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY && closebuy) {
         bres=CloseAtMarket(OrderTicket(),OrderLots());
         continue;
      }
      if(OrderType()==OP_SELL && closesell) {
         bres=CloseAtMarket(OrderTicket(),OrderLots());
         continue;
      }
   }
}

bool CloseAtMarket(int ticket,double lot) {
   bool bres=false; int tr;
   tries=0;
   while (!bres && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      bres=OrderClose(ticket,lot,OrderClosePrice(),slippage,White);
      tries++;
   }
   if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
   return(bres);
}

void TrailStop() {
   bool bres;
   double StopLoss;
   if ( TrailingStop > 0 ) {
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic )  continue;
         if ( OrderType() == OP_BUY ) {
            if ( Bid < OrderOpenPrice()+TrailingStop*Point )  continue;
            StopLoss = Bid-TrailingStop*Point;
            if ( StopLoss >= OrderStopLoss()+TrailingStep*Point ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
                                           if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            if ( Ask > OrderOpenPrice()-TrailingStop*Point )  continue;
            StopLoss = Ask+TrailingStop*Point;
            if ( StopLoss <= OrderStopLoss()-TrailingStep*Point || OrderStopLoss()<Point ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
                                           if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
   return;
}

void BreakEvenStop() {
   bool bres;
   double StopLoss;
   if ( BreakEvenStop > 0 ) {
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic )  continue;
         if ( OrderType() == OP_BUY ) {
            if ( Bid < OrderOpenPrice()+BreakEvenStop*Point )  continue;
            StopLoss = OrderOpenPrice();
            if ( StopLoss >= OrderStopLoss()+Point ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
                                           if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            if ( Ask > OrderOpenPrice()-BreakEvenStop*Point )  continue;
            StopLoss = OrderOpenPrice();
            if ( StopLoss <= OrderStopLoss()-Point || OrderStopLoss()<Point ) {
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
                                           if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
   return;
}

 

My programming knowledge is very basic and I attempted to modify the code and the mod makes sense to me but it doesn't seem to work. Here is what I attempted. The changes are in red. I added global variables, prefru, prefrd,

 

double prefru, prefrd, fru2, frd2;




void start()  {

   if(IsTradeAllowed()==false) return;

   co=CalculateCurrentOrders();
   
   CheckForSignals();

   if (co==0)     //---- calculate open orders by current symbol
      CheckForOpen();
   else {
      CheckForClose();
      BreakEvenStop();
      TrailStop();
   }
        
}

double LotsRisk(int StopLoss)  { 
   double lot=Lots,risk=MaximumRisk;
   int SL=StopLoss;
   if (SL<=0) {
      SL=100;
      if (Digits==5 || Digits==3) SL=1000;
   }
   double minlot=MarketInfo(Symbol(),MODE_MINLOT);
   int nl=0;
   if (minlot<1) nl=1;
   if (minlot<0.1) nl=2;
   if (minlot<0.01) nl=3;
//---- select lot size
   if (!FixedLot)
      lot=NormalizeDouble(AccountBalance()*risk*0.001/SL,nl);
//---- return lot size
   lot=MathMax(lot,minlot);
   return(lot);
}

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()  {
   int ord;
//----
   for(int i=0;i<OrdersTotal();i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==myMagic) ord++;
   }
//---- return orders volume
   return(ord);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+


void CheckForSignals() {

      //indicators variables
      double fru1,frd1;
      for (int i=3;i<Bars;i++) {
         if (fru1<Point || fru1>999999)
            fru1 = iFractals(NULL,0,MODE_UPPER,i); 
         if (frd1<Point || frd1>999999)
            frd1 = iFractals(NULL,0,MODE_LOWER,i); 
         if (fru1>Point && fru1<999999 && frd1>Point && frd1<999999)
            break;
      }
      fru2=fru1;
      frd2=frd1;
      
      buysig=false;
      sellsig=false;
      //long entry signal condition
      if (fru1!=prefru) {
         if (fru1>Point && fru1<999999 && Bid>=fru1+BreakoutPips*Point && Low[0]<fru1+BreakoutPips*Point) {
            buysig=true;
         }
      }
      //short entry signal
      if (frd1!=prefrd) {
         if (frd1>Point && frd1<999999 && Ask<=frd1-BreakoutPips*Point && High[0]>frd1+BreakoutPips*Point) {
            sellsig=true;
         }
      }

      closebuy=false;
      closesell=false;
}

  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()  {
   bool bres;
   for(int i=OrdersTotal()-1;i>=0;i--) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  continue;
      if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue;
      //---- check order type 
      if(OrderType()==OP_BUY && closebuy) {
         bres=CloseAtMarket(OrderTicket(),OrderLots());
         prefru=fru2;
         continue;
      }
      if(OrderType()==OP_SELL && closesell) {
         bres=CloseAtMarket(OrderTicket(),OrderLots());
         prefrd=frd2;
         continue;
      }
   }
}

bool CloseAtMarket(int ticket,double lot) {
   bool bres=false; int tr;
   tries=0;
   while (!bres && tries<OrderTriesNumber) {
      tr=0; while (tr<5 && !IsTradeAllowed()) { tr++; Sleep(2000); }
      RefreshRates();
      bres=OrderClose(ticket,lot,OrderClosePrice(),slippage,White);
      tries++;
   }
   if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
   return(bres);
}

 
Any feedback will be greatly appreciated. Thank you for your time in advance.
 
vikneshk:

My programming knowledge is very basic and I attempted to modify the code and the mod makes sense to me but it doesn't seem to work. Here is what I attempted. The changes are in red. I added global variables, prefru, prefrd,

Comparing doubles is going to get you in trouble,  read this:  Can price != price ?

Why not check the datetime instead ?

 
If you don't mind, could you explain how I could use datetime please
Reason: