2023-06-29 07:06:03 +00:00
|
|
|
package riskcontrol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-08-31 06:50:50 +00:00
|
|
|
"time"
|
2023-06-29 07:06:03 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2023-07-10 08:54:22 +00:00
|
|
|
indicatorv2 "github.com/c9s/bbgo/pkg/indicator/v2"
|
2023-06-29 07:06:03 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_IsHalted(t *testing.T) {
|
|
|
|
var (
|
|
|
|
price = 30000.00
|
|
|
|
realizedPnL = fixedpoint.NewFromFloat(-100.0)
|
|
|
|
breakCondition = fixedpoint.NewFromFloat(-500.00)
|
|
|
|
)
|
|
|
|
|
|
|
|
window := types.IntervalWindow{Window: 30, Interval: types.Interval1m}
|
2023-07-10 08:54:22 +00:00
|
|
|
priceEWMA := indicatorv2.EWMA2(nil, window.Window)
|
2023-07-03 09:09:13 +00:00
|
|
|
priceEWMA.PushAndEmit(price)
|
2023-06-29 07:06:03 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
position fixedpoint.Value
|
|
|
|
averageCost fixedpoint.Value
|
|
|
|
isHalted bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "PositivePositionReachBreakCondition",
|
|
|
|
position: fixedpoint.NewFromFloat(10.0),
|
|
|
|
averageCost: fixedpoint.NewFromFloat(30040.0),
|
|
|
|
isHalted: true,
|
|
|
|
}, {
|
|
|
|
name: "PositivePositionOverBreakCondition",
|
|
|
|
position: fixedpoint.NewFromFloat(10.0),
|
|
|
|
averageCost: fixedpoint.NewFromFloat(30050.0),
|
|
|
|
isHalted: true,
|
|
|
|
}, {
|
|
|
|
name: "PositivePositionUnderBreakCondition",
|
|
|
|
position: fixedpoint.NewFromFloat(10.0),
|
|
|
|
averageCost: fixedpoint.NewFromFloat(30030.0),
|
|
|
|
isHalted: false,
|
|
|
|
}, {
|
|
|
|
name: "NegativePositionReachBreakCondition",
|
|
|
|
position: fixedpoint.NewFromFloat(-10.0),
|
|
|
|
averageCost: fixedpoint.NewFromFloat(29960.0),
|
|
|
|
isHalted: true,
|
|
|
|
}, {
|
|
|
|
name: "NegativePositionOverBreakCondition",
|
|
|
|
position: fixedpoint.NewFromFloat(-10.0),
|
|
|
|
averageCost: fixedpoint.NewFromFloat(29950.0),
|
|
|
|
isHalted: true,
|
|
|
|
}, {
|
|
|
|
name: "NegativePositionUnderBreakCondition",
|
|
|
|
position: fixedpoint.NewFromFloat(-10.0),
|
|
|
|
averageCost: fixedpoint.NewFromFloat(29970.0),
|
|
|
|
isHalted: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var riskControl = NewCircuitBreakRiskControl(
|
|
|
|
&types.Position{
|
|
|
|
Base: tc.position,
|
|
|
|
AverageCost: tc.averageCost,
|
|
|
|
},
|
|
|
|
priceEWMA,
|
|
|
|
breakCondition,
|
2023-08-31 06:50:50 +00:00
|
|
|
&types.ProfitStats{},
|
|
|
|
24*time.Hour,
|
2023-06-29 07:06:03 +00:00
|
|
|
)
|
2023-08-31 06:50:50 +00:00
|
|
|
now := time.Now()
|
|
|
|
riskControl.profitStats.ResetToday(now)
|
|
|
|
riskControl.profitStats.TodayPnL = realizedPnL
|
|
|
|
assert.Equal(t, tc.isHalted, riskControl.IsHalted(now.Add(time.Hour)))
|
2023-06-29 07:06:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|