Calculate the stop loss at the close of the candle - page 2

 
Edinson Perez #... but I don't understand how I should take the close of the candlestick as a stoploss once it reaches or exceeds (Bid+90000*_Point)

Please explain in more detail and differently please, because I am not following what you want.

 
Fernando Carreiro #:

Please explain in more detail and differently please, because I am not following what you want.



When I open a sell trade I set the stoploss at +5 USD, but the price rises sharply and breaks the stoploss at +15 USD in one move, and the tester calculates that blow at +5 USD in the final balance and does not at +15 USD which is the close of the candle, do you understand me? How to calculate the stoploss at the close of that candle?


The tester gives me wrong results for this reason, example: the final balance should be $10020 and not $10125.

 
Edinson Perez #When I open a sell trade I set the stoploss at +5 USD, but the price rises sharply and breaks the stoploss at +15 USD in one move, and the tester calculates that blow at +5 USD in the final balance and does not at +15 USD which is the close of the candle, do you understand me? How to calculate the stoploss at the close of that candle? The tester gives me wrong results for this reason, example: the final balance should be $10020 and not $10125.

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.

 
Fernando Carreiro #:

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.



I understand, can you give me a small code example of what you are telling me? I am new to mql5 and trying to learn as much as possible

 
Edinson Perez #: I understand, can you give me a small code example of what you are telling me? I am new to mql5 and trying to learn as much as possible

Unfortunately not, because this is something that is interwoven into my own EAs code and functionality. It's not something that can be shown separately as isolated code. As i stated, this is something targeted for skilled coders, not for newbie coders.

Reason: