In TradingView we can size orders based on a cash amount and calculate the strategy’s performance in a specific currency. But if the strategy has a different currency than the instrument it trades, how is the default cash order size computed then?

In this article:

Sizing orders with a fixed cash amount in TradingView

We programmatically configure the settings of a TradingView strategy with the strategy() function. Each strategy needs to use this function (Pine Script Language Tutorial, n.d.) and its title argument, which specifies the strategy’s name, always needs to be set (TradingView, n.d.).

Another thing we can configure with strategy() is the default order size, for which we use two arguments: default_qty_type and default_qty_value. With the latter we specify the default order size while the first defines how that order size is calculated (TradingView, n.d.).

Note: The default order size of a strategy sets the number of contracts, shares, units, lots, or whichever quantity an instrument trades in (TradingView, n.d.). For this article we simply use the term “contracts”, but know that this can also be shares, units, or lots (depending on the instrument).

A strategy calculates its default order size in one of three ways depending on the combination of the default_qty_value and default_qty_type arguments (TradingView, n.d.):

So if we want to size orders based on a fixed cash amount of 2,500, we use the strategy() function like so:

strategy(title="Example - order sizing based on cash amount",
     default_qty_type=strategy.cash, default_qty_value=2500)

Then when TradingView generates an order, it divides the value of default_qty_value with the instrument’s price. The result is then rounded downward to get the number of contracts to trade. We explore this order sizing approach in sizing TradingView orders with a fixed amount of cash.

The cash amount that we set with default_qty_value is, however, in the strategy’s currency. That means when the instrument trades in a different currency than the strategy uses, TradingView converts the cash amount to the instrument’s currency. Before we look at that more closely, let’s see how a strategy’s currency setting works.

Setting a strategy’s currency with code in TradingView

We set the currency of a TradingView strategy either by hand or with the currency argument of the strategy() function (Pine Script Language Tutorial, n.d.). That way the strategy’s performance metrics (like its net profit and equity) are calculated in a specific currency, even when it trades instruments that quote in a different currency.

For instance, our strategy can behave like it’s trading an euro account with:

strategy(title="Example - strategy that trades an euro account", 
     currency=currency.EUR)

This currency.EUR value sets the strategy’s currency to euros. The currency of a strategy is sometimes also 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.). And so when we don’t set this argument or set it explicitly to currency.NONE, the strategy always uses the same currency as the instrument it trades and no currency conversion will take place.

We can set the currency argument of the strategy() function to the following values (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 in TradingView, see setting the currency of a TradingView strategy. Now let’s see how combining a strategy’s currency with order sizing based on a fixed cash amount works.

Order sizing based on a cash amount & currency conversion

As discussed above, when we set default_qty_type to strategy.cash then default_qty_value specifies how much cash the strategy should invest in each order (TradingView, n.d.). That cash amount is in the currency of the strategy, and we specify that currency with the currency argument or with the manual setting in the strategy’s properties.

So with default_qty_value=5000, each order’s size is based on 5k from the same currency as the strategy has. There are three different ways in which TradingView calculates our default order size:

  • When currency is set to currency.NONE or when we haven’t specified the strategy’s currency settings, then it doesn’t matter which currency an instrument trades in: each order is simply sized by dividing 5,000 with the instrument’s price. So a stock trading at $20 while another one priced € 20 will both get an order size of 250 shares.
  • When the currency argument is set to a specific currency that matches the currency of the instrument we’re trading, then no currency conversion takes place. Instead, the value of default_qty_value is directly divided by the instrument’s price. So with currency=currency.EUR, our € 20 stock gets a position size of 250 shares.
  • Now when currency is set to a different currency than the one used by our instrument, then TradingView converts the value of default_qty_value to the instrument’s currency. So with currency=currency.EUR, the € 5000 order size (default_qty_value=5000) is first converted to US dollars before determining how many shares of a $20 stock we can buy.

In other words, the default order size that we set with default_qty_value (when default_qty_type=strategy.cash) is an amount of cash that’s in the strategy’s account currency. That value is then either converted to the instrument’s currency or not.

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 way trading strategies use the same currency conversion rate for the whole day. This can cause, especially for short-term intra-day strategies, a difference due to currency conversion when comparing real-time trading with backtest performance.

Now let’s examine a programming example that sizes orders based on a cash amount that’s converted to the strategy’s currency.

Example: orders sized based on cash with currency conversion

The straightforward example strategy below opens alternating long and short positions at 10:00 in the morning and closes them after the lunch break at 13:00. We set the strategy’s account currency to euros and will have it trade the Microsoft stock, which quotes in US dollars. This means TradingView translates our default order size of 10,000 euros into the dollar equivalent and then figures out how many Microsoft shares we get for that dollar amount.

Our strategy also plots the EUR/USD exchange rate on the chart. That way we can manually verify the order sizes that TradingView calculates. The image below gives a quick look at the strategy; after discussing the code we’ll examine the strategy more closely.

Example chart of the TradingView strategy trading a fixed-cash amount that's converted to another currency
strategy(title="Example - strategy.cash & currency conversion", 
     default_qty_type=strategy.cash, default_qty_value=10000,
     currency=currency.EUR, precision=5)

// Inputs
entryTime = input(title="Entry Time", type=integer, defval=10)
exitTime  = input(title="Exit Time", type=integer, defval=13)

// Get and plot currency exchange rate
dailyEURUSD = security("EURUSD", "D", close[1])

plot(series=dailyEURUSD, color=orange)

// Order conditions
enterLong = (dayofmonth % 2 == 0) and 
     (hour == entryTime) and (minute == 0)

enterShort = (dayofmonth % 2 != 0) and
     (hour == entryTime) and (minute == 0)

exitPosition = (strategy.position_size != 0) and
     (hour == exitTime) and (minute == 0)

// Submit orders
strategy.entry(id="Enter Long", long=true, when=enterLong)
strategy.entry(id="Enter Short", long=false, when=enterShort)

strategy.close_all(when=exitPosition)
Note: This programming example expands on the example script we discussed in sizing TradingView orders with a fixed amount of cash. That’s why we won’t discuss every line of code in this article – please refer to the previous article for the full discussion. That article also provides more information about sizing orders based on a cash amount.

We’ve added several things to this strategy example to have it calculate the default order size based on an amount of cash with currency conversion. The first is the strategy() function:

strategy(title="Example - strategy.cash & currency conversion", 
     default_qty_type=strategy.cash, default_qty_value=10000,
     currency=currency.EUR, precision=5)

Here we specify the strategy’s name with the title argument. With default_qty_type set to strategy.cash we size orders based on a cash amount, and that amount of cash is 10,000 (default_qty_value=10000). With the currency argument we set the strategy’s base currency to euros (currency.EUR).

The last argument of strategy() set here, precision, specifies the number of decimals used by the strategy when plotting values (TradingView, n.d.). We set that argument to 5; that makes the EUR/USD exchange rate show with all of its decimals.

By setting the default_qty_type and default_qty_value arguments of strategy(), we’ve made all of the required changes to have the strategy size orders based on a cash amount. But to closely examine how TradingView calculates our default order size, we also retrieve and plot the EUR/USD rate on the chart:

dailyEURUSD = security("EURUSD", "D", close[1])

plot(series=dailyEURUSD, color=orange)

Before plotting the foreign exchange rate, we first need to load this data. We do that with security(), a function that can request data from other symbols and/or resolutions different from the one that the script currently calculates on (Pine Script Language Tutorial, n.d.).

We use security() with three arguments here. The first specifies which symbol to load ("EURUSD") while the second defines which resolution of that symbol to load. We set that argument to "D" to make it load data with a daily resolution (Pine Script Language Tutorial, n.d.). The last argument defines which values security() should return (TradingView, n.d.), and here we have it return the closing price (close).

But because TradingView performs currency conversion with the closing price of the previous forex session and not with the current exchange rate (Pine Script Language Tutorial, n.d.), we want security() to return the previous day close. So we place the history referencing operator ([]) with a value of 1 just behind close. That way security(), coupled with the daily resolution request, doesn’t return the closing price of the current, open session but the previous day’s close.

We assign those prices to the dailyEURUSD variable followed by plotting that variable on the chart. We use plot() for that, a function that displays the values of its series argument as a line by default (TradingView, n.d.). That argument is set to the dailyEURUSD variable here and, with the color argument set to the orange basic TradingView colour, those daily EUR/USD prices plot as an orange line.

Now let’s see how this example strategy calculates order sizes for a stock trading in US dollars by sizing orders with € 10,000.

TradingView orders sized with cash converted to another currency

Before looking at the trades generated by this strategy, let’s recap which strategy settings we used:

strategy(title="Example - strategy.cash & currency conversion", 
     default_qty_type=strategy.cash, default_qty_value=10000,
     currency=currency.EUR, precision=5)

In the strategy example that we discussed in sizing TradingView orders with a fixed cash amount, we also traded 10,000 currency with each order. But at that time we didn’t specify the strategy’s currency setting. And when we don’t set a strategy’s currency, then the script uses ‘Default’ currency (the equivalent of currency=currency.NONE) and no currency conversion takes place.

However, with the strategy() function used in this article’s example, we explicitly set the strategy’s account currency to euros (currency=currency.EUR). And so the 10,000 value that we set the default_qty_value argument to isn’t ‘default currency’ but 10,000 euros.

That means when we add the example strategy to a Microsoft chart, the script sizes orders with 10,000 euros even though the instrument quotes in US dollars:

Example: TradingView strategy trading the Microsoft stock with currency conversion

There are three trades here: “Enter Short” for 207 shares, “Enter Long” for 205 shares, and “Enter Short” for 206 shares.

In the subchart below the instrument the strategy plots the EUR/USD closing price of the previous session. To recalculate the size of each order:

Trade Cash per trade (in account currency) EUR USD rate Cash per trade (in instrument currency) Closing price when order generates Calculated position size
“Enter Short” for 207 shares € 10,000 1.13763 € 10,000 x 1.13763 = $11,376.30 $54.93 $11,376.30 / $54.93 = 207.10540
“Enter Long” for 205 shares € 10,000 1.13904 € 10,000 x 1.13904 = $11,390.40 $55.35 $11,390.40 / $55.35 = 205.78862
“Enter Short” for 206 shares € 10,000 1.13870 € 10,000 x 1.13870 = $11,387 $55.09 $11,387 / $55.09 = 206.69813

As we see here, TradingView takes the following steps when calculating the default order size based on a cash amount that’s converted to another currency:

  • First, the amount of cash we’ve set with default_qty_value is converted into the currency of the instrument. For this, TradingView uses the recent closing price of the applicable foreign exchange rate (Pine Script Language Tutorial, n.d.).
  • That amount of cash (which is at that point denominated in the instrument’s currency) is then divided by the instrument’s price. Since TradingView strategies calculate on bar close by default (Pine Script Language Tutorial, n.d.), that instrument price is the closing price of the bar on which the order generates.
  • And that resulting position size is then rounded downward to the nearest integer. That means our orders are practically always smaller than the cash amount we’ve set with default_qty_value.

See sizing TradingView orders with a fixed cash amount for how trading Microsoft based on 10,000 cash without currency conversion works.

Summary

Every TradingView strategy has to use the strategy() function. One thing that function can configure is the default order size with the default_qty_type and default_qty_value arguments. The strategy.entry(), strategy.exit(), and strategy.order() functions then use that standard order size when submitting an order. When we set default_qty_type to strategy.cash, TradingView sizes orders based on the cash amount that we’ve set with default_qty_value. That amount of cash is in the strategy’s currency, which we can set with the currency argument of strategy(). Now when the instrument trades in the same currency as the strategy or when we didn’t set the currency, then the cash amount per order isn’t converted to another currency. But when the instrument quotes in a different currency than used by the strategy, TradingView converts the cash amount set with default_qty_value to the instrument’s currency. Then that converted cash amount is 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 TradingView strategies use the same conversion rate for the whole trading day.

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 20, 2016, from https://www.tradingview.com/study-script-reference/