2022-08-10 11:36:30 +00:00
|
|
|
package drift
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/jedib0t/go-pretty/v6/table"
|
2022-08-22 18:12:26 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/dynamic"
|
|
|
|
style2 "github.com/c9s/bbgo/pkg/style"
|
2022-08-10 11:36:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Strategy) ParamDump(f io.Writer, seriesLength ...int) {
|
|
|
|
length := 1
|
|
|
|
if len(seriesLength) > 0 && seriesLength[0] > 0 {
|
|
|
|
length = seriesLength[0]
|
|
|
|
}
|
|
|
|
val := reflect.ValueOf(s).Elem()
|
|
|
|
for i := 0; i < val.Type().NumField(); i++ {
|
|
|
|
t := val.Type().Field(i)
|
|
|
|
if ig := t.Tag.Get("ignore"); ig == "true" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
field := val.Field(i)
|
|
|
|
if t.IsExported() || t.Anonymous || t.Type.Kind() == reflect.Func || t.Type.Kind() == reflect.Chan {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fieldName := t.Name
|
|
|
|
typeName := field.Type().String()
|
2022-08-15 11:50:34 +00:00
|
|
|
log.Infof("fieldName %s typeName %s", fieldName, typeName)
|
2022-08-10 11:36:30 +00:00
|
|
|
value := reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
|
|
|
|
isSeries := true
|
|
|
|
lastFunc := value.MethodByName("Last")
|
|
|
|
isSeries = isSeries && lastFunc.IsValid()
|
|
|
|
lengthFunc := value.MethodByName("Length")
|
|
|
|
isSeries = isSeries && lengthFunc.IsValid()
|
|
|
|
indexFunc := value.MethodByName("Index")
|
|
|
|
isSeries = isSeries && indexFunc.IsValid()
|
|
|
|
|
|
|
|
stringFunc := value.MethodByName("String")
|
|
|
|
canString := stringFunc.IsValid()
|
|
|
|
|
|
|
|
if isSeries {
|
|
|
|
l := int(lengthFunc.Call(nil)[0].Int())
|
|
|
|
if l >= length {
|
|
|
|
fmt.Fprintf(f, "%s: Series[..., %.4f", fieldName, indexFunc.Call([]reflect.Value{reflect.ValueOf(length - 1)})[0].Float())
|
|
|
|
for j := length - 2; j >= 0; j-- {
|
|
|
|
fmt.Fprintf(f, ", %.4f", indexFunc.Call([]reflect.Value{reflect.ValueOf(j)})[0].Float())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(f, "]\n")
|
|
|
|
} else if l > 0 {
|
|
|
|
fmt.Fprintf(f, "%s: Series[%.4f", fieldName, indexFunc.Call([]reflect.Value{reflect.ValueOf(l - 1)})[0].Float())
|
|
|
|
for j := l - 2; j >= 0; j-- {
|
|
|
|
fmt.Fprintf(f, ", %.4f", indexFunc.Call([]reflect.Value{reflect.ValueOf(j)})[0].Float())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(f, "]\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(f, "%s: Series[]\n", fieldName)
|
|
|
|
}
|
|
|
|
} else if canString {
|
|
|
|
fmt.Fprintf(f, "%s: %s\n", fieldName, stringFunc.Call(nil)[0].String())
|
2022-08-18 09:05:52 +00:00
|
|
|
} else if dynamic.CanInt(field) {
|
2022-08-10 11:36:30 +00:00
|
|
|
fmt.Fprintf(f, "%s: %d\n", fieldName, field.Int())
|
2022-08-15 12:46:13 +00:00
|
|
|
} else if field.CanConvert(reflect.TypeOf(float64(0))) {
|
2022-08-10 11:36:30 +00:00
|
|
|
fmt.Fprintf(f, "%s: %.4f\n", fieldName, field.Float())
|
|
|
|
} else if field.CanInterface() {
|
|
|
|
fmt.Fprintf(f, "%s: %v", fieldName, field.Interface())
|
|
|
|
} else if field.Type().Kind() == reflect.Map {
|
|
|
|
fmt.Fprintf(f, "%s: {", fieldName)
|
2022-08-15 11:50:34 +00:00
|
|
|
iter := value.MapRange()
|
2022-08-10 11:36:30 +00:00
|
|
|
for iter.Next() {
|
|
|
|
k := iter.Key().Interface()
|
|
|
|
v := iter.Value().Interface()
|
|
|
|
fmt.Fprintf(f, "%v: %v, ", k, v)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(f, "}\n")
|
|
|
|
} else if field.Type().Kind() == reflect.Slice {
|
|
|
|
fmt.Fprintf(f, "%s: [", fieldName)
|
|
|
|
l := field.Len()
|
|
|
|
if l > 0 {
|
|
|
|
fmt.Fprintf(f, "%v", field.Index(0))
|
|
|
|
}
|
|
|
|
for j := 1; j < field.Len(); j++ {
|
|
|
|
fmt.Fprintf(f, ", %v", field.Index(j))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(f, "]\n")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(f, "%s(%s): %s\n", fieldName, typeName, field.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Print(f io.Writer, pretty bool, withColor ...bool) {
|
2022-08-17 04:42:01 +00:00
|
|
|
var style *table.Style
|
2022-08-10 11:36:30 +00:00
|
|
|
if pretty {
|
2022-08-22 18:12:26 +00:00
|
|
|
style = style2.NewDefaultTableStyle()
|
2022-08-10 11:36:30 +00:00
|
|
|
}
|
2022-08-22 18:12:26 +00:00
|
|
|
dynamic.PrintConfig(s, f, style, len(withColor) > 0 && withColor[0], dynamic.DefaultWhiteList()...)
|
2022-08-10 11:36:30 +00:00
|
|
|
}
|