bbgo_origin/bbgo/kline_regression.go

152 lines
3.7 KiB
Go
Raw Normal View History

2020-09-05 08:22:46 +00:00
package bbgo
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
2020-09-19 01:05:06 +00:00
"github.com/c9s/bbgo/pkg/bbgo/accounting"
2020-09-05 08:22:46 +00:00
"github.com/c9s/bbgo/pkg/bbgo/types"
"github.com/c9s/bbgo/pkg/util"
)
type BackTestStream struct {
types.StandardPrivateStream
}
func (s *BackTestStream) Connect(ctx context.Context) error {
return nil
}
func (s *BackTestStream) Close() error {
return nil
}
type BackTestTrader struct {
2020-09-05 08:22:46 +00:00
// Context is trading Context
2020-09-07 06:20:03 +00:00
Context *Context
2020-09-05 08:22:46 +00:00
SourceKLines []types.KLine
2020-09-19 01:05:06 +00:00
ProfitAndLossCalculator *accounting.ProfitAndLossCalculator
2020-09-05 08:22:46 +00:00
doneOrders []*types.SubmitOrder
pendingOrders []*types.SubmitOrder
}
func (trader *BackTestTrader) SubmitOrder(cxt context.Context, order *types.SubmitOrder) {
2020-09-05 08:22:46 +00:00
trader.pendingOrders = append(trader.pendingOrders, order)
}
2020-09-28 07:01:10 +00:00
func (trader *BackTestTrader) RunStrategy(ctx context.Context, strategy MarketStrategy) (chan struct{}, error) {
2020-09-05 08:22:46 +00:00
logrus.Infof("[regression] number of kline data: %d", len(trader.SourceKLines))
done := make(chan struct{})
defer close(done)
2020-09-28 07:01:10 +00:00
if err := strategy.OnLoad(trader.Context, trader); err != nil {
2020-09-05 08:22:46 +00:00
return nil, err
}
stream := &BackTestStream{}
if err := strategy.OnNewStream(stream); err != nil {
2020-09-05 08:22:46 +00:00
return nil, err
}
var tradeID int64 = 0
for _, kline := range trader.SourceKLines {
logrus.Debugf("kline %+v", kline)
fmt.Print(".")
stream.EmitKLineClosed(kline)
2020-09-05 08:22:46 +00:00
for _, order := range trader.pendingOrders {
switch order.Side {
case types.SideTypeBuy:
fmt.Print("B")
case types.SideTypeSell:
fmt.Print("S")
}
var price float64
if order.Type == types.OrderTypeLimit {
price = util.MustParseFloat(order.PriceString)
2020-09-05 08:22:46 +00:00
} else {
price = kline.GetClose()
}
volume := util.MustParseFloat(order.QuantityString)
2020-09-05 08:22:46 +00:00
fee := 0.0
feeCurrency := ""
trader.Context.Lock()
if order.Side == types.SideTypeBuy {
fee = price * volume * 0.001
feeCurrency = "USDT"
quote := trader.Context.Balances[trader.Context.Market.QuoteCurrency]
if quote.Available < volume*price {
logrus.Fatalf("quote balance not enough: %+v", quote)
}
quote.Available -= volume * price
trader.Context.Balances[trader.Context.Market.QuoteCurrency] = quote
base := trader.Context.Balances[trader.Context.Market.BaseCurrency]
base.Available += volume
trader.Context.Balances[trader.Context.Market.BaseCurrency] = base
} else {
fee = volume * 0.001
feeCurrency = "BTC"
base := trader.Context.Balances[trader.Context.Market.BaseCurrency]
if base.Available < volume {
logrus.Fatalf("base balance not enough: %+v", base)
}
base.Available -= volume
trader.Context.Balances[trader.Context.Market.BaseCurrency] = base
quote := trader.Context.Balances[trader.Context.Market.QuoteCurrency]
quote.Available += volume * price
trader.Context.Balances[trader.Context.Market.QuoteCurrency] = quote
}
trader.Context.Unlock()
trade := types.Trade{
ID: tradeID,
Price: price,
Quantity: volume,
Side: string(order.Side),
IsBuyer: order.Side == types.SideTypeBuy,
IsMaker: false,
Time: kline.EndTime,
2020-09-05 08:22:46 +00:00
Symbol: trader.Context.Symbol,
Fee: fee,
FeeCurrency: feeCurrency,
}
tradeID++
trader.ProfitAndLossCalculator.AddTrade(trade)
trader.doneOrders = append(trader.doneOrders, order)
}
// clear pending orders
trader.pendingOrders = nil
}
fmt.Print("\n")
report := trader.ProfitAndLossCalculator.Calculate()
report.Print()
logrus.Infof("wallet balance:")
for _, balance := range trader.Context.Balances {
logrus.Infof(" %s: %f", balance.Currency, balance.Available)
}
return done, nil
}