mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
Merge pull request #140 from c9s/fix/orderbook-validation
fix: fix orderbook valudation logic
This commit is contained in:
commit
30efb06b34
|
@ -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 {
|
||||
|
|
50
pkg/types/orderbook_test.go
Normal file
50
pkg/types/orderbook_test.go
Normal 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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user