Type/to search
Built-in Functions
Global
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
_G
DBExec
SetChannelData
GetChannelData
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

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

Set, change and delete persistent variables, and read the robot ID.

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")) // Output: null (num has been deleted) // 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")) # Output: null (num has been deleted) _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!(); }

Returns

TypeDescription

string / number / bool / object / array / null value

The value data in the persistently stored k-v key-value pair.

Arguments

NameTypeRequiredDescription

k

string / null value

No

The parameter k is the key name in the key-value pair to be stored; it is case-sensitive.

v

string / number / bool / object / array / null value

No

The parameter v is the value in the key-value pair to be stored; it can be any data that can be JSON serialized.

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.