3

Is it possible to create a custom class in Pine and how can I create one? I have searched the web on how to make a class in Pine Script, but I have not found a single page.

Here is a class example made in Python:

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)
Marcus Cazzola
  • 313
  • 1
  • 9

2 Answers2

4

As of v5, Pine Script does not support classes. Types are the closest thing to a class in the Pine Script.

For example:

// @version=5
library('mylib')

// Custom types can only have attributes. No methods are allowed.
export type person        // lower first case is just a convention as seen in builtin objects
    string  name
    int     age
    over18  boolean
    bool    isVip = false // Only constant literals, no expressions historical access allowed in default value.

export new(string name, int age) =>
    isOver18 = age >= 18
    person   = person.new(name, age, isOver18)
    person

You can use it as below after you published your library. (You don't have to make it a library, you can just add it to your code, but when used as a library, it is closer to a class.)

import your_username/mylib/1

// Create with our factory/constructor
john         = mylib.new("John", 25)
isJohnOver18 = john.over18                  // true

// Unfortunately there is nothing to prevent direct object creation
mike         = mylib.person.new("Mike", 25) // Create object with type's builtin constructor.
isMikeOver18 = mike.over18                  // na

Edit: I see strange behavior currently I couldn't understand the reason when used typed arguments and access their historical value. See the example below:


weirdFunction(person user) =>
    // This is somehow not working as expected in the function.
    // However it works flawlessly in a global context.
    previousAge = user.age[1] // Gets wrong value

// Workaround

correctFunction(person user) =>
    previousUser = user[1]
    previousAge = previousUser.age
özüm
  • 1,166
  • 11
  • 23
  • This is great thanks! It is even possible to define a function that takes in a Type. For example `fun(Person p) => p.age + p.money` – Marcus Cazzola Nov 26 '22 at 10:21
  • @MarcusCazzola, I want to warn you about weird behavior I see when I use typed arguments with function parameters. See my new edit in the answer. – özüm Nov 27 '22 at 13:18
  • @ozm: when I assign a new var to user[1] (not in any func), I get "Error on bar 0: Cannot access the 'person.age' field of an undefined object. The object is na' and the script fails to render. Error makes sense, but do you know how to resolve it? (also, thanks for this example, helped me get started with objects). – John Smith Jan 05 '23 at 18:48
  • 1
    @JohnSmith. You cannot use "[1]" on bar 0, because "[1]" means 1 previous bar and there isn't any previous bar on bar 0. You may add an "if" statement like `if bar_index > 0` to skip the calculation on bar 0. – özüm Jan 08 '23 at 09:19
2

No, Pine Script wasn't designed with concept of Object-oriented programming and classes support.

Starr Lucky
  • 1,578
  • 1
  • 7
  • 8
  • Thanks for the answer. Do you know how I can create something that works similar to a class in Pine? There is an object called “table” in Pine. I can maybe use it as a class. – Marcus Cazzola Jul 15 '21 at 05:58