CODE EXAMPLES
The same rule, pulled apart.
Session drift, one percent risk, stop at 1.6 ATR, target at 2R. The blocks below are cut from the file the builder downloads. Change the inputs there if you want a fade or a break instead.
The size, with numbers
EURUSD, balance 10,000, risk 1%. A 1.6 ATR stop of 19.2 pips. One pip on a standard lot is about 10 in account currency.
risk cash = 10000 * 0.01 = 100 stop = 19.2 pips loss per lot = 19.2 * 10 = 192 lots = 100 / 192 = 0.52 stepped = floor to the lot step = 0.52
If the broker minimum is 0.10 and the step is 0.01, 0.52 is a legal order. If the stop were 2 pips, the same math would ask for 5 lots and then refuse anything above the symbol maximum.
Closed-bar signal
Buf reads one value from an indicator handle, as a series, so index 1 is the closed bar. The same cross rule as MT4, without calling iMA on every tick.
double fast1 = Buf(fastHandle, 1), fast2 = Buf(fastHandle, 2);
double slow1 = Buf(slowHandle, 1), slow2 = Buf(slowHandle, 2);
bool longSig = fast2 <= slow2 && fast1 > slow1;
bool shortSig = fast2 >= slow2 && fast1 < slow1;
if (!longSig && !shortSig) return;
double lots = LotsFor(stopDist);
if (lots <= 0) return;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
if (longSig) { if (trade.Buy(lots, _Symbol, 0, ask - stopDist, ask + stopDist * RewardR, "SENTRA")) fillsToday++; }Size from the stop
Tick size and tick value come from the symbol. Account balance, not equity, is the base, so an open loss does not shrink the next size by itself.
double LotsFor(double stopDist) {
double tick = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double value = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
if (tick <= 0 || value <= 0 || stopDist <= 0) return 0;
double riskCash = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100.0;
double lots = riskCash / ((stopDist / tick) * value);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
if (step <= 0) step = 0.01;
lots = MathFloor(lots / step) * step;
if (lots < minLot || lots > maxLot) return 0;
return NormalizeDouble(lots, 2);
}The only order
CTrade sends the market order with the stop and the target already attached. A second call never happens while HasPosition sees this symbol and this magic number.
if (longSig) { if (trade.Buy(lots, _Symbol, 0, ask - stopDist, ask + stopDist * RewardR, "SENTRA")) fillsToday++; }The full Sessiondrift.mq5 is in the builder. These excerpts are the parts that decide to trade, how much, and the single call that sends it.