add base-asset-baseline flag for backtest

This commit is contained in:
c9s 2020-11-11 14:39:33 +08:00
parent 04f6da3cb8
commit 097b2d30a6

View File

@ -20,6 +20,7 @@ import (
func init() { func init() {
BacktestCmd.Flags().String("exchange", "", "target exchange") BacktestCmd.Flags().String("exchange", "", "target exchange")
BacktestCmd.Flags().Bool("sync", false, "sync backtest data") BacktestCmd.Flags().Bool("sync", false, "sync backtest data")
BacktestCmd.Flags().Bool("base-asset-baseline", false, "use base asset performance as the competitive baseline performance")
BacktestCmd.Flags().CountP("verbose", "v", "verbose level") BacktestCmd.Flags().CountP("verbose", "v", "verbose level")
BacktestCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file") BacktestCmd.Flags().String("config", "config/bbgo.yaml", "strategy config file")
RootCmd.AddCommand(BacktestCmd) RootCmd.AddCommand(BacktestCmd)
@ -44,6 +45,11 @@ var BacktestCmd = &cobra.Command{
return errors.New("--config option is required") return errors.New("--config option is required")
} }
wantBaseAssetBaseline, err := cmd.Flags().GetBool("base-asset-baseline")
if err != nil {
return err
}
wantSync, err := cmd.Flags().GetBool("sync") wantSync, err := cmd.Flags().GetBool("sync")
if err != nil { if err != nil {
return err return err
@ -183,13 +189,15 @@ var BacktestCmd = &cobra.Command{
log.Infof("FINAL BALANCES:") log.Infof("FINAL BALANCES:")
finalBalances.Print() finalBalances.Print()
initBaseAsset := InBaseAsset(initBalances, market, startPrice) if wantBaseAssetBaseline {
finalBaseAsset := InBaseAsset(finalBalances, market, lastPrice) initBaseAsset := InBaseAsset(initBalances, market, startPrice)
log.Infof("INITIAL ASSET ~= %s %s (1 %s = %f)", market.FormatQuantity(initBaseAsset), market.BaseCurrency, market.BaseCurrency, startPrice) finalBaseAsset := InBaseAsset(finalBalances, market, lastPrice)
log.Infof("FINAL ASSET ~= %s %s (1 %s = %f)", market.FormatQuantity(finalBaseAsset), market.BaseCurrency, market.BaseCurrency, lastPrice) log.Infof("INITIAL ASSET ~= %s %s (1 %s = %f)", market.FormatQuantity(initBaseAsset), market.BaseCurrency, market.BaseCurrency, startPrice)
log.Infof("FINAL ASSET ~= %s %s (1 %s = %f)", market.FormatQuantity(finalBaseAsset), market.BaseCurrency, market.BaseCurrency, lastPrice)
log.Infof("%s BASE ASSET PERFORMANCE: %.2f%% (= (%.2f - %.2f) / %.2f)", symbol, (finalBaseAsset-initBaseAsset)/initBaseAsset*100.0, finalBaseAsset, initBaseAsset, initBaseAsset) log.Infof("%s BASE ASSET PERFORMANCE: %.2f%% (= (%.2f - %.2f) / %.2f)", symbol, (finalBaseAsset-initBaseAsset)/initBaseAsset*100.0, finalBaseAsset, initBaseAsset, initBaseAsset)
log.Infof("%s PERFORMANCE: %.2f%% (= (%.2f - %.2f) / %.2f)", symbol, (lastPrice-startPrice)/startPrice*100.0, lastPrice, startPrice, startPrice) log.Infof("%s PERFORMANCE: %.2f%% (= (%.2f - %.2f) / %.2f)", symbol, (lastPrice-startPrice)/startPrice*100.0, lastPrice, startPrice, startPrice)
}
} }
} }