Object: object oriented programming made possible!

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 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!

  • 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
파인 라이브러리

트레이딩뷰 정신에 따라 오써는 이 파인 코드를 오픈 소스 라이브러리로 퍼블리쉬하여 당사 커뮤니티의 다른 파인 프로그래머들이 쓸 수 있도록 하였습니다. 오써에게 찬사를! 여러분은 이 라이브러리를 프라이빗 또는 오픈 소스 퍼블리케이션에 쓸 수 있지만 퍼블리케이션에 재사용은 하우스룰을 따릅니다.


이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

이 라이브러리를 쓰시겠습니까?

텍스트를 클립보드에 카피한 뒤 님의 스크립트에 붙여 넣기.