Bitcoin / US Dollar Perpetual Inverse Swap ContractBitMEX
Huraian
⋅
Currently in PineScript you cannot modify global variables in functions because of scope limitations. One way to work around that is to use arrays. This Library simplifies the use of arrays as global variables to make your code look cleaner. If you're coming from other programming languages, I'm sure you will come across this issue in your PineScript journey at some point.
------------------------------------ The code below will throw an error that says: Cannot modify global variable 'price' in function. var price = 0.0 function() => price := 5.5
------------------------------------ To work around that you can do: var price = array.new_float(1, 0.0) function() => array.set(price, 0, 5.5)
But that code does not spark joy. ------------------------------------ So I bring to you the global library: import marspumpkin/global/1 var price = global.init(0.0) function() => global.set(price, 5.5)
It's a very useful library for all of us, thanks a lot for your contribution to the world. Keep enriching the community in this way...
ArincAkkin
⋅
Hello marspumpkin . How can i use your approach to slove my problem: ma(x, y)=> sma_1 = ta.sma(x, y) smoothVFI ? sma_1 : x
Cannot use global variables in overloaded functions. Function: ma
BDTrading
⋅
Will the global variable set via this method persist on a shutdown and reboot of the software? If not, is this something comparable which is stored to a file which persists across reboots? The concern here is with restoring state after a system reboot which of course can occur (system maintenance) while a strategy is running.
marspumpkin
⋅
@BDTrading, I'm sorry, I don't understand this question.
karatedog
⋅
@BDTrading, Tradingview's Pine has no concept of "reboot". It works almost similar to a FaAS (function as a service or Lambda) solution and there is no persistent storage behind it, your code is a gigantic real-time calculation. If you can store something in RAM, you can do it but there is no "HDD" you save data to as there is no need to. There is no "saving" neither "loading" concepts and commands in Pine.