Download MetaTrader 5

Move to break even once takeprofit() is hit? - page 2

To add comments, please log in or register
MQL5 Cloud Network speeds up calculations. Try it!
Dominic Gilbert
1639
Dominic Gilbert 2014.09.17 19:20  
Finally got it working! Thanks WHRoeder for you help! I haven't written it easy to read for some peoples monitors, but none the less I finally fixed the bugger!

//+----------------------------------------------------------------------------------------------------------------------------------------+
// Move to break even function  
//+----------------------------------------------------------------------------------------------------------------------------------------+ 

void Move_To_BreakEven()
 {
  
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( !OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
      if( OrderMagicNumber()== MagicNumber1 || OrderMagicNumber()== MagicNumber2 
         || OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 )
        if(OrderSymbol() == Symbol())
        { 
                   
        double new_sell_SL = OrderOpenPrice()-(PipsToLockIn*pips);
        double new_buy_SL  = OrderOpenPrice()+(PipsToLockIn*pips);

        if( OrderType() == OP_BUY )
          {     
            
            if(OrderMagicNumber() == MagicNumber1 ){
            TakeProfit_1 = OrderTakeProfit();}
                   
          if( OrderStopLoss() > new_buy_SL - Point )continue;  
          if( Bid > TakeProfit_1 - Point )
            {
            Buy_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),new_buy_SL,OrderTakeProfit(),0,CLR_NONE);  
            if(Buy_StopLoss_Break_Even1 == True)Print("BUY B/E was successful. Order ticket was: ", OrderTicket()); 
            if(Buy_StopLoss_Break_Even1 != True)Print("BUY B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
            } 
          }    
               
        if( OrderType() == OP_SELL )     
           { 
             if(OrderMagicNumber() == MagicNumber1 ){
             TakeProfit_1 = OrderTakeProfit();}
                  
           if( new_sell_SL > OrderStopLoss() - Point )continue;
           if( TakeProfit_1 > Ask - Point ) 
             { 
             Sell_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),new_sell_SL,OrderTakeProfit(),0,CLR_NONE);
             if(Sell_StopLoss_Break_Even1 == True)Print("SELL B/E was successful. Order ticket was:", OrderTicket());
             if(Sell_StopLoss_Break_Even1 != True)Print("SELL B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());             
             }    
           }
        }
    }   
 }
Dominic Gilbert
1639
Dominic Gilbert 2014.09.18 13:49  
Hmmm I stand corrected - still getting OrderModify error 130 on this one?
Dominic Gilbert
1639
Dominic Gilbert 2014.09.19 10:47  

Anyone able? I've come up with this as a solution... seems to be running ok!

//+-------------------------------------------------------------------------------
// Move to break even function  
//+-------------------------------------------------------------------------------

void Move_To_BreakEven()
 {
    //- Select SELL Trade with SECOND target --------------------// 
    if(OrderSelect(SellTicketOrder1, SELECT_BY_TICKET) == True)
      {
         if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() ){
         Sell_TakeProfit_1 = OrderTakeProfit();}
      }
      
    //- Select BUY Trade with SECOND target ---------------------//      
    if(OrderSelect(BuyTicketOrder1, SELECT_BY_TICKET) == True)
      {
         if( OrderMagicNumber() == MagicNumber1 && OrderSymbol() == Symbol() ){
         Buy_TakeProfit_1 = OrderTakeProfit();}
      }


   for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( !OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
      if( OrderMagicNumber()== MagicNumber1  )
        if(OrderSymbol() == Symbol())
        { 
                   
        double new_sell_SL = OrderOpenPrice()-(PipsToLockIn*pips);
        double new_buy_SL  = OrderOpenPrice()+(PipsToLockIn*pips);

        if( OrderType() == OP_BUY )
          {     
      
          if( OrderStopLoss() > new_buy_SL - Point )continue;  
          if( Bid > Buy_TakeProfit_1 - Point )
            {
            Buy_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),new_buy_SL,OrderTakeProfit(),0,CLR_NONE);  
            if(Buy_StopLoss_Break_Even1 == True)Print("BUY B/E was successful. Order ticket was: ", OrderTicket()); 
            if(Buy_StopLoss_Break_Even1 != True)Print("BUY B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
            } 
          }    
               
        if( OrderType() == OP_SELL )     
           { 
   
           if( new_sell_SL > OrderStopLoss() - Point )continue;
           if( Sell_TakeProfit_1 > Ask - Point ) 
             { 
             Sell_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),new_sell_SL,OrderTakeProfit(),0,CLR_NONE);
             if(Sell_StopLoss_Break_Even1 == True)Print("SELL B/E was successful. Order ticket was:", OrderTicket());
             if(Sell_StopLoss_Break_Even1 != True)Print("SELL B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());             
             }    
           }
        }
    }   
 }
12
To add comments, please log in or register