2022-12-16 03:51:52 +00:00
|
|
|
package dynamicrisk
|
2022-10-21 09:20:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DynamicExposure struct {
|
|
|
|
// BollBandExposure calculates the max exposure with the Bollinger Band
|
|
|
|
BollBandExposure *DynamicExposureBollBand `json:"bollBandExposure"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize dynamic exposure
|
2022-11-23 04:28:38 +00:00
|
|
|
func (d *DynamicExposure) Initialize(symbol string, session *bbgo.ExchangeSession) {
|
2022-10-21 09:20:31 +00:00
|
|
|
switch {
|
|
|
|
case d.BollBandExposure != nil:
|
2022-11-23 04:28:38 +00:00
|
|
|
d.BollBandExposure.initialize(symbol, session)
|
2022-10-21 09:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DynamicExposure) IsEnabled() bool {
|
|
|
|
return d.BollBandExposure != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMaxExposure returns the max exposure
|
2022-11-23 09:23:18 +00:00
|
|
|
func (d *DynamicExposure) GetMaxExposure(price float64, trend types.Direction) (maxExposure fixedpoint.Value, err error) {
|
2022-10-21 09:20:31 +00:00
|
|
|
switch {
|
|
|
|
case d.BollBandExposure != nil:
|
2022-11-23 09:23:18 +00:00
|
|
|
return d.BollBandExposure.getMaxExposure(price, trend)
|
2022-10-21 09:20:31 +00:00
|
|
|
default:
|
|
|
|
return fixedpoint.Zero, errors.New("dynamic exposure is not enabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DynamicExposureBollBand calculates the max exposure with the Bollinger Band
|
|
|
|
type DynamicExposureBollBand struct {
|
|
|
|
// DynamicExposureBollBandScale is used to define the exposure range with the given percentage.
|
|
|
|
DynamicExposureBollBandScale *bbgo.PercentageScale `json:"dynamicExposurePositionScale"`
|
|
|
|
|
2022-11-22 10:24:04 +00:00
|
|
|
types.IntervalWindowBandWidth
|
2022-10-21 09:20:31 +00:00
|
|
|
|
|
|
|
dynamicExposureBollBand *indicator.BOLL
|
|
|
|
}
|
|
|
|
|
2022-11-18 08:42:51 +00:00
|
|
|
// initialize dynamic exposure with Bollinger Band
|
2022-11-23 04:28:38 +00:00
|
|
|
func (d *DynamicExposureBollBand) initialize(symbol string, session *bbgo.ExchangeSession) {
|
|
|
|
d.dynamicExposureBollBand = session.StandardIndicatorSet(symbol).BOLL(d.IntervalWindow, d.BandWidth)
|
2022-10-21 09:20:31 +00:00
|
|
|
|
|
|
|
// Subscribe kline
|
|
|
|
session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{
|
|
|
|
Interval: d.dynamicExposureBollBand.Interval,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMaxExposure returns the max exposure
|
2022-11-23 09:23:18 +00:00
|
|
|
func (d *DynamicExposureBollBand) getMaxExposure(price float64, trend types.Direction) (fixedpoint.Value, error) {
|
2023-05-31 11:35:44 +00:00
|
|
|
downBand := d.dynamicExposureBollBand.DownBand.Last(0)
|
|
|
|
upBand := d.dynamicExposureBollBand.UpBand.Last(0)
|
|
|
|
sma := d.dynamicExposureBollBand.SMA.Last(0)
|
2022-10-21 09:20:31 +00:00
|
|
|
log.Infof("dynamicExposureBollBand bollinger band: up %f sma %f down %f", upBand, sma, downBand)
|
|
|
|
|
|
|
|
bandPercentage := 0.0
|
|
|
|
if price < sma {
|
|
|
|
// should be negative percentage
|
|
|
|
bandPercentage = (price - sma) / math.Abs(sma-downBand)
|
|
|
|
} else if price > sma {
|
|
|
|
// should be positive percentage
|
|
|
|
bandPercentage = (price - sma) / math.Abs(upBand-sma)
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:23:18 +00:00
|
|
|
// Reverse if downtrend
|
|
|
|
if trend == types.DirectionDown {
|
|
|
|
bandPercentage = 0 - bandPercentage
|
|
|
|
}
|
|
|
|
|
2022-10-21 09:20:31 +00:00
|
|
|
v, err := d.DynamicExposureBollBandScale.Scale(bandPercentage)
|
|
|
|
if err != nil {
|
|
|
|
return fixedpoint.Zero, err
|
|
|
|
}
|
|
|
|
return fixedpoint.NewFromFloat(v), nil
|
|
|
|
}
|