bbgo_origin/pkg/bbgo/position_test.go

37 lines
758 B
Go
Raw Normal View History

2021-01-20 08:08:14 +00:00
package bbgo
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func TestPosition(t *testing.T) {
trades := []types.Trade{
{
2021-01-20 08:10:20 +00:00
Side: types.SideTypeBuy,
Price: 1000.0,
Quantity: 0.01,
QuoteQuantity: 1000.0 * 0.01,
2021-01-20 08:08:14 +00:00
},
{
2021-01-20 08:10:20 +00:00
Side: types.SideTypeBuy,
Price: 2000.0,
Quantity: 0.03,
QuoteQuantity: 2000.0 * 0.03,
2021-01-20 08:08:14 +00:00
},
}
pos := Position{}
for _, trade := range trades {
pos.AddTrade(trade)
}
2021-01-20 08:10:20 +00:00
assert.Equal(t, fixedpoint.NewFromFloat(-70.0), pos.Quote)
2021-01-20 08:08:14 +00:00
assert.Equal(t, fixedpoint.NewFromFloat(0.04), pos.Base)
assert.Equal(t, fixedpoint.NewFromFloat((1000.0*0.01+2000.0*0.03)/0.04), pos.AverageCost)
}