New features in Pine Script®: realistic backtests on Heikin Ashi charts, built-ins to access symbol info, and more

Jun 30, 2023

As we continue to add the improvements you request to our programming language, it can be a challenge to keep up. This post goes through our most important recent additions, so you don’t miss out on them.

More realistic backtests on Heikin Ashi charts

Backtests running on Heikin Ashi charts typically produced unreliable backtest results because the broker emulator always fills orders using the chart’s prices, which in the case of HA charts are synthetic. A new feature called “Fill orders using standard OHLC” alleviates this problem. When used, strategies running on HA charts will fill orders at actual market prices, which more closely simulates how your strategy would behave in real time.

Strategy users can enable the feature in the “Properties” tab of strategies. The feature is off by default.

Note that this only changes the behavior of strategies running on HA charts. On other synthetic chart types such as Renko, backtesting will continue to fill orders at the synthetic chart prices, regardless of the feature’s setting.

New display parameter for input.*() functions

A new display parameter provides you with more control over the display of input values next to a script’s name. Four arguments can be used: display.status_line, display.data_window, display.all and display.none. Combinations of the arguments using plus or minus signs are allowed, and regardless of the argument used, input values will always continue to appear in the “Inputs” tab of the script’s settings.

The new parameter allows you to hide input values from the status line when they would normally appear there, or display the values of inputs that would normally be hidden, namely boolean, color or date inputs. Using the new parameter, you can also show inputs next to the script’s name in the data window. The default display of inputs is unchanged when the parameter is not used.

//@version=5

indicator("RSI")

rsiLengthInput = input.int(14, minval=1, title="RSI Length", display=display.data_window+display.status_line)

rsiSourceInput = input.source(close, "Source", display=display.data_window)

rsi = ta.rsi(rsiSourceInput, rsiLengthInput)

plot(rsi, "RSI")

New built-ins to access instrument information

We added three built-ins to the syminfo namespace: syminfo.sector, syminfo.industry, and syminfo.country. They return strings providing you with more information on the current symbol. The country information is provided in the ISO 3166-1 alpha-2 format.

Disabling alerts for executed orders in strategies

You can now disable notifications for individual orders in strategies by using disable_alert = true in functions used to create those orders. For example:

//@version=5
strategy("My strategy", overlay=true)
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
    strategy.entry("Long", strategy.long, alert_message = "Go Long!")
    strategy.exit("Exit", "Long", profit = 300, loss = 100, disable_alert = true)

This is useful to filter out specific orders when you are using the alert() function to generate custom messages for notifications or webhooks during strategy execution.

Calculation change for maximum drawdown and run-up in strategies

The calculation of maximum drawdown and maximum run-up has been improved to take into account the potential capital that could have been achieved within a trade. Previously, only the capital values at the entry and exit points were considered, which could result in maximum drawdown and run-up values that didn’t correspond to their maximum potential values. The new methods are explained in the Help Center articles for maximum drawdown and maximum run-up.

Support for the varip modifier in user-defined types

The varip modifier, which allows variables to retain values between each execution of the script on the same bar, can now be applied to fields of user-defined types. Note that fields not defined using varip will be rolled back on each bar, so values assigned to them during multiple executions on the same bar will not be preserved, even if the object containing them was created with varip.

For example:

//@version=5
indicator("`varip` field")

type TypeWithVaripField
    varip int varipCounter = 0
    int counter = 0

varip TypeWithVaripField myCounter = TypeWithVaripField.new(0)
// This field preserves values between script executions.
myCounter.varipCounter := myCounter.varipCounter + 1
// This field does not because it wasn't declared using `varip`.
myCounter.counter := myCounter.counter + 1

plot(myCounter.varipCounter, "myCounter.varipCounter")
plot(myCounter.counter, "myCounter.counter")

Currency conversion rate request

We have introduced a new request.currency_rate() function that allows you to retrieve the conversion rate from one currency to another. This function requires two parameters: from and to, each being a three-letter string code according to ISO 4217.

Here’s an example of how to use it to convert the price of an instrument to British pounds (GBP):

//@version=5
indicator("Close in British Pounds")
rate = request.currency_rate(syminfo.currency, "GBP")
plot(close * rate)

New functions in the array namespace

Four new functions were added for arrays:

  • array.first() — returns the first element of the array.
  • array.last() — returns the last element of the array.
  • array.every() — returns true if every element of the array is true; otherwise, returns false.
  • array.some() — returns true if at least one element of the array is true; otherwise, returns false.

To stay up to date on new Pine Script® features, keep an eye on the User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box on Telegram, its Twitter account, and from the Pine Script® Q&A public chat on TradingView.

We believe you find these highly-requested features useful, and please do keep sending us your feedback and suggestions, so we can make the platform the best it can be. We build TradingView for you, and we’re always keen to hear your thoughts.

Team TradingView

Look first Then leap

TradingView is built for you, so make sure you're getting the most of our awesome features
Launch Chart