mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
test trade profit calculation
This commit is contained in:
parent
169af63846
commit
617f5119fd
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user