Pine Script® does objects

Dec 14, 2022

We launched Pine Script® in 2013 to empower our traders with a programming language they could use to build their own trading models. From its humble beginnings when the big thing was to calculate and plot an RSI line, your suggestions have helped us steadily augment the language you love. Your scripts can now do much more: better backtesting, alerts for trading bots, improved visuals, more flexibility with inputs, a superior editing experience, libraries, and more sophisticated data structures like arrays and matrices. We weren’t kidding when we told you two years ago that Pine was going places.

Pine objects are instantiations of the new user-defined composite types (UDTs) declared using the type keyword. Experienced programmers can think of UDTs as method-less classes.

Now, instead of using independent variables like pivotTime and pivotLevel to track pivot data, you can define a pivotPoint type with two fields to hold those values:

type pivotPoint
    int openTime
    float level

You can then create a new object of that type using the new() constructor:

newPivot = pivotPoint.new(time, high)

And then refer to an object’s field using dot notation:

label.new(newPivot.openTime, newPivot.level, str.tostring(newPivot.level), xloc.bar_time)

Arrays or matrices of objects can also be defined using a type template built with a UDT:

pivotArray = array.new<pivotPoint>()

Objects can be used in tandem with all the other Pine features: you can export user-defined types from libraries, request objects from the request.security() function, and so forth. A function that previously returned a tuple of ten values, for example, can now return a single object containing ten fields. 

A detailed rundown of the new functionality can be found in our User Manual’s page on objects.

See objects in use in the following scripts:

Electrocardiogram Chart indicator by HeWhoMustNotBeNamed

ZigZag library by TradingView, and the built-in ZigZag indicator that was rewritten to use it:

To stay informed of 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 hope you find this highly-requested feature useful. Please keep sending us your feedback and suggestions for improvement. We build TradingView for you, and we’re always keen to hear from you.

Look first Then leap

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