-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: refactor risk control and integrate risk control into scmaker
- Loading branch information
Showing
7 changed files
with
125 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,41 @@ | ||
package riskcontrol | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/indicator" | ||
"github.com/c9s/bbgo/pkg/types" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type CircuitBreakRiskControl struct { | ||
// Since price could be fluctuated large, | ||
// use an EWMA to smooth it in running time | ||
price *indicator.EWMA | ||
position *types.Position | ||
profitStats *types.ProfitStats | ||
breakCondition fixedpoint.Value | ||
price *indicator.EWMAStream | ||
position *types.Position | ||
profitStats *types.ProfitStats | ||
lossThreshold fixedpoint.Value | ||
} | ||
|
||
func NewCircuitBreakRiskControl( | ||
position *types.Position, | ||
price *indicator.EWMA, | ||
breakCondition fixedpoint.Value, | ||
price *indicator.EWMAStream, | ||
lossThreshold fixedpoint.Value, | ||
profitStats *types.ProfitStats) *CircuitBreakRiskControl { | ||
|
||
return &CircuitBreakRiskControl{ | ||
price: price, | ||
position: position, | ||
profitStats: profitStats, | ||
breakCondition: breakCondition, | ||
price: price, | ||
position: position, | ||
profitStats: profitStats, | ||
lossThreshold: lossThreshold, | ||
} | ||
} | ||
|
||
// IsHalted returns whether we reached the circuit break condition set for this day? | ||
func (c *CircuitBreakRiskControl) IsHalted() bool { | ||
var unrealized = c.position.UnrealizedProfit(fixedpoint.NewFromFloat(c.price.Last(0))) | ||
log.Infof("[CircuitBreakRiskControl] Realized P&L = %v, Unrealized P&L = %v\n", | ||
c.profitStats.TodayPnL, | ||
unrealized) | ||
return unrealized.Add(c.profitStats.TodayPnL).Compare(c.breakCondition) <= 0 | ||
log.Infof("[CircuitBreakRiskControl] realized PnL = %f, unrealized PnL = %f\n", | ||
c.profitStats.TodayPnL.Float64(), | ||
unrealized.Float64()) | ||
return unrealized.Add(c.profitStats.TodayPnL).Compare(c.lossThreshold) <= 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters