help prevent my EA open new trades after TP or SL

 

Hi, I learned coding step by step from this website https://www.mql5.com/en/articles/1456  around two weeks ago, followed examples given and made an ea. The problem however is that whenever it hits SL or TP, it opens another position in the same direction as the previous if the price is still below or above the moving average which is my buy/sell condition. I don’t know how to correct this; any help will be greatly appreciated

Here is the part that I know should be changed

 

bool BuySignal()
{ 
   if (iClose(Symbol(),0,1) > iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1) )
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool SellSignal()
{
   if (iClose(Symbol(),0,1) < iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1) )
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool bolehTrade()
{
   if (DayOfWeek()==5 || OrdersTotal()>0) { return (false); } else { return(true); }
}

double itungLot()
{
   double xLots=Lots;
   //xLots=NormalizeDouble(AccountBalance()*RiskPercent/100 /StopLoss / 10,1);
   return (xLots);
}

int closePos(int otype)
{
   
   for (int a=0; a<OrdersTotal(); a++)
   {
      Print("Order found");
      OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
      if (OrderMagicNumber()==MagicNumber)
      {
         if (otype==0)
         {
            if (OrderType()==OP_BUY)
            {
            Print("Closing buy");
            OrderClose(OrderTicket(),OrderLots(),Bid,Slippage);
            }
         }
         else if (otype==1)
         {
            if (OrderType()==OP_SELL)
            {
            Print("Closing sell");
            OrderClose(OrderTicket(),OrderLots(),Ask,Slippage);
            }
         }
      }
   }
}
 
9690:

Hi, I learned coding step by step from this website https://www.mql5.com/en/articles/1456  around two weeks ago, followed examples given and made an ea. The problem however is that whenever it hits SL or TP, it opens another position in the same direction as the previous if the price is still below or above the moving average which is my buy/sell condition. I don’t know how to correct this; any help will be greatly appreciated

First,  read this,  understand it and change your code:  Loops and Closing or Deleting Orders

You don't check if your OrderSelect() works,  you don't check if your OrderClose() works,  why not ?  you don't report errors back to the log, why not ?  read this:   What are Function return values ? How do I use them ?

Don't use this . . .

if (otype==0)

 use this . . .

if (otype == OP_BUY)

 it makes your code readable.  You don't need to use that code at all as you are checking the OrderType() on the next line . . .


There is no need to distinguish between a Buy and a Sell if you use OrderClosePrice() in place of Bid and Ask. 

The code you have shown does not control when a new order is placed . . .  it is not relevant to your problem. 

 
am sorry i seem not to comprehend that
bool BuySignal()
{
   if (iClose(Symbol(),0,1) > iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1) )
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool SellSignal()
{
   if (iClose(Symbol(),0,1) < iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1) )
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool bolehTrade()
{
   if (DayOfWeek()==5 || OrdersTotal()>0) { return (false); } else { return(true); }
}

double itungLot()
{
   double xLots=Lots;
   //xLots=NormalizeDouble(AccountBalance()*RiskPercent/100 /StopLoss / 10,1);
   return (xLots);
}

int closePos(int otype)
{
   
   for (int a=0; a<OrdersTotal(); a++)
   {
      Print("Order found");
      OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
      if (OrderMagicNumber()==MagicNumber)
      {
         if (otype==0)
         {
            if (OrderType()==OP_BUY)
            {
            Print("Closing buy");
            OrderClose(OrderTicket(),OrderLots(),Bid,Slippage);
            }
         }
         else if (otype==1)
         {
            if (OrderType()==OP_SELL)
            {
            Print("Closing sell");
            OrderClose(OrderTicket(),OrderLots(),Ask,Slippage);
            }
         }
      }
   }
}
.
please use the above code as specific point of reference
 
9690:
am sorry i seem not to comprehend that.

You need to read the threads I gave links too . . .  if you don't understand what is written you need to read the Book again . . . 
 
somebody help me pleeeeeeeease :'(
 
9690:
somebody help me pleeeeeeeease :'(
If you aren't prepared to learn then pay someone to code for you:  MT4 & MT5 coding
 

the problem is not with closing the orders sir, its opening another order after T.P or S.L has been hit. i want the system to open short after price closes below 50ema and long if the price closes above 50ema. if this condition is met, and i ride along with the trend, i take profit at some point.

lets say the conditions are met and the ea goes short. my trade hits t.p. when that trade is closed( either tp or sl), i want the ea to open the next position when the price closes above the ema (long).

hope am clear now sir

 
bool BuySignal()
{
   if (iClose(Symbol(),0,1) > iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1) )// && last opened trade EA = SELL
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool SellSignal()
{
   if (iClose(Symbol(),0,1) < iMA(Symbol(),0,50,0,MODE_EMA,PRICE_CLOSE,1) )// && last opened trade EA = BUY
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

This has to be changed 

hope am clear now   "  somebody help me pleeeeeeeease :'(  "
 

 
9690:

the problem is not with closing the orders sir, its opening another order after T.P or S.L has been hit. i want the system to open short after price closes below 50ema and long if the price closes above 50ema. if this condition is met, and i ride along with the trend, i take profit at some point.

lets say the conditions are met and the ea goes short. my trade hits t.p. when that trade is closed( either tp or sl), i want the ea to open the next position when the price closes above the ema (long).

hope am clear now sir


Hi 9690, you are asking for help but you are not showing all the code, so it is impossible to help you.

In your code there must be somewhere a line with the following function: Ordersend(....,...,..,)

this function is used for opening a trade

I guess your code first finds if BuySignal() or SellSignal() is true, then sends the corresponding parameters to any of those two functions, so the Ordersend function must be inside those two functions.

For example, if in your BuySignal() function there is a piece of code that closes all trades after it opens a new buy signal, then the answer to your questions will be right there.

English is not my native language, anyway I hope I help you.

 
Justhavingfun:


Hi 9690, you are asking for help but you are not showing all the code, so it is impossible to help you.

In your code there must be somewhere a line with the following function: Ordersend(....,...,..,)

this function is used for opening a trade

I guess your code first finds if BuySignal() or SellSignal() is true, then sends the corresponding parameters to any of those two functions, so the Ordersend function must be inside those two functions.

For example, if in your BuySignal() function there is a piece of code that closes all trades after it opens a new buy signal, then the answer to your questions will be right there.

English is not my native language, anyway I hope I help you.

this is the whole code Justhavingfun
extern double RiskPercent=5;
extern int StopLoss=500;
extern int TakeProfit=750;
extern string txComment="Order EA1";
extern int MagicNumber=12345;
extern double Lots=0.5;
extern int Slippage=3;
extern int TrailingStop=500;
extern int TrailingStep=0;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if(TrailingStop>0)MoveTrailingStop();
//----
         if (BuySignal() )
         {
            Print("Switch to buy");
            
            //1=sell
            closePos(1);
            if (bolehTrade())
            {
               OrderSend(Symbol(),OP_BUY,itungLot(),Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,txComment,MagicNumber,Blue);
            }
         }
         else if (SellSignal() )
         {
            Print("Switch to sell");
            
            //0=buy
            
            closePos(0);
            if (bolehTrade())
            {
               OrderSend(Symbol(),OP_SELL,itungLot(),Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,txComment,MagicNumber,Red);
            }
         }
         
      
//----
   return(0);
  }
//+------------------------------------------------------------------+
void MoveTrailingStop()
{
   int cnt,total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
   {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol())
      {
         if(OrderType()==OP_BUY)
         {
            if(TrailingStop>0)  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-Point*(TrailingStop+TrailingStep),Digits))||(OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Point*TrailingStop,Digits),OrderTakeProfit(),0,Green);
                  return(0);
               }
            }
         }
         else 
         {
            if(TrailingStop>0)  
            {                 
               if((NormalizeDouble(OrderStopLoss(),Digits)>(NormalizeDouble(Ask+Point*(TrailingStop+TrailingStep),Digits)))||(OrderStopLoss()==0))
               {
                  OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Point*TrailingStop,Digits),OrderTakeProfit(),0,Red);
                  return(0);
               }
            }
         }
      }
   }
}

bool BuySignal()
{
  
   if (iClose(Symbol(),0,1) > iMA(Symbol(),0,100,0,MODE_EMA,PRICE_CLOSE,1) )
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool SellSignal()
{
   
   if (iClose(Symbol(),0,1) < iMA(Symbol(),0,100,0,MODE_EMA,PRICE_CLOSE,1) )
   {
      return(true);
   }
   else
   {
      return(false);
   }
}

bool bolehTrade()
{
   
   if (DayOfWeek()==5 || OrdersTotal()>0) { return (false); } else { return(true); }
}

double itungLot()
{
  
   double xLots=Lots;
   return (xLots);
}

int closePos(int otype)
{
   
   for (int a=0; a<OrdersTotal(); a++)
   {
      Print("Order found");
      OrderSelect(a,SELECT_BY_POS,MODE_TRADES);
      if (OrderMagicNumber()==MagicNumber)
      {
         if (otype==0)
         {
            if (OrderType()==OP_BUY)
            {
            Print("Closing buy");
            OrderClose(OrderTicket(),OrderLots(),Bid,Slippage);
            }
         }
         else if (otype==1)
         {
            if (OrderType()==OP_SELL)
            {
            Print("Closing sell");
            OrderClose(OrderTicket(),OrderLots(),Ask,Slippage);
            }
         }
      }
   }
}



 
9690:
this is the whole code Justhavingfun

Try this . . .   but read through the code first,  understand the changes I have made . . . .  THEN   try it in the Strategy Tester.

Files:
9690.mq4  8 kb
Reason: