Merge pull request #140 from c9s/fix/orderbook-validation

fix: fix orderbook valudation logic
This commit is contained in:
Yo-An Lin 2021-03-02 10:45:09 +08:00 committed by GitHub
commit 30efb06b34
2 changed files with 55 additions and 1 deletions

View File

@ -154,7 +154,11 @@ func (b *OrderBook) IsValid() (bool, error) {
return false, errors.New("empty asks")
}
return bid.Price < ask.Price, fmt.Errorf("bid price %f > ask price %f", bid.Price.Float64(), ask.Price.Float64())
if bid.Price > ask.Price {
return false, fmt.Errorf("bid price %f > ask price %f", bid.Price.Float64(), ask.Price.Float64())
}
return true, nil
}
func (b *OrderBook) PriceVolumesBySide(side SideType) PriceVolumeSlice {

View File

@ -0,0 +1,50 @@
package types
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
func TestOrderBook_IsValid(t *testing.T) {
ob := OrderBook{
Bids: PriceVolumeSlice{
{fixedpoint.NewFromFloat(100.0), fixedpoint.NewFromFloat(1.5)},
{fixedpoint.NewFromFloat(90.0), fixedpoint.NewFromFloat(2.5)},
},
Asks: PriceVolumeSlice{
{fixedpoint.NewFromFloat(110.0), fixedpoint.NewFromFloat(1.5)},
{fixedpoint.NewFromFloat(120.0), fixedpoint.NewFromFloat(2.5)},
},
}
isValid, err := ob.IsValid()
assert.True(t, isValid)
assert.NoError(t, err)
ob.Bids = nil
isValid, err = ob.IsValid()
assert.False(t, isValid)
assert.EqualError(t, err, "empty bids")
ob.Bids = PriceVolumeSlice{
{fixedpoint.NewFromFloat(80000.0), fixedpoint.NewFromFloat(1.5)},
{fixedpoint.NewFromFloat(120.0), fixedpoint.NewFromFloat(2.5)},
}
ob.Asks = nil
isValid, err = ob.IsValid()
assert.False(t, isValid)
assert.EqualError(t, err, "empty asks")
ob.Asks = PriceVolumeSlice{
{fixedpoint.NewFromFloat(100.0), fixedpoint.NewFromFloat(1.5)},
{fixedpoint.NewFromFloat(90.0), fixedpoint.NewFromFloat(2.5)},
}
isValid, err = ob.IsValid()
assert.False(t, isValid)
assert.EqualError(t, err, "bid price 80000.000000 > ask price 100.000000")
}