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%
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%
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).
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.
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.
-
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.
-
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.)
-
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 -
You must normalize lots properly and check against min and max.
-
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.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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
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.