EA stops out extremely early [solved]

Filip  
Hello
When backtesting my EA, it stops out after two transactions, which should have netted a loss of ~500$, however the tester claims that only $90 are left of the original $10000 test balance
Here is a log of the run
GP      0       00:43:52.645    127.0.0.1       login (build 2835)
NN      0       00:43:52.651    Tester  account info found with currency USD
LI      0       00:43:52.656    Tester  expert file added: Experts\Advisors\CustomExpert.ex5. 114347 bytes loaded
HG      0       00:43:52.656    Tester  program file added: Indicators\Exit.ex5. 17384 bytes loaded
IE      0       00:43:52.656    Tester  program file added: Indicators\Entry.ex5. 25891 bytes loaded
FQ      0       00:43:53.370    Tester  initial deposit 10000.00 USD, leverage 1:1
LI      0       00:43:53.370    Tester  successfully initialized
QS      0       00:43:53.370    Network 252 Kb of total initialization data received
RK      0       00:43:53.370    Tester  AMD Ryzen 5 3600 6-Core, 36215 MB
HM      0       00:43:53.427    Symbols BTCUSD: symbol to be synchronized
QI      0       00:43:53.428    Symbols BTCUSD: symbol synchronized already, 18 bytes received
DD      0       00:43:53.428    History BTCUSD: history synchronization started
QO      0       00:43:53.432    History BTCUSD: load 27 bytes of history data to synchronize in 0:00:00.003
FE      0       00:43:53.432    History BTCUSD: history synchronized from 2012.01.01 to 2021.03.21
DM      0       00:43:53.502    History BTCUSD,Daily: history cache allocated for 2020 bars and contains 478 bars from 2014.01.01 00:00 to 2015.04.23 00:00
PI      0       00:43:53.502    History BTCUSD,Daily: history begins from 2014.01.01 00:00
PL      0       00:43:53.502    Tester  BTCUSD,Daily (EvolveMarkets-MT5 Demo Server): every tick generating
OH      0       00:43:53.502    Tester  testing with execution random delay
FL      0       00:43:53.503    Tester  BTCUSD,Daily: testing of Experts\Advisors\CustomExpert.ex5 from 2015.04.24 00:00 to 2021.03.21 00:00 started with inputs:
DK      0       00:43:53.503    Tester    StopLoss=0.05
OD      0       00:43:53.503    Tester    TakeProfit=0.2
FJ      0       00:43:53.526    Tester  program file added: \Indicators\Entry.ex5. 25892 bytes loaded
GH      0       00:43:53.532    Tester  program file added: \Indicators\Exit.ex5. 17385 bytes loaded
MH      0       00:53:30.665    Trade   2015.05.09 00:00:04   market buy 40.08 BTCUSD sl: 235 tp: 296 (245 / 247 / 245)
GI      0       00:53:30.665    Trades  2015.05.09 00:00:04   deal #2 buy 40.08 BTCUSD at 247 done (based on order #2)
LR      0       00:53:30.665    Trade   2015.05.09 00:00:04   deal performed [#2 buy 40.08 BTCUSD at 247]
IO      0       00:53:30.665    Trade   2015.05.09 00:00:04   order performed buy 40.08 at 247 [#2 buy 40.08 BTCUSD at 247]
EF      0       00:53:30.666    Expert (BTCUSD,D1)      2015.05.09 00:00:04   CTrade::OrderSend: market buy 40.08 BTCUSD sl: 235 tp: 296 [done at 247]
QJ      0       00:53:30.795    Trade   2015.05.13 21:52:42   stop loss triggered #2 buy 40.08 BTCUSD 247 sl: 235 tp: 296 [#3 sell 40.08 BTCUSD at 235]
DS      0       00:53:30.795    Trades  2015.05.13 21:52:42   deal #3 sell 40.08 BTCUSD at 235 done (based on order #3)
HF      0       00:53:30.795    Trade   2015.05.13 21:52:42   deal performed [#3 sell 40.08 BTCUSD at 235]
ID      0       00:53:30.795    Trade   2015.05.13 21:52:42   order performed sell 40.08 at 235 [#3 sell 40.08 BTCUSD at 235]
GS      0       00:53:32.059    Trade   2015.05.23 00:00:34   market buy 13.42 BTCUSD sl: 231 tp: 292 (241 / 243 / 241)
GN      0       00:53:32.059    Trades  2015.05.23 00:00:34   deal #4 buy 13.42 BTCUSD at 243 done (based on order #4)
JS      0       00:53:32.059    Trade   2015.05.23 00:00:34   deal performed [#4 buy 13.42 BTCUSD at 243]
ID      0       00:53:32.059    Trade   2015.05.23 00:00:34   order performed buy 13.42 at 243 [#4 buy 13.42 BTCUSD at 243]
DJ      0       00:53:32.060    Expert (BTCUSD,D1)      2015.05.23 00:00:34   CTrade::OrderSend: market buy 13.42 BTCUSD sl: 231 tp: 292 [done at 243]
OI      3       00:53:32.141    Trade   2015.05.28 00:00:01   position stop out triggered at 2.86% [#4 buy 13.42 BTCUSD 243 sl: 231 tp: 292]
EJ      0       00:53:32.141    Trades  2015.05.28 00:00:01   deal #5 sell 13.42 BTCUSD at 237 done (based on order #5)
ND      0       00:53:32.141    Trade   2015.05.28 00:00:01   deal performed [#5 sell 13.42 BTCUSD at 237]
HS      0       00:53:32.141    Trade   2015.05.28 00:00:01   order performed sell 13.42 at 237 [#5 sell 13.42 BTCUSD at 237]
PS      0       00:53:32.141    Tester  final balance 90.88 USD
JF      3       00:53:32.151    Tester  stop out occurred on 1% of testing interval
KL      0       00:53:32.151    Tester  BTCUSD,Daily: 197528 ticks, 35 bars generated. Environment synchronized in 0:00:00.758. Test passed in 0:09:38.736 (including ticks preprocessing 0:00:57.286).
II      0       00:53:32.151    Tester  BTCUSD,Daily: total time from login to stop testing 0:09:39.494 (including 0:00:00.758 for history data synchronization)
QI      0       00:53:32.153    Tester  17170 Mb memory used including 0.47 Mb of history data, 16960 Mb of cached tick data (total memory for tick data 21759 Mb)
OK      0       00:53:32.153    Tester  log file "C:\Program Files\Evolve Markets MetaTrader 5\Tester\Agent-127.0.0.1-3000\logs\20210324.log" written
EG      0       00:53:32.153            test Experts\Advisors\CustomExpert.ex5 on BTCUSD,Daily thread finished
QF      0       00:53:32.225    127.0.0.1       prepare for shutdown


Any help will be much appreciated, thank you.

EDIT: Solution
The issue was in the historical data. The broker I am using is Evolve Markets, which wasn't around in 2015, which is when I was testing from, so the historic data there is not accurate or reliable. After a response from support, they explained this and recommended testing on data from 2018 onwards, which works as expected.
Paul Anscombe  
Filip Kalný:
Hello
I have a stoploss, when backtesting it gets triggered, but the balance of the test account goes completely whack. The buy is at 247, the sell is at 235, the leverage is 1:1, which should come out to a 4.8% loss.
However from the initial test balance of 10000USD, it goes down to ~3000. There must be something here I'm fundamentally misunderstanding (I'm a programmer, not a trader), buy I haven't been able to find any answers on the forums.
Here is the code I use for opening the position:
Any help will be much appreciated, thank you.

you are trading a 5% SL of the product price not your account account balance.

You have not said what volume you are trading...

if you had $1000 in your account then a 5% SL would normally mean 5% of the account balance ie $50   

so you can calculate your volume and SL to trade according to when $50 loss is incurred in the account

if your volume traded was say 1.0 lot and 1 lot movement by 1 point of the product (ie: 247 to 246 in your case) = $10  then to get a 5% SL it is 5 points 247 - 5 = 242   which equals 5 x $10 = $50 =  5%

Filip  
Paul Anscombe:

you are trading a 5% SL of the product price not your account account balance.

You have not said what volume you are trading...

if you had $1000 in your account then a 5% SL would normally mean 5% of the account balance ie $50   

so you can calculate your volume and SL to trade according to when $50 loss is incurred in the account

if your volume traded was say 1.0 lot and 1 lot movement by 1 point of the product (ie: 247 to 246 in your case) = $10  then to get a 5% SL it is 5 points 247 - 5 = 242   which equals 5 x $10 = $50 =  5%

Thank you so much for your reply. Right now I'm just having it trade 100% of the balance, money management will come after I get this working.
However if you look at the attached screenshots you can see that everything SEEMS to work right. The "profit" comes out to -$480 (ie -4.8% of 10000USD), but the the resulting balance in the account is 3005.32USD (not 9520USD as I'd expect).
Paul Anscombe  
Filip Kalný:
Thank you so much for your reply. Right now I'm just having it trade 100% of the balance, money management will come after I get this working.
However if you look at the attached screenshots you can see that everything SEEMS to work right. The "profit" comes out to -$480 (ie -4.8% of 10000USD), but the the resulting balance in the account is 3005.32USD (not 9520USD as I'd expect).

well the balance you show must include more trades than you are showing  10000 - 480 does not make 3xxx

look carefully through the journals there will be an audit trail of the trades placed.  

William Roeder  

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  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.