One setting of TradingView strategies is the default order size. Of the different ways to calculate that size, one bases orders on a percentage of the strategy’s equity. But what if the strategy’s equity is in a different instrument than the instrument it trades, how is the order size computed then?

In this article:

Sizing orders based on an equity percentage in TradingView

We programmatically configure a TradingView strategy with the strategy() function, which has to be used in every TradingView strategy (Pine Script Language Tutorial, n.d.). Furthermore, this function’s title argument, which sets the strategy’s name, always needs to be defined too (TradingView, n.d.).

Something else we can set with strategy() is the default order size, and we use two of the function’s arguments for that: default_qty_type and default_qty_value. With the latter we specify the default order size while the first defines which approach is used to compute that order size (TradingView, n.d.).

Note: A strategy’s default order size specifies the number of contracts, shares, units, lots, or whichever quantity an instrument trades in (TradingView, n.d.). In this article we refer to these as “contracts”, although a default order size can also be in shares, units, or lots (depending on the traded instrument).

Depending on how we combine those default_qty_type and default_qty_value arguments, the strategy calculates its default order size in one of three ways (TradingView, n.d.):

Note: A strategy’s equity is the sum of its initial capital, net profit, and unrealised profit/loss (see TradingView, n.d.).

So to invest 20% of the strategy’s equity in each order, we use the strategy() function like this:

strategy(title="Example - order sizing based on equity percentage",
     default_qty_type=strategy.percent_of_equity, default_qty_value=20)

That 20% of the strategy’s equity is then divided by the instrument’s price, with the result rounded downward to get the number of contracts to trade. This approach is explored in sizing TradingView orders with an equity percentage.

But when an instrument quotes in a different currency than the strategy’s currency setting, TradingView first converts the strategy’s equity to the currency of the instrument. Then it computes the default order size based on that cash amount. Before exploring how that works, let’s first look at the strategy’s currency setting.

Setting a strategy’s currency in TradingView programmatically

Specifying which currency a strategy uses is done manually or with the currency argument of the strategy() function (Pine Script Language Tutorial, n.d.). With that setting the strategy’s performance metrics (like its net profit and equity) are calculated in a specific currency, even when the script trades an instrument quoting in a different currency. That way we can simulate the strategy’s performance of trading in a different currency than our broker account uses.

For instance, our strategy can backtest like it’s an account denominated in British pounds with this:

strategy(title="Example - strategy trading pound sterling", 
     currency=currency.GBP)

Now with the currency argument set to currency.GBP, the strategy’s account currency is set to British pounds. That strategy currency is sometimes referred to as the base currency (see Pine Script Language Tutorial, n.d.).

The currency argument has a default value of currency.NONE (TradingView, n.d.). That means when we don’t set this argument (or set it explicitly to currency.NONE), then the strategy doesn’t perform currency conversion and defaults to using the same currency as that of the instrument.

All values that we can set the currency argument to are (TradingView, n.d.):

currency argument value Currency used by strategy
currency.AUD Australian dollar
currency.CAD Canadian dollar
currency.CHF Swiss franc
currency.EUR Euro
currency.GBP Pound sterling
currency.HKD Hong Kong dollar
currency.JPY Japanese yen
currency.NOK Norwegian krone
currency.NONE Unspecified currency; the default currency. In this case, the strategy’s results are presented in the currency that the chart’s instrument quotes in
currency.NZD New Zealand dollar
currency.RUB Russian ruble
currency.SEK Swedish krona
currency.SGD Singapore dollar
currency.TRY Turkish lira
currency.USD United States dollar
currency.ZAR South African rand

For more on the different currency settings, see configuring a TradingView strategy’s currency setting. Now let’s see how the strategy’s currency affects the default order size when we calculate that order size with an equity percentage. After that we’ll examine a programming example.

Order sizing based on an equity percentage with currency conversion

As mentioned above, when we set the default_qty_type argument of the strategy() function to strategy.percent_of_equity, then we specify which equity percentage to invest with each order with default_qty_value (TradingView, n.d.). Since that equity is denominated in the strategy’s currency, the value of the currency argument (or its manual equivalent option) also affects order sizing.

Let’s say we size each order to 10% of the strategy’s equity, which we assume to be 25,000. The currency of the instrument that we’re trading and the strategy’s currency influence the default order size in three ways:

  • When currency is set to currency.NONE or when we don’t specify the strategy’s currency setting, then it doesn’t matter which currency an instrument trades in: each order is simply sized by dividing 10% of the strategy’s equity with the instrument’s price. So with an equity of 25,000, orders have a capital investment of 2,500. A stock that trades at $50 while another one is priced € 50 will therefore both get an order size of 50 shares.
  • When the strategy’s currency matches the currency of the instrument we’re trading, then no currency conversion happens. So when trading a $50 stock while the strategy is set to US dollars (currency=currency.USD), the equity of 25,000 gives a default order size of $2,500 (10% x $25,000). That, in turn, translates into an investment of 50 shares ($2,500 / $50 = 50).
  • Now when the strategy uses a different currency than the instrument quotes in, then TradingView performs currency conversion: the strategy’s equity is converted into the currency of the instrument, and then the position is sized based on a percentage of that converted equity. So if our strategy’s account is set to euros (currency=currency.EUR), our strategy has an equity of € 25,000 and 10% of that gives an order size of 2,500 euros. That amount is then converted into US dollars before the strategy determines how many shares of the $50 stock it can buy.

In other words, the default order size as a percentage of the strategy’s equity is in the account currency. And that value is converted into another currency depending on the instrument’s currency.

Tip: TradingView performs a strategy’s currency conversion with the previous session’s closing price of the relevant foreign exchange cross-rate (Pine Script Language Tutorial, n.d.). This means a real-time conversion rate isn’t used and consequently intra-day trading strategies use the same conversion rate for the whole trading day. As such, a difference due to currency conversion can happen between real-time trading and backtest performance.

Example: sizing equity-based orders with currency conversion

To see how position sizing based on a percentage of the strategy’s equity with currency conversion works, let’s look at the programming example below. The strategy opens a long or short position when the trading day starts, and closes that position later in the day. We keep this logic deliberately to a minimum so we can focus on how currency conversion affects order sizing.

We also plot the strategy’s equity in a separate subchart to verify the computed order sizes by TradingView. That plotted value is converted to Swedish krona (from British pounds) since we’re adding the strategy to the stock of Volvo, the Swedish car manufacturer that’s listed at the NASDAQ OMX Nordic exchange and quotes in Swedish krona (SEK).

The chart below gives a quick look at the strategy’s behaviour; after discussing the code we’ll look at the strategy’s trades in depth.

Sizing TradingView orders based on equity with currency conversion
//@version=2
strategy(title="Example - strategy.percent_of_equity & currency conversion", 
     default_qty_type=strategy.percent_of_equity, initial_capital=100000,
     default_qty_value=10, precision=2, currency=currency.GBP)

// Input
exitTime = input(title="Exit Time (hours)", type=integer, defval=15)

// Determine and plot equity
dailyGBPSEK = security("GBPSEK", "D", close[1])

plot(series=strategy.equity * dailyGBPSEK, color=blue)

// Determine order conditions
marketOpen = dayofmonth != dayofmonth[1]

enterLong  = marketOpen and (dayofmonth % 2 == 0)
enterShort = marketOpen and (dayofmonth % 2 != 0)

exitMarket = (hour == exitTime)

// Submit orders
if (enterLong)
    strategy.entry(id="Enter Long", long=true)

if (enterShort)
    strategy.entry(id="Enter Short", long=false)

if (exitMarket)
    strategy.close_all()
Note: This programming example expands on the script we’ve discussed in sizing TradingView orders based on the strategy equity percentage. To not repeat the same explanation, we won’t discuss every line of code. Instead, please refer to the previous article for the full discussion. That article also contains more information about sizing orders based on a strategy’s equity.

Several things in this programming example make it size orders based on an equity percentage with currency conversion. The first is the strategy() function:

strategy(title="Example - strategy.percent_of_equity & currency conversion", 
     default_qty_type=strategy.percent_of_equity, initial_capital=100000,
     default_qty_value=10, precision=2, currency=currency.GBP)

Here we specify the strategy’s name with the title argument. With the default_qty_type argument set to strategy.percent_of_equity and default_qty_value given a value of 10, the strategy sizes orders based on a tenth of its equity. That equity begins with an initial capital of 100,000.

We set the strategy’s currency to British pounds (currency=currency.GBP). That makes our initial capital not an unspecified, ‘Default’ currency (which happens when we don’t set this argument), but £ 100,000 instead. Another argument is precision that, with a value of 2, specifies the strategy’s plotted precision to 2 decimals. That makes our plotted equity curve easier to read.

Another feature of the script is that it plots the strategy’s equity in Swedish krona with the GBP/SEK exchange rate:

dailyGBPSEK = security("GBPSEK", "D", close[1])

plot(series=strategy.equity * dailyGBPSEK, color=blue)

Before we can convert the strategy’s equity (which is in British pounds) to another currency, we need to get the relevant exchange rate (GBP/SEK in our case). To retrieve those forex prices, we use security(). That function makes it possible for the script to request data from additional symbols and resolutions other than the chart it currently calculates on (Pine Script Language Tutorial, n.d.).

We use security() with three arguments. The first defines the symbol ("GBPSEK") while the second specifies the resolution to load. Here we set that latter argument to "D" to have security() load daily data. The last argument specifies which values the function should return (TradingView, n.d.), and with close we have it return closing prices. We do this because TradingView performs currency conversion with forex closing prices (Pine Script Language Tutorial, n.d.).

However, that currency conversion is performed with the closing price of the previous session (Pine Script Language Tutorial, n.d.). To have security() return that closing price (and not the then-current daily price), we place the history referencing operator ([]) with a value of 1 just behind close. This way we retrieve GBP/SEK’s previous session close, and we assign that value to the dailyGBPSEK variable.

We then plot that variable on the chart with plot(), a function that displays the values of its series argument as a line by default (TradingView, n.d.). That argument is set here to the strategy’s current equity (that is, initial capital + net profit + open profit; TradingView, n.d.) multiplied with the GBP/SEK exchange rate.

This way we convert the equity in British pounds to the buying power in Swedish krona. With an GBP/SEK exchange rate of 11.6675 (when this article was written), our strategy equity of £ 100,000 equals 1,166,750 SEK (excluding conversion fees).

Analysing TradingView trades sized with an equity percentage and currency conversion

Now let’s analyse a couple of trades taken by our example strategy. We set our script’s position sizing to 10% of the strategy’s equity, which started off with a value of 100,000 and is denominated in British pounds:

strategy(title="Example - strategy.percent_of_equity & currency conversion", 
     default_qty_type=strategy.percent_of_equity, initial_capital=100000,
     default_qty_value=10, precision=2, currency=currency.GBP)

When we apply the strategy with these settings to the chart, it looks like:

Example of sizing orders in TradingView based on equity and currency conversion

The strategy opens 4 positions here: “Enter Long” for 1348 shares, “Enter Short” for 1332 shares, “Enter Short” for 1314 shares, and an “Enter Long” order for 1299 shares.

When we retrace the steps TradingView takes when computing the size of these orders, the result is:

Trade Closing price when order generates Equity when order generates Percentage of equity Computed position size
“Enter Long” for 1348 shares SEK 87.95 SEK 1,185,587.01 10% (10% * SEK 1,185,587.01) / SEK 87.95 = 1348.0239
“Enter Short” for 1332 shares SEK 88.40 SEK 1,177,605.39 10% (10% * SEK 1,177,605.39) / SEK 88.40 = 1332.1328
“Enter Short” for 1314 shares SEK 89.15 SEK 1,171,655.21 10% (10% * SEK 1,171,655.21) / SEK 89.15 = 1314.2514
“Enter Long” for 1299 shares SEK 89.50 SEK 1,163,088.42 10% (10% * SEK 1,163,088.42) / SEK 89.50 = 1299.5401

As we see here, with the default_qty_type argument of the strategy() function set to strategy.percent_of_equity, the following happens:

  • When the strategy’s currency setting differs from the currency used by the instrument, TradingView converts the strategy’s equity to the instrument’s currency. This currency conversion is done with the previous daily closing price of the applicable exchange rate (Pine Script Language Tutorial, n.d.).
  • The equity percentage we’ve set with default_qty_value is then multiplied with the strategy’s equity. That amount of the strategy’s equity is what we can invest in the order.
  • That equity portion is then divided by the instrument’s price. Since TradingView calculates strategies on bar close by default (Pine Script Language Tutorial, n.d.), that price is the closing price of the bar on which the order generates.
  • And the resulting position size is rounded downward to the number of contracts, shares, units, or lots to trade. This way our orders are practically always a bit smaller than the equity amount we’ve specified with default_qty_value.

See sizing TradingView orders based on an equity percentage for how trading Volvo shares without currency conversion goes.

Summary

Every TradingView strategy has to use the strategy() function. This function configures several strategy properties, including the default order size with its default_qty_type and default_qty_value arguments. That default order size is used by the strategy.entry(), strategy.exit(), and strategy.order() functions when we submit an order programmatically. When we set default_qty_type to strategy.percent_of_equity, TradingView sizes orders based on a percentage of the strategy’s equity (that is, initial capital + net profit + open profit). We specify that percentage with the accompanying default_qty_value argument. A strategy’s equity is, however, denominated in the currency of the strategy. That currency is set manually or with the currency argument of strategy(). Now when a strategy calculates on an instrument trading in a different currency, TradingView performs currency conversion when computing the default order size. Our specified percentage of equity is then taken from that currency converted equity amount, and divided by the instrument’s price to get the number of contracts, shares, units, or lots to trade. Note that TradingView performs currency conversion with the exchange rate from the previous day’s close. That means trading strategies use the same conversion rate for the full day of trading.

Learn more:


References

Pine Script Language Tutorial (n.d.). Retrieved on February 24, 2016, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

TradingView (n.d.). Script Language Reference Manual. Retrieved on April 22, 2016, from https://www.tradingview.com/study-script-reference/