Multiple Order Entry Problem for live account with a specific broker - page 3

 
figurelli:
Hello FinanceEngineer, maybe would be better start checking your original code multiple orders problem, since if we do this probably we will address other critical points here and don't lose the focus, what do you think about?

Hello figurelli

 

This is my new code. I changed since I raised multiple order issue on this thread. 

So far this code is working fine with several brokers. No multiple order problem found yet after the change.   BlindMist can try this code to see if he can avoid the multiple order problem with that broker.

 

for(int i=0;i<10;i++)
   {

      
 
      if(volume<=0.0) break;
      

      if(Type==POSITION_TYPE_SELL)
      {
         request.type=ORDER_TYPE_SELL;
         request.price=SymbolInfoDouble(mSymbol,SYMBOL_BID);
         
         if(TP!=0) takeprofit = request.price-TP*mPoint;
         if(SL!=0) stoploss = request.price+SL*mPoint;
      }
      
      
      if(Type==POSITION_TYPE_BUY)
      {
         request.type=ORDER_TYPE_BUY;
         request.price=SymbolInfoDouble(mSymbol,SYMBOL_ASK);

         if(TP!=0) takeprofit = request.price+TP*mPoint;
         if(SL!=0) stoploss = request.price-SL*mPoint;
      }
      
      
      
      
      request.action          = TRADE_ACTION_DEAL;
      request.symbol        = mSymbol;
      request.volume        = MathMin(volume,SymbolInfoDouble(mSymbol,SYMBOL_VOLUME_MAX));
      request.sl                = stoploss;
      request.tp               = takeprofit;
      request.deviation     = SymbolInfoInteger(mSymbol,SYMBOL_SPREAD);
      request.type_filling   = ORDER_FILLING_FOK;
      request.comment     = strComment;
      request.magic         = EAMagic;
      
      
      
      if(!OrderCheck(request,check))
      {
         if(check.margin_level<100) volume-=SymbolInfoDouble(mSymbol,SYMBOL_VOLUME_STEP);
         Print("OrderCheck Code: ",check.retcode);
         continue;
      }
      

      
      bool checkOrderSend = OrderSend(request, result);
      
      if(result.retcode==10009 || result.retcode==10008)
      { 
          Print("OrderSend was successful. Code: ",result.retcode);
          volume-=result.volume; //If order was successful then reduce volume to 0.0, then the loop will be terminated.

          
          break;
      }
      else
      {
          Print(ResultRetcodeDescription(result.retcode));
      }
      

      Sleep(1000);
      
   }
   

 

 
 
FinanceEngineer:

Hello figurelli

 

This is my new code. I changed since I raised multiple order issue on this thread. 

So far this code is working fine with several brokers. No multiple order problem found yet after the change.   BlindMist can try this code to see if he can avoid the multiple order problem with that broker.

Please use SRC button when posting code.

The code you posted can't avoid (all) double orders, and in contrary it can provoke double orders in some case.

 
angevoyageur:

Please use SRC button when posting code.

The code you posted can't avoid (all) double orders, and in contrary it can provoke double orders in some case.

What would be better way if there is solution out there, except setting 10 hours of sleep inside loop ?
 
FinanceEngineer:

Hello figurelli

 

This is my new code. I changed since I raised multiple order issue on this thread. 

So far this code is working fine with several brokers. No multiple order problem found yet after the change.   BlindMist can try this code to see if he can avoid the multiple order problem with that broker.

 

Hi FinanceEngineer, note that at your original code you have Print("OrderSend Code: "...) at if { } and else { }. 

But else { } has a break, and your debug code shows retcode = 10008 (TRADE_RETCODE_PLACED) x 10.

So, by inference, you was printing the if { } condition debug and your break was never used.

if(!OrderSend(request,result) || result.deal==0 )
      {
         Print("OrderSend Code: ",result.retcode);
         if(result.retcode==TRADE_RETCODE_TRADE_DISABLED) break;
         if(result.retcode==TRADE_RETCODE_MARKET_CLOSED) break;
         if(result.retcode==TRADE_RETCODE_NO_MONEY) break;
         if(result.retcode==TRADE_RETCODE_TOO_MANY_REQUESTS) Sleep(5000);
         if(result.retcode==TRADE_RETCODE_FROZEN) break;
         if(result.retcode==TRADE_RETCODE_CONNECTION) Sleep(15000);
         if(result.retcode==TRADE_RETCODE_LIMIT_VOLUME) break;
         
      }
      else if(result.retcode==10009 || result.retcode==10008)
      { 
          Print("OrderSend Code: ",result.retcode);
          volume-=result.volume; //If order was successful then reduce volume to 0.0, then the loop will be terminated.
          
          if(Type == POSITION_TYPE_BUY) {mBuyPositionCnt = mBuyPositionCnt + 1.0; cntLotCalculation = cntLotCalculation + 1;}
          if(Type == POSITION_TYPE_SELL) {mSellPositionCnt = mSellPositionCnt + 1.0; cntLotCalculation = cntLotCalculation + 1;}
          break;
      }

Now you changed your code, and looks clearer, but note that you don't test anymore the return value of OrderSend(), just the result.retcode. You could correct this using variable checkOrderSend (*** look here), that I asked about before:

      bool checkOrderSend=OrderSend(request,result);

      if(checkOrderSend) // (*** look here)
        {
         if(result.retcode==10009 || result.retcode==10008)
           {
            Print("OrderSend was successful. Code: ",result.retcode);
            volume-=result.volume; //If order was successful then reduce volume to 0.0, then the loop will be terminated.
            break;
           }
         else
           {
            Print(ResultRetcodeDescription(result.retcode));
           }
        }
      else 
        {
         Print("OrderSend execution error.");
        }

So, in my opinion, first thing to do is correct this test and check again using the same problematic broker you reported. If the problem don't happen again, congratulations, you really solved the bug, i.e., this don't indicates that your code is future proof or the safest, but that we found the bug.

Actually, you can forget or avoid change your code again, since the bug is not happening, however, in this case, you must beware that you are not testing the return value of OrderSend() anymore.

Hope this information can help you.

 
figurelli:

Hi FinanceEngineer, note that at your original code you have Print("OrderSend Code: "...) at if { } and else { }. 

But else { } has a break, and your debug code shows retcode = 10008 (TRADE_RETCODE_PLACED) x 10.

So, by inference, you was printing the if { } condition debug and your break was never used.

Now you changed your code, and looks clearer, but note that you don't test anymore the return value of OrderSend(), just the result.retcode. You could correct this using variable checkOrderSend (*** look here), that I asked about before:

So, in my opinion, first thing to do is correct this test and check again using the same problematic broker you reported. If the problem don't happen again, congratulations, you really solved the bug, i.e., this don't indicates that your code is future proof or the safest, but that we found the bug.

Actually, you can forget or avoid change your code again, since the bug is not happening, however, in this case, you must beware that you are not testing the return value of OrderSend() anymore.

Hope this information can help you.

What about returned code 10010 ?

The double entry order can occurs on any broker, the probability to get it with Alpari is simply greater as you receive a lot more ticks.

 
angevoyageur:

What about returned code 10010 ?

The double entry order can occurs on any broker, the probability to get it with Alpari is simply greater as you receive a lot more ticks.

If you are asking me, as I told before, I don't consider this code future proof and safe enough and this is just one example about the lack of return code, so please read again.

However, if you are asking the code author, what about ask direct to him?

 
figurelli:

Hi FinanceEngineer, note that at your original code you have Print("OrderSend Code: "...) at if { } and else { }. 

But else { } has a break, and your debug code shows retcode = 10008 (TRADE_RETCODE_PLACED) x 10.

So, by inference, you was printing the if { } condition debug and your break was never used.

Now you changed your code, and looks clearer, but note that you don't test anymore the return value of OrderSend(), just the result.retcode. You could correct this using variable checkOrderSend (*** look here), that I asked about before:

So, in my opinion, first thing to do is correct this test and check again using the same problematic broker you reported. If the problem don't happen again, congratulations, you really solved the bug, i.e., this don't indicates that your code is future proof or the safest, but that we found the bug.

Actually, you can forget or avoid change your code again, since the bug is not happening, however, in this case, you must beware that you are not testing the return value of OrderSend() anymore.

Hope this information can help you.

Hello

 

I know it sounds strange. When I checked the returned value of OrderSend(request,result) in previous code, I got multiple order problem. Now in my new code, I don't check the returned value of OrderSend(request,result) ( but I still assigned the returned value to some variable to avoid error on new build of the terminal.

With new code, I don't get multiple order problem. I used the Alpari UK who has a reputation of sending a lot of ticks. My code may be not perfect but think this way. There are quite some number of returned code need to be checked in Meta Trader 5.

First of them, the returned value from OrderCheck, second of them is the returned value from OrderSend and third of them is the returned value assigned at result.retcode. I think whichever first two returned values are, what we should really care most is the last one plus amount of volume actually executed.

So based on this fact, I made my code simpler just going straight to check result.retcode. Please correct me if I am wrong. I think the order execution in MT5 is certainly far more sophisticated than MT4 and many of us are confused.

If we can't build a clear case using logic only then we can build a clear case using experiments. So I recommend BlindMist or any other people to try this code with their broker to see if skipping to check OrderSend function might be actually helpful.

 

Kind regards.

 
figurelli:
If you are asking me, as I told before, I don't consider this code future proof and safe enough and this is just one example about the lack of return code, so please read again.

However, if you are asking the code author, what about ask direct to him?

I asked to everyone participating in this topic. I missed your message about code 10010, where is it ?

 
angevoyageur:

I asked to everyone participating in this topic. I missed your message about code 10010, where is it ?

Hi Alain,

It's not clear to me what you need to know, since we are just talking about the new code of FinanceEngineer, and an advice about test the return code of OrderSend() that was changed from the original code.

Note that either his original code and his new one don't have code 10010 test, so if it's relevant to you why didn't you asked since the first post of him?

Anyway, can you explain why you would need code 10010 test for a FOK filling policy?

Since this is not the first time I see you and other Moderators talk about, do you know some cases where it's really necessary this code test for FOK (Fill Or Kill) orders, that you could share with us?

Thanks in advance.

 
FinanceEngineer:

Hello

 

I know it sounds strange. When I checked the returned value of OrderSend(request,result) in previous code, I got multiple order problem. Now in my new code, I don't check the returned value of OrderSend(request,result) ( but I still assigned the returned value to some variable to avoid error on new build of the terminal.

With new code, I don't get multiple order problem. I used the Alpari UK who has a reputation of sending a lot of ticks. My code may be not perfect but think this way. There are quite some number of returned code need to be checked in Meta Trader 5.

First of them, the returned value from OrderCheck, second of them is the returned value from OrderSend and third of them is the returned value assigned at result.retcode. I think whichever first two returned values are, what we should really care most is the last one plus amount of volume actually executed.

So based on this fact, I made my code simpler just going straight to check result.retcode. Please correct me if I am wrong. I think the order execution in MT5 is certainly far more sophisticated than MT4 and many of us are confused.

If we can't build a clear case using logic only then we can build a clear case using experiments. So I recommend BlindMist or any other people to try this code with their broker to see if skipping to check OrderSend function might be actually helpful.

 

Kind regards.

  • Skipping the OrderSend() return code is irrelevant about double orders problem. A simple coincidence.
  • OrderCheck() is also irrelevant, as it's only the prevent some errors BEFORE sending an order.
  • If OrderSend() returned false, you know there was a problem, so you can check which one and process it according to your needs.
  • If OrderSend() returned true, here begin the difficulties...see next message.
Reason: