fix regression balance handling

This commit is contained in:
c9s 2020-07-16 18:25:40 +08:00
parent f92108bd23
commit 60efce96b6
2 changed files with 24 additions and 12 deletions

View File

@ -46,18 +46,6 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
var feeRate = 0.001
var askFee = 0.0
// find the first buy trade
//var firstBidIndex = -1
//for idx, t := range trades {
// if t.IsBuyer {
// firstBidIndex = idx
// break
// }
//}
//if firstBidIndex > 0 {
// trades = trades[firstBidIndex:]
//}
for _, t := range trades {
if t.IsBuyer {
bidVolume += t.Volume
@ -108,6 +96,9 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
CurrentPrice: c.CurrentPrice,
NumTrades: len(trades),
BidVolume: bidVolume,
AskVolume: askVolume,
Profit: profit,
AverageBidPrice: averageBidPrice,
Stock: stock,
@ -123,6 +114,8 @@ type ProfitAndLossReport struct {
NumTrades int
Profit float64
AverageBidPrice float64
BidVolume float64
AskVolume float64
Stock float64
Fee float64
}
@ -130,6 +123,8 @@ type ProfitAndLossReport struct {
func (report ProfitAndLossReport) Print() {
log.Infof("trades since: %v", report.StartTime)
log.Infof("average bid price: %s", USD.FormatMoneyFloat64(report.AverageBidPrice))
log.Infof("total bid volume: %f", report.BidVolume)
log.Infof("total ask volume: %f", report.AskVolume)
log.Infof("stock volume: %f", report.Stock)
log.Infof("current price: %s", USD.FormatMoneyFloat64(report.CurrentPrice))
log.Infof("overall profit: %s", USD.FormatMoneyFloat64(report.Profit))

View File

@ -74,9 +74,26 @@ func (trader *KLineRegressionTrader) RunStrategy(ctx context.Context, strategy S
if order.Side == types.SideTypeBuy {
fee = price * volume * 0.001
feeCurrency = "USDT"
quote := trader.Context.Balances[trader.Context.Market.QuoteCurrency]
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"
quote := trader.Context.Balances[trader.Context.Market.QuoteCurrency]
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
}
trade := types.Trade{