# How to make a library of functions

2 632 Views No Comments 1 year ago
HTS CLOUDSTABLE
• Update: see at the bottom for important note from pshai about using memory in CCs!

I wanted to port some open-source scripts from TradingView to Haas and wanted to have a neat way of collection all the PineScript functions that I needed in 1 place. Basically, how to make a library, which I can then import 1 time and use with any new converted script. Creating Custom Commands for each PS function would be a hassle, so I wanted something that allows the Pine code to look as close to original as possible AND doesn’t require porting 10 different CCs to use.

So, I found one way to do it like this:
1) create a Custom Command (CC)
2) define functions inside of it
3) put the functions into a table
4) define the output of that CC as the table we created

Then in the script we can assign:
`_ = CC_ThatCCWeJustCreated()`

Then we can use it like this for example:

`_.someTVfunction(parameters)`

_________________________________________________

Here’s the script code to supplement the example CC code that is below.

``````
-- assign the CC to an underscore (so that the code for the functions from the library would look neat, but doesn't have to be underscore)
_ = CC_o()

-- calling a zzz function from the library to print "zzz"
_.zzz()

-- calling a ddd function from the library to print "zzz" + the parameter
_.ddd("kekw")

-- rsi in the library just adds the third parameter to the RSI (so RSI()+50)
rsi_from_library = _.rsi(ClosePrices(), 14, 50)

rsi_local = RSI(ClosePrices(), 14)

Plot(1, "RSI from the library", rsi_from_library, Red)

Plot(1, "RSI from script", rsi_local, Blue)

arr = ArrayIndex

Log(arr(rsi_from_library, 1), Red)
Log(arr(rsi_local, 1), Blue)
``````

________________________________________________pshai tip on memory (Load/Save) in Custom Commands:

just one thing to note with CC’s: you cannot load stuff in CC and save them outside that CC. both needs to happen in the main-script if the object returned has a feature for saving info
that function is then called in the main script AFTER CC has returned the object, so it loads them from the proper memory block; which is main script, not CC

so here is the “dont do this”:

``````
local someLib = CC_SomeLib() -- this handles loading when called, but it loads shit from the CC's memory block

-- do stuff

someLib.SaveStuff() -- this will save the shit in the main scripts memory block
``````

and here is the “do this instead”:

``````
local someLib = CC_SomeLib()

-- do shit

someLib.SaveStuff()
``````

so even though the load function is inside CC, it uses that script’s memory block cuz the code inside that function is executed in main script
so see it like – it copypastes the code where the function is called, and uses the stuff in there

HaasScript Code
{"ft_lang_label":"__label__en","ft_lang_prob":0.8680285,"math_prob":0.7520207,"size":3315,"snap":"2023-40-2023-50","text_gpt3_token_len":821,"char_repetition_ratio":0.14436726,"word_repetition_ratio":0.09913044,"special_character_ratio":0.27662143,"punctuation_ratio":0.0984375,"nsfw_num_words":3,"has_unicode_error":false,"math_prob_llama3":0.9668212,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-09T22:01:08Z\",\"WARC-Record-ID\":\"<urn:uuid:735b1eeb-454a-470a-b910-8430567d9e1c>\",\"Content-Length\":\"135293\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2ba1f2cc-ee6a-44b9-9926-7c32cadcbc38>\",\"WARC-Concurrent-To\":\"<urn:uuid:dd58882e-9794-4033-b694-8c305d2def78>\",\"WARC-IP-Address\":\"104.26.12.192\",\"WARC-Target-URI\":\"https://www.haasscripts.com/t/how-to-make-a-library-of-functions/\",\"WARC-Payload-Digest\":\"sha1:KU3ERK62ESEMJO4TFAAUQQYLL4SUEK5S\",\"WARC-Block-Digest\":\"sha1:YWGW5MULXCIGFBDNUCDEHQ3BRM2ZQB26\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100972.58_warc_CC-MAIN-20231209202131-20231209232131-00837.warc.gz\"}"}