add doc for writing your own strategy

This commit is contained in:
c9s 2020-10-27 20:36:20 +08:00
parent 96d0be31eb
commit 8e932b3f43

View File

@ -98,6 +98,64 @@ Check out the strategy directory [strategy](pkg/strategy) for all built-in strat
- xpuremaker strategy demonstrates how to maintain the orderbook and submit maker orders [xpuremaker](pkg/strategy/xpuremaker) - xpuremaker strategy demonstrates how to maintain the orderbook and submit maker orders [xpuremaker](pkg/strategy/xpuremaker)
- buyandhold strategy demonstrates how to subscribe kline events and submit market order [buyandhold](pkg/strategy/buyandhold) - buyandhold strategy demonstrates how to subscribe kline events and submit market order [buyandhold](pkg/strategy/buyandhold)
## Write your own strategy
Create your go package, and initialize the repository with `go mod` and add bbgo as a dependency:
```
go mod init
go get github.com/c9s/bbgo
```
Write your own strategy in the strategy directory like `pkg/strategy/mystrategy`:
```
mkdir pkg/strategy/mystrategy
vim pkg/strategy/mystrategy/strategy.go
```
You can grab the skeleton strategy from <https://github.com/c9s/bbgo/blob/main/pkg/strategy/skeleton/strategy.go>
Now add your config:
```
mkdir config
(cd config && curl -o bbgo.yaml https://raw.githubusercontent.com/c9s/bbgo/main/config/buyandhold.yaml)
```
Add your strategy package path to the config file `config/bbgo.yaml`
```yaml
imports:
- github.com/xxx/yyy/pkg/strategy/mystrategy
```
Run `bbgo run` command, bbgo will compile a wrapper binary that imports your strategy:
```sh
dotenv -f .env.local -- bbgo run --config config/bbgo.yaml
```
## Dynamic Injection
In order to minimize the strategy code, bbgo supports dynamic dependency injection.
Before executing your strategy, bbgo injects the components into your strategy object if
it found the embedded field that is using bbgo component. for example:
```go
type Strategy struct {
*bbgo.Notifiability
}
```
And then, in your code, you can call the methods of Notifiability.
Supported components (single exchange strategy only for now):
- `*bbgo.Notifiability`
- `bbgo.OrderExecutor`
## Exchange API Examples ## Exchange API Examples
Please check out the example directory: [examples](examples) Please check out the example directory: [examples](examples)