Why Stop loss and Take Profit dont execute closing order in sequence wise through strategy tester.

 

Hi , 


I am testing a HEDGING EA through Strategy Tester and most of the time that EA hits TP & SL in non sequence order wise or

can say at the time of closing trade It closes all TP and SL at the in non-order wise.





I want my trade should close based on Order. From the above image you can see order executed for SL 9,11,13,15,17 

and for TP 8,10,12,14,16,18,20. Which is not in sequence. its like closing orders based on  odd no and even no separately.



Please help me out. 




Thanks  

Files:
 
First off is this MT4?
It looks as if they were opened all at the same price, then the long ran into tp and the short ran into sl or vice versa.
It seems they were opened one long, one short one long, one short etc. That is why they are exactly on odd and even numbers. It looks to me they are closed in the order that they were opened.

 
Tobias Johannes Zimmer #:
First off is this MT4?
It looks as if they were opened all at the same price, then the long ran into tp and the short ran into sl or vice versa.
It seems they were opened one long, one short one long, one short etc. That is why they are exactly on odd and even numbers. It looks to me they are closed in the order that they were opened.

Hi , 


Yes this one is for MT4.


Its a Hedging EA (Zone recovery ). Here 1 Buy trade is executing then next trade is executing for sell with 1 pip of gap . Where both TP and SL of every trade is 3, and 4. Hence the gap is 1 pip between sell and buy so have to keep difference of 1 pip too in between TP & SL. 


My question is while closing every trade why it is not closing based on order wise . Its closing based on all TP and SL at once iteration. Why not TP, SL, TP , SL based on order no. 




Thanks & Regards,

ShayaanMoiz

 
Oh you mean why do the tp close ten minutes later? Good question, but No way to tell this without the code.
 
Tobias Johannes Zimmer #:
Oh you mean why do the tp close ten minutes later? Good question, but No way to tell this without the code.


TP and SL will close based on the target. So once the target will hit all trades of sl and tp will close at once. Bcs its already mentioned 3 and 4 pips of TP and SL. It may take 5 mins or 10 mins or 15 or 20 mins to hit target based on market movement. 

 
shayaanmoiz: I am testing a HEDGING EA
  1. Hedging, grid trading, same as Martingale.
              Martingale, Hedging and Grid : MHG - General - MQL5 programming forum (2016)

    Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020)

  2. Since 2009, hedging is not permitted for US traders.
              NFA Enforces FIFO Rule, Bans Forex Hedging in US Forex Accounts - Trading Heroes (September 12, 2016)
              FAQ: FIFO in the Forex Market - BabyPips.com (May 31, 2011)

    "Hedging" in Forex trading -Why do it? - Trading Systems - MQL5 programming forum (2017)

 

Forum on trading, automated trading systems and testing trading strategies

Is the backtest information correct?

Fernando Carreiro, 2022.03.27 13:14

Unfortunately, any EA relying on pending orders or broker stops (S/L & T/P) will seriously be misrepresented in the Strategy Tester and on the most part it will be more favourable than on live trading, especially when running the test with tick values.

The reason is as follows — the tester will take pending orders and the stops at the exact prices they are set to be triggered instead of the real quote value of the test market at the time. Even if in the tester the quote price would cause slippage, it still marks them as been taken at the trigger price.

For my own EAs, I never use pending orders and implement virtual stops to get a more realist back test, and the difference is always very noticeable. Here is a reference to another thread in which a user notice the same problem in his back-testing and my answer to the problem:


Forum on trading, automated trading systems and testing trading strategies

Calculate the stop loss at the close of the candle

Fernando Carreiro, 2022.03.26 01:00

Ok, now I understand the issue, but unfortunately all we have discussed so far has nothing to do with it nor will it solve the problem.

The issue is that the Strategy Tester will take the S/L and T/P at the exact price you set it instead of the real price slippage. The same also happens for the opening price of pending orders.

This is a real problem which MetaQuotes has never corrected and it has been like that even in MT4 (and it also makes back tests of Market products look very nice in the Strategy Tester, but bad in live trading).

The only solution I have come up with, is to implement virtual stops that are handled within the code (instead of the broker/tester side), that simulates the stops being hit and closes the positions when the target stops are reached.

In my own EAs I do the following:

  • I add an offset (a fixed amount of points, a very large size) to all my stops when the order is placed. For example, I add an extra 10000 points to the real stop price.
  • Then in the code, I monitor the trades and read the current stops (from open positions) and subtract the offset from their distances (the 10000 points) to get the real stop prices.
  • I then check if the Bid (for buys) or Ask (for sells) has reached those real stops.
  • If they are indeed hit, I then close the positions in the code and in this way, the positions are closed at the real prices with slippage.

When the EA is trading live, all I do is set the virtual stop offset to 0, instead of 10000 for the Strategy Tester.

The reason I use this offset is so that I still have the stops information stored with the order and position and I don't need to keep an internal cache of the data of the real stops. It is all stored within the normal trading records but just offset in value.

The value I choose, for example 10000, is so that the stops don't get accidentality triggered (in the tester system) by a very big spike in spread or slippage and also because it is a "nice" number and easy to calculate visually when reading the test reports.

I hope you will be able to understand all this process. However, for a beginner it may be difficult to implement, as it requires a higher level of coding skills.

Is the backtest information correct?
Is the backtest information correct?
  • 2022.03.26
  • www.mql5.com
Is the backtest information correct...
Reason: