We can configure several settings of a TradingView strategy programmatically, like its currency and the initial capital that’s used with backtesting. But how are those starting funds influenced by the currency setting?

In this article:

Using code to set the initial capital of a TradingView strategy

Each TradingView strategy has to use strategy(), a function that configures several strategy properties (Pine Script Language Tutorial, n.d.; TradingView, n.d.). By defining a strategy’s features programmatically, our strategy is always added with the right settings to the chart and we can’t accidentally forget to configure something.

One of the strategy() function’s arguments is initial_capital, which specifies the strategy’s initial capital (TradingView, n.d.). Those starting funds are used by the script when backtesting and forward testing on real-time data.

For instance, to set a strategy’s initial capital to 25k, we use the strategy() function and initial_capital like this:

strategy(title="Example - initial_capital", initial_capital=25000)

For more on this, see setting the initial capital of a TradingView strategy. In this article, we explore how the strategy’s currency setting influences those initial funds. For that, let’s first look at how we configure which currency a strategy uses.

Setting a strategy’s currency in TradingView with code

We set a strategy’s currency with the manual option in the script’s settings, or with the currency argument of the strategy() function (Pine Script Language Tutorial, n.d.). With this feature the strategy’s results calculate in a specific currency, even when the script trades an instrument that quotes in a different currency. This way we can, for example, simulate how our Australian dollar account would perform when trading a stock priced in euros.

Having a strategy act like it’s trading Australian dollars is done like this:

strategy(title="Example - strategy trading Australian dollars", 
     currency=currency.AUD)

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

currency 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, default currency. With this option, the strategy’s results are in the same currency as the chart’s instrument uses.
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
Note: The currency argument of the strategy() function has a default value of currency.NONE (TradingView, n.d.). This means that when we don’t set the strategy’s currency, TradingView doesn’t perform currency conversion and the strategy implicitly uses the same currency as the instrument it trades.

For more on a strategy’s currency setting (including how to set this option by hand), see configuring the currency of a TradingView strategy. Now let’s see how a strategy’s currency affects the initial capital.

Combining currency conversion with initial capital in TradingView

So to summarise, with the initial_capital argument of the strategy() function we specify a strategy’s starting capital. And since those funds are denominated in the strategy’s currency, the value of the currency argument (or its manual equivalent option) can affect the script’s starting capital.

Let’s say our strategy begins with 25,000 in funds (initial_capital=25000). There are three ways in which this capital can or cannot be influenced the strategy’s currency setting:

  • When we set currency to currency.NONE or don’t specify the strategy’s currency setting, then the instrument’s currency doesn’t matter: TradingView implicitly assumes that our initial capital of 25,000 is in the same currency as the instrument. So when an instrument quotes at $1,500 or € 1,500, we can still buy 16 contracts without using margin (25,000 / 1,500 = 16.67 ≈ 16).
  • When the strategy’s currency matches the currency of the instrument it trades, then no currency conversion takes place. So with the instrument priced € 1,500 while our strategy uses an euro account (currency=currency.EUR), then the initial capital is in euros too and we have a buying power of € 25,000.
  • Now when the strategy uses a different currency than the instrument it trades in, TradingView performs currency conversion. The initial capital is converted into the currency of the instrument, and that amount determines how much cash there’s available for the script’s first trade. So with an initial capital of € 25,000 while the instrument is priced at $1,500, TradingView determines the buying power (in US dollars) of our initial capital. With an EUR/USD rate of 1.1138, our starting funds of € 25,000 allow us to invest up to $27,845. All profit and losses incurred when trading the US dollar denominated instrument are converted by TradingView back into euros before being added to the strategy’s equity (which consists out of initial capital + net profit + open profit).

In other words, the strategy’s initial capital is in the account currency. And TradingView converts that value into another currency depending on the instrument’s currency.

Tip: TradingView’s currency conversion uses 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 that makes intra-day trading strategies use the same conversion rate for the whole day of trading. This way small differences due to currency conversion happen when comparing backtest results with real-time trading performance, since broker accounts convert profit and losses using a much more current exchange rate.

Example: setting the initial capital and currency with code

The above explanation of how the strategy’s initial capital and its currency setting work together is illustrated with the quick programming example below. In it, we won’t submit trades but instead visually show how the strategy’s initial capital is influenced by the script’s currency.

strategy(title="Initial capital without currency conversion", overlay=false, 
     initial_capital=100000)

plot(series=strategy.initial_capital)

We configure the strategy’s properties with the strategy() function: with title we name the strategy and with overlay=false the strategy displays in a separate subchart (TradingView, n.d.). The initial_capital argument is set to 100,000 and, given that we don’t specify the currency argument, the strategy defaults to the standard, unspecified currency (currency.NONE). As such, no currency conversion takes place.

Then we use plot(), a function that displays the data of its series argument on the chart (TradingView, n.d.). That argument is set here to strategy.initial_capital, the built-in variable that returns the strategy’s initial capital (TradingView, n.d.).

When we add the script to an instrument that quotes in US dollars (like the S&P 500 Index CFD), it plots the following:

Example of a TradingView strategy without currency conversion

Here we see the strategy’s initial capital plotted at 100,000 and, more importantly, this value remains the same. Without translating one currency into another, the strategy has 100,000 available to invest – independent from the time period of the strategy’s backtest. That, however, is not the case with currency conversion as we’ll see below.

Example: a strategy’s initial capital influenced by currency conversion

In the previous example, TradingView didn’t perform currency conversion. That happened because we didn’t use the strategy() function to specify that the strategy used another currency than the instrument did. That made our initial capital of 100,000 denominated in the same currency as the instrument (US dollars). But what is the strategy’s buying power with € 100,000?

To see how that works out, we update the previous example to the following:

strategy(title="Initial capital with currency conversion", overlay=false,
     currency=currency.EUR, initial_capital=100000)

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

plot(series=strategy.initial_capital * eurUSDrate)

We’ve made a couple of adjustments here. First, we set the currency argument of strategy() to currency.EUR. That makes the strategy’s account quote in euros instead of ‘Default’ currency.

To plot the buying power of our starting funds, we have to do our own currency conversion. That’s because the strategy.initial_capital variable returns the initial capital setting, which is in the strategy’s currency and those funds are euros now. But we’re interested here in how much US dollars we get for € 100,000 to trade the S&P 500 Index CFD with.

So we need to get the EUR/USD exchange rate. For that we use security(), a function that lets us request data from different symbols and/or resolutions other than the one that the script currently calculates on (Pine Script Language Tutorial, n.d.). We use that function with three arguments here. The first specifies the symbol to load, the second the resolution, and the last the values that security() should return (TradingView, n.d.).

For the first argument we use "EURUSD" to get the Euro/US dollar forex exchange pair. With the second argument, "D", we specify that security() should load daily data (Pine Script Language Tutorial, n.d.). The last argument is close, and that makes the function return daily EUR/USD closing prices.

However, currency conversion in TradingView isn’t done with current forex prices but with the closing price of the previous day (Pine Script Language Tutorial, n.d.). This means our security() function call should return the previous day’s closing price. For that we use the history referencing operator ([]) with a value of 1 between its square brackets. We store those EUR/USD prices of the previous day in the eurUSDrate variable.

We again end the example by plotting the strategy’s initial equity. But this time we multiply the value of strategy.initial_capital with the EUR/USD exchange rate (stored in the eurUSDrate variable). That way we don’t plot the initial capital in euros, but the equivalent buying power in US dollars.

The updated script looks like this when added to the chart:

Example of a TradingView strategy that uses currency conversion

Even though this chart displays the same time period as the previous, there are a couple of differences. First, due to the exchange rate the buying power in US dollars is bigger than in euros. This means the strategy can take larger positions (in dollars) without the profits and risks (both measured in euros) also being bigger.

The second consequence of currency conversion and the strategy’s initial capital, is that the strategy’s buying power (without margin) differs every single day. While we can expect these daily fluctuations to be minor most of the time, big currency moves will have an important impact on our strategy’s initial capital. And that influences anything that derives from the strategy’s starting funds, like its equity, performance metrics (like return and drawdown), and order sizing based on equity percentage.

Summary

Every TradingView strategy needs to use strategy(). That function configures the strategy’s settings with several arguments, including the required title and optional initial_capital. That latter sets the strategy’s initial capital, and the script uses those funds when backtesting and forward testing. When we don’t set this argument, the strategy’s initial capital defaults to 100,000. A strategy’s capital is denominated in the currency used by the strategy, and we set that currency with the currency argument of strategy(). Without setting that argument, the strategy implicitly uses the same currency as the instrument it trades in. But when we set it to a specific value, the strategy’s initial capital is denominated in that currency. When we then trade an instrument that quotes in a different currency, our buying power is affected by currency differences. TradingView performs those currency conversions with the foreign exchange rate of the previous forex session close. That means the same currency conversion rate is used for the whole day of trading, while another consequence is that each day our buying power differs.

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