Wrong Exit Price on MT5 Tester

 

I have this situation where I exit on a bar that even exceeds its high/low on the tester. (See picture for reference). M5 Timeframe.

Wrong Exit Price

Here is the code on how I exit trades. I have been using it since a long time. Its just accumulation of codes i read here and inputs of my own preference.

void a_ExitShortPosition() //Exit order for SHORT positions. one symbol only
  {
   FailedExitShort=false;
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      m_position.SelectByIndex(i);
      if(m_position.Symbol()==currency_var && m_position.PositionType()==POSITION_TYPE_SELL)
        {
         for(int i2=1; i2<=LiquidityReorder; i2++)
           {
            m_trade.PositionClose(m_position.Ticket(),Slippage);
            Sleep(1000);
            if((m_trade.ResultRetcode()!=10008) && (m_trade.ResultRetcode()!=10009) && (m_trade.ResultRetcode()!=10010))
               Print("Exit Short Position Return Code: ",m_trade.ResultRetcodeDescription());
            else
              {
               Print("Success Exiting SHORT");
               break;
              }
            if(i==10)
              {
               Print("ReEXIT SHORT ",i," times but FAILED");
               FailedExitShort=true;
              }
           }
        }
      if(i==0 && FailedExitShort==false)
         HaveShortPosition=false;
     }
  }

Slippage is set to 10 points, so 1 pip. SL is set to zero/none. Evidently, the trades says it "exits" and not "sl" triggered. Assuming even if it has a SL because of a bug, it must not exceed the prices. let alone the high or low of the bar.

Can anyone enlighten me on whats happening? Maybe on the "Delay" setting on mt5? But I have even set it to zero latency just to be sure.

 
For visual testing, it is not necessary to set the maximum speed of the rewind.
 

There's probably a bug in your loop:

            if(i==10)
              {
               Print("ReEXIT SHORT ",i," times but FAILED");
               FailedExitShort=true;
              }

should read:

            if(i2==LiquidityReorder)

And you should consider that Ask may exceed a bar top depending on the spread.

 
lippmaje:

There's probably a bug in your loop:

should read:

And you should consider that Ask may exceed a bar top depending on the spread.

The spread on the exited price explains it exactly! Thanks! It's my first time testing high spread pairs so the situation looks new to me.

Another question but not exactly related, i always thought that spreads are only paid once. and it's during entry, exiting spread wont be affected. So mt5 basically charging me spreads on both entry and exiting the position?

From my understanding:

Ideally (Enter Long):

Entry Price = 5.00000

Bid = 4.99999

Ask = 5.00001 -- use this because of Long position

Spread = 0.2 pips

------------------

Exit Price = 5.50000

Bid = 5.49998 --use for exit

Ask  = 5.50002 

Spread = 0.4 pips

Ideally we use bid and ask prices. so the actual spread is halved at entry and exit. But in my experience in mt5, it only charges me FULLY at entry. When I exited, the profit is as I computed as if the spread was at entry. whether or not the spread widens at exit doesnt matter (or maybe im wrong). So mt5 terminal tester should at least halve the Spread at entry and exit since its charging it both?


I replied to another thread in which spread is the topic for anyone who wants to help.

https://www.mql5.com/en/forum/106753/page2

The "opening" spread vs The "closing" spread
The "opening" spread vs The "closing" spread
  • 2018.10.01
  • www.mql5.com
Hi. I'm sort of new to forex. I have a question. Let's say I open a position; any position. There's an initial spread cost (ex. a spread of 3...
 

I think the mistake you're making is introducing an "entry price" as a third price apart from bid and ask. If you go long the ask price IS(!) your entry price. If you go short the bid price IS(!) your entry price.

Although from the broker's point of view the spread is part of the pricing model, the spread is no actual commission, so it's not "entry price plus spread", but more like just a "bad" entry price (which makes considerations such as "being charged half the spread" difficult).

And it goes both ways: paying a little too much when we're buying into a position (ask) and receiving too little if we're selling out of a position (bid). So in a way we "pay" for the spread with every transaction.

But when exactly how much depends on the point of view, i.e. a hypothetical reference price: if your reference is the bid price, then you might say that you pay only when entering long (=ask) or exiting short (=ask).

If your reference is a central (/mean) price, then you might say you pay on both entry and exit. But again, this is hypothetical and it really doesn't matter, because the sum is the same.

 
Chris70:

I think the mistake you're making is introducing an "entry price" as a third price apart from bid and ask. If you go long the ask price IS(!) your entry price. If you go short the bid price IS(!) your entry price.

Although from the broker's point of view the spread is part of the pricing model, the spread is no actual commission, so it's not "entry price plus spread", but more like just a "bad" entry price (which makes considerations such as "being charged half the spread" difficult).

And it goes both ways: paying a little too much when we're buying into a position (ask) and receiving too little if we're selling out of a position (bid). So in a way we "pay" for the spread with every transaction.

But when exactly how much depends on the point of view, i.e. a hypothetical reference price: if your reference is the bid price, then you might say that you pay only when entering long (=ask) or exiting short (=ask).

If your reference is a central (/mean) price, then you might say you pay on both entry and exit. But again, this is hypothetical and it really doesn't matter, because the sum is the same.

That makes sense. What we see on the chart is Bid Price? (My tester uses its price levels when I opened Short, so i assumed thats it.)

Is it also Bid Price when we download/export the bars to excel?

 
franzzzz:

That makes sense. What we see on the chart is Bid Price? (My tester uses its price levels when I opened Short, so i assumed thats it.)

Is it also Bid Price when we download/export the bars to excel?

Yes, what you see the bid price. Activate the ask line in the chart properties (F8) if you need it. There are also some indicators that display the current spread (or spread value) as a number or as a function over time (use the search function for examples).

If you trade major pairs (maybe even with an ECN broker) during the main sessions you probably won't need it, but if you trade exotics not knowing the current spread can be expensive.

If you want to avoid scenarios like in post #1, use limit orders and/or set a limit for the max. spread (or max. spread value per lot) of the last quote at the time of your order as an input setting in your EA.

For the second question: I guess there is no general answer; it will depend on what exactly you export with which method (e.g. if you generate a .csv file that you import to Excel, you can write pretty much anything to that file, so also OHLC as bid/ask separately).

 
Chris70:
...

For the second question: I guess there is no general answer; it will depend on what exactly you export with which method (e.g. if you generate a .csv file that you import to Excel, you can write pretty much anything to that file, so also OHLC as bid/ask separately).

To avoid confusion, I would say the general answer is yes. Bars (OHLC) are bid prices. Exporting through the GUI is always bid prices.

Beside that, as you said it's technically possible to export anything from code and to build OHLC using ask prices...if historical ticks are available to do it.

 
Alain Verleyen:

To avoid confusion, I would say the general answer is yes. Bars (OHLC) are bid prices. Exporting through the GUI is always bid prices.

Beside that, as you said it's technically possible to export anything from code and to build OHLC using ask prices...if historical ticks are available to do it.

Thanks. That makes everything clear to me.

 
Chris70 #:
ers and/or set a limi

Hi Chris, would it be possible to use limit order for position exit as well