From 858339b761b2fb1ce8853c7a25c1893e745e5b13 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 4 Jul 2022 13:38:09 +0800 Subject: [PATCH] doc: add more details to UserDataStream --- doc/topics/developing-strategy.md | 55 +++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/doc/topics/developing-strategy.md b/doc/topics/developing-strategy.md index d6d9b64fe..332dba5d0 100644 --- a/doc/topics/developing-strategy.md +++ b/doc/topics/developing-strategy.md @@ -328,8 +328,63 @@ next available market price. ## UserDataStream +When you submit an order to the exchange, you might want to know when the order is filled or not, user data stream is +the real time notification let you receive the order update event. +To get the order update from the user data stream: +```go +session.UserDataStream.OnOrderUpdate(func(order types.Order) { + if order.Status == types.OrderStatusFilled { + log.Infof("your order is filled: %+v", order) + } +}) +``` + +However, order update only contains the status of the order, if you need to get the details of the trade execution, +you need the trade update event: + +```go +session.UserDataStream.OnTrade(func(trade types.Trade) { + log.Infof("trade price %f, fee %f %s", trade.Price.Float64(), trade.Fee.Float64(), trade.FeeCurrency) +}) +``` + +To monitor your balance change, you can use the balance update event callback: + +```go +session.UserDataStream.OnBalanceUpdate(func(balances types.BalanceMap) { + log.Infof("balance update: %+v", balances) +}) +``` + +Note that, as we mentioned above, the user data stream is a session-wide stream, that means you might receive the order update event for other strategies. + +To prevent that, you need to manage your active order for your strategy: + +```go +activeBook := bbgo.NewActiveOrderBook("BTCUSDT") +activeBook.Bind(session.UserDataStream) +``` + +Then, when you create some orders, you can register your order to the active order book, so that it can manage the order +update: + +```go +createdOrders, err := session.Exchange.SubmitOrders(ctx, types.SubmitOrder{ + Symbol: "BTCUSDT", + Type: types.OrderTypeLimit, + Price: fixedpoint.NewFromFloat(18000.0), + Quantity: fixedpoint.NewFromFloat(1.0), +}) +if err != nil { + log.WithError(err).Errorf("can not submit orders") +} + +activeBook.Add(createdOrders...) +``` + +## Notification ## Handling Trades and Profit