Need Modification in EA.

 

Hi,

I am trying to code an EA for pending buy stop and sell stop orders.

buy stop : open[0]+0.0003

sell stop : open[0]-0.0003

This  EA is placing only buy stop orders and not placing any sell stop. Kindly suggest necessary modification.

//+------------------------------------------------------------------+
//|                                             Parag Pending EA.mq4 |
//|                                                   Parag Pisolkar |
//|                                              ppisolkar@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Parag Pisolkar"
#property link      "ppisolkar@gmail.com"
#property version   "1.00"
#property strict

extern int MagicNumber=12345;
extern double Lots =1;
extern double StopLoss=50;
extern double TakeProfit=5;
extern int TrailingStop=0;
extern int Slippage=0;

//+------------------------------------------------------------------+
//    expert start function
//+------------------------------------------------------------------+
int start()
{
  double MyPoint=Point;
  double BuyPrice=Open[0]+0.0003; 
  double SellPrice=Open[0]-0.0003;
  if(Digits==3 || Digits==5 || Digits==2) MyPoint=Point*10;
  
  double TheStopLoss=0;
  double TheTakeProfit=0;
  
  if( TotalOrdersCount()==0 ) 
  {
        
     {
        int ticket1=OrderSend(Symbol(),OP_BUYSTOP,Lots,BuyPrice,Slippage,0,0,"My Order",MagicNumber,0,Blue);
        if(ticket1>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint;
         if(OrderSelect(ticket1,SELECT_BY_TICKET)==true)
         bool res = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
        }
        return(0);
     }
     
     {
        int ticket2=OrderSend(Symbol(),OP_SELLSTOP,Lots,SellPrice,Slippage,0,0,"My Order",MagicNumber,0,Red);
        if(ticket2>0)
        {
         TheStopLoss=0;
         TheTakeProfit=0;
         if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint;
         if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint;
         if(OrderSelect(ticket2,SELECT_BY_TICKET)==true)
         if(OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green))Print(GetLastError());
        }
        return(0);
     }
  }
  
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)
      if(OrderType()<=OP_SELLSTOP &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUYSTOP)  
           {
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)
                    {
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green))Print(GetLastError());
                     return(0);
                    }
                 }
              }
           }
         else 
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))
                    {
                     if(OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red))Print(GetLastError());
                     return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
}

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

   }
  return (result);
}
 
Please use the SRC button when posting code. I've fixed it for you this time
 

Instead of this

  double BuyPrice=Open[0]+0.0003; 
  double SellPrice=Open[0]-0.0003;

Why not use

extern double Distance=0.3

int ticket1=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Distance*MyPoint,Slippage,0,0,"My Order",MagicNumber,0,Blue);
 
Ndumiso Mavuso:

Instead of this

Why not use

After using this, it is not doing anything.

extern double Distance=0.3

int ticket1=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Distance*MyPoint,Slippage,0,0,"My Order",MagicNumber,0,Blue);
 

Seems to me that, after executing the buy order,

the return instruction exits the loop.

So remove the "return (0) " in the buy section.

You probably need to check all the "return" s too!