Why multiple buys and close at stops from the tester ?

 

I'll keep working on finding out why this happens, and post some code tomorrow but the code is long so I wanted to ask generically with this pic. 

Tester places multiple orders in a row, then closes at stops multiple times in a row. 

Stops were not hit as shown in the tester. I know how to limit orders using generally known methods and this should not happen. 

I don't think it happens in the demo account for testing, but I'll double check again. 

See images and terminal output. 

See multiple buys with closes at stop with prices well below marketprices.


terminal orders


Notice the chart multiple buys and closes at stop, and notice the terminla buys and close at stops. 
Stops are 10 pips away which would be about -$100+spread etc. not -$13 ?
You can see on the chart stops were not hit and candle prices were not even close. This all happened on the same candle it would seem. 
As if the stop was hit based on 5 digits, but you can see my stop accounts for the 5 digits and correctly sets the stop accordingly. 

I will try on the demo but I don't think it does this and from the terminal read out you can see that it should not have reacted this way. 

Anyhow, speculations ? 

I'll post code tomorrow, but wanted to see if anyone has seen anything like this before or could speculate about it for now. 
Thanks

 

The positions were closed by the end of the test, but the closing price is weird.

Check the tester log.

 
Alain Verleyen #:

The positions were closed by the end of the test, but the closing price is weird.

Check the tester log.

So mostly the logs look like this at the beginning. 

0       08:19:52.103    Custom indicator MACD True EURUSD,M5: loaded successfully
0       08:19:52.119    TestGenerator: current spread 14 used
2       08:19:52.133    2023.06.19 00:00:00  MultiMACD inputs: TakeProfit=20; Lots=1; StopLoss=10; MagicNumber=123486; StartTime=6; EndTime=12; NumberOfTrades=1; fibo_retrace=0.5; fibo_extension=0.618; 
2       08:20:03.209    2023.06.19 00:55:00  MultiMACD EURUSD,M5: open #1 buy 1.00 EURUSD at 1.09401 sl: 1.09287 tp: 1.09587 ok
0       08:20:03.209    2023.06.19 00:55:00  MultiMACD EURUSD,M5: BUY order opened : 1.09401
2       08:20:03.218    2023.06.19 00:55:11  MultiMACD EURUSD,M5: open #2 buy 1.00 EURUSD at 1.09398 sl: 1.09284 tp: 1.09584 ok
0       08:20:03.218    2023.06.19 00:55:11  MultiMACD EURUSD,M5: BUY order opened : 1.09398
2       08:20:03.227    2023.06.19 00:55:22  MultiMACD EURUSD,M5: open #3 buy 1.00 EURUSD at 1.09396 sl: 1.09282 tp: 1.09582 ok

Then after all these buys it gets to the point where it looks like spread and margin problems or something. I don't completely understand the log. 

3       08:20:03.415    2023.06.19 01:00:00  Tester: not enough money for buy 1.00 EURUSD at 1.09380 sl: 1.09266 tp: 1.09566 [2023.06.19 01:00]
3       08:20:03.415    2023.06.19 01:00:00  Tester: PrevBalance: 50000.00, PrevPL: -483.00, PrevEquity 49517.00, PrevMargin: 48127.62, NewMargin: 50312, FreeMargin: -794.58
3       08:20:03.415    2023.06.19 01:00:00  MultiMACD EURUSD,M5: OrderSend error 134
0       08:20:03.415    2023.06.19 01:00:00  MultiMACD EURUSD,M5: Error opening BUY order : 134
3       08:20:03.424    2023.06.19 01:00:03  Tester: not enough money for buy 1.00 EURUSD at 1.09383 sl: 1.09269 tp: 1.09569 [2023.06.19 01:00]
3       08:20:03.424    2023.06.19 01:00:03  Tester: PrevBalance: 50000.00, PrevPL: -417.00, PrevEquity 49583.00, PrevMargin: 48127.62, NewMargin: 50313, FreeMargin: -729.96
3       08:20:03.425    2023.06.19 01:00:03  MultiMACD EURUSD,M5: OrderSend error 134
0       08:20:03.425    2023.06.19 01:00:03  MultiMACD EURUSD,M5: Error opening BUY order : 134
3       08:20:03.434    2023.06.19 01:00:06  Tester: not enough money for buy 1.00 EURUSD at 1.09385 sl: 1.09271 tp: 1.09571 [2023.06.19 01:00]
3       08:20:03.434    2023.06.19 01:00:06  Tester: PrevBalance: 50000.00, PrevPL: -373.00, PrevEquity 49627.00, PrevMargin: 48127.62, NewMargin: 50314, FreeMargin: -686.88

Then this happens after I stop the tester. 

[2023.06.19 01:43]
3       08:20:17.455    2023.06.19 01:43:28  Tester: PrevBalance: 50000.00, PrevPL: 18.00, PrevEquity 50018.00, PrevMargin: 50315.82, NewMargin: 52511, FreeMargin: -2492.56
3       08:20:17.455    2023.06.19 01:43:28  MultiMACD EURUSD,M5: OrderSend error 134
0       08:20:17.456    2023.06.19 01:43:28  MultiMACD EURUSD,M5: Error opening BUY order : 134
2       08:20:17.456    2023.06.19 01:43:28  Tester: order #23 is closed
2       08:20:17.456    2023.06.19 01:43:28  Tester: order #22 is closed
2       08:20:17.456    2023.06.19 01:43:28  Tester: order #21 is closed
2       08:20:17.456    2023.06.19 01:43:28  Tester: order #20 is closed

With my BUY(), SELL(), CloseTrades() and input  int  NumberOfTrades=1 it doesn't seem this should even be allowed to occur. 

I'll post my functions since the rest of the code should not be relevant since the trading is limited with the NumberofTrades. 
Am I missing something in these functions that could allow such a number of trades ? 

//+------------------------------------------------------------------+ 
//| BUY                      BUY                 BUY                 |
//+------------------------------------------------------------------+  
//------------------------------------------------------------------------------------
void BUY()
  {
   for(int i=NumberOfTrades-1; i>=0; i--)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,(Ask),3,(Bid-StopLoss*pips2dbl),(Bid+TakeProfit*pips2dbl),"BUY",MagicNumber+i,0,PaleGreen);
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("BUY order opened : ",OrderOpenPrice());
        }
      else
         Print("Error opening BUY order : ",GetLastError());
      // return;
     }
  }
//+------------------------------------------------------------------+  
//+------------------------------------------------------------------+
//| SELL             SELL                       SELL                 |
//+------------------------------------------------------------------+                  
//------------------------------------------------------------------------------------
void SELL()
  {
   for(int i=NumberOfTrades-1; i>=0; i--)
     {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,(Bid),3,(Ask+StopLoss*pips2dbl),(Ask-TakeProfit*pips2dbl),"SELL",MagicNumber+i,0,Red);
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("SELL order opened : ",OrderOpenPrice());
        }
      else
         Print("Error opening SELL order : ",GetLastError());
      //return;
     }
  }

//-------------FUTURE WORK TO ADD == enable or disable trading feature so that you can use graphics part only without trading.
//------------------------------Close Open Trades Signal AND Notification-------
void Close_Trades()
   {
      {
       for (int i = OrdersTotal() - 1; i >= 0; i--)
         {
         if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
            if( OrderType() == OP_BUY && Symbol() == OrderSymbol())
               {
               if(!histo_M5())
                  {
                  if (!OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),3, White))
                     {
                     Print("Order", OrderTicket()," failed to close Error ",GetLastError());
                     }
                  SendNotification("Close Buy Trade--M5 MACD Reversal");
                  alert_time=btime;  
                  //return(true);
                  }
               }
            if( OrderType() == OP_SELL && Symbol() == OrderSymbol())
               {
               if(histo_M5())
                  {
                  if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(),3, White))
                     {
                     Print("Order", OrderTicket()," failed to close Error ", GetLastError());
                     }                
               SendNotification("Close Sell Trade--M5 MACD Reversal");
               alert_time=btime;  
               //return(true);
               }
            }
         }
      }
      //return(false);       
    }

I'll keep reading, testing and trying in the Demo but it doesn't seem that this should be allowed with how the functions are setup. 

I thought I specifically coded to avoid this.

Yesterday, and as the tester image shows, it said closed by stop ? That does not seem to be the case in the logs from yesterday though. 
This is strange and confusing me. 
 

 
OK, so the DEMO actually does the same thing and violates FIFO so now I have to figure that demo and tester are actually doing the same thing ?

I am not sure why the close price is so strange, and it should not allow multiple orders at the same time. 

At least I know that it's likely a code problem that I can sort out. 

Thanks for any help on this. 
 
So I think I understand the function more now. 

These BUY/SELL functions seem to allow multiple trades and from different currencies with use of the magic+i while not closing any opposing symbols when using it on a different symbol. I get that part, but confused about why multiple trades allowed on each symbol with this for loop. 


Anyhow this seems to solve it for the functions, but confused about why it didn't limit trades to begin with. 

void BUY()
  {
   if(OrdersTotal()==0)
   //for(int i=NumberOfTrades-1; i>=0; i--)
     {
      ticket=OrderSend(Symbol(),OP_BUY,Lots,(Ask),3,(Bid-StopLoss*pips2dbl),(Bid+TakeProfit*pips2dbl),"BUY",MagicNumber+i,0,PaleGreen);
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("BUY order opened : ",OrderOpenPrice());
        }
      else
         Print("Error opening BUY order : ",GetLastError());
      // return;
     }
  }
Reason: