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
- cumulatedVolumeTakeProfit:
minQuoteVolume: 100_000_000
interval: 5m
window: 2
backtest:

View File

@ -2,6 +2,8 @@ package dynamic
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{}) {
rtA := reflect.TypeOf(dst)
srcStructType := reflect.TypeOf(src)
@ -12,7 +14,9 @@ func MergeStructValues(dst, src interface{}) {
for i := 0; i < rtA.NumField(); i++ {
fieldType := rtA.Field(i)
fieldName := fieldType.Name
// if there is a field with the same name
if fieldSrcType, ok := srcStructType.FieldByName(fieldName); ok {
// ensure that the type is the same
if fieldSrcType.Type == fieldType.Type {
srcValue := reflect.ValueOf(src).Elem().FieldByName(fieldName)
dstValue := reflect.ValueOf(dst).Elem().FieldByName(fieldName)

View File

@ -10,6 +10,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/dynamic"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
@ -93,7 +94,6 @@ type Strategy struct {
session *bbgo.ExchangeSession
orderExecutor *bbgo.GeneralOrderExecutor
stopLossPrice fixedpoint.Value
lastLow fixedpoint.Value
pivot *indicator.Pivot
resistancePivot *indicator.Pivot
@ -122,7 +122,9 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
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)
}
}
@ -435,7 +437,6 @@ func (s *Strategy) placeOrder(ctx context.Context, price fixedpoint.Value, quant
})
}
func (s *Strategy) useQuantityOrBaseBalance(quantity fixedpoint.Value) fixedpoint.Value {
balance, hasBalance := s.session.Account.Balance(s.Market.BaseCurrency)