0
votes

I have made an Expert Advisor (please see code below). It only takes one trade at a time though. There are several instances that the conditions for trade entry are met more than once but only one trade is taken at a time. How can I make it a new trade whenever an entry condition is met and exit all trades when an exit condition is met?

extern int MagicNumber=10001;
extern double Lots =0.01;
double StopLoss=0;
double TakeProfit=50;
int TrailingStop=0;
int Slippage=3;

int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;

  double TheStopLoss=0;
  double TheTakeProfit=0;
  if( TotalOrdersCount()==0 ) 
  {
     int result=0;
     if(
     (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)<iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
 (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)<iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0)) 
     ) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Test",MagicNumber,0,Blue);
    Alert("BUY OPENED");
    //if(result>0)
    //{
    // TheStopLoss=0;
    // TheTakeProfit=0;
    // if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
    // if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
    // OrderSelect(result,SELECT_BY_TICKET);
    // OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
    //}
    return(0);
 }
 if(
 (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
 (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)>iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0)) 
 ) // Here is your open Sell rule
 {
    result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
    Alert("SELL OPENED");
    //if(result>0)
    //{
    // TheStopLoss=0;
    // TheTakeProfit=0;
    // if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
    // if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
    // OrderSelect(result,SELECT_BY_TICKET);
    // OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
    //}
    return(0);
     }
  }

  for(int cnt=0;cnt<OrdersTotal();cnt++)
      {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderType()<=OP_SELL &&   
     OrderSymbol()==Symbol() &&
     OrderMagicNumber()==MagicNumber 
     )  
    {
     if(OrderType()==OP_BUY)  
       {
          if(
          (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)>iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
          (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)>iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0))) //here is your close buy rule
          {
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
               Alert("BUY CLOSED");
          }
        if(TrailingStop>0)  
          {                 
           if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
             {
              if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                {
                 OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);
                 return(0);
                }
             }
          }
       }
     else 
       {
            if(
            (iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_SIGNAL,0)<iStochastic(NULL,PERIOD_M15,5,3,3,MODE_EMA,1,MODE_MAIN,0))&&
            (iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,1)<iMACD(NULL,PERIOD_M15,12,26,1,PRICE_CLOSE,MODE_MAIN,0))) // here is your close sell rule
            {
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
               Alert("SELL CLOSED");
            }
        if(TrailingStop>0)  
          {                 
           if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
             {
              if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                {
                 OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);
                 return(0);
                }
             }
          }
       }
    }
 }
return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}
1

1 Answers

1
votes

Your code checks whether there is at least one open deal, or not by using TotalOrdersCount() function. You need to remove that function, or hide it or edit in order to allow multiple trades. Currently the ea checks how much deals are opened by expert advisors with the same magic number (and probably different symbols) and checks the long/short rules only if number of open deals is zero: see line if( TotalOrdersCount()==0 ).

If you need to found the latest opened trade - update the TotalOrdersCountand have time either a global variable, or pass it by reference.

int totalOrdersCount(datetime &lastOpenTrade)
  {
   int result=0; datetime last=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderMagic()!=InpMagicNumber)continue;
      if(OrderSymbol()!=_Symbol)continue;
      result++;
      if(OrderOpenTime()>last)last=OrderOpenTime();
     }
   lastOpenTrade=last;
   return(result);
  }