Download MetaTrader 5

Breakeven not working

To add comments, please log in or register
Morgan Morden
682
Morgan Morden  

I have this in my EA and it compile and run with no errors. All trades are made in backtester as well. But it seems to ignore the breakeven totally...

Is there something wrong here?

extern double    Breakeven = 30;


//+------------------------------------------------------------------+
//| My BreakEven                                                     |
//+------------------------------------------------------------------+
void BreakEven()
  {
   double MyPoint=Point;
   if(Digits==3 || Digits==5) MyPoint=Point*10;

   for(int i=OrdersTotal()-1; i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(OrderMagicNumber()==Magic)
               if(OrderType()==OP_BUY)
                 {
                  if(Bid-OrderOpenPrice()>Breakeven*MyPoint)
                     if(OrderOpenPrice()>OrderStopLoss())
                        int BBM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
                 }

      else if(OrderType()==OP_SELL)
        {
         if(OrderOpenPrice()-Ask>Breakeven*MyPoint)
            if(OrderOpenPrice()<OrderStopLoss())
               int BSM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);
        }
     }
  }
whroeder1
16286
whroeder1  
fabian waldo: But it seems to ignore the breakeven totally...
  1. Do you even call the function?
  2. You would know the answer had you bothered to check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Morgan Morden
682
Morgan Morden  
whroeder1:
  1. Do you even call the function?
  2. You would know the answer had you bothered to check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

I added this to my code but nothing came up in errors in the journal. Also code compiles with no errors at all. 
     if ( OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == false)   //  do something if the OrderSelect did not work
       Alert("the order isn't close because Err no. ", GetLastError());
     }
Keith Watford
Moderator
10570
Keith Watford  

if ( OrderSelect(i, SELECT_BY_POS,MODE_TRADES) == false)   //  do something if the OrderSelect did not work
       Alert("the order isn't close because Err no. ", GetLastError());

That doesn't make sense. The alert should relate to the OrderSelect() failing.

Keith Watford
Moderator
10570
Keith Watford  
int BBM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE);

OrderModify() returns a bool, not an int.

If it returns false then print an error report

   if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrNONE))
      Print("Order Modify on ticket #",OrderTicket()," Failed. ERROR CODE",GetLastError());   
To add comments, please log in or register