2020-10-10 05:14:06 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-10-11 08:02:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-10 05:14:06 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/cmd/cmdutil"
|
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"
|
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-11 12:08:54 +00:00
|
|
|
pnlCmd.Flags().String("exchange", "", "target exchange")
|
|
|
|
pnlCmd.Flags().String("symbol", "BTCUSDT", "trading symbol")
|
|
|
|
pnlCmd.Flags().String("since", "", "pnl since time")
|
|
|
|
RootCmd.AddCommand(pnlCmd)
|
2020-10-10 05:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 12:08:54 +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
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
db, err := cmdutil.ConnectMySQL()
|
2020-10-10 05:14:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
since, err := cmd.Flags().GetString("since")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var startTime = time.Now().AddDate(-2, 0, 0)
|
|
|
|
if len(since) > 0 {
|
|
|
|
loc, err := time.LoadLocation("Asia/Taipei")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime, err = time.ParseInLocation("2006-01-02", since, loc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tradeService := &service.TradeService{DB: db}
|
|
|
|
tradeSync := &service.TradeSync{Service: tradeService}
|
|
|
|
|
2020-10-11 08:02:17 +00:00
|
|
|
log.Info("syncing trades from exchange...")
|
2020-10-10 09:50:49 +00:00
|
|
|
if err := tradeSync.Sync(ctx, exchange, symbol, startTime); err != nil {
|
2020-10-10 05:14:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-10-11 08:02:17 +00:00
|
|
|
log.Infof("loading all trading fee currency related trades: %s", symbol)
|
2020-10-10 05:14:06 +00:00
|
|
|
trades, err = tradeService.QueryForTradingFeeCurrency(symbol, tradingFeeCurrency)
|
|
|
|
} else {
|
|
|
|
trades, err = tradeService.Query(symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-11 08:02:17 +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
|
|
|
|
}
|
|
|
|
|
2020-10-11 08:02:17 +00:00
|
|
|
log.Infof("found checkpoints: %+v", checkpoints)
|
|
|
|
log.Infof("stock: %f", stockManager.Stocks.Quantity())
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2020-10-10 09:50:49 +00:00
|
|
|
currentPrice, err := exchange.QueryAveragePrice(ctx, symbol)
|
2020-10-10 05:14:06 +00:00
|
|
|
|
2020-10-16 02:16:42 +00:00
|
|
|
calculator := &pnl.AverageCostCalculator{
|
2020-10-10 05:14:06 +00:00
|
|
|
TradingFeeCurrency: tradingFeeCurrency,
|
|
|
|
Symbol: symbol,
|
|
|
|
StartTime: startTime,
|
|
|
|
}
|
2020-10-18 03:18:12 +00:00
|
|
|
|
|
|
|
report := calculator.Calculate(trades, currentPrice)
|
2020-10-10 05:14:06 +00:00
|
|
|
report.Print()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|