Stoploss to break Even; OrderModify;

 
//+------------------------------------------------------------------+
//|                                                         myMA.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

double direction,stopbuy,ATR,EMACur,longcondition;
double topline, bottomline;

double a = 2.0;
int barstemp=0;

static double OP,takeprofitbuy;
static bool Buy_Once=false; 
static bool Buy_Once2=false; 
static bool Sell_Once=false; 
static bool Sell_Once2=false; 


int buy = 0;
int buy1 = 0;

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int DeleteObjects()
{
   int obj_total=ObjectsTotal();
   for(int i = obj_total - 1; i >= 0; i--)
   {
       string label = ObjectName(i);

   }     
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

//| Custom indicator deinitialization function                       |
  int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+----------------------------------------------------

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int start()
{
       
         double PositionSize = NormalizeDouble (AccountBalance()/100000,2);
         int    counted_bars=IndicatorCounted();

         int limit=Bars-counted_bars;
         int i=0; i<limit; i++;            
      
OP = OrderOpenPrice();

            
         if(barstemp!=Bars)
         {
         barstemp = Bars;
  
         ATR = iATR(Symbol(),Period(),14,1);
         EMACur = iMA(Symbol(),Period(), 27, 0, MODE_EMA, PRICE_WEIGHTED, 1);

         longcondition = (Close[1] > EMACur && Open[1]<EMACur && !Buy_Once);   
         stopbuy =  (EMACur - 80*Point);       
         takeprofitbuy = (Close[1] + (ATR * a));


  if (longcondition)  
         {

         if (OrdersTotal() <= 2)
         
          buy = OrderSend (Symbol(),OP_BUY,PositionSize,Ask,3,stopbuy,takeprofitbuy,NULL,0,0,Green);
          buy1 = OrderSend (Symbol(),OP_BUY,PositionSize,Ask,3,stopbuy,(Ask + 1200*Point),NULL,0,0,Green);
 
         if (buy > 0)
            {
               Buy_Once=true;
               Sell_Once=false;
            }
            
         if (buy1 > 0)
            {
               Buy_Once=true;
              Sell_Once=false;
            } 
         
         }

   
//check the break even stop
CheckBreakEvenStop(); 
       
     
      } //End of 'DaysToPlot 'if' statement. 
       return(0);
}


void CheckBreakEvenStop ()
 {
         for (int b = OrdersTotal()-1; b>=0;b--)
           {
               if (OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
                  if (OrderSymbol() == Symbol())
                     if(OrderType() == OP_BUY)
                        //if (Ask - OP > takeprofitbuy*Point())
                           if (Ask > OP + (takeprofitbuy*Point()))
                              if (OP > OrderStopLoss())
                                  OrderModify(OrderTicket(),OP,(OP + (10*Point())),OrderTakeProfit(),0,Purple);       
            }       
            
 }         


My EA opens two trades, with different take profit values, everything else remains the same. Now what i want to do is Move my stop loss to entry when take profit 1 is hit. The problem is my take profit value is not in pips so i cannot just say to move stop loss to entry when after 50 pips.

takeprofitbuy = (Close[1] + (ATR * a));

So when i'm setting the rules to modify the order i stated that if the Ask price is greater than the Order Open Price +  take profit then the order should be modified

if (Ask > OP + (takeprofitbuy*Point()))
  OrderModify(OrderTicket(),OP,(OP + (10*Point())),OrderTakeProfit(),0,Purple);   

and move the stop loss to breakeven. However, as soon as the trade is executed the stop loss moves to breakeven instead of waiting for the first position to close


            

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. midnightcowboy: The problem is my take profit value is not in pips.
    SL/TP is always a price, never PIPs, and it has nothing to do with your want.

  3. midnightcowboy: i want to do is Move my stop loss to entry when take profit 1 is hit.
    You opened two orders. When you only have one opened, you know the other must have hit the TP. Move to BE.
  4.          int    counted_bars=IndicatorCounted();
    

    Indicators can not trade. Function fails on EAs.

  5. OP = OrderOpenPrice();

    You can not use any Trade Functions until you first select an order.

  6.          int i=0; i<limit; i++;
    What do you think that line of code does?

  7.          if(barstemp!=Bars)
             {
             barstemp = Bars;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum 2011.05.06

  8.    int obj_total=ObjectsTotal();
       for(int i = obj_total - 1; i >= 0; i--)
       {
           string label = ObjectName(i);
    
       }     
    What do you think those lines of code do?

  9. OrderModify(OrderTicket(),OP,(OP + (10*Point())),OrderTakeProfit(),0,Purple);

    Check your return codes, and report your errors.

    Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

 
Kindly see Code above
 
midnightcowboy: Kindly see Code above

Kindly see comments #1.4 onward.

 
William Roeder:

Kindly see comments #1.4 onward.

Thank you for your comments, you have pointed me in the right direction and now the code does what i wanted it to. 

I also made some other changes based on your suggestion, Thanks again man. 

Reason: