mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
add position roi tests
Signed-off-by: c9s <yoanlin93@gmail.com>
This commit is contained in:
parent
0715437cc5
commit
88059016b4
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user