Merge pull request #784 from c9s/strategy/pivotshort

strategy: pivotshort: fix stopEMA
This commit is contained in:
Yo-An Lin 2022-06-29 17:04:24 +08:00 committed by GitHub
commit ccfaf0e070
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 7 deletions

View File

@ -14,7 +14,7 @@ type ExitMethod struct {
CumulatedVolumeTakeProfit *CumulatedVolumeTakeProfit `json:"cumulatedVolumeTakeProfit"`
}
func (m *ExitMethod) Subscribe() {
func (m *ExitMethod) Subscribe(session *ExchangeSession) {
// TODO: pull out this implementation as a simple function to reflect.go
rv := reflect.ValueOf(m)
rt := reflect.TypeOf(m)
@ -22,11 +22,13 @@ func (m *ExitMethod) Subscribe() {
rv = rv.Elem()
rt = rt.Elem()
infType := reflect.TypeOf((*types.Subscriber)(nil)).Elem()
argValues := toReflectValues(session)
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
if fieldType.Type.Implements(infType) {
method := rv.Field(i).MethodByName("Subscribe")
method.Call(nil)
method.Call(argValues)
}
}
}

View File

@ -4,5 +4,5 @@ import "testing"
func TestExitMethod(t *testing.T) {
em := &ExitMethod{}
em.Subscribe()
em.Subscribe(&ExchangeSession{})
}

View File

@ -125,7 +125,6 @@ func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fix
}
submitOrder.Tag = strings.Join(tags, ",")
_, err := e.SubmitOrders(ctx, *submitOrder)
return err
}

View File

@ -110,3 +110,11 @@ func newTypeValueInterface(typ reflect.Type) interface{} {
dst := reflect.New(typ)
return dst.Interface()
}
func toReflectValues(args ...interface{}) (values []reflect.Value) {
for _, arg := range args {
values = append(values, reflect.ValueOf(arg))
}
return values
}

View File

@ -174,7 +174,6 @@ func (s *Strategy) CurrentPosition() *types.Position {
}
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
bbgo.Notify("Closing position", s.Position)
return s.orderExecutor.ClosePosition(ctx, percentage)
}
@ -338,7 +337,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
log.Infof("%s breakLow signal detected, closed price %f < breakPrice %f", kline.Symbol, closePrice.Float64(), breakPrice.Float64())
// stop EMA protection
if s.stopEWMA != nil && !s.BreakLow.StopEMARange.IsZero() {
if s.stopEWMA != nil {
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last())
if ema.IsZero() {
return

View File

@ -45,7 +45,7 @@ func (s *TradeStats) Add(pnl fixedpoint.Value) {
s.WinningRatio = fixedpoint.NewFromFloat(float64(s.NumOfProfitTrade) / float64(s.NumOfLossTrade))
}
s.ProfitFactor = s.GrossProfit.Div(s.GrossLoss)
s.ProfitFactor = s.GrossProfit.Div(s.GrossLoss.Abs())
}
func (s *TradeStats) String() string {