In TradingView we can configure strategy settings by hand or specify them with code. But which options are available and how do we set them manually and programmatically?

In this article:

Setting a TradingView’s strategy settings: by hand or with code

There are two ways to configure a TradingView strategy. The manual options make it possible to configure multiple instances of the same strategy differently, and they also allow for quickly changing something without having to jump into the code.

The other way to configure strategies is with the strategy() function. This function is something that we already need to add to every TradingView strategy (Pine Script Language Tutorial, n.d.). The advantage of specifying a strategy’s settings with strategy() is that the script will always be added with the right settings to the chart, without the need to manually adjust its settings. That saves time and makes sharing the strategy with the right settings easier.

To configure a strategy with code, we use one or several arguments of strategy(). All of this function’s arguments are (TradingView, n.d.):

strategy(title, shorttitle, overlay, precision, scale, max_bars_back,
    pyramiding, calc_on_order_fills, calc_on_every_tick,
    backtest_fill_limits_assumption, default_qty_type, default_qty_value,
    initial_capital, currency)

Most of these arguments, however, also have their own manual setting. And with those manual options we can override whichever setting we specified in the strategy’s code. This also means we can quickly change a certain setting of the strategy with the corresponding manual option without having to edit the script’s code.

But which manual setting is affected by which code argument, or vice versa: which argument of the strategy() function affects which manual option? Let’s look at the strategy settings to answer that.

Opening the strategy settings window in TradingView

Practically all of the strategy’s settings are configured in its settings window. We can open that window in three ways. The first approach is to click on the gear icon ( ) that’s displayed to the right of the strategy’s name on the chart:

Opening the strategy settings in TradingView

The second way is to left or right click on the strategy’s name and choose ‘Format…’:

Opening the strategy settings in TradingView

When the strategy has plot some values on the chart, then the third and last approach is to double-click on one of those plots:

Opening the strategy settings in TradingView

Now let’s explore what code argument affects which manual option of a TradingView strategy.

Configuring where to display and plot a TradingView strategy

The overlay argument of the strategy() function specifies whether the strategy should display on the chart’s instrument or not (TradingView, n.d.). This argument defaults to false when we don’t set it, and in that case the strategy won’t display on the chart’s price instrument but in a separate chart panel below it.

For instance, we use strategy() like this to have our trading strategy display in the same chart area as the instrument:

strategy(title="Strategy settings - example", overlay=true)

So when the strategy for instance plots a moving average, it displays like:

Displaying a TradingView strategy on the chart

To manually change where the strategy appears (for instance, to have it display below the chart’s instrument), we need to right-click on the strategy’s name on the chart and select ‘Unmerge Down’:

Moving a TradingView strategy off the instrument's chart

That makes the strategy and its value display below the chart’s instrument (the strategy’s trades are, by the way, always displayed on the price chart):

TradingView strategy plotting below the main instrument

But let’s say we’ve configured our TradingView strategy with strategy() to display in a separate chart panel like this:

strategy(title="Strategy settings - example", overlay=false)

Then to manually move the strategy up to the instrument’s chart area, we right-click on the strategy name and select ‘Merge Up’:

Merging a TradingView strategy back to the chart's instrument

And then the strategy plots in the same area as the chart’s instrument:

TradingView strategy back overlaid on the chart's instrument

For more on the overlay argument of the strategy() function, see overlaying a TradingView strategy on the instrument or not.

Specifying the number of decimals plotted by the strategy

precision is another argument of the strategy() function. This argument specifies with how many decimals the strategy’s values display on the chart (TradingView, n.d.). This is an optional argument; when not set, the strategy shows with 4 decimals (TradingView, n.d.). Note that this setting only affects how the strategy’s values show on the chart; it doesn’t affect the number of decimals that the strategy calculates with.

We can use precision like this:

strategy(title="Strategy settings - example", precision=2)

A strategy that has its precision set to 2 displays its values with as many decimals on the price axis:

TradingView strategy with 2 decimals on the price axis

To manually change that number of decimals, we need to open the strategy settings window. To do so we can click the gear icon ( ) that’s displayed to the right of the strategy’s name (alternatively, we can double-click on one of the strategy’s plotted values):

Opening te strategy settings window in TradingView

This brings up a window with the strategy settings. When we select the ‘Style’ tab we can use the ‘Precision’ setting to specify how many decimals the strategy’s values should use:

Changing a TradingView strategy's precision by hand

When we, for example, change that option to 6 and press ‘OK’, then the strategy’s values display as follows:

Precision of a TradingView strategy changed: more decimals displayed now

To learn more about precision, see setting the precision of a TradingView strategy with code and why a strategy has a different precision than configured.

Configuring a TradingView strategy’s price scale

Another argument of the strategy() function is scale. This argument specifies which price scale the strategy’s values display on, and does so with one of three values (TradingView, n.d.):

  • When we set scale to scale.right, the strategy’s values display on the right price axis. This is also the default behaviour whenever we don’t configure the scale setting.
  • With scale set to scale.left the strategy’s values shown on the left price axis.
  • And with scale set to scale.none the strategy doesn’t use a price scale but the full chart area instead. For this setting to work we need to display the strategy on the chart’s instrument.

The advantage of this scale setting is that we can plot very high or low strategy values on a different scale to prevent distorting the existing price scale. Using the scale argument goes like this:

strategy(title="Strategy settings - example", overlay=true, scale=scale.left)

A strategy that displays its values on the left price axis looks like:

Attaching a TradingView strategy to the left price scale

To manually change which price scale the strategy uses for its values, we first double-click on one of the strategy’s plots or click on the gear icon ( ):

Opening the strategy settings window in TradingView

That brings us to the strategy settings window. There we go to the ‘Style’ tab and change the price scale used by the strategy with the ‘Scale’ option:

Changing the scaling of a TradingView strategy by hand

Now when we set the strategy’s scale to ‘Scale Right’, then our previous chart becomes distorted because the strategy’s values differ strongly from the instrument’s prices:

Skewed TradingView chart due to strongly different strategy values

See changing the price scale of a TradingView strategy programmatically for more on the scale argument and its different options.

Setting a TradingView strategy’s maximum number of entries

Another strategy option that can be set with code and manually is the strategy’s pyramiding setting. This option is set with the pyramiding argument of the strategy() function, and it specifies the number of entries that are allowed in the same direction (TradingView, n.d.).

We use the pyramiding argument as follows:

strategy(title="Strategy settings - example", pyramiding=2)

The number of entries in the same direction can also be set by hand. For that we open the strategy’s settings window, for instance by clicking on the gear icon ( ):

Opening the strategy settings in TradingView

In the strategy settings window we then go to the ‘Properties’ tab and there we configure the strategy’s pyramiding settings with two options. The ‘Pyramiding’ checkbox enables or disables pyramiding, and the ‘Allow Up To n orders’ setting defines how many entries in the same direction are allowed:

Configuring the pyramiding settings in TradingView by hand

We can also turn off that ‘Pyramiding’ checkbox programmatically by setting the pyramid argument of the strategy() function to 0.

To learn more about the number of entries in the same direction, see configuring the pyramiding settings of a TradingView strategy. For a better insight into how pyramiding affects the strategy’s position size, see multiple entry orders and their effect on pyramiding and the influence of the strategy.order() function on a strategy’s pyramiding behaviour.

An additional intra-bar strategy calculation whenever an order fills

Another argument of the strategy() function is calc_on_order_fill. This argument, when set to true, makes the strategy perform an additional intra-bar calculation each time an order fills (TradingView, n.d.). The argument’s default value is false, and in that case no additional calculation happens after an order fills and the next time that the strategy calculates will be on bar close.

To have our TradingView strategy perform that additional intra-bar calculation on both historical and real-time data, we use strategy() like this:

strategy(title="Strategy settings - example", calc_on_order_fills=true)

Configuring this setting by hand is possible too. To do so we open the strategy’s settings by clicking on the gear icon ( ) besides the strategy’s name:

Opening the strategy settings window in TradingView

This brings up a window with the strategy settings. There, in the ‘Properties’ tab, we can enable (or disable) an additional intra-bar script calculation when an order fills with the ‘Recalculate After Order Filled’ checkbox:

Setting the recalculate settings of a TradingView strategy

See recalculating a TradingView strategy once an order fills for more on calc_on_order_fills. For an in-depth discussion of this argument, see why a TradingView strategy isn’t always affected by calc_on_order_fills and the backtest and real-time difference with calc_on_order_fills.

Calculating a TradingView strategy with every price update (or not)

A TradingView strategy can also calculate with every real-time price update. We configure that by setting the calc_on_every_tick argument of the strategy() function to true (TradingView, n.d.). Those additional calculations, however, won’t happen on historical price bars – those bars are still calculated on bar close only (Pine Script Language Tutorial, n.d.).

Enabling that real-time calculation feature is done like:

strategy(title="Strategy settings - example", calc_on_every_tick=true)

This strategy setting can also be configured by hand. For that we need to open the strategy settings window, for instance with the gear icon ( ):

Opening the TradingView strategy settings window

This opens the strategy settings window. We select the ‘Properties’ tab here and then we can enable (or disable) whether the strategy calculates on every real-time price update with the ‘Recalculate On Every Tick’ checkbox:

Enabling a strategy to calculate with every real-time tick

For more on this argument and how it affects TradingView strategies, see calculating a TradingView strategy with every real-time tick and inaccurate backtest results when a strategy processes every real-time price update.

Specifying how strict TradingView fills limit orders during a backtest

With the strategy() function we can also configure how strict TradingView backtests limit orders. We use the backtest_fill_limits_assumption argument for that. TradingView only fills limit orders during backtesting when the bar’s price exceeds the limit order price by the number of ticks set by that argument (TradingView, n.d.).

The default value of backtest_fill_limits_assumption is 0, and with that limit orders fill as soon as their limit price is touched. When the strategy is tested on real-time data, TradingView uses market prices to determine whether or not fill.

Setting that option programmatically is done like this:

strategy(title="Strategy settings - example", 
     backtest_fill_limits_assumption=1)

Like most options, this feature can also be configured manually. For that we open the strategy settings window, for instance by clicking the gear icon ( ):

Opening the strategy settings window in TradingView

This brings up a window with the strategy’s settings. Here we select the ‘Properties’ tab and there we can configure how strict TradingView deals with limit orders with the ‘Verify Price For Limit Orders’ option:

Setting the backtest fill limit assumption in TradingView by hand

See specifying how strict TradingView fills limit orders during backtesting for more on the backtest_fill_limits_assumption argument.

Setting a strategy’s default order size in TradingView

Another thing we can configure with the strategy() function is the default order size, and we use the default_qty_type and default_qty_value arguments for that (TradingView, n.d.). These specify the default order size.

Our strategy uses that default size whenever we don’t specify the order quantity ourselves. That is, the default order size applies when we use the strategy.entry(), strategy.exit(), or strategy.order() functions to submit an order without specifying its size.

By combining the default_qty_type and default_qty_value arguments we can set the strategy’s default order size in three ways (TradingView, n.d.):

So to have our strategy trades orders based on 10,000 cash, we use the default_qty_type and default_qty_value arguments of the strategy() function like this:

strategy(title="Strategy settings - example", 
    default_qty_type=strategy.cash,
    default_qty_value=10000)

And to invest 15% of the strategy’s equity in each order we use strategy() like:

strategy(title="Strategy settings - example", 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=15)

A strategy’s default order size can also be set by hand. We do that in the strategy settings window, which we can open with the gear icon ( ):

Opening the strategy settings window in TradingView

This opens a window in which we select the ‘Properties’ tab. There we set the default order size with the ‘Order Size’ option. And with the pull-down menu that’s behind this option we configure how the default order size is calculated:

Options for configuring the default order size in TradingView

Setting the strategy’s initial backtest capital with code or by hand

Another strategy setting that we can specify with the strategy() function is the strategy’s initial capital, and for that we use the function’s initial_capital argument (TradingView, n.d.). That capital amount is used by the strategy when simulating its performance on historical and real-time data.

For instance, to set a strategy’s starting funds to 12,500 we use strategy() like so:

strategy(title="Strategy settings - example", initial_capital=12500)

Besides setting that option with code we can configure it by hand too. For that we click on the gear icon ( ) displayed to the right of the strategy’s name:

Opening the strategy settings window in TradingView

That brings us back to the strategy settings window. Here we go to the ‘Properties’ tab and there we configure the strategy’s backtest capital with the ‘Initial Capital’ setting:

Changing the initial capital of a TradingView strategy by hand

For more on this option, see setting the initial capital of a TradingView strategy with code. The initial capital is also affected by the strategy’s currency setting, and that’s something we discuss in a strategy’s initial capital and currency conversion.

Configuring a strategy’s currency with code or by hand

The last argument of the strategy() function is currency, and this one specifies the currency used by the strategy (TradingView, n.d.). With this option we can have the strategy calculate with a different currency than used by the instrument it trades (Pine Script Language Tutorial, n.d.). That means things like the strategy’s net profit and equity are converted to the account currency that we set with currency. Note that, when we don’t set this argument, no currency conversion takes place and the strategy reports in the same currency as the instrument it trades.

To programmatically set the strategy to a specific currency, we give the currency argument one of the following values (TradingView, n.d.):

currency argument value Currency used by the 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 (that is, the default currency). With this value the strategy reports its results in the same currency that the chart’s instrument uses (and no currency conversion takes place).
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

So when we want the strategy to use Australian Dollars, we use strategy() like this:

strategy(title="Strategy settings - example", currency=currency.AUD)

It’s also possible to change the strategy’s currency settings by hand. For that we need to open the strategy settings window, and one way to do that is to right-click on the strategy’s name on the chart and select ‘Format’:

Opening the settings window of a TradingView strategy

This opens the window with strategy settings. Here we go to the ‘Properties’ tab, and there we change the strategy’s currency with the ‘Base Currency’ option:

Currency settings in TradingView

For a better understanding of the currency option, see setting a TradingView strategy’s currency with code and a difference between TradingView’s currency conversion and real-time conversion.

Strategy settings that can only be set programmatically

Besides the different strategy options discussed above, there are two options that don’t have an equivalent manual option. That means these can only be configured with the strategy() function:

Summary

We programmatically configure the settings of a TradingView strategy with strategy(); a function that we need to add to every strategy script. Most options that we configure with strategy() can also be set by hand. This has the benefit that we can configure multiple instances of the same strategy differently. It also makes it possible to quickly and easily change the strategy’s settings without having to edit its code. Most of the strategy’s settings are configured in the ‘Properties’ tab of the strategy settings window, which we open by right-clicking on the strategy’s name and choosing ‘Format’ or by pressing the gear icon displayed to the right of the strategy’s name ( ).

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