types: add test for PriceHeartBeat

This commit is contained in:
c9s 2022-01-12 14:42:11 +08:00
parent 5755c44845
commit c3356fa694
2 changed files with 29 additions and 3 deletions

View File

@ -107,9 +107,6 @@ type Strategy struct {
orderStore *bbgo.OrderStore orderStore *bbgo.OrderStore
tradeCollector *bbgo.TradeCollector tradeCollector *bbgo.TradeCollector
lastBidPrice, lastAskPrice fixedpoint.Value
lastBidPriceTime, lastAskPriceTime time.Time
askPriceHeartBeat, bidPriceHeartBeat types.PriceHeartBeat askPriceHeartBeat, bidPriceHeartBeat types.PriceHeartBeat
lastPrice float64 lastPrice float64

View File

@ -0,0 +1,29 @@
package types
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
func TestPriceHeartBeat_Update(t *testing.T) {
hb := PriceHeartBeat{}
updated, err := hb.Update(PriceVolume{Price: fixedpoint.NewFromFloat(22.0), Volume: fixedpoint.NewFromFloat(100.0)}, time.Minute)
assert.NoError(t, err)
assert.True(t, updated)
updated, err = hb.Update(PriceVolume{Price: fixedpoint.NewFromFloat(22.0), Volume: fixedpoint.NewFromFloat(100.0)}, time.Minute)
assert.NoError(t, err)
assert.False(t, updated, "should not be updated when pv is not changed")
updated, err = hb.Update(PriceVolume{Price: fixedpoint.NewFromFloat(23.0), Volume: fixedpoint.NewFromFloat(100.0)}, time.Minute)
assert.NoError(t, err)
assert.True(t, updated, "should be updated when the price is changed")
updated, err = hb.Update(PriceVolume{Price: fixedpoint.NewFromFloat(23.0), Volume: fixedpoint.NewFromFloat(200.0)}, time.Minute)
assert.NoError(t, err)
assert.True(t, updated, "should be updated when the volume is changed")
}