Study ›
[Course] [Build an EA 5] Stops, targets and 1%-risk lot sizing
The EA in part 4 always trades 0.01 lot. But using 0.01 lot whether the account is $1,000 or $100,000 makes no sense. Professionals usually size so that "if the stop is hit, I lose only 1% of the account." Let's code that.
The idea (gold example)
- $10,000 account, 1% = you can lose up to $100
- Stop $30 (stopped out if gold moves $30 against you)
- 1 lot of gold = 100 oz → a $1 move = $100 per lot
- Stopped out with 1 lot: 30 × 100 = $3,000 loss
- So 100 ÷ 3,000 = 0.033 lot → 0.03 lot
The code (add to the part 4 EA)
Paste it anywhere above OnTick.
input double RiskPct = 1.0; // % of the account to lose on one stop
double LotsByRisk(double riskPct, double stopPoints)
{
double risk = AccountInfoDouble(ACCOUNT_EQUITY) * riskPct / 100.0;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
if(tickValue <= 0 || tickSize <= 0 || stopPoints <= 0) return 0;
double lossPerLot = stopPoints * _Point / tickSize * tickValue; // loss per lot at the stop
double lots = risk / lossPerLot;
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double minL = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxL = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
lots = MathFloor(lots / step) * step; // round down to the broker's step
if(lots < minL) return 0; // smaller than the minimum? don't trade
return MathMin(lots, maxL);
}Then, in the order part of the part 4 code, use this instead of `Lots`:
double lots = LotsByRisk(RiskPct, StopPts);
if(lots <= 0) return;
if(goldenCross) trade.Buy(lots, _Symbol, ask, ...); // the rest stays the sameWhy it's calculated this way
- TICK_VALUE / TICK_SIZE: the broker tells you "how much 1 lot changes when price moves one step." It differs for gold, FX and indices, so using it makes the calculation correct for any symbol.
- Round down with MathFloor: 0.0337 lot can't be ordered. Rounding down to the broker's step (usually 0.01) keeps risk at or under 1%.
- Below the minimum, don't trade: if even 0.01 lot is more than 1% on a small account, it's right not to force the trade.
Tips for stops and targets
- Put the stop where "if price gets here, my idea is wrong." Too tight and noise stops you out; too wide and your lots get tiny.
- Start the target at 1.5–2× the stop and compare with backtests.
- Never build an EA without a stop. One big move can end the account.
The point of the 1% rule: "even 10 losses in a row cost only about 10% of the account." You have to survive to get the next chance.
