How to prevent opposite deal after stoploss in strategy tester?

 
In strategy testing, after the order has reached SL, then an opposite order is created, can this be prevented?
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Avi: In strategy testing, after the order has reached SL, then an opposite order is created, can this be prevented?
That is not an issue with the Strategy Tester. That is an issue with the EA, the MQL program you are testing — you will need to fix the EA's logic and code.
 
Fernando Carreiro #:
That is not an issue with the Strategy Tester. That is an issue with the EA, the MQL program you are testing — you will need to fix the EA's logic and code.

Thank you 

How to fix?

trade.Buy(volume,Symbol(),0,SL,0)
 
Avi #: How to fix?
A single line of code will not fix the issue. You need to analyse your entire code logic and correct it.
 
Fernando Carreiro #:
A single line of code will not fix the issue. You need to analyse your entire code logic and correct it.
#include <Trade\Trade.mqh>
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if (PositionsTotal()==0)
   {
    double price=SymbolInfoDouble(_Symbol,SYMBOL_ASK); 
    double SL=price-100*Point(); 
    double TP=price+100*Point(); 
    trade.Buy(0.01,Symbol(),0,SL,TP);
   }
}
 
Avi #:

Think about your code logic!

  • What will happen when your position hits the stop-loss or the take-profit?
  • It will close, and what do you think "PositionsTotal()" will then return?
  • It will return zero, and what do you think your code will do?
  • It will place another trade!

Programming is about thinking in logical sequencial steps!

 
Fernando Carreiro #:

Think about your code logic!

  • What will happen when your position hits the stop-loss or the take-profit?
  • It will close, and what do you think "PositionsTotal()" will then return?
  • It will return zero, and what do you think your code will do?
  • It will place another trade!

Programming is about thinking in logical sequencial steps!

I know that

My problem is after position close

The order is closed by a Sell order and thus I receive 2 orders and 2 deals for each order I opened and I want to prevent this.

 
Avi #: I know that. My problem is after position close. The order is closed by a Sell order and thus I receive 2 orders and 2 deals for each order I opened and I want to prevent this.

Yes, that is normal. That is how orders, positions and deals work in MT5.

Please read the following ...

Articles

Orders, Positions and Deals in MetaTrader 5

MetaQuotes, 2011.02.01 16:13

Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
 
Fernando Carreiro #:

Yes, that is normal. That is how orders, positions and deals work in MT5.

Please read the following ...

I understand that, but when I open an order live in MT5 and not through MQL, then there is no such thing

 
Avi #:I understand that, but when I open an order live in MT5 and not through MQL, then there is no such thing

Yes, there is. In your trade "History" tab, enable the "Orders & Deals" view (instead of "Positions" view) and you will see.

When you place a "buy" order and a "buy" position is opened, there is a "buy/in" deal for it. And when it hits one of the stops, then there is another "sell" order and a "sell/out" deal for that too.

The opening deal is called the "in" deal, and the closing deal is called the "out" deal.

Avi: In strategy testing, after the order has reached SL, then an opposite order is created, can this be prevented?

Now I understand your question more clearly.

You were not referring to the EA placing a opposite position, you were referring to the reverse order/deal that accompanies the closing of a position.

That is how it works. To close a position, there is always a "out" order/deal to close it, just as there is a "in" order/deal to open it.