Just like all TradingView indicators need to use the study() function, all strategy scripts have to use the strategy() function to configure the script’s settings. In this chapter we discuss what this function does, the settings it affects, and how its features can be a help or obstacle. Let’s first look at the structure of the chapter in this article.

In this article:

Configuring TradingView strategies programmatically

Before discussing the different TradingView strategy settings in detail, we first explore the basics of the strategy() function and configuring TradingView strategies by hand:

Setting the visual appearance of a TradingView strategy

After getting the basics of configuring a TradingView strategy down, we look at changing a strategy’s visual appearance with code:

Configuring a TradingView strategy’s initial capital and currency

Beside the visual settings of a TradingView strategy, most options of course relate to how the strategy trades. One group of those settings are the script’s starting capital and currency. Both affect how much cash the strategy has available when simulating trade performance on historical and real-time data.

Both settings – a strategy’s capital and its currency – affect each other, and that’s something we discuss in:

A thing to keep in mind when analysing strategy results converted from another currency is:

Pyramiding in TradingView and tips for configuring position sizing

After examining TradingView’s settings for cash and currency, the next part of the chapter explores TradingView’s position sizing setting:

The large majority of the time, our strategy respects that pyramiding setting and won’t enter more often in a position than allowed. However, there are two situations in which TradingView doesn’t follow our pyramiding setting:

Order sizing of a TradingView strategy script: settings and tips

While a strategy’s pyramiding setting affects the position size, two other settings specify the default size of each individual order. There are three ways in which we can have TradingView calculate the default order size for us:

  • In setting a strategy’s default order size to a fixed amount we discuss how we can have our TradingView strategy submit an order for a certain number of contracts, shares, units, or lots.
  • Another option is sizing orders based on a fixed amount of cash. With this we can specify a certain cash amount (like 25,000) and then when we generate an order, TradingView divides the instrument’s price by that cash amount to get the total number of contracts, units, or lots to trade.
  • The last approach sizes orders based on an equity percentage. We use this feature to set how much of the strategy’s equity to invest in each order (say 20%). Then when an order generates, TradingView figures out the order size based on that amount of the strategy’s current equity.

The last two order sizing approaches (fixed cash amount and equity percentage) are influenced by the strategy’s currency setting. That’s something we explore in the following articles:

Specifying how a TradingView strategy deals with historical bars

Other strategy settings that are configurable in TradingView relate to how the script behaves on historical price bars. Those settings are discussed in:

Configuring if, and when, a strategy performs extra calculations

In the next part of the chapter we discuss which settings of the strategy() function affect how often the strategy calculates per price bar:

  • In calculating a strategy one time after an order fills we examine how to have a strategy perform a single intra-bar calculation after an order fills. This feature allows the strategy to perform another action between the time that an order fills and when the price bar closes (which is when the strategy always calculates).
  • Besides calculating a TradingView strategy after an order fills, our strategy can also calculate with every real-time tick. This makes the script update with each new price, and that makes it respond to price movements that happen inside the price bar instead of having to ‘wait’ till the price bar closes.

Both settings (calculating once after an order fills and calculating with every price update) are not without caveats, however:

But while a one-time intra-bar script calculation immediately after an order fills can influence the strategy’s behaviour, sometimes this feature doesn’t affect the script at all:

Summary of configuring TradingView strategies with code

We end the chapter on the different strategy settings with a summary of configuring TradingView strategies programmatically in which we repeat the most important points.


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