I have lots of error in my Risk free function .

 

Hi 
I wrote a function for set SL equal to OrderOpenPrice. 

It is working but in strategy tester  Journal I see many errors befor and even after this action .

would you please tell me whats wrong?

void RiskFree()
  {
   bool result;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_BUY)
           {
            if(Bid>=OrderOpenPrice()+(point*BreakEvenPips) && 
               OrderStopLoss()<OrderOpenPrice())
              {
               result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
               if(result)
                 {
                  Alert("RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket());
                  Print("**************************************************  RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket(),"  **************************************************");
                 }
               if(!result)
                 {
                  err=GetLastError();
                  Alert("RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                  Print("**************************************************  RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                 }
              }
           }

         if(OrderType()==OP_SELL)
           {
            if(Ask<=OrderOpenPrice() -(point*BreakEvenPips) && 
               (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0))
              {
               result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
               if(result) Alert("RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket());
               Print("**************************************************  RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket(),"  **************************************************");
               if(!result)
                 {
                  err=GetLastError();
                  Alert("RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                  Print("**************************************************  RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                 }
              }
           }
        }
     }
  }


 

 
if(Bid>=OrderOpenPrice()+(point*BreakEvenPips) && 
What is the value of point*BreakEvenPips? You can't move stops (or pending prices) closer to the market than the minimum (MODE_STOPLEVEL * _Point.)
          Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
 
whroeder1:
What is the value of point*BreakEvenPips? You can't move stops (or pending prices) closer to the market than the minimum (MODE_STOPLEVEL * _Point.)
          Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

Hi whroeder1, thanks for reply.
It seems the minimum distance between stop and price  is not the problem .
I send  complete code , 
note there is not any strategy and I'm just trying to write a riskfree function. 

input double   LOT=0.1;
input int      TakeProfit=22;     
input int      StopLoss=15;    
int         BreakEvenPips=7;
double point;
bool modified;

int err;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   point=Point;
   if((Digits==3) || (Digits==5))
     {
      point*=10;
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//
   if(OrdersTotal()==0)
     {
      int ticket=OrderSend(Symbol(),OP_BUY,LOT,Ask,3,Bid-(StopLoss*point),Bid+(TakeProfit*point),"My order",0,0,clrGreen);
      if(ticket<0)
        {
         Print("OrderSend failed with error #",GetLastError());
        }
      else
        {
         Print("************************************************ Order Placed Buy Type #1 ************************************************");
        }
     }

   RiskFree();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void RiskFree()
  {
   bool result;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_BUY)
           {
            if(Bid>=OrderOpenPrice()+(point*BreakEvenPips) && 
               OrderStopLoss()<OrderOpenPrice() )
              {
               result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
               if(result)
                 {
                  Alert("RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket());
                  Print("**************************************************  RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket(),"  **************************************************");
                 }
               if(!result)
                 {
                  err=GetLastError();
                  Alert("RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                  Print("**************************************************  RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                 }
              }
           }

         if(OrderType()==OP_SELL)
           {
            if(Ask<=OrderOpenPrice() -(point*BreakEvenPips) && 
               (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0))
              {
               result=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
               if(result) Alert("RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket());
               Print("**************************************************  RISK FREE DONE  ",OrderSymbol(),"Ticket #",OrderTicket(),"  **************************************************");
               if(!result)
                 {
                  err=GetLastError();
                  Alert("RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                  Print("**************************************************  RISK FREE FAILED ",OrderSymbol()," ticket no ",OrderTicket()," failed with error (",err);
                 }
              }
           }
        }
     }
  }
 
  1.    if(OrdersTotal()==0){
    
       for(int i=OrdersTotal()-1;i>=0;i--){
          if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  2. Use the debugger or print out your variables, including _LastError and prices and find out why.
Reason: