One of the most notable features of a TradingView strategy is its name, and we always need to provide our strategies with a name. So how do we programmatically set a strategy’s name?

In this article:

Setting the name of a TradingView strategy with code

In TradingView we configure a strategy’s settings, like its initial capital and account currency, with the strategy() function (TradingView, n.d.). This function needs to be added to the code of every trading strategy (Pine Script Language Tutorial, n.d.), and its title argument always needs to be set too (TradingView, n.d.).

That title argument specifies the name of the strategy (TradingView, n.d.), and that name shows up on the TradingView chart, in the ‘Data Window’, and in the ‘Strategy Tester’ window. A quick example of this argument is:

strategy(title="My example trading script")

We can also set the strategy’s name with shorttitle, an argument that specifies the strategy’s abbreviated name (TradingView, n.d.). Using shorttitle looks like:

strategy(title="My example trading script", shorttitle="My script")

As we’ll see later on in this article, there’s no practical difference between title and shorttitle. And since title is required but shorttitle is optional (TradingView, n.d.), it’s more convenient to just stick with title for naming our strategy.

Note: Both the title and shorttitle arguments of the strategy() function only accept a literal string (like title="My strategy"). They cannot be set to a built-in or user-defined string variable (like title=myTextVariable) nor do they accept a concatenated TradingView string (such as title="Strategy on " + ticker).

Also, a strategy’s name (which is something else than the strategy’s filename) cannot be set manually. This means we always need to use the title argument when setting or changing the name of a strategy.

Example: naming a moving average crossover strategy programmatically

Let’s look at a programming example to examine the title and shorttitle arguments. In the crossover example strategy below, we go long when the quick Simple Moving Average (SMA) crosses above the smoothed SMA (and go short when the quick SMA drops below the slow one).

The image below provides a quick view of what the finished strategy script looks like (with the strategy name highlighted). After discussing the code, we’ll look at how title and shorttitle affect the strategy’s name and what role the script’s filename plays.

TradingView strategy whose name is set programmatically
//@version=2
strategy(title="Moving average crossover", overlay=true)

// Inputs
fastLen = input(title="Fast MA", type=integer, defval=12)
slowLen = input(title="Slow MA", type=integer, defval=24)

// Compute moving averages
fastMA = sma(close, fastLen)
slowMA = sma(sma(close, slowLen), slowLen)

// Plot values
plot(series=fastMA, color=#FF6347, linewidth=2)
plot(series=slowMA, color=#7B68EE, linewidth=2)

// Submit orders
if (crossover(fastMA, slowMA))
    strategy.entry(id="Enter Long", long=true)
    
if (crossunder(fastMA, slowMA))
    strategy.entry(id="Enter Short", long=false)

We start with a comment saying @version=2. This way we make the script use the second version of Pine Script, a version that makes it possible to use if statements (Pine Script Language Tutorial, n.d.). Then we use strategy() to configure the strategy’s properties. With the required title argument we set the strategy’s name and with the function’s overlay argument set to true we display the strategy on the chart’s instrument.

Next we add two input options to the script:

fastLen = input(title="Fast MA", type=integer, defval=12)
slowLen = input(title="Slow MA", type=integer, defval=24)

User-configurable inputs are added to a script with input(), a function that also returns the input’s current setting (TradingView, n.d.). Here we assign those returned values to variables; that way we can refer to the input’s value later on in the script by using the variable.

Both inputs are numerical integer inputs. These accept whole numbers only and are made by setting the type argument of the input() function to integer (Pine Script Language Tutorial, n.d.). The first argument is named “Fast MA” with the title argument, and this name appears before the input in the script’s settings (see image further down below for how the inputs look like). We give this input a default value (defval) of 12 and store its value in the fastLen variable. That variable is used afterward when calculating the fastest moving average.

The other input is named “Slow MA” and starts with a value of 24 (defval=24). We use the assignment operator (=) to store the input’s value in slowLen, a variable that we use later on when computing the slowest moving average.

Those moving averages are then calculated next:

fastMA = sma(close, fastLen)
slowMA = sma(sma(close, slowLen), slowLen)

The value of the moving averages is returned by sma(), a function that calculates a Simple Moving Average (SMA) with two arguments: a series of values to process and an integer that sets the moving average’s length (TradingView, n.d.).

To determine the value of the fastest moving average, we have the sma() function compute on closing prices (close) with a length set by fastLen, our input variable that we gave a default value of 12.

Calculating the value of the slower moving average is a bit more involved. To perform the smoothing, we don’t use sma() once but twice, with one sma() function placed inside another. The first function that’s processed by TradingView is the inner function. That one calculates on the instrument’s closing prices (close) with a length set by slowLen, the input variable that has a default value of 24.

The 24-bar moving average that’s returned by this inner function is the data that the second, outer sma() function calculates on. This latter function also has its length set with the slowLen input variable, and so the outer sma() function calculates the 24-bar moving average of the 24-bar moving average. That value is then assigned to the slowMA variable for use later on in the code.

With the SMAs calculated, we plot them on the chart:

plot(series=fastMA, color=#FF6347, linewidth=2)
plot(series=slowMA, color=#7B68EE, linewidth=2)

The plot() function displays the values of its series argument on the chart (TradingView, n.d.). In the first plot() statement we set that argument to fastMA, the 12-bar moving average. The plot (which displays as a line by default; TradingView, n.d.) has its colour set to #FF6347, the hexadecimal colour value of tomato red. The linewidth argument sets the size of the plot starting with 1 for the default size (TradingView, n.d.), and we set that argument to 2 here for a line that’s a bit bigger than normal.

Similar to the first is the second plot() statement. This one plots the slowMA values of the smoothed moving average. The linewidth of this plot is also 2, and its colour is set to #7B68EE (the hexadecimal colour value of medium slate blue).

The last part of our example strategy performs the actual trading by submitting the orders:

if (crossover(fastMA, slowMA))
    strategy.entry(id="Enter Long", long=true)
    
if (crossunder(fastMA, slowMA))
    strategy.entry(id="Enter Short", long=false)

Here we use two if statements to submit the strategy’s orders conditionally. This works because the indented code below an if statement is only executed when the if statement’s condition evaluates to true (Pine Script Language Tutorial, n.d.).

The condition of the first if statement consists out of the crossover() function. This function requires two arguments and returns true when, on the current bar, the first argument’s value is above that of the second while, on the previous bar, the first argument was below the second (TradingView, n.d.). When no crossover happened, the crossover() function returns false.

The values that we pass into crossover() are the fastMA and slowMA variables. This means the function returns true whenever the 12-bar moving average crossed above the smoothed 24-bar moving average. And the function returns false when such a crossover didn’t happen.

But when the if statement’s condition is true, we call the strategy.entry() function. This functions opens a market position and can, when there’s already a position in the opposite direction, reverse that position (TradingView, n.d.).

We specify two arguments of the strategy.entry() function here. The first is id, which we set to “Enter Long”. This argument specifies the order identifier (TradingView, n.d.), and that name is displayed on the chart and in the ‘Strategy Tester’, but also used by other functions to reference this order.

The other argument that we define is long, and we give it a value of true. This argument, when set to true, makes strategy.entry() submit an enter long order while the function submits an enter short order when long is set to false (TradingView, n.d.).

The second if statement is much like the first. Now we use the crossunder() function with the fastMA and slowMA variables. Given how crossunder() works, it returns true when the faster moving average has crossed below the slower one on the current bar, while on the previous bar the faster SMA was still above the slower average. In all other cases, so when no ‘cross below’ took place, the crossunder() function returns false (TradingView, n.d.).

When the fast average dropped below the slow average, we submit an enter short order with the strategy.entry() function (and reverse any open long position). We set the function’s id argument to “Enter Short” and its long argument is set to false to create an enter short order.

That concludes the programming example. After saving the script and adding it to the chart, we can configure the script’s settings with the following input options:

Example of the TradingView strategy's input options

Now let’s see where the name of our strategy pops up.

Naming a TradingView trading strategy programmatically

In the above example, we set the title argument of the strategy() function to “Moving average crossover”. Once place where this name appears is in the strategy’s chart legend:

Example of the TradingView strategy and its name set with code

The name we gave our strategy also shows up in the chart’s ‘Data Window’:

Example of the TradingView strategy's Data Window

And it appears in the ‘Strategy Tester’ window too:

Name of the TradingView strategy in the Strategy Tester window

So the value that we set to the title argument of the strategy() function is the name that’s used in TradingView for our strategy script. But what about the related shorttitle argument?

Giving a strategy an abbreviated name in TradingView

The shorttitle argument of the strategy() function specifies the abbreviated name of the trading strategy (TradingView, n.d.). To see how this argument affects the strategy’s displayed name, let’s add it to the above programming example by changing the strategy() function to:

strategy(title="Moving average crossover", 
     shorttitle="MA crossover", overlay=true)

After adding the shorttitle argument set to “MA crossover” we save the script. The strategy now displays with the following name on the chart:

Name of the TradingView strategy as set by the shorttitle argument

The chart’s legend name now has changed to “MA crossover”, signalling that the title argument’s value is overridden by shorttitle. This also happens in the ‘Data Window’:

Name of the strategy set in the Data Window

However, in the ‘Strategy Tester’ window the strategy still displays with its name set by title:

Strategy name displayed in the TradingView Strategy Tester window

And so the title and shorttitle arguments of the strategy() function are nearly identical; both affect the script’s name that’s used on the chart and in the ‘Data Window’. The trivial difference between them is that title affects the strategy’s name in the ‘Strategy Tester’ window, whereas shorttitle doesn’t do that.

Tip: Since title is a required argument whereas shorttitle is optional (TradingView, n.d.) and given that there’s no significant difference between them, it’s more straightforward to just use the title argument.

The filename of a TradingView strategy: different than its screen name

Slightly confusing is that neither the title nor the shorttitle argument of the strategy() function set the strategy’s name as it displays in the list of indicators and strategies. Instead, we need to look for the strategy’s filename when adding it to the chart.

To see this in practice, let’s continue with the programming example made above. To rename this script, we click on the ‘Rename script’ icon in the Pine Editor:

Renaming a script in TradingView's Pine Editor

Now let’s rename our script’s filename to the non-descriptive “Example strategy #2”:

Renamed the TradingView strategy's filename in the Pine Editor

Note that this different filename doesn’t affect how the strategy is named on the chart, in the ‘Data Window’, and in the ‘Strategy Tester’ – in those places the script still has the name that we specified with title and shorttitle.

But let’s look at the strategy’s name in the list of scripts. For that we right-click on the chart and select ‘Insert Indicator’:

Adding a script to the TradingView chart

This opens the list of indicators. Here we select the ‘My Scripts’ option to the left:

Filtering the TradingView script with 'My Scripts'

Then we click on the filter setting to the right and select the ‘Strategies’ option:

Filtering by type of script in TradingView

Now we can see all of our trading strategy scripts, including the “Example strategy #2”:

Name of the TradingView strategy as set by its filename in the Pine Editor

So while our strategy is named “Moving average crossover” (with title) and “MA crossover” (with shorttitle), it’s listed here as “Example strategy #2”: the filename that we gave to our script. And so the name that we’re most accustomed to (namely, the name that’s shown on the chart) isn’t the name that we need to remember when we’re looking to add the script to the chart.

Tip: Set the script’s filename to something that closely resembles the name that’s shown on the chart. That makes it easier to find the strategy in the script list when you do remember its chart name but don’t recall its filename.

Summary

All TradingView strategies are required to have the strategy() function in their source code. This function configures the strategy’s properties with several arguments, including title. That required argument sets the strategy’s name with a literal string, and that name is what’s displayed on the chart, in the ‘Data Window’, and in the ‘Strategy Tester’ window. It’s also possible to give the strategy an alternative name – we do that with the shorttitle argument of the strategy() function. But in practice there’s little difference between title and shorttitle, which makes it more convenient to only use title. Note that, when we want to add a strategy to the chart, we need to look for its filename in the scripts list, not the names set by title or shorttitle.

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/