default_qty_type in MQL5: fixed, cash and percent as lots
Short answer
MQL5 orders are in lots, so default_qty_type becomes a lot calculation at order time: money divided by one lot's value (price / SYMBOL_TRADE_TICK_SIZE × SYMBOL_TRADE_TICK_VALUE), rounded down to SYMBOL_VOLUME_STEP, with no order below SYMBOL_VOLUME_MIN.
default_qty_type and default_qty_value in the strategy() declaration size every entry that has no qty: a fixed number of contracts, an amount of money, or a percentage of current equity. MQL5 orders are in lots, so the equivalent is a function that turns the same rule into a lot count at the moment of the order: money divided by the value of one lot (price / SYMBOL_TRADE_TICK_SIZE × SYMBOL_TRADE_TICK_VALUE), rounded down to SYMBOL_VOLUME_STEP, and no order when the result is below SYMBOL_VOLUME_MIN. The full derivation, with risk-based sizing, is in the position sizing guide.
What default_qty_type computes
strategy.fixed(the default, with value 1):default_qty_valuecontracts, shares or base-currency units per order.strategy.cash:default_qty_valuein the strategy currency, divided by the price, gives the quantity.strategy.percent_of_equity: that percentage of current strategy equity (initial capital plus net and open profit), divided by the price.
The price used is the close of the bar on which the order is generated, not the fill price. The quantity is rounded down to the symbol's minimum quantity and an order that rounds to zero is not placed. No leverage or margin enters the formula: 100% of equity buys a position whose notional value equals equity. An explicit qty on strategy.entry or strategy.order always overrides the default.
The MQL5 equivalent
//@version=6
strategy("Sizing demo", overlay = true, initial_capital = 10000,
default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
strategy.entry("L", strategy.long) // 10% of equity, no qty=
if ta.crossunder(fast, slow)
strategy.close("L")The EA makes the sizing rule an enum input so one EA covers all three types; OrderLots is the conversion, called with the signal bar's close as Pine does.
#include <Trade/Trade.mqh>
enum QtyType { QTY_FIXED_LOTS, QTY_CASH, QTY_PERCENT_OF_EQUITY };
input QtyType InpQtyType = QTY_PERCENT_OF_EQUITY; // default_qty_type
input double InpQtyValue = 10.0; // default_qty_value: lots, money or percent
input long InpMagic = 60022;
CTrade trade;
int hFast = INVALID_HANDLE, hSlow = INVALID_HANDLE;
int OnInit()
{
hFast = iMA(_Symbol, _Period, 10, 0, MODE_SMA, PRICE_CLOSE);
hSlow = iMA(_Symbol, _Period, 30, 0, MODE_SMA, PRICE_CLOSE);
if(hFast == INVALID_HANDLE || hSlow == INVALID_HANDLE)
return INIT_FAILED;
trade.SetExpertMagicNumber(InpMagic);
trade.SetTypeFillingBySymbol(_Symbol);
return INIT_SUCCEEDED;
}
bool IsNewBar()
{
static datetime last = 0;
const datetime t = iTime(_Symbol, _Period, 0);
if(t == last)
return false;
last = t;
return true;
}
// round down to the broker's lot step; 0 below the minimum (order skipped)
double ToLotStep(const double lots)
{
const double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
const double vmin = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
const double vmax = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double v = step > 0 ? MathFloor(lots / step + 1e-9) * step : lots;
if(v < vmin)
return 0.0;
return MathMin(v, vmax);
}
// the order size Pine would compute, in lots, at `price`
double OrderLots(const double price)
{
if(InpQtyType == QTY_FIXED_LOTS)
return ToLotStep(InpQtyValue);
double cash = InpQtyValue;
if(InpQtyType == QTY_PERCENT_OF_EQUITY)
cash = AccountInfoDouble(ACCOUNT_EQUITY) * InpQtyValue / 100.0;
// money value of one lot: price in ticks times the value of one tick
const double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
const double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
if(tickValue <= 0 || tickSize <= 0 || price <= 0)
return 0.0;
return ToLotStep(cash / (price / tickSize * tickValue));
}
bool HasLong()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
if(PositionGetTicket(i) != 0 && PositionGetString(POSITION_SYMBOL) == _Symbol
&& PositionGetInteger(POSITION_MAGIC) == InpMagic)
return true;
return false;
}
void OnTick()
{
if(!IsNewBar())
return;
double f[2], s[2];
if(CopyBuffer(hFast, 0, 1, 2, f) != 2 || CopyBuffer(hSlow, 0, 1, 2, s) != 2)
return;
if(f[1] > s[1] && f[0] <= s[0] && !HasLong())
{
// Pine sizes on the signal bar's close
const double lots = OrderLots(iClose(_Symbol, _Period, 1));
if(lots > 0)
trade.Buy(lots, _Symbol, 0.0, 0.0, 0.0, "L");
}
if(f[1] < s[1] && f[0] >= s[0])
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
const ulong ticket = PositionGetTicket(i);
if(ticket != 0 && PositionGetString(POSITION_SYMBOL) == _Symbol
&& PositionGetInteger(POSITION_MAGIC) == InpMagic)
trade.PositionClose(ticket);
}
}Tick value already contains the contract size and the conversion from the quote currency to the deposit currency, so the same formula works for EURUSD on a USD account, USDJPY on a EUR account and gold. For strategy.fixed the EA takes lots directly: Pine's "1 contract" means one unit of the base currency on forex, which is 0.00001 lot, so a literal 1.0 is almost never what the Pine script meant.
Where the two differ
| Topic | Pine | MQL5 |
|---|---|---|
| Unit | contracts / units | lots of SYMBOL_TRADE_CONTRACT_SIZE units |
| Rounding | to the symbol's minimum quantity, often fractional | down to SYMBOL_VOLUME_STEP, often 0.01 lot = 1,000 units |
| Equity | simulated, from initial_capital | ACCOUNT_EQUITY of the real or tester account |
| Currency | strategy currency | deposit currency, converted through tick value |
| Too small | order skipped | below SYMBOL_VOLUME_MIN: skip it yourself, or the request fails |
| Margin | ignored (unless margin_long/short set) | enforced; a 100%-of-equity order on a leveraged account is a small margin use, on a 1:1 account it may fail |
What the Stratlate converter emits
The converter reads default_qty_type and default_qty_value when they are written as named literal arguments. strategy.fixed uses the value verbatim as the lot size (the default of 1 becomes 1.0 lot). strategy.percent_of_equity becomes pine_qty_percent_equity(10.0, close[__pineBar]) in each entry's OrderSend, and strategy.cash becomes pine_qty_from_cash(value, close[__pineBar]); both compute lots at run time from ACCOUNT_EQUITY, tick value, tick size and the lot step, return 0 below the minimum lot so the order is refused, and are noted in the report as PM303 (informational; the script above converts CLEAN). Any other sizing type, or a value given by an input or expression, falls back to an InpLots input that you set. Check the lot sizes in the first tester trades before trusting the backtest.
Pitfalls
- Porting
default_qty_value = 1as 1 lot. Symptom: 100,000 units of EURUSD instead of one; the tester fails with "not enough money" or the P&L is wildly larger. - Using
SYMBOL_TRADE_CONTRACT_SIZE × priceon a cross pair. Symptom: sizes off by the exchange rate on symbols whose quote currency is not the deposit currency; tick value avoids it. - Rounding to nearest instead of down. Symptom: positions slightly larger than the percentage allows, and orders above
SYMBOL_VOLUME_MAX. - Sizing from the ask at fill time. Symptom: small size differences against the Pine report, which uses the signal bar's close.
- Expecting identical sizes to TradingView. Symptom: Pine buys 0.37 BTC, MT5 0.3 or 0.4 depending on the broker's lot step; compare after rounding.
FAQ
How do I convert percent_of_equity to lots in MQL5?
Money = ACCOUNT_EQUITY × percent / 100; value of one lot = price / SYMBOL_TRADE_TICK_SIZE × SYMBOL_TRADE_TICK_VALUE; lots = money / value of one lot, floored to SYMBOL_VOLUME_STEP.
Is percent_of_equity a risk percentage?
No. It is the notional value of the position as a share of equity, with no stop distance involved. Sizing so that a stop-loss costs 1% of equity is a different formula, covered in the sizing guide.
What happens when the computed lot size is below the broker minimum?
Skip the order, as TradingView skips a zero quantity. Sending it anyway returns "Invalid volume" (retcode 10014), and rounding it up to the minimum would risk more than the strategy specifies.
Convert the whole script
Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.
Open the converter