PINE LIBRARY

Object: object oriented programming made possible!

Telah dikemas kini
Hash map's in Pinescript?? Absolutely

This Library is the first step towards bringing a much needed data structure to the Pine Script community.
"Object" allows Pine coders to finally create objects full or unique key:value pairs, which are converted to strings and stored in an array. Data can be stored and accessed using dedicated get and set methods.

The workflow is simple, but has a few nuances:

0. Import this library into your project; you can give it whatever alias you'd like (I'll be using obj)

1. Create your first object using the obj.new() method and assign it a variable or "ID".

2. Use the object's ID as the first argument into the obj.set() method, for the key and value there's one extra step required. They must be added as arguments to the appropriate prop_<type>() method.
Note: While objects in this library technically only store data as strings, any primitive data type can be converted to a string before being stored, meaning that one object can hold data from multiple types at once. There's a trade off though..Pine Script requires that all exported function parameters have pre-defined types, meaning that as convenient as it would be to have a single method for storing and returning data of every type, it's not currently possible. Instead there are functions to add properties for each individual type, which are then converted to strings automatically (the original type is flagged and stored along with the data). Furthermore, since switch/if statements can only return values of the same type, there must also be "get" methods which correspond with each type. Again, a single "get" method which auto-detects the returned value's type was the goal but it's just not currently possible. Instead each get method is only allowed to return a value of its own type. No worries though, all the "get" methods will throw errors if they can't access the data you're trying to access. In that error message, you'll be informed exactly which "get" method you need to use if you ever lose track of what type of data you should be returning.

3. The second argument for obj.set() method is the obj.prop_<type>() method. You just plug in your key as a string and your value and you're done. Easy as that.
Please do not skip this step, properties must be formatted correctly for data to be stored and accessed correctly

4. Obj.get_<type> (s: string, f: float, b: bool, i: int) methods are even easier, just choose whichever method will return the data type you need, then plug in your ID, and key and that's it. Objects will output data of the same type they were stored as!

There's a short example at the end of the script if you'd like to see more!

prop_string(string: key, string: value)
  • returns property formatted to string and flagged as string type

prop_float(string: key, float: value)
  • returns property formatted to string and flagged as float type

prop_bool(string: key, bool: value)
  • returns property formatted to string and flagged as bool type

prop_int(string: key, int: value)
  • returns property formatted to string and flagged as int type


Support for lines and shapes coming soon!

new()
  • returns an empty object


set(string[]: ID, string: property)
  • adds new property to object


get_f(string[]: ID, string: key)
  • returns float values

get_s(string[]: ID, string: key)
  • returns string values

get_b(string[]: ID, string: key)
  • returns boolean values

get_i(string[]: ID, string: key)
  • returns int values


More methods like Obj.remove(), Obj.size(), Obj.fromString, Obj.fromArray, Obj.toJSON, Obj.keys, & Obj.values coming very soon!!
Nota Keluaran
v2 Minor adjustment in collision handling
arraysdatastructuredictionaryformattinghashhashmapjsonkeyvalueMAPobjectpropertiesstrings

Perpustakaan Pine

Dalam semangat sebenar TradingView, penulis telah menerbitkan kod Pine ini sebagai satu perpustakaan sumber terbuka supaya pengaturcara Pine lain dari komuniti kami boleh menggunakannya semula. Sorakan kepada penulis! Anda boleh menggunakan perpustakaan ini secara peribadi atau dalam penerbitan sumber terbuka lain, tetapi penggunaan semula kod dalam penerbitan ini adalah dikawal oleh Peraturan dalaman.

Penafian