doc: add persistence section

This commit is contained in:
c9s 2022-07-05 11:49:48 +08:00
parent 193703a9a0
commit 13fdea0978
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -457,9 +457,47 @@ bbgo.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
## Persistence ## Persistence
When you need to adjust the parameters and restart BBGO process, everything in the memory will be reset after the
restart, how can we keep these data?
Although BBGO is written in Golang, BBGO provides a useful dynamic system to help you persist your data.
If you have some state needs to preserve before shutting down, you can simply add the `persistence` struct tag to the field,
and BBGO will automatically save and restore your state. For example,
```go
type Strategy struct {
Position *types.Position `persistence:"position"`
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"`
}
```
And remember to add the `persistence` section in your bbgo.yaml config:
```yaml
persistence:
redis:
host: 127.0.0.1
port: 6379
db: 0
```
In the Run method of your strategy, you need to check if these fields are nil, and you need to initialize them:
```go
if s.Position == nil {
s.Position = types.NewPositionFromMarket(s.Market)
}
if s.ProfitStats == nil {
s.ProfitStats = types.NewProfitStats(s.Market)
}
if s.TradeStats == nil {
s.TradeStats = types.NewTradeStats(s.Symbol)
}
```
That's it. Hit Ctrl-C and you should see BBGO saving your strategy states.