Download MetaTrader 5

This EA only sells

To add comments, please log in or register
mql4student
147
mql4student  

What's wrong with this code?

It only open sell orders but not buy orders.

 

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=120;
extern double TakeProfit=120;
extern int TrailingStop=50;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
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((Close[0]>iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iRSI(NULL,PERIOD_H4,50,PRICE_MEDIAN,0)>50)&&(iFractals(NULL,PERIOD_H4,MODE_LOWER,0)>iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iDeMarker(NULL,PERIOD_H4,50,0)>0.50))
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,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((Close[0]<iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iRSI(NULL,PERIOD_H4,50,PRICE_MEDIAN,0)<50)&&(iFractals(NULL,PERIOD_H4,MODE_UPPER,4)<iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iDeMarker(NULL,PERIOD_H4,50,0)<0.50))
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,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);
     }
  }
  
  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(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(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);
}
Marco vd Heijden
Moderator
5172
Marco vd Heijden  
mql4student:

What's wrong with this code?

It only open sell orders but not buy orders.

 

extern int MagicNumber=10001;
extern double Lots =0.1;
extern double StopLoss=120;
extern double TakeProfit=120;
extern int TrailingStop=50;
extern int Slippage=3;
//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
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((Close[0]>iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iRSI(NULL,PERIOD_H4,50,PRICE_MEDIAN,0)>50)&&(iFractals(NULL,PERIOD_H4,MODE_LOWER,0)>iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iDeMarker(NULL,PERIOD_H4,50,0)>0.50))
     {
        result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,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((Close[0]<iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iRSI(NULL,PERIOD_H4,50,PRICE_MEDIAN,0)<50)&&(iFractals(NULL,PERIOD_H4,MODE_UPPER,4)<iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0))&&(iDeMarker(NULL,PERIOD_H4,50,0)<0.50))
     {
        result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,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);
     }
  }
  
  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(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(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);
}
Close[0] does not exist.
Alain Verleyen
Moderator
32472
Alain Verleyen  
Marco vd Heijden:
Close[0] does not exist.
Of course Close[0] exists, it's equal to current price (Bid).
Marco vd Heijden
Moderator
5172
Marco vd Heijden  

it will never close is what i want to say.

maybe you can point our student to the problem ? 

Alain Verleyen
Moderator
32472
Alain Verleyen  
mql4student:

What's wrong with this code?

It only open sell orders but not buy orders.

 

The problem comes from this condition :

iFractals(NULL,PERIOD_H4,MODE_LOWER,0)>iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0)

It's ALWAYS false, you can't have a Fractal at current bar.

While the SELL condition is almost always true :

iFractals(NULL,PERIOD_H4,MODE_UPPER,4)<iMA(NULL,PERIOD_H4,50,0,MODE_SMA,PRICE_MEDIAN,0)

When there is no fractals, iFractals() returns 0, so the condition is true.

To add comments, please log in or register