NeoDaNomad

Object: object oriented programming made possible!

NeoDaNomad 已更新   
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!!
版本注释:
v2 Minor adjustment in collision handling
Pine脚本库

本着真正的TradingView精神,作者将此Pine代码以开源脚本库发布,以便我们社区的其他Pine程序员可以重用它。向作者致敬!您可以私下或在其他开源出版物中使用此库,但在出版物中重用此代码受网站规则约束。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想使用这个脚本库吗?

复制以下行并将其粘贴到您的脚本中。