diff --git a/README.md b/README.md index ac1a02863..6b4271420 100644 --- a/README.md +++ b/README.md @@ -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) - 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 + +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 Please check out the example directory: [examples](examples)