pivotshort: call MergeStructValues to update the field value

This commit is contained in:
c9s 2022-06-30 12:37:54 +08:00
parent cf0ca70d24
commit 7d5474e3dd
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 9 additions and 3 deletions

View File

@ -91,6 +91,7 @@ exchangeStrategies:
# (5) cumulatedVolumeTakeProfit is used to take profit when the cumulated quote volume from the klines exceeded a threshold # (5) cumulatedVolumeTakeProfit is used to take profit when the cumulated quote volume from the klines exceeded a threshold
- cumulatedVolumeTakeProfit: - cumulatedVolumeTakeProfit:
minQuoteVolume: 100_000_000 minQuoteVolume: 100_000_000
interval: 5m
window: 2 window: 2
backtest: backtest:

View File

@ -2,6 +2,8 @@ package dynamic
import "reflect" import "reflect"
// MergeStructValues merges the field value from the source struct to the dest struct.
// Only fields with the same type and the same name will be updated.
func MergeStructValues(dst, src interface{}) { func MergeStructValues(dst, src interface{}) {
rtA := reflect.TypeOf(dst) rtA := reflect.TypeOf(dst)
srcStructType := reflect.TypeOf(src) srcStructType := reflect.TypeOf(src)
@ -12,7 +14,9 @@ func MergeStructValues(dst, src interface{}) {
for i := 0; i < rtA.NumField(); i++ { for i := 0; i < rtA.NumField(); i++ {
fieldType := rtA.Field(i) fieldType := rtA.Field(i)
fieldName := fieldType.Name fieldName := fieldType.Name
// if there is a field with the same name
if fieldSrcType, ok := srcStructType.FieldByName(fieldName); ok { if fieldSrcType, ok := srcStructType.FieldByName(fieldName); ok {
// ensure that the type is the same
if fieldSrcType.Type == fieldType.Type { if fieldSrcType.Type == fieldType.Type {
srcValue := reflect.ValueOf(src).Elem().FieldByName(fieldName) srcValue := reflect.ValueOf(src).Elem().FieldByName(fieldName)
dstValue := reflect.ValueOf(dst).Elem().FieldByName(fieldName) dstValue := reflect.ValueOf(dst).Elem().FieldByName(fieldName)

View File

@ -10,6 +10,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/dynamic"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
@ -93,7 +94,6 @@ type Strategy struct {
session *bbgo.ExchangeSession session *bbgo.ExchangeSession
orderExecutor *bbgo.GeneralOrderExecutor orderExecutor *bbgo.GeneralOrderExecutor
stopLossPrice fixedpoint.Value
lastLow fixedpoint.Value lastLow fixedpoint.Value
pivot *indicator.Pivot pivot *indicator.Pivot
resistancePivot *indicator.Pivot resistancePivot *indicator.Pivot
@ -122,7 +122,9 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{}) session.Subscribe(types.MarketTradeChannel, s.Symbol, types.SubscribeOptions{})
} }
for _, m := range s.ExitMethods { for i := range s.ExitMethods {
m := s.ExitMethods[i]
dynamic.MergeStructValues(&m, s)
m.Subscribe(session) m.Subscribe(session)
} }
} }
@ -435,7 +437,6 @@ func (s *Strategy) placeOrder(ctx context.Context, price fixedpoint.Value, quant
}) })
} }
func (s *Strategy) useQuantityOrBaseBalance(quantity fixedpoint.Value) fixedpoint.Value { func (s *Strategy) useQuantityOrBaseBalance(quantity fixedpoint.Value) fixedpoint.Value {
balance, hasBalance := s.session.Account.Balance(s.Market.BaseCurrency) balance, hasBalance := s.session.Account.Balance(s.Market.BaseCurrency)