xmaker: add back profit function

This commit is contained in:
c9s 2021-12-27 02:59:55 +08:00
parent a31e2743ee
commit 1fa03cdfd6
2 changed files with 18 additions and 2 deletions

View File

@ -6,8 +6,9 @@ import (
"net/url" "net/url"
"time" "time"
"github.com/c9s/bbgo/pkg/util"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/util"
) )
// ApiClient defines the request builder method and request method for the API service // ApiClient defines the request builder method and request method for the API service
@ -16,7 +17,7 @@ type ApiClient interface {
NewAuthenticatedRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error) NewAuthenticatedRequest(method, refURL string, params url.Values, payload interface{}) (*http.Request, error)
// NewRequest builds up the http request for public endpoints // NewRequest builds up the http request for public endpoints
NewRequest(method, refURL string, params url.Values, body []byte) (*http.Request, error) NewRequest(method, refURL string, params url.Values, payload []byte) (*http.Request, error)
// SendRequest sends the request object to the api gateway // SendRequest sends the request object to the api gateway
SendRequest(req *http.Request) (*util.Response, error) SendRequest(req *http.Request) (*util.Response, error)

View File

@ -766,6 +766,21 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
log.WithError(err).Error("save state error") log.WithError(err).Error("save state error")
} }
}) })
s.tradeCollector.OnProfit(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
p := bbgo.Profit{
Symbol: s.Symbol,
Profit: profit,
NetProfit: netProfit,
TradeAmount: fixedpoint.NewFromFloat(trade.QuoteQuantity),
ProfitMargin: profit.DivFloat64(trade.QuoteQuantity),
NetProfitMargin: netProfit.DivFloat64(trade.QuoteQuantity),
QuoteCurrency: s.state.Position.QuoteCurrency,
BaseCurrency: s.state.Position.BaseCurrency,
Time: trade.Time.Time(),
}
s.state.ProfitStats.AddProfit(p)
s.Notify(&p)
})
s.tradeCollector.OnPositionUpdate(func(position *types.Position) { s.tradeCollector.OnPositionUpdate(func(position *types.Position) {
s.Notifiability.Notify(position) s.Notifiability.Notify(position)
}) })