Is Mql4 stable? Part2

 

Hi, I recently posed a question why I was getting unstable results and I got some helpful responses about the problems with the scope of the code. However, I've had such bizarre results (that have nothing to do with the scope) lately that have really stumped me and make me wonder if I should even continue with this project. Today, the ea made a sell order when I was at the computer which I knew could not possibly be correct. It was based on the values of two ema's. The ea printed the values of the ema's (which I coded to help in these situations) and they were nothing like the charts. I checked the data window and they were the same values as on the chart. Easy you say, you must have entered the wrong parameters into your ema code. But alas no, as I printed the values again a few minutes later (at a different point) and they had the correct values, ie the same as in the data window and on the chart. So, the values were all fine, but at that particular instant (ie minute, as it was a minute chart) it had wrong values.

Also I have a function that is a trailing stop that trails 10 pips. Usually, no problem, it works fine. But sometimes, for no reason it just doesn't work. I've included the code. Last night, I had a sell that was 40 pips in profit, but it didn't move from the first stop loss (ie. it just placed a stoploss 5 pips behind the original opening price. This is a function and can't see how it can stuff up as it is automatic. I know it's always in the code, but this seems illogical. 

Any help would be much appreciated. By the way this is a demo, but can't see how that would affect it.

void SStopVer_5 (int &TicketNo)
   {
   if(OrderSelect(TicketNo,SELECT_BY_TICKET)==true)
      {
      if(OrderStopLoss()== 0)
         {
         bool res=OrderModify(TicketNo,OrderOpenPrice(),OrderOpenPrice() +50*Point,OrderOpenPrice() -500*Point,0,0);
         if(res==0)Alert(TicketNo,"failed to modify. Error",GetLastError());
         }
         
      if(Ask +105*Point < OrderStopLoss() && Ask +100*Point < OrderOpenPrice() )
         {
         bool res=OrderModify(TicketNo,OrderOpenPrice(),Ask +100*Point,OrderOpenPrice() -500*Point,0,0);
         if(res==0)Alert(TicketNo,"failed to modify. Error",GetLastError());
         }
      
      if(OrderCloseTime() > 0)
         {
         TicketNo = 0;
         return;
         }
      }
      return;
   } 
 

Mark Boc:, I recently posed a question why I was getting unstable results and I got some helpful responses about the problems with the scope of the code.

However, I've had such bizarre results (that have nothing to do with the scope) lately that have really stumped me and make me wonder if I should even continue with this project.

Today, the ea made a sell order when I was at the computer which I knew could not possibly be correct. It was based on the values of two ema's.

Also I have a function that is a trailing stop that trails 10 pips. Usually, no problem, it works fine. But sometimes, for no reason it just doesn't work.

  1. The problem was in your code, so stop making posts with the title MQl4 stable. It's the same code you should have continued that thread.
  2. The problem is still your code.
  3. Couldn't possibly be correct, the problem is still your code but you don't show that part, so no one can help you with it.
  4. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here and our crystal balls are cracked.
    Use the debugger or print out your variables, including _LastError and find out why.

  5. Why are you testing the OrderCloseTime at the bottom instead of the first thing?

  6. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
 
Mark Boc:

Hi, I recently posed a question why I was getting unstable results and I got some helpful responses about the problems with the scope of the code. However, I've had such bizarre results (that have nothing to do with the scope) lately that have really stumped me and make me wonder if I should even continue with this project. Today, the ea made a sell order when I was at the computer which I knew could not possibly be correct. It was based on the values of two ema's. The ea printed the values of the ema's (which I coded to help in these situations) and they were nothing like the charts. I checked the data window and they were the same values as on the chart. Easy you say, you must have entered the wrong parameters into your ema code. But alas no, as I printed the values again a few minutes later (at a different point) and they had the correct values, ie the same as in the data window and on the chart. So, the values were all fine, but at that particular instant (ie minute, as it was a minute chart) it had wrong values.

Also I have a function that is a trailing stop that trails 10 pips. Usually, no problem, it works fine. But sometimes, for no reason it just doesn't work. I've included the code. Last night, I had a sell that was 40 pips in profit, but it didn't move from the first stop loss (ie. it just placed a stoploss 5 pips behind the original opening price. This is a function and can't see how it can stuff up as it is automatic. I know it's always in the code, but this seems illogical. 

Any help would be much appreciated. By the way this is a demo, but can't see how that would affect it.

There could be lots of problems in your code. E.G. if your EA would like to manage an order with another Symbol than in the chart, if your EA can't select the order by ticket... Of course it is also important when you call the trailing function. But we don't see your whole code. I've done a basic fix your code. You can try it and you'll see.

void SStopVer_5(int &TicketNo)
  {
   if(OrderSelect(TicketNo,SELECT_BY_TICKET))
     {
      if(OrderCloseTime()>0)
        {
         TicketNo=0;
         return;
        }
      string symbol=OrderSymbol();
      double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
      if(OrderStopLoss()<DBL_EPSILON)
         if(!OrderModify(TicketNo,OrderOpenPrice(),NormalizePrice(OrderOpenPrice()+50*point,symbol),NormalizePrice(OrderOpenPrice()-500*point,symbol),0,0))
            Alert(TicketNo,"failed to modify. Error",GetLastError());
      double ask=SymbolInfoDouble(symbol,SYMBOL_ASK);
      if(ask+105*point<OrderStopLoss() && ask+100*point<OrderOpenPrice())
         if(!OrderModify(TicketNo,OrderOpenPrice(),NormalizePrice(ask+100*point,symbol),NormalizePrice(OrderOpenPrice()-500*point,symbol),0,0))
            Alert(TicketNo,"failed to modify. Error",GetLastError());
     }
   else Alert(TicketNo,"failed to select order. Error",GetLastError());
  }

double NormalizePrice(const double price,const string symbol)
  {
   if(price<DBL_EPSILON) return(0.0);
   double tickSize=SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_SIZE);
   return(round(price/tickSize)*tickSize);
  }

BTW you should stop using: if(OrderSelect(TicketNo,SELECT_BY_TICKET)==true) or if(res==0).

Just: if(OrderSelect(TicketNo,SELECT_BY_TICKET)) or if(!res)

 
Petr Nosek:

There could be lots of problems in your code. E.G. if your EA would like to manage an order with another Symbol than in the chart, if your EA can't select the order by ticket... Of course it is also important when you call the trailing function. But we don't see your whole code. I've done a basic fix your code. You can try it and you'll see.

BTW you should stop using: if(OrderSelect(TicketNo,SELECT_BY_TICKET)==true) or if(res==0).

Just: if(OrderSelect(TicketNo,SELECT_BY_TICKET)) or if(!res)

Thanks Petr! I should have added that it was only for one Symbol. I will try your suggestions out. Thanks!

Reason: