2023-06-29 07:06:03 +00:00
# Risk Control
------------
### 1. Introduction
Two types of risk controls for strategies is created:
- Position-limit Risk Control (pkg/risk/riskcontrol/position.go)
- Circuit-break Risk Control (pkg/risk/riskcontrol/circuit_break.go)
### 2. Position-Limit Risk Control
Initialization:
2023-07-03 09:09:13 +00:00
```go
s.positionRiskControl = riskcontrol.NewPositionRiskControl(s.PositionHardLimit, s.MaxPositionQuantity, s.orderExecutor.TradeCollector())
s.positionRiskControl.OnReleasePosition(func(quantity fixedpoint.Value, side types.SideType) {
createdOrders, err := s.orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: s.Symbol,
Market: s.Market,
Side: side,
Type: types.OrderTypeMarket,
Quantity: quantity,
2023-06-29 07:06:03 +00:00
})
2023-07-03 09:09:13 +00:00
if err != nil {
log.WithError(err).Errorf("failed to submit orders")
return
}
log.Infof("created position release orders: %+v", createdOrders)
})
2023-06-29 07:06:03 +00:00
```
Strategy should provide OnReleasePosition callback, which will be called when position (positive or negative) is over hard limit.
Modify quantity before submitting orders:
```
buyQuantity, sellQuantity := s.positionRiskControl.ModifiedQuantity(s.Position.Base)
```
It calculates buy and sell quantity shrinking by hard limit and position.
### 3. Circuit-Break Risk Control
Initialization
2023-07-03 09:09:13 +00:00
```go
s.circuitBreakRiskControl = riskcontrol.NewCircuitBreakRiskControl(
s.Position,
session.Indicators(s.Symbol).EWMA(s.CircuitBreakEMA),
s.CircuitBreakLossThreshold,
2023-08-31 06:50:50 +00:00
s.ProfitStats,
24*time.Hour)
2023-06-29 07:06:03 +00:00
```
2023-07-03 09:09:13 +00:00
2023-06-29 07:06:03 +00:00
Should pass in position and profit states. Also need an price EWMA to calculate unrealized profit.
Validate parameters:
2023-07-03 09:09:13 +00:00
2023-06-29 07:06:03 +00:00
```
2023-07-03 09:09:13 +00:00
if s.CircuitBreakLossThreshold.Float64() > 0 {
return fmt.Errorf("circuitBreakLossThreshold should be non-positive")
}
return nil
2023-06-29 07:06:03 +00:00
```
2023-07-03 09:09:13 +00:00
2023-06-29 07:06:03 +00:00
Circuit break condition should be non-greater than zero.
Check for circuit break before submitting orders:
```
// Circuit break when accumulated losses are over break condition
2023-08-31 06:50:50 +00:00
if s.circuitBreakRiskControl.IsHalted(kline.EndTime) {
2023-06-29 07:06:03 +00:00
return
}
submitOrders, err := s.generateSubmitOrders(ctx)
if err != nil {
log.WithError(err).Error("failed to generate submit orders")
return
}
log.Infof("submit orders: %+v", submitOrders)
if s.DryRun {
log.Infof("dry run, not submitting orders")
return
}
```
Notice that if there are multiple place to submit orders, it is recommended to check in one place in Strategy.Run() and re-use that flag before submitting orders. That can avoid duplicated logs generated from IsHalted().