2020-10-10 05:14:06 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-14 03:04:56 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-10-10 05:14:06 +00:00
|
|
|
"strings"
|
2021-03-14 03:04:56 +00:00
|
|
|
"time"
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-01-19 18:45:50 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-10 05:14:06 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-10-18 03:33:13 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/accounting"
|
2020-10-16 02:16:42 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/accounting/pnl"
|
2021-01-14 07:10:11 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-10-10 05:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-03-14 03:04:56 +00:00
|
|
|
PnLCmd.Flags().String("session", "", "target exchange")
|
|
|
|
PnLCmd.Flags().String("symbol", "", "trading symbol")
|
|
|
|
PnLCmd.Flags().Bool("include-transfer", false, "convert transfer records into trades")
|
2021-02-05 06:49:42 +00:00
|
|
|
PnLCmd.Flags().Int("limit", 500, "number of trades")
|
2020-10-23 06:28:07 +00:00
|
|
|
RootCmd.AddCommand(PnLCmd)
|
2020-10-10 05:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 06:28:07 +00:00
|
|
|
var PnLCmd = &cobra.Command{
|
2020-10-10 05:14:06 +00:00
|
|
|
Use: "pnl",
|
|
|
|
Short: "pnl calculator",
|
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
2020-10-10 09:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
if len(configFile) == 0 {
|
|
|
|
return errors.New("--config option is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userConfig, err := bbgo.Load(configFile, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sessionName, err := cmd.Flags().GetString("session")
|
2020-10-10 09:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-10 05:14:06 +00:00
|
|
|
symbol, err := cmd.Flags().GetString("symbol")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
if len(symbol) == 0 {
|
|
|
|
return errors.New("--symbol [SYMBOL] is required")
|
|
|
|
}
|
|
|
|
|
2021-02-05 02:12:10 +00:00
|
|
|
limit, err := cmd.Flags().GetInt("limit")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
2021-04-08 16:46:36 +00:00
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
if err := environ.ConfigureDatabase(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
|
2020-10-12 14:46:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("session %s not found", sessionName)
|
|
|
|
}
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2021-04-09 04:43:13 +00:00
|
|
|
if err := environ.SyncSession(ctx, session) ; err != nil {
|
2020-10-10 05:14:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 03:04:56 +00:00
|
|
|
exchange := session.Exchange
|
|
|
|
|
|
|
|
market, ok := session.Market(symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("market config %s not found", symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Now().AddDate(-1, 0, 0)
|
|
|
|
until := time.Now()
|
|
|
|
|
|
|
|
includeTransfer, err := cmd.Flags().GetBool("include-transfer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeTransfer {
|
|
|
|
transferService, ok := exchange.(types.ExchangeTransferService)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("session exchange %s does not implement transfer service", sessionName)
|
|
|
|
}
|
|
|
|
|
|
|
|
deposits, err := transferService.QueryDepositHistory(ctx, market.BaseCurrency, since, until)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = deposits
|
|
|
|
|
|
|
|
withdrawals, err := transferService.QueryWithdrawHistory(ctx, market.BaseCurrency, since, until)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = withdrawals
|
|
|
|
|
|
|
|
// we need the backtest klines for the daily prices
|
|
|
|
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
|
|
|
|
if err := backtestService.SyncKLineByInterval(ctx, exchange, symbol, types.Interval1d, since, until); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 16:58:34 +00:00
|
|
|
|
2020-10-10 05:14:06 +00:00
|
|
|
var trades []types.Trade
|
2020-10-10 09:50:49 +00:00
|
|
|
tradingFeeCurrency := exchange.PlatformFeeCurrency()
|
2020-10-10 05:14:06 +00:00
|
|
|
if strings.HasPrefix(symbol, tradingFeeCurrency) {
|
2021-01-19 18:45:50 +00:00
|
|
|
log.Infof("loading all trading fee currency related trades: %s", symbol)
|
2021-02-06 01:56:26 +00:00
|
|
|
trades, err = environ.TradeService.QueryForTradingFeeCurrency(exchange.Name(), symbol, tradingFeeCurrency)
|
2020-10-10 05:14:06 +00:00
|
|
|
} else {
|
2021-02-06 01:56:26 +00:00
|
|
|
trades, err = environ.TradeService.Query(service.QueryTradesOptions{
|
2021-01-29 10:48:00 +00:00
|
|
|
Exchange: exchange.Name(),
|
|
|
|
Symbol: symbol,
|
2021-03-14 03:04:56 +00:00
|
|
|
Limit: limit,
|
2021-01-29 10:48:00 +00:00
|
|
|
})
|
2020-10-10 05:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:45:50 +00:00
|
|
|
log.Infof("%d trades loaded", len(trades))
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2020-10-18 03:33:13 +00:00
|
|
|
stockManager := &accounting.StockDistribution{
|
2020-10-10 05:14:06 +00:00
|
|
|
Symbol: symbol,
|
|
|
|
TradingFeeCurrency: tradingFeeCurrency,
|
|
|
|
}
|
|
|
|
|
|
|
|
checkpoints, err := stockManager.AddTrades(trades)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:45:50 +00:00
|
|
|
log.Infof("found checkpoints: %+v", checkpoints)
|
|
|
|
log.Infof("stock: %f", stockManager.Stocks.Quantity())
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2021-02-06 21:11:58 +00:00
|
|
|
tickers, err := exchange.QueryTickers(ctx, symbol)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-06 18:57:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:02:08 +00:00
|
|
|
currentTick, ok := tickers[symbol]
|
2021-02-06 21:11:58 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no ticker data for current price")
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPrice := currentTick.Last
|
|
|
|
|
2020-10-16 02:16:42 +00:00
|
|
|
calculator := &pnl.AverageCostCalculator{
|
2020-10-10 05:14:06 +00:00
|
|
|
TradingFeeCurrency: tradingFeeCurrency,
|
|
|
|
}
|
2020-10-18 03:18:12 +00:00
|
|
|
|
2020-10-22 07:57:50 +00:00
|
|
|
report := calculator.Calculate(symbol, trades, currentPrice)
|
2020-10-10 05:14:06 +00:00
|
|
|
report.Print()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|