add position roi tests

Signed-off-by: c9s <yoanlin93@gmail.com>
This commit is contained in:
c9s 2022-06-26 16:03:42 +08:00
parent 0715437cc5
commit 88059016b4
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -11,19 +11,43 @@ import (
const Delta = 1e-9
func TestPosition_ROI(t *testing.T) {
pos := &Position{
Symbol: "BTCUSDT",
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
Base: fixedpoint.NewFromFloat(10.0),
AverageCost: fixedpoint.NewFromFloat(8000.0),
Quote: fixedpoint.NewFromFloat(-8000.0 * 10.0),
}
t.Run("short position", func(t *testing.T) {
// Long position
pos := &Position{
Symbol: "BTCUSDT",
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
Base: fixedpoint.NewFromFloat(-10.0),
AverageCost: fixedpoint.NewFromFloat(8000.0),
Quote: fixedpoint.NewFromFloat(8000.0 * 10.0),
}
currentPrice := fixedpoint.NewFromFloat(10000.0)
roi := pos.ROI(currentPrice)
assert.Equal(t, "0.25", roi.String())
assert.Equal(t, "25%", roi.Percentage())
assert.True(t, pos.IsShort(), "should be a short position")
currentPrice := fixedpoint.NewFromFloat(5000.0)
roi := pos.ROI(currentPrice)
assert.Equal(t, "0.375", roi.String())
assert.Equal(t, "37.5%", roi.Percentage())
})
t.Run("long position", func(t *testing.T) {
// Long position
pos := &Position{
Symbol: "BTCUSDT",
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
Base: fixedpoint.NewFromFloat(10.0),
AverageCost: fixedpoint.NewFromFloat(8000.0),
Quote: fixedpoint.NewFromFloat(-8000.0 * 10.0),
}
assert.True(t, pos.IsLong(), "should be a long position")
currentPrice := fixedpoint.NewFromFloat(10000.0)
roi := pos.ROI(currentPrice)
assert.Equal(t, "0.25", roi.String())
assert.Equal(t, "25%", roi.Percentage())
})
}
func TestPosition_ExchangeFeeRate_Short(t *testing.T) {