Please help me with this strange problem!

 

I have an EA that I run on 2 live accounts with 2 different brokers. With the first broker the EA has been running for more than 6 months and I have perhaps seen this problem appear maybe once or twice. With the second broker which i have been using for just more than a month now the problem has appeared three times already in this short time and is costing me a lot of money.

What happens is that when the EA sends through an order to BUY or SELL - instead of one order executed - it executes two or three similar orders simultaneously.

I have tried to recreate this problem in the Strategy Tester over many days without any success. It only happens on a live account.

I have a limit of 10 orders that can be executed but the process stops at 2 or 3 orders and never goes beyond that although there i enough funds to do so.

From the code below you will see that i make the condition for executing an order "false" immediately after the ordersend has been successfully executed:

input int      StochHighLevel1 = 75;
input int      StochHighLevel2 = 70;
input int      StochLowLevel1 = 25;
input int      StochLowLevel2 = 30;
input int      ReSellBuyTotal = 10;

   Buy = 0;
   Sell = 0; 

   Count = 0; 

   for(Count = OrdersTotal()-1; Count >= 0; Count--)
     {
      if(OrderSelect(Count, SELECT_BY_POS)
         && (OrderMagicNumber() == MagicNumber || OrderMagicNumber() == 0)
         && OrderSymbol() == Symbol()
         && OrderCloseTime() == 0)

        {
         if(OrderType()== OP_SELL)
            Sell++;

         if(OrderType()== OP_BUY)
            Buy++;
        }
     }
     
     if(Buy > 0 && Stoch1 < StochLowLevel1) ReBuy = true;
     
     
     if(ReBuy == true && Buy <= ReSellBuyTotal && Stoch1 > StochLowLevel2)
     
     {
     
         BuyLotSize1 = NormalizeDouble(BuyLotSize*BuyLot,2);
     

       if(IsTradeContextBusy())
            Sleep(10);

         RefreshRates();
         
         // Print("BuyStopLoss is ", BuyStopLoss);
         
         BuyTicket = OrderSend(Symbol(),OP_BUY,BuyLotSize1,Ask,UseSlippage,0,0,"OBOS system ReBuy",MagicNumber,0,Green);


         if(BuyTicket == -1)
           {
            ErrorCode = GetLastError();
            ErrDesc = ErrorDescription(ErrorCode);

            ErrAlert = StringConcatenate("OBOS ReBuy Order - Error ",ErrorCode,": ",ErrDesc);
            Alert(ErrAlert);

            ErrLog = StringConcatenate(" Lots: ",BuyLotSize1," Price: ",Ask);
            Print(ErrLog);

           }
         BuyCurrPair = Symbol();
         
ReBuy = false;
         PlaySound("Alert.wav");
         Alert("ALERT: OBOS ReBUY trade opened " + BuyCurrPair);
         SendMail("OBOS System ReBUY trade opened ", BuyCurrPair + " Date = " + TimeToStr(TimeCurrent(),TIME_DATE) + "; " + " Time = " +  TimeToStr(TimeCurrent(),TIME_MINUTES) + "; " + "GMT = +3");
        
      }
      
      Print("Rebuy is ",ReBuy);
      
     if(Sell > 0 && Stoch1 > StochHighLevel1) ReSell = true;
     
     if(ReSell == true && Sell <= ReSellBuyTotal && Stoch1 < StochHighLevel2)
     
      {
           
     SellLotSize1 = NormalizeDouble(SellLotSize*SellLot,2);
     
     
         if(IsTradeContextBusy())
            Sleep(10);
            
         RefreshRates();   

         SellTicket = OrderSend(Symbol(),OP_SELL,SellLotSize1,Bid,UseSlippage,0,0,"OBOS system ReSell",MagicNumber,0,Red);


         if(SellTicket == -1)
           {
            ErrorCode = GetLastError();
            ErrDesc = ErrorDescription(ErrorCode);

            ErrAlert = StringConcatenate("Open OBOS Sell Order - Error ",ErrorCode,": ",ErrDesc);
            Alert(ErrAlert);

            ErrLog = StringConcatenate(" Lots: ",SellLotSize1," Price: ",Bid);
            Print(ErrLog);

           }
            SellCurrPair = Symbol();
            
ReSell = false;
            PlaySound("Alert.wav");
            Alert("ALERT: OBOS System ReSELL trade opened " + SellCurrPair);
            SendMail("OBOS System ReSELL trade opened ", SellCurrPair + " Date = " + TimeToStr(TimeCurrent(),TIME_DATE) + "; " + " Time = " +  TimeToStr(TimeCurrent(),TIME_MINUTES) + "; " + "GMT = +3");
         
     }     
 
 

Overnight the EA opened 10 trades simultaneously with one broker but luckily not wit the other broker. Could it be that the problem is caused by the one particular broker's price feed? Or does it  only happen with particular currency pairs? Or is it something to do with my EA?

 
  1. Ernest Klokow: Or is it something to do with my EA?

    How To Ask Questions The Smart Way. 2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2.      if(Buy > 0 && Stoch1 < StochLowLevel1) ReBuy = true;
         if(ReBuy == true && Buy <= ReSellBuyTotal && Stoch1 > StochLowLevel2)
    If Stoch1 < StochLowLevel1 you buy. On the next tick is the condition will likely be true, and you buy again …
 

Thank you William for that response. Also for the links of how to ask questions the smart way. I can certainly learn from that (And I am not groveling! :-))

I am not sure if I understand correctly.

You say "If Stoch1 < StochLowLevel1 you buy. On the next tick is the condition will likely be true, and you buy again … ", but I only actually Buy when the next condition/line is true (which has a completely different Stoch level (as can be seen from my inputs).

Then I immediately set the ReBuy condition as "false" after the ordersend has activated a Buy transaction. And since this only happens when the Stoch is a completely different value the ReBuy cannot be set to "true" again during the same (milli)second. So why activate more Buy transactions during the same (milli)second again? And why most of the time does it not happen and when it happens it is only for one or two extra transactions. Not the 10 that the ReBuyTotal allows?

Am I missing something here?

 
Ernest Klokow: Am I missing something here?

You are guessing. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

 

I do not expect anyone to debug my code. All I was hoping for was some guidance or direction where to look for the problem. I was hoping somebody would have had the same experience as me and could give some advice. I have printed out the variables but could not discover any problem from that. As you can see I do test for errors in my code but there are no errors reported.

            ErrorCode = GetLastError();
            ErrDesc = ErrorDescription(ErrorCode);

            ErrAlert = StringConcatenate("Open OBOS Sell Order - Error ",ErrorCode,": ",ErrDesc);
            Alert(ErrAlert);

            ErrLog = StringConcatenate(" Lots: ",SellLotSize1," Price: ",Bid);
            Print(ErrLog);

From the logic of the code this problem should not occur in my opinion - unless I miss something. But I really hope that my problem does lie in my code because to find the problem with e.g. the broker's price feed will be nigh impossible.

Something that worries me is that this problem only occurs in a minority of cases and seemingly only with certain brokers and currency pairs. But I know from expereince that one can overlook a problem in the code even after going through it tens of times. So hopefully that is where the problem is.

 
Ernest Klokow: I do not expect anyone to debug my code. All I was hoping for was some guidance or direction where to look for the problem.

Asked and answered - #2.2. We can't debug your code — we can't see your code.  What part of “Use the debugger or print out your variables” is unclear? Why did you post again without doing it?

 

Your comment: " What part of “Use the debugger or print out your variables” is unclear? Why did you post again without doing it?"

Perhaps you missed the following in my previous post?

"I have printed out the variables but could not discover any problem from that."

 
     if(Buy > 0 && Stoch1 < StochLowLevel1) ReBuy = true;
     if(ReBuy == true && Buy <= ReSellBuyTotal && Stoch1 > StochLowLevel2)

Show me the printed output of Buy, Stoch1, and ReBuy when it opened another order.

I saw your previous post — irrevalent, you did not print your variables as asked.
 

Ok! Just one thing - the EA works perfectly in the Strategy Tester so i assume it would not be of any help to give you those variables. 

I think what you actually want is the variables printed from a live account as the problems occur. I will put that in place although it may be a while before i can have the variables as when they appear when multiple trades are opened - since this happens only now and then as well as randomly on different currency pairs.

Thank you for your assistance so far as well as your patience.