_G
Persistently store data. This function implements a persistently stored global dictionary, saving data as key-value (KV) pairs permanently in the local database file of the hosting device (docker).
_G()
_G(k)
_G(k, v)Examples
javascript
function main(){
// Set a global variable num with a value of 1
_G("num", 1)
// Change the global variable num to the string value ok
_G("num", "ok")
// Delete the global variable num
_G("num", null)
// Return the value of the global variable num
Log(_G("num"))
// Delete all global variables
_G(null)
// Return the live trading bot ID
var robotId = _G()
}
python
def main():
_G("num", 1)
_G("num", "ok")
_G("num", None)
Log(_G("num"))
_G(None)
robotId = _G()
rust
fn main() {
// Set a global variable num with a value of 1
_G!("num", 1);
// Change the global variable num to the string value ok
_G!("num", "ok");
// Delete the global variable num
_G!("num", null);
// Return the value of the global variable num
Log!(_G!("num"));
// Rust does not support the _G!(null) form for deleting all global variables
// Return the live trading bot ID
let robotId = _G!();
}
c++
void main() {
_G("num", 1);
_G("num", "ok");
_G("num", NULL);
Log(_G("num"));
_G(NULL);
// auto robotId = _G(); is not supported
}Returns
| Type | Description |
string / number / bool / object / array / null | The value data in the persistently stored |
Arguments
| Name | Type | Required | Description |
k | string / null | No | The parameter |
v | string / number / bool / object / array / null | No | The parameter |
See Also
Remarks
Each live trading bot corresponds to a separate database. After the strategy restarts or the hosting device (docker) stops running, the data saved by the _G() function will still persist. However, after a backtest ends, the data saved by the _G() function in the backtesting system will be cleared. When using the _G() function to persistently store data, use it reasonably according to the memory and disk space of the hardware device, and never abuse it.
In live trading, when the _G() function is called without passing any parameters, the _G() function returns the Id of the current live trading bot.
When calling the _G() function, passing a null value for the parameter v indicates deleting the corresponding k-v key-value pair.
When calling the _G() function, if only the parameter k is passed as a string, then the _G() function returns the stored value corresponding to the parameter k.
When calling the _G() function, if only the parameter k is passed as a null value, it indicates deleting all recorded k-v key-value pairs.
After a k-v key-value pair has been persistently stored, calling the _G() function again and passing the persistently stored key name as the parameter k and a new value as the parameter v will update that k-v key-value pair.
Taking a live trading bot with Id 123456 as an example, the K-V key-value data persistently stored using the _G() function is stored in the /logs/storage/123456/123456.db3 database file located in the directory of the hosting device (docker) to which the live trading bot (i.e., the strategy instance program) belongs, and the data is recorded in the kvdb table.