PositionModify() changing position's open price in Strategy Tester

 

Hi

The test code below opens a buy trade at market price with a stop loss and take profit price 10 points from the market rate. If the market rate returned from the broker differs from the original market rate requested (ie. slips) the test code below uses PositionModify() method to modify the stop loss and take profit prices of the open position to be the same 10 points against the new market rate.

The code has been run in the strategy tester, on a hedging account, with delay set to 1000ms and modelling on real ticks to increase the possibility of generating a slippage.

What I have found is that the stop loss and take profit gets correctly adjusted by PositionModify() but it seems to also change the position's open price.

My understanding was PositionModify does not adjust the position's open price.

Is my understanding incorrect, what am I missing?

Below is the test code used.

#property copyright "Copyright 2021"
#property link      "https://"
#property version   "1.00"

#include<AVS\Bars\CBars.mqh>
#include<Trade\Trade.mqh>

CBars       Bars;
CTrade      Trade;

input int inp_stopLossPts   = 10;  //Stop Loss (in points)
input int inp_takeProfitPts = 10;  //Take Profit (in points)

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

void OnTick()
{

    // Own implementation of opening on a new bar 
    Bars.GetData(Symbol(), Period(),0, 3);
    if (Bars.isNewBar())
    {
        if (PositionsTotal() > 1) return;

        double  requestPrice    = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
        double  symbolTickSize  = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);
        int     symbolDigits    = (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS);

        double  stopLoss        = NormalizeDouble(requestPrice - (inp_stopLossPts * symbolTickSize), symbolDigits);
        double  takeProfit      = NormalizeDouble(requestPrice + (inp_takeProfitPts * symbolTickSize), symbolDigits);       
        Trade.Buy(0.01, Symbol(), 0.0, stopLoss, takeProfit);
        
        ulong   resultOrder     = Trade.ResultOrder();
        double  resultPrice     = NormalizeDouble(Trade.ResultPrice(), symbolDigits);

        if (resultPrice != requestPrice) 
        {
            double priceDiff        = NormalizeDouble (resultPrice - requestPrice, symbolDigits);
            double newStopLoss      = NormalizeDouble (stopLoss + priceDiff, symbolDigits); 
            double newTakeProfit    = NormalizeDouble (takeProfit + priceDiff, symbolDigits); 
            
            Trade.PositionModify(resultOrder, newStopLoss, newTakeProfit);

            PositionSelectByTicket(resultOrder);
            double modifiedPrice = NormalizeDouble (PositionGetDouble(POSITION_PRICE_OPEN), symbolDigits);

            PrintFormat("RESULT of Position %i\n Request Price=%s\n Result Price=%s\n Price Difference=%s\n Modified Price=%s\n Request SL=%s\n Modified SL=%s\n Request TP=%s\n Modified TP=%s", 
                        resultOrder, (string)requestPrice, (string)resultPrice, (string)priceDiff, (string)modifiedPrice, (string)stopLoss, (string)newStopLoss, (string)takeProfit, (string)newTakeProfit);

        }
    }
}   


In the attached Strategy Tester Journal output related to Ticket 36:

The price changed from the request price of 83.667 to the result price of 83.668 an increase of 1 point.

The stop loss and take profit prices have both been moved up by 1 point, as expected.

The modified price of 83.668 (which is derived from PositionSelectByTicket and POSITION_PRICE_OPEN property in code) is as I would expect it unchanged from the result price.


In the attached Strategy Tester Operations output for the same Ticket 36:

The position price looks like it has now been modified to 83.665. This is not what I would have expected. I would have expected to also see 83.668 here.

Am I misunderstanding PositionModify here or mis-interpreting the strategy tester operations output?

Thanks in advance for any clarification.

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Arn:

Hi

The test code below opens a buy trade at market price with a stop loss and take profit price 10 points from the market rate. If the market rate returned from the broker differs from the original market rate requested (ie. slips) the test code below uses PositionModify() method to modify the stop loss and take profit prices of the open position to be the same 10 points against the new market rate.

The code has been run in the strategy tester, on a hedging account, with delay set to 1000ms and modelling on real ticks to increase the possibility of generating a slippage.

What I have found is that the stop loss and take profit gets correctly adjusted by PositionModify() but it seems to also change the position's open price.

My understanding was PositionModify does not adjust the position's open price.

Is my understanding incorrect, what am I missing?

Below is the test code used.


In the attached Strategy Tester Journal output related to Ticket 36:

The price changed from the request price of 83.667 to the result price of 83.668 an increase of 1 point.

The stop loss and take profit prices have both been moved up by 1 point, as expected.

The modified price of 83.668 (which is derived from PositionSelectByTicket and POSITION_PRICE_OPEN property in code) is as I would expect it unchanged from the result price.


In the attached Strategy Tester Operations output for the same Ticket 36:

The position price looks like it has now been modified to 83.665. This is not what I would have expected. I would have expected to also see 83.668 here.

Am I misunderstanding PositionModify here or mis-interpreting the strategy tester operations output?

Thanks in advance for any clarification.

The open price was not modified, it can't be modified. The Strategy Tester just show what was the current price when you SL/TP were modified, that is 83.665
 
Alain Verleyen #:
The open price was not modified, it can't be modified. The Strategy Tester just show what was the current price when you SL/TP were modified, that is 83.665

So I was interpreting the Strategy Tester output incorrectly.

Pity the price field works that way as it makes sense for opening positions, but I think it is misleading when modifying positions. 

No matter. As a workaround I'll create an export file, with the values I would prefer to see in the price field.

Thanks Alain for your help with this.

Reason: