2020-10-10 05:14:06 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2021-01-14 06:56:13 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-10-10 05:14:06 +00:00
|
|
|
|
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-21 07:58:58 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
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() {
|
2020-10-23 06:28:07 +00:00
|
|
|
PnLCmd.Flags().String("exchange", "", "target exchange")
|
|
|
|
PnLCmd.Flags().String("symbol", "BTCUSDT", "trading symbol")
|
|
|
|
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()
|
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
exchangeNameStr, err := cmd.Flags().GetString("exchange")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
exchangeName, err := types.ValidExchangeName(exchangeNameStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-10 05:14:06 +00:00
|
|
|
symbol, err := cmd.Flags().GetString("symbol")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
exchange, err := cmdutil.NewExchange(exchangeName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2021-01-14 07:10:11 +00:00
|
|
|
db, err := bbgo.ConnectMySQL(viper.GetString("mysql-url"))
|
2020-10-10 05:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tradeService := &service.TradeService{DB: db}
|
|
|
|
|
|
|
|
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)
|
2020-11-05 03:00:51 +00:00
|
|
|
trades, err = tradeService.QueryForTradingFeeCurrency(exchange.Name(), symbol, tradingFeeCurrency)
|
2020-10-10 05:14:06 +00:00
|
|
|
} else {
|
2021-01-29 10:48:00 +00:00
|
|
|
trades, err = tradeService.Query(service.QueryTradesOptions{
|
|
|
|
Exchange: exchange.Name(),
|
|
|
|
Symbol: symbol,
|
|
|
|
})
|
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
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
now := time.Now()
|
|
|
|
kLines, err := exchange.QueryKLines(ctx, symbol, types.Interval1m, types.KLineQueryOptions{
|
2021-01-19 18:45:13 +00:00
|
|
|
Limit: 100,
|
|
|
|
EndTime: &now,
|
2020-11-06 18:57:50 +00:00
|
|
|
})
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2020-11-06 18:57:50 +00:00
|
|
|
if len(kLines) == 0 {
|
|
|
|
return errors.New("no kline data for current price")
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:45:13 +00:00
|
|
|
currentPrice := kLines[len(kLines)-1].Close
|
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
|
|
|
|
},
|
|
|
|
}
|