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提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。

想使用這個腳本庫嗎?

複製以下行並將其黏貼到您的腳本中。