I need help :(

 

Hello, I have an EMas crossover EA, one of 5 periods and another of 100. when ema 5 crosses the long ema and is 500 points away from ema 100 an operation is opened. but if the price goes against the tp and crosses the ema in the opposite direction, another operation opens in the direction of the trend without closing the previous operation.

The problem is the following:

The first operation that opens works well, either buy or sell. but when it goes against and crosses the ema 100 in the opposite direction, the other operation opens but many operations begin to open at the same time. I just want only one to open in that direction, and it will go in I want another one to open again, but without closing the previous one, but for a single operation to be opened every time the ema 100 is crossed.

What I want is that every time the ema crosses in a direction before the tp of the open operation is reached, a new operation is opened, either a purchase or a sale without closing the previous operations that are open. and that each new order has a lot and a higher tp that covers the losses of all the operations opened up to that moment.

Heres my code:

extern int MagicNumber=10001;
extern double Lots =0.10;
extern double StopLoss=0;
extern double TakeProfit=50;
extern int TrailingStop=0;
extern int Slippage=3;
extern int Limite_Ordenes=20;
double Preciobuy=0;
double Preciosell=0;
double preciobuy2=0;
double preciosell2=0;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  if(Digits==3 || Digits==5) MyPoint=Point*10;
  double TheStopLoss=0;
  double TheTakeProfit=0;
//---
Preciobuy=iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);
Preciosell=iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0);
preciobuy2=Preciobuy+50*MyPoint;
preciosell2=Preciosell-50*MyPoint;
  if( TotalOrdersCount()==0) 
  {
     int result=0;
     if((iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0))&&(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0)>preciobuy2)) // Here is your open buy rule
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator",MagicNumber,0,Blue);
        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((iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0))&&(iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,0)<preciosell2)) // Here is your open Sell rule
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator",MagicNumber,0,Red);
        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);
     }
  }
//+------------------------------------------------------------------+
//   Ordenes SuperPuestas
//+------------------------------------------------------------------+
  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((iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,0)<iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0))&&(iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,0)<preciosell2)) 
              {
               result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"EA Generator",MagicNumber,0,Red);
                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);
        }
            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((iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,0)>iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,0))&&(iMA(NULL,0,4,0,MODE_EMA,PRICE_CLOSE,0)>preciobuy2)) // here is your close sell rule
                {
                  result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"EA Generator",MagicNumber,0,Blue);
                  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);
                   }
                }
            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);
}
 

you need to fix at least this:

int MyTotalOrdersCount()
  {
   int count=0;
   for(int i=OrdersTotal()-1;i>=0;i--) // always back to front
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; // an OrderSelect might fail!!!
      if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber) count++; // only my magic and my symbol
     }
   return count;
  }
 
vzlademd:

Hello, I have an EMas crossover EA, one of 5 periods and another of 100. when ema 5 crosses the long ema and is 500 points away from ema 100 an operation is opened. but if the price goes against the tp and crosses the ema in the opposite direction, another operation opens in the direction of the trend without closing the previous operation.

The problem is the following:

The first operation that opens works well, either buy or sell. but when it goes against and crosses the ema 100 in the opposite direction, the other operation opens but many operations begin to open at the same time. I just want only one to open in that direction, and it will go in I want another one to open again, but without closing the previous one, but for a single operation to be opened every time the ema 100 is crossed.

What I want is that every time the ema crosses in a direction before the tp of the open operation is reached, a new operation is opened, either a purchase or a sale without closing the previous operations that are open. and that each new order has a lot and a higher tp that covers the losses of all the operations opened up to that moment.

Heres my code:

Hello,

should be replace start() function with OnTick() function.

OnStart() function run once time.

OnTick() function run every tick.


The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
Right. When you have fixed the issues pointed out so far come back here.
 

try my version of the 'MyTotalOrdersCount'

int MyTotalOrdersCount()
  {
   int count=0;
   for(int i=OrdersTotal()-1; i>=0; i--) // always back to front
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         break; // an OrderSelect might fail!!!
      if(OrderSymbol()!=_Symbol && OrderMagicNumber()!=MagicNumber)
         continue;
      if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber)
         if(OrderType() == OP_SELL || OrderType() == OP_BUY)
            count++; // only my magic and my symbol
     }
   return(count);
  }
Reason: