Need Help Fixing an Expert Adviser Code

 

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 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(OrderTakeProfit(), 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(OrderOpenPrice() + (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(OrderOpenPrice() - (MissingTakeProfit / factor), digits);

      if (UseSlAtr) 

         take = NormalizeDouble(OrderOpenPrice() - AtrVal, digits);

   }//if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)

   

   result = ModifyOrder(OrderTicket(), OrderOpenPrice(), take, OrderTakeProfit(),

                        OrderExpiration(), clrNONE, __FUNCTION__, tpim);

   

   

}// End void InsertTakeProfit(int ticket)

 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.

 
Sergey Golubev:

Dear Sergey,


Thank you for the reply,


I will be posting the code I need looking at, the MQ4 file is attached to the original post.


All credit goes to the author of the code.

I am simply trying to find a fix to a section of the code.


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);
Reason: