Different behavior by using Buystop() or PositionClose()

 

Hello everyone!

I am experiencing something weird while backtesting my trading system.

I made two versions of the same TS:


1st VERSION:

This is how I open short positions (I skipped useless code):

//OPENING SHORT POSITION
   if (order == "sell_first_A")
   {
    TP_price = IN_price-multiplier*(SL_price-IN_price);  	//take profit price

    trade.SellStop(1.00,IN_price,_Symbol,SL_price,NULL,ORDER_TIME_GTC,0,"");
    position = "short 1-A";
   }

I put "NULL" on take-profit field because I use the following code to close positions in take-profit:

//CLOSING SHORT POSITIONS IN TAKE PROFIT
   if (position == "short 1-A")
   if (Ask_price < TP_price && PositionsTotal()==1)
   {
    for(int i=PositionsTotal()-1; i>=0; i--)
    {
    int ticket = int(PositionGetTicket(i));
    trade.PositionClose(ticket);
    }
   }

Obvoulsly I used analogue code for long positions but it is not relevant.


2nd VERSION:

This is how I open short positions:

//OPENING SHORT POSITION
   if (order == "sell_first_A")
   {
    TP_price = IN_price-multiplier*(SL_price-IN_price);        //take profit price

    trade.SellStop(1.00,IN_price,_Symbol,SL_price,TP_price,ORDER_TIME_GTC,0,"");
    position = "short 1-A";
   }

Here I put "TP_price" in the the specific field of SellStop command to close positions in take-profit.

So there is no need of PositionClose code here.


The problem is that I get different values for TP_price using these two versions, and I don't understand why.


As an example, let's focus on a specific deal on EURUSD where we have IN_price=1.31430 and SL_price=1.31331.

Here following the exit price at which the deal is closed by the trading system using different values of the variable "multiplier":

multiplier 1st VERSION 2nd VERSION
0.3  1.31250 1.31331
0.4 1.31250 1.31298
0.5 1.31250 1.31265
0.6 1.31220 * 1.31232 *
0.7 1.31180 * 1.31199 *

All these deals are in profit.

The fields without asterisk mean that the deal is opened and closed in the same candle.

The fields with the asterisk mean that the deal is closed in the candle just after the opening candle.


In the 2nd version the exit prices correspond to the TP_prices expected, using the formula TP_price = IN_price-multiplier*(SL_price-IN_price)

On the countrary, in the 1st version they don't correspond! Why?

Besides, in the 1st version the exit price doesn't change if the deal is opened and closed in the same candle (it remains 1.31250). Why?


In conclusion, the 1st version seems not working as expected.

Probably I'm missing something... Does anyone know what?


Thank you in advance for your help!

 

For most accuracy use every tick based on real ticks. Different modes use different means to check TP/SL levels. Read more https://www.mql5.com/en/articles/2612

But even in best case, the tester can give you *free* money with TP/SL/LIMIT orders by means of unrealistic high positive slippage.

 

Dear Enrique Dangeroux, this was exactly the problem.

I switched to "Every tick based on real ticks" and the two TS produced the same results.

So when functions working on real time ticks are used (e.g. buystop) it is fundamental to use such modes.

Your help has been precious! Thank you!!

Reason: