TradingView
NeoDaNomad
2022년 6월 13일 오전 8시 33분

Object: object oriented programming made possible! 

EUR/USDOANDA

설명

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
코멘트
Omni-Trading
Is it possible to add a password to your indicators using this library?
NeoDaNomad
@OmniTekTrading, Not this hash function, it only returns integers. You would need an alphanumeric string, and a stronger encoder. But it's possible. You need help?
Omni-Trading
kurtsmock
This is really fascinating. Brilliant idea. I'd love to see more evolved examples of how this can be applied and utilized. I'm sure many are like me and this goes at least slightly over my head in terms of practical applications. I guarantee this is over most users heads. But, seems to be a good one to study and more examples would definitely highlight the value of your work. Thanks for publishing this.
NeoDaNomad
@kurtsmock, Thanks, I've been meaning to update this library for a while, I have a lot of projects I'm working on right now, so this got put on the back burner for now. I wanted to publish it though, because I found a thread on Reddit where multiple people were asking about something like this. The point of these data structures is to be able to store multiple properties that can be referenced by name. For instance if I wrote a script that could auto-detect consolidation zones (something i'm currently working on), I would need a way to store all the properties of a consolidation zone (ex. the highest point, the lowest point, the median, the m and b values for linear best fit (not all consolidation zones are horizontal; sometimes consolidation happens at an angle), etc). In Pine Script we locate things like that using conditions, and to make sure the data persists over every bar not just the current one, the script has to be able to store all of those properties while the condition is true so that the data will still be accessible while the condition isn't. The only way to do that currently is to store those properties in a matrix; an array would hold each property and become an individual row or column in the matrix. This is fine, in a for loop you could assign each array index a variable name corresponding to the data at that index (ex. highest = array.get(arr, index) would return the highest price for that specific consolidation zone). The biggest drawback is that hard coding variables to array indices makes debugging really difficult. You're basically assuming that the data you've populated the array with is at the correct index. What if it's not? What if for some reason you accidentally hard coded the index storing the "high" value to the "lowest" variable. Both are float values so the compiler wouldn't catch this mistake but your script wouldn't work correctly, and you'd be sitting for hours scratching your head trying to figure out why. Hash maps take the keyword "highest" and convert it to a unique number or 'hash' and then stores the data at the array index matching the hash code. The data can only be accessed by using the keyword "highest" so you can know that your data will always be where it's supposed to be. If it's not, your program will throw an error and it will never fly under the radar like hard coding array indices would. The same goes for returning values in tuples, assigning a variable to a tuple index ex. <itemA, itemB, itemC> = f() doesn't guarantee that the expected 'itemA' value will be the data that it's supposed to be. Especially since you can only return values of the same type!!! It's setting you up for disaster. Instead of returning a tuple, your function could just return a map holding all of your data (ex MAP = f() ) and you could just reference each zone property by its key (ex. obj.get_<type>(MAP, "highest")). You need to know when your data is mixed up. It saves so much time, and shortens development cycles.
NeoDaNomad
@NeoDaNomad, These are also useful because the whole data set can be compressed into a single string. So if you were using server side alerts and wanted to pass some additional data to an external server you could literally compress the entire dataset into a string (preferably a JSON) and your data could be interpreted by any other programming language that supports JSON, like Python or JS.
kurtsmock
@NeoDaNomad, Thanks so much for taking the time to break that down. Really awesome stuff. I am native to coding in pine and this really cuts to the core of programming language structure so it's definitely a bit outside of my grasp, but I really think you've put it in our grasp by publishing this and providing the explanation here. Pine is such a great language for beginners. Its perfect really. Its a great way to store information that needs to be retrieved later and/or export that information to facilitate automated trading.
NeoDaNomad
@kurtsmock, No problem, man! They definitely keep things simple, which was a little frustrating at first because I'm not used to all of the constraints. In languages like Python and Node.JS there's really no right or wrong way to code, there's so many different ways you can build something out, I took a lot of that for granted. With Pine Script there's a clear cut workflow, and you aren't really able to deviate from it. The idea of a time series made my head spin lol. I actually almost gave up trying to learn Pine. But what kept me around is that coding in Pine doesn't require a development environment. Everything is native and your scripts can run 24/7 on their servers. After a few months I started to appreciate it more, TV put a lot of thought into this language. There's just a few more things I wish they would add, like user defined types and Objects, but other than that it works really well.
goofoffgoose
@kurtsmock, I'm with you. This is very over my head, but so interesting. Even more so because learning Pine is pushing me to learn Python and C# to bring this type a data compiling into other platforms like Quantower and web based alerts. But since I'm new at that, this is mind blowing.
kurtsmock
@goofoffgoose, In speaking with some PineCoding pros, they seem to think that its inefficient, rendering it unusable in its most potent form because you'll run up on PineScript limitations too quickly. That said, I still think its something I want to explore more.
더보기