_G
Persistently save data. This function implements a savable global dictionary feature. The data structure is a key-value pair table, permanently saved in the docker's local database file.
_G()
_G(k)
_G(k, v)Examples
javascript
function main(){
// 设置一个全局变量num,值为1
_G("num", 1)
// 更改一个全局变量num,值为字符串ok
_G("num", "ok")
// 删除全局变量num
_G("num", null)
// 返回全局变量num的值
Log(_G("num"))
// 删除所有全局变量
_G(null)
// 返回实盘ID
var robotId = _G()
}
python
def main():
_G("num", 1)
_G("num", "ok")
_G("num", None)
Log(_G("num"))
_G(None)
robotId = _G()
c++
void main() {
_G("num", 1);
_G("num", "ok");
_G("num", NULL);
Log(_G("num"));
_G(NULL);
// 不支持 auto robotId = _G();
}Returns
| Type | Description |
string / number / bool / object / array / null | The key value data in the persistently saved |
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 uses a separate database. When the strategy restarts or the docker stops running, the data saved by the _G() function will persist. If the backtest ends, the data saved by the _G() function in the backtest system will be cleared. When using the _G() function to persistently save data, it should be used reasonably according to the memory and disk space of the hardware device to avoid abuse.
In live trading, when calling the _G() function without passing any parameters, the _G() function returns the Id of the current live trading.
When calling the _G() function, passing null for parameter v means deleting the k-v key-value pair.
When calling the _G() function and only passing parameter k (string type), the _G() function returns the saved key value corresponding to parameter k.
When calling the _G() function and only passing null for parameter k, it means deleting all recorded k-v key-value pairs.
After a k-v key-value pair has been persistently saved, calling the _G() function again with the already persistently saved key name as parameter k and a new key value as parameter v will update the k-v key-value pair.
Taking live trading Id 123456 as an example, the key-value pair data persistently saved using the _G() function is stored in the database file /logs/storage/123456/123456.db3 under the docker directory to which the live trading (i.e., strategy instance program) belongs, and the data is recorded in the kvdb table.