How to make a library of functions

2 877 Views No Comments 2 years ago
  • 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:



    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"
    -- calling a ddd function from the library to print "zzz" + the parameter
    -- 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
    so instead of loading everything from memory when CC is called, create a function that handles loading
    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

    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
    Sign in or Register to download for free
  • Phemex x HaasOnline Promotion Type: contest Expires: May 31, 2024 Exclusive
    Receive up to 1,200 USDT! The more you trade, the more you earn.
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free