Pine percent_of_equity and cash Sizing as MT5 Lots

Short answer

TradingView sizes orders in units of the instrument and MetaTrader 5 in lots, so percent_of_equity and cash sizing must be recomputed in lots per order from the broker's symbol settings. Both describe the position's notional value, with no leverage and no stop distance, and an explicit qty= overrides them.

TradingView sizes strategy orders in units of the instrument; MetaTrader 5 sizes them in lots. A strategy declared with default_qty_type = strategy.percent_of_equity or strategy.cash therefore cannot be copied as a number: it has to be recomputed in lots, per order, from the broker's symbol settings. This guide shows the formulas, the MQL5 code and the cases where the result surprises people.

What Pine's quantity types mean

  • strategy.fixed (the default): default_qty_value is a quantity of the instrument: shares, coins, contracts, or units of the base currency on a forex symbol.
  • strategy.cash: the value is an amount of money; the order quantity is that amount divided by the price.
  • strategy.percent_of_equity: the value is a percentage of the strategy's equity (initial capital plus closed and open profit); the order quantity is that share of equity divided by the price.

All three are overridden by an explicit qty = on strategy.entry or strategy.order. The key point: percent of equity and cash describe the notional value of the position, with no leverage and no stop distance involved.

//@version=6
strategy("Sized by equity", overlay = true, initial_capital = 10000,
     default_qty_type = strategy.percent_of_equity, default_qty_value = 10)
if ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
    strategy.entry("Long", strategy.long)   // no qty=: 10% of equity

Units vs lots

In MT5 one lot is SYMBOL_TRADE_CONTRACT_SIZE units: 100,000 for most forex pairs, while metals, indices and crypto vary by broker. A Pine quantity of 9,259 EURUSD units is 0.09259 lots, which the broker then rounds to its volume step. So even a strategy.fixed quantity needs dividing by the contract size; carried over as-is, a Pine quantity of 1 becomes 1.0 lot, which is 100,000 times larger on a forex pair.

Cash and percent of equity in MQL5

The value of one lot in the account currency is price × tick value / tick size. Tick value already contains the contract size and the conversion from the quote currency to your deposit currency, so the same formula works for EURUSD on a USD account, USDJPY on a USD account and EURUSD on a EUR account. Divide the money amount by it, round down to SYMBOL_VOLUME_STEP, and refuse anything under SYMBOL_VOLUME_MIN:

#property script_show_inputs

input double InpPctEquity = 10.0;   // default_qty_value (percent of equity)
input double InpCash      = 5000.0; // default_qty_value (cash)
input double InpRiskPct   = 1.0;    // risk per trade, % of equity
input double InpStopDist  = 0.0050; // stop distance in price units

// Account-currency value of ONE lot at this price.
double NotionalPerLot(const double price)
  {
   const double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
   const double tickSize  = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   if(tickValue > 0.0 && tickSize > 0.0)
      return price * tickValue / tickSize;
   return price * SymbolInfoDouble(_Symbol, SYMBOL_TRADE_CONTRACT_SIZE);
  }

// Round DOWN to the lot step; 0 when below the broker minimum.
double NormalizeLots(const double rawLots)
  {
   double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
   if(step <= 0.0)
      step = 0.01;
   double lots = MathFloor(rawLots / step + 1e-9) * step;
   const double vmin = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   const double vmax = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   if(lots < vmin)
      return 0.0;
   if(vmax > 0.0 && lots > vmax)
      lots = vmax;
   return lots;
  }

// strategy.cash: spend `cash` of notional
double LotsFromCash(const double cash, const double price)
  {
   const double perLot = NotionalPerLot(price);
   if(cash <= 0.0 || perLot <= 0.0)
      return 0.0;
   return NormalizeLots(cash / perLot);
  }

// strategy.percent_of_equity
double LotsFromPercentEquity(const double pct, const double price)
  {
   return LotsFromCash(AccountInfoDouble(ACCOUNT_EQUITY) * pct / 100.0, price);
  }

// Risk sizing: lose `riskPct` of equity if the stop is hit
double LotsFromRisk(const double riskPct, const double stopDist)
  {
   const double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE_LOSS);
   const double tickSize  = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
   if(stopDist <= 0.0 || tickValue <= 0.0 || tickSize <= 0.0)
      return 0.0;
   const double riskMoney = AccountInfoDouble(ACCOUNT_EQUITY) * riskPct / 100.0;
   const double lossPerLot = stopDist / tickSize * tickValue;
   return NormalizeLots(riskMoney / lossPerLot);
  }

void OnStart()
  {
   const double price = iClose(_Symbol, _Period, 1);
   PrintFormat("%s close=%.5f  1 lot = %.2f %s",
               _Symbol, price, NotionalPerLot(price),
               AccountInfoString(ACCOUNT_CURRENCY));
   PrintFormat("percent_of_equity %.1f%% -> %.2f lots", InpPctEquity,
               LotsFromPercentEquity(InpPctEquity, price));
   PrintFormat("cash %.2f -> %.2f lots", InpCash, LotsFromCash(InpCash, price));
   PrintFormat("risk %.1f%% over %.5f -> %.2f lots", InpRiskPct, InpStopDist,
               LotsFromRisk(InpRiskPct, InpStopDist));
  }

This is a script you can run on any chart; in an EA call the same functions with the close of the signal bar (shift 1), since that is the price TradingView sizes the order on.

A worked example

A 10,000 USD account trades EURUSD at 1.0800. Tick size is 0.00001 and tick value is 1.00 USD per lot, so one lot is worth 1.08 × 1.00 / 0.00001 = 108,000 USD.

  • 10% of equity: 1,000 USD / 108,000 = 0.0093 lots. Below a 0.01 minimum lot, so no order. On TradingView the same strategy would buy roughly 925 units.
  • 100% of equity: 10,000 / 108,000 = 0.0926, rounded down to 0.09 lots.
  • USDJPY at 150.00 on the same account: one lot is 100,000 USD whatever the price, because the base currency is the account currency; the tick value (about 0.67 USD per 0.001) carries the conversion.

Forex traders used to leverage often find that a converted percent-of-equity strategy trades nothing at all, or trades the minimum lot. That is the formula working correctly: percent of equity is unleveraged notional.

Risk-based sizing instead

Most MT5 traders size by risk: lose a fixed share of equity if the stop is hit. In Pine you compute qty yourself and pass it to the entry:

//@version=6
strategy("Sized by risk", overlay = true, initial_capital = 10000)
riskPct  = input.float(1.0, "Risk % per trade")
stopDist = ta.atr(14) * 2
if ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
    qty = strategy.equity * riskPct / 100 / (stopDist * syminfo.pointvalue)
    strategy.entry("Long", strategy.long, qty = qty)
    strategy.exit("Stop", "Long", stop = close - stopDist)

In MQL5 the loss per lot for a stop distance d is d / tick size × tick value; LotsFromRisk in the script above implements it with SYMBOL_TRADE_TICK_VALUE_LOSS. Both versions give the same exposure in money, but the MQL5 one is rounded to the broker's lot step, so small accounts again hit the minimum lot.

What Stratlate does with sizing

When a strategy declares percent_of_equity or cash with a value, the converted EA computes the volume at run time with the method above: account equity, tick value and tick size, rounded down to the lot step, on the close of the bar that places the order, and an order below the minimum lot is skipped. The report notes this sizing. A fixed quantity, from strategy.fixed or an explicit qty =, is carried over unchanged as the lot volume, so a Pine quantity meant as units must be adjusted to your contract size before you trade. A strategy with no sizing at all trades 1.0, Pine's default quantity. Always check the volumes in a backtest.

Pitfalls

  • Account equity is not strategy equity. ACCOUNT_EQUITY includes every other position on the account. Pine's strategy.equity only knows this strategy. Run the EA on its own account, or track its profit by magic number, if the difference matters.
  • Rounding up. NormalizeDouble(lots, 2) rounds to nearest and can exceed your risk; round down to the volume step instead.
  • Assuming a 0.01 step. Some symbols use 0.1 or 1.0 steps and minimums. Symptom: error 10014 (invalid volume).
  • Using the contract size alone. It is in quote-currency terms. Symptom: wrong sizes on JPY pairs, crosses and CFDs quoted in another currency.
  • Different initial capital. TradingView defaults to 1,000,000; the tester deposit is whatever you set. Percent-based sizes scale with it.
  • Margin. MT5 rejects an order the free margin cannot cover (error 10019, not enough money), where Pine's simulation may still have filled it.

FAQ

Why does my converted strategy place no trades on MT5 when it trades on TradingView?

Often the computed lot size is below the broker's minimum lot. A small percent of equity on a small account is a tiny notional amount, which rounds down to zero lots, and the order is skipped. Check the computed volume in the Experts log and raise the percentage or switch to risk-based sizing.

Is percent_of_equity the same as risking that percent per trade?

No. percent_of_equity sets the position's value as a share of equity, not the amount you lose at the stop. With 10 percent of equity and a 2 percent stop, the trade risks about 0.2 percent of equity. Risk-based sizing needs the stop distance.

Should I use SYMBOL_TRADE_CONTRACT_SIZE or tick value to compute lots?

Use tick value divided by tick size. It already includes the contract size and the conversion from the quote currency to your account currency, so it works for pairs like USDJPY on a USD account. The contract size alone is in quote currency units.

Convert a sized strategy

Stratlate converts percent-of-equity and cash sizing into run-time lot calculations from your broker's symbol settings.

Open the converter

Related guides