base/common/balance_lever_test.go

62 lines
1.3 KiB
Go
Raw Normal View History

2024-06-25 16:59:13 +00:00
package common
import (
"testing"
. "git.qtrade.icu/coin-quant/trademodel"
)
func TestCheckLiquidationLong(t *testing.T) {
lb := NewLeverBalance()
lb.Set(100)
lb.SetFee(0.0002)
lb.SetLever(10)
_, _, err := lb.AddTrade(Trade{Action: OpenLong, Price: 100, Amount: 9})
if err != nil {
t.Fatal("Liq lever AddTrade failed:" + err.Error())
}
liqPrice, isLiq := lb.CheckLiquidation(90.1)
if isLiq {
t.Fatal("Liq cal too large")
}
t.Log(liqPrice, isLiq)
liqPrice, isLiq = lb.CheckLiquidation(90)
if !isLiq {
t.Fatal("Liq cal too small")
}
t.Log(liqPrice, isLiq)
}
func TestCheckLiquidationShort(t *testing.T) {
lb := NewLeverBalance()
lb.Set(100)
lb.SetFee(0.0002)
lb.SetLever(10)
_, _, err := lb.AddTrade(Trade{Action: OpenShort, Price: 100, Amount: 9})
if err != nil {
t.Fatal("Liq lever AddTrade failed:" + err.Error())
}
liqPrice, isLiq := lb.CheckLiquidation(109)
if isLiq {
t.Fatal("Liq cal too small")
}
t.Log(liqPrice, isLiq)
liqPrice, isLiq = lb.CheckLiquidation(109.99)
if !isLiq {
t.Fatal("Liq cal too large")
}
t.Log(liqPrice, isLiq)
}
func TestCheckLeverBalance(t *testing.T) {
lb := NewLeverBalance()
lb.Set(100)
lb.SetFee(0.0002)
lb.SetLever(10)
_, _, err := lb.AddTrade(Trade{Action: OpenLong, Price: 100, Amount: 10})
if err == nil {
t.Fatal("Liq not work")
}
t.Log(err.Error())
}