test trade profit calculation

This commit is contained in:
c9s 2021-01-20 16:14:02 +08:00
parent 169af63846
commit 617f5119fd
2 changed files with 12 additions and 2 deletions

View File

@ -34,7 +34,7 @@ func (p *Position) AddTrade(t types.Trade) (fixedpoint.Value, bool) {
p.Base -= quantity
p.Quote += fixedpoint.NewFromFloat(t.QuoteQuantity)
return price - p.AverageCost, true
return (price - p.AverageCost).Mul(quantity), true
}
return 0, false

View File

@ -30,7 +30,17 @@ func TestPosition(t *testing.T) {
pos.AddTrade(trade)
}
expectedAverageCost := (1000.0*0.01 + 2000.0*0.03) / 0.04
assert.Equal(t, fixedpoint.NewFromFloat(-70.0), pos.Quote)
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)
assert.Equal(t, fixedpoint.NewFromFloat(expectedAverageCost), pos.AverageCost)
amount, profit := pos.AddTrade(types.Trade{
Side: types.SideTypeSell,
Price: 3000.0,
Quantity: 0.01,
QuoteQuantity: 3000.0 * 0.01,
})
assert.True(t, profit)
assert.Equal(t, fixedpoint.NewFromFloat((3000.0-expectedAverageCost)*0.01), amount)
}