Under what circumstances does a trade opened by an EA get closed prematurely without reaching the TP or SL levels?

 

Hey Guys,

The strategy works well in manual trading in both a trending or ranging market but due to work commitments, I've decided to convert it into an EA after learning mql4 coding for a couple of years. The EA is run on M30, H1 or H4 chart where a trade is opened after the first tick of a new candle when the TP or SL is hit or when a signal appears. For 60% of the time the TP and SL is hit correctly as per the strategy but for the other 40% of the time the trade is closed prematurely before reaching the TP or SL (both buy and sell). This occurs in the strategy tester, demo and live testing on a high speed VPS - the trade is closed almost immediately after the EA has just opened a new trade. There are no error messages in the experts tab, journal tab of log file. The max spread + slippage + commission + swap and the brokers mini distance are accounted for in the EA and all testing has been done on low spread currency pairs. I don't have a lot of coding experience under my belt but hopefully the advanced coders of this forum might be able to shed some light on this issue and hopefully a solution.

 
Noo Porima:

Hey Guys,

The strategy works well in manual trading in both a trending or ranging market but due to work commitments, I've decided to convert it into an EA after learning mql4 coding for a couple of years. The EA is run on M30, H1 or H4 chart where a trade is opened after the first tick of a new candle when the TP or SL is hit or when a signal appears. For 60% of the time the TP and SL is hit correctly as per the strategy but for the other 40% of the time the trade is closed prematurely before reaching the TP or SL (both buy and sell). This occurs in the strategy tester, demo and live testing on a high speed VPS - the trade is closed almost immediately after the EA has just opened a new trade. There are no error messages in the experts tab, journal tab of log file. The max spread + slippage + commission + swap and the brokers mini distance are accounted for in the EA and all testing has been done on low spread currency pairs. I don't have a lot of coding experience under my belt but hopefully the advanced coders of this forum might be able to shed some light on this issue and hopefully a solution.

Without seeing your code nobody will be able to help..

 
An experienced advanced mql4 coder does not need to look at the code - the title says it all. As I mentioned, the EA works perfectly 60% of the time which means my code is fine. There must be some other issues I haven't accounted for that is impacting the EA to fail 40% of the time and I'm sure advanced coders must've come across this problem all the time.
 
Noo Porima:

Hey Guys,

The strategy works well in manual trading in both a trending or ranging market but due to work commitments, I've decided to convert it into an EA after learning mql4 coding for a couple of years. The EA is run on M30, H1 or H4 chart where a trade is opened after the first tick of a new candle when the TP or SL is hit or when a signal appears. For 60% of the time the TP and SL is hit correctly as per the strategy but for the other 40% of the time the trade is closed prematurely before reaching the TP or SL (both buy and sell). This occurs in the strategy tester, demo and live testing on a high speed VPS - the trade is closed almost immediately after the EA has just opened a new trade. There are no error messages in the experts tab, journal tab of log file. The max spread + slippage + commission + swap and the brokers mini distance are accounted for in the EA and all testing has been done on low spread currency pairs. I don't have a lot of coding experience under my belt but hopefully the advanced coders of this forum might be able to shed some light on this issue and hopefully a solution.

First, well, since you have learnt mql for a couple of years, you should know a term called slippage. 

But again, you do not have a lot of coding experience. ;)

Lol!

 
Noo Porima:
An experienced advanced mql4 coder does not need to look at the code - the title says it all. As I mentioned, the EA works perfectly 60% of the time which means my code is fine. There must be some other issues I haven't accounted for that is impacting the EA to fail 40% of the time and I'm sure advanced coders must've come across this problem all the time.

The title does not say it all.

If the EA works perfectly 60% of the time, it doesn't mean that the code is fine because it doesn't work perfectly 40 % of the time.

This means that there is something wrong in the code and an experienced coder cannot possibly comment without seeing the code.

 
I've already accounted for slippage and tested the EA in both volatile and low liquidity markets on the EURUSD pair. I found that in most cases there were more wins than loses.
 
Noo Porima:
I've already accounted for slippage and tested the EA in both volatile and low liquidity markets on the EURUSD pair. I found that in most cases there were more wins than loses.

without checking the code anybody would just be guessing..... but just for you and as a very experienced programmer, I shall go against all instincts and take your word for it that your code is 100%

therefore, I conclude that the problem must be either:

a) bug in the metatrader terminal

b) sh*t data

c) user error

 
Read the title again, its a simple question. If you can't list a single circumstance of why/when a trade gets closed almost immediately after the EA has opened it then I must be in the wrong forum. I'm not asking for someone to write me the code, I can do it myself.
 
Thanks Paul, I will add your 3 points to my list although I have already looked inconclusively at the MT 4 terminal.
 
  1. Noo PorimaUnder what circumstances does a trade opened by an EA get closed prematurely without reaching the TP or SL levels?
    1. Closed manually.
    2. Stop out.
      Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

      Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No SL means you have infinite risk.

      1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
      2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
      3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                  MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
                  Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
                  Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
      4. You must normalize lots properly and check against min and max.
      5. You must also check FreeMargin to avoid stop out

      Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

    3. Sell order's Ask hit the SL, Bid doesn't have to.
      You buy at the Ask and sell at the Bid.
      1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Using the Ask±n, makes your SL is shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
      2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                  MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
      3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

  2. Noo Porima: the EA works perfectly 60% of the time which means my code is fine.
    OnTick(){ /*EMPTY*/} works perfectly 100% of the time but does nothing. Your code is not fine. Do you really expect an answer with the information you've provided? There are no mind readers here and our crystal balls are cracked. We can't see your broken code.
 
Noo Porima:
Thanks Paul, I will add your 3 points to my list although I have already looked inconclusively at the MT 4 terminal.

your welcome, but I would suggest that it could also still be a code issue. For example if the code is not setup to handle the data types correctly it might get by until certain parameters are met at which point it fails etc etc.

Reason: