Need a coder to take a look at an EA that is not functioning properly

 

Dear Any Programmers,

I need help fixing this code

At the moment only the Add Missing Stop Loss is working.

I need to Add Missing Take Profit to be working aswell.


Below is the specific part of the file I need looking at.

Thank you


void InsertStopLoss(int ticket)
{
   //Inserts a stop los into a trade that lacks one.

   if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return;//Order has closed, so nothing to do.    
   
   if (!CloseEnough(OrderStopLoss(), 0) || (MissingStopLossPips == 0 && !UseSlAtr) ) 
      return; //Nothing to do
   
   double stop = 0;
   bool result = false;
  
   //There is the option for the user to use Atr to calculate the stop
   if (UseSlAtr) 
      AtrVal = iATR(OrderSymbol(), AtrSlTimeFrame, AtrSlPeriod, 0) * AtrSlMultiplier;
   
   // Buy trade
   if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   {
      stop = NormalizeDouble(OrderOpenPrice() - (MissingStopLoss / factor), digits);    
      if (UseSlAtr) 
         stop = NormalizeDouble(OrderOpenPrice() - AtrVal, digits);
   }//if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   
   
   // Sell trade
   if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   {
      stop = NormalizeDouble(OrderOpenPrice() + (MissingStopLoss / factor), digits); 
      if (UseSlAtr) 
         stop = NormalizeDouble(OrderOpenPrice() + AtrVal, digits);
   }//if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   
   result = ModifyOrder(OrderTicket(), OrderOpenPrice(), stop, OrderTakeProfit(), 
                        OrderExpiration(), clrNONE, __FUNCTION__, slim);
   
   
}// End void InsertStopLoss(int ticket)

void InsertTakeProfit(int ticket)
{
 
    //Inserts a take profit into a trade that lacks one.

   if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return;//Order has closed, so nothing to do.    
  
   if (!CloseEnough(OrderStopLoss(), 0) || (MissingTakeProfitPips == 0 && !UseTpAtr) ) 
      return; //Nothing to do

   double take = 0;
   bool result = false;
   
   //There is the option for the user to use Atr to calculate the stop
   if (UseTpAtr) 
      AtrVal = iATR(OrderSymbol(), AtrTpTimeFrame, AtrTpPeriod, 0) * AtrTpMultiplier;
   
   // Buy trade
   if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   {
      take = NormalizeDouble(ask + (MissingTakeProfit / factor), digits);
      if (UseSlAtr) 
         take = NormalizeDouble(OrderOpenPrice() + AtrVal, digits);
   }//if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   
   
   // Sell trade
   if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   {
      take = NormalizeDouble(bid - (MissingTakeProfit / factor), digits);
      if (UseSlAtr) 
         take = NormalizeDouble(OrderOpenPrice() - AtrVal, digits);
   }//if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   
   result = ModifyOrder(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), take,
                        OrderExpiration(), clrNONE, __FUNCTION__, tpim);

 
UR97:

Dear Any Programmers,

I need help fixing this code

At the moment only the Add Missing Stop Loss is working.

I need to Add Missing Take Profit to be working aswell.


Below is the specific part of the file I need looking at.

Thank you



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Trade_SL_TP()
  {
// External
//-------------------------------------------------------------------------------------------------
//Average True Range for StopLoss and TakeProfit 
/*-------------------------------------------------------------------------------------------------
extern int    ATR_Period=14;//ATR Period
extern int    ATR_BufferShift=1;//ATR Buffer Shift
extern int    ATR_StopLoss_Multiplier=0;
extern int    ATR_TakeProfit_Multiplier=0;
extern string ATRStopsbreak="";//=======================================
double ATR;
int    ATR_SL_Multiplier;
int    ATR_TP_Multiplier;
double Ticket_Modify;
double StopLoss;
double TakeProfit;
/*-----------------------------------------------------------------------------------------------*/
//Average True Range StopLoss
   ATR=iATR(NULL,0,ATR_Period,ATR_BufferShift);

   if(Period()>=240)
     {
      ATR_StopLoss_Multiplier=2;
      ATR_TakeProfit_Multiplier=4;
     }
   if(Period()<240)
     {
      ATR_StopLoss_Multiplier=4;
      ATR_TakeProfit_Multiplier=6;
     }
/*-----------------------------------------------------------------------------------------------*/
   if(OrdersTotal()>0)
      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if((OrderType()==OP_BUY) || (OrderType()==OP_BUYLIMIT) || (OrderType()==OP_BUYSTOP))
              {
               if((OrderStopLoss()==0) || (OrderTakeProfit()==0))
                 {
                  StopLoss=OrderOpenPrice()-(ATR*ATR_StopLoss_Multiplier);
                  TakeProfit=OrderOpenPrice()+(ATR*ATR_TakeProfit_Multiplier);
                  Ticket_Modify=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,TakeProfit,0,clrNONE);
                 }
              }
            if((OrderType()==OP_SELL) || (OrderType()==OP_SELLLIMIT) || (OrderType()==OP_SELLSTOP))
              {
               if((OrderStopLoss()==0) || (OrderTakeProfit()==0))
                 {
                  StopLoss=OrderOpenPrice()+(ATR*ATR_StopLoss_Multiplier);
                  TakeProfit=OrderOpenPrice()-(ATR*ATR_TakeProfit_Multiplier);
                  Ticket_Modify=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,TakeProfit,0,clrNONE);
                 }
              }
           }
/*-----------------------------------------------------------------------------------------------*/
  }
 
Donald Gibson:

Dear Donald,


Thank you for posting a fix,


Someone has managed to fix the code however I am having a slight issue with the stop loss and take profit being printed one after the other.


Is it possible to have them print at the same time?


Thank you.

void InsertStopLoss(int ticket)
{
   //Inserts a stop los into a trade that lacks one.

   if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return;//Order has closed, so nothing to do.    
 //Print("Stop");
   if (!CloseEnough(OrderStopLoss(), 0) || (MissingStopLossPips == 0 && !UseSlAtr) ) 
      return; //Nothing to do
 //Print("Stop");
   double stop = 0;
   bool result = false;
  
   //There is the option for the user to use Atr to calculate the stop
   if (UseSlAtr) 
      AtrVal = iATR(OrderSymbol(), AtrSlTimeFrame, AtrSlPeriod, 0) * AtrSlMultiplier;
   
   // Buy trade
   if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   {
      stop = NormalizeDouble(OrderOpenPrice() - (MissingStopLoss / factor), digits);    
      if (UseSlAtr) 
         stop = NormalizeDouble(OrderOpenPrice() - AtrVal, digits);
   }//if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   
   
   // Sell trade
   if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   {
      stop = NormalizeDouble(OrderOpenPrice() + (MissingStopLoss / factor), digits); 
      if (UseSlAtr) 
         stop = NormalizeDouble(OrderOpenPrice() + AtrVal, digits);
   }//if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   
   result = ModifyOrder(OrderTicket(), OrderOpenPrice(), stop, OrderTakeProfit(), 
                        OrderExpiration(), clrNONE, __FUNCTION__, slim);
   
   
}// End void InsertStopLoss(int ticket)

void InsertTakeProfit(int ticket)
{
 
    //Inserts a take profit into a trade that lacks one.

   if (!BetterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return;//Order has closed, so nothing to do.    
 // Print("Take");
   if (!CloseEnough(OrderTakeProfit(), 0) || (MissingTakeProfitPips == 0 && !UseTpAtr) ) 
      return; //Nothing to do
 //Print("Take");
   double take = 0;
   bool result = false;
   
   //There is the option for the user to use Atr to calculate the stop
   if (UseTpAtr) 
      AtrVal = iATR(OrderSymbol(), AtrTpTimeFrame, AtrTpPeriod, 0) * AtrTpMultiplier;
   
   // Buy trade
   if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   {
      take = NormalizeDouble(OrderOpenPrice() + (MissingTakeProfit / factor), digits);
      if (UseTpAtr) 
         take = NormalizeDouble(OrderOpenPrice() + AtrVal, digits);
   }//if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
   
   
   // Sell trade
   if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   {
      take = NormalizeDouble(OrderOpenPrice() - (MissingTakeProfit / factor), digits);
      if (UseTpAtr) 
         take = NormalizeDouble(OrderOpenPrice() - AtrVal, digits);
   }//if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
   
   result = ModifyOrder(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), take,
                        OrderExpiration(), clrNONE, __FUNCTION__, tpim);
   
   
}// End void InsertTakeProfit(int ticket)
Files:
MPTM4V2r.mq4  53 kb
 
UR97:

Dear Donald,


Thank you for posting a fix,


Someone has managed to fix the code however I am having a slight issue with the stop loss and take profit being printed one after the other.


Is it possible to have them print at the same time?


Thank you.

I have no idea what you mean by Print at the same time.

 
Donald Gibson:

I have no idea what you mean by Print at the same time.

So right now, the stop loss is placed first then the take profit. So in my push notifications i get two separate messages about the trade being modified. I would like to know if its possible to have them placed at exactly the same time?
 
UR97:
So right now, the stop loss is placed first then the take profit. So in my push notifications i get two separate messages about the trade being modified. I would like to know if its possible to have them placed at exactly the same time?

The way I would do it is to combine the 2 functions and then send the notifications as 1.

Maybe 1 of theses experts around will provide a better solution.
 
Donald Gibson:

The way I would do it is to combine the 2 functions and then send the notifications as 1.

Maybe 1 of theses experts around will provide a better solution.
Hey donald,

Thanks for your quick reply. Would you be able to code that into the file attached?
 
UR97:
Hey donald,

Thanks for your quick reply. Would you be able to code that into the file attached?

No.

 
UR97:
So right now, the stop loss is placed first then the take profit. So in my push notifications i get two separate messages about the trade being modified. I would like to know if its possible to have them placed at exactly the same time?

Your code that you have posted does not include any SendNotification()

Post the section of code that does include both SendNotification()'s and somebody may be able to help you.