From 2568a81dfe4369b921174bc4f571fbd8ed5ce85b Mon Sep 17 00:00:00 2001 From: zenix Date: Tue, 26 Jul 2022 16:42:34 +0900 Subject: [PATCH] fix: binance time sync, exchange interval query interface, yaml for fixedpoint --- pkg/exchange/binance/exchange.go | 47 ++++++++++++++++++++++++++++++++ pkg/exchange/max/exchange.go | 23 ++++++++++++++++ pkg/fixedpoint/convert.go | 12 -------- pkg/fixedpoint/dec.go | 4 +++ pkg/fixedpoint/dec_test.go | 41 ++++++++++++++++++++++++++++ pkg/types/interval.go | 6 ++++ 6 files changed, 121 insertions(+), 12 deletions(-) diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index 9dc464e65..45b1a48f0 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -118,7 +118,30 @@ func New(key, secret string) *Exchange { if err != nil { log.WithError(err).Error("can not set server time") } + + if err = client2.SetTimeOffsetFromServer(context.Background()); err != nil { + log.WithError(err).Error("can not set server time") + } }) + go func() { + ticker := time.NewTicker(time.Hour) + defer ticker.Stop() + for _ = range ticker.C { + _, err = client.NewSetServerTimeService().Do(context.Background()) + if err != nil { + log.WithError(err).Error("can not set server time") + } + + _, err = futuresClient.NewSetServerTimeService().Do(context.Background()) + if err != nil { + log.WithError(err).Error("can not set server time") + } + + if err = client2.SetTimeOffsetFromServer(context.Background()); err != nil { + log.WithError(err).Error("can not set server time") + } + } + }() } return &Exchange{ @@ -1611,6 +1634,30 @@ func (e *Exchange) QueryPositionRisk(ctx context.Context, symbol string) (*types return convertPositionRisk(risks[0]) } +var SupportedIntervals = map[types.Interval]int{ + types.Interval1m: 1, + types.Interval5m: 5, + types.Interval15m: 15, + types.Interval30m: 30, + types.Interval1h: 60, + types.Interval2h: 60 * 2, + types.Interval4h: 60 * 4, + types.Interval6h: 60 * 6, + types.Interval12h: 60 * 12, + types.Interval1d: 60 * 24, + types.Interval3d: 60 * 24 * 3, + types.Interval1w: 60 * 24 * 7, +} + +func (e *Exchange) SupportedInterval() map[types.Interval]int { + return SupportedIntervals +} + +func (e *Exchange) IsSupportedInterval(interval types.Interval) bool { + _, ok := SupportedIntervals[interval] + return ok +} + func getLaunchDate() (time.Time, error) { // binance launch date 12:00 July 14th, 2017 loc, err := time.LoadLocation("Asia/Shanghai") diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index 098dd8ccf..264044bc8 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -979,3 +979,26 @@ func (e *Exchange) DefaultFeeRates() types.ExchangeFee { TakerFeeRate: fixedpoint.NewFromFloat(0.01 * 0.150), // 0.15% } } + +var SupportedIntervals = map[types.Interval]int{ + types.Interval1m: 1, + types.Interval5m: 5, + types.Interval15m: 15, + types.Interval30m: 30, + types.Interval1h: 60, + types.Interval2h: 60 * 2, + types.Interval4h: 60 * 4, + types.Interval6h: 60 * 6, + types.Interval12h: 60 * 12, + types.Interval1d: 60 * 24, + types.Interval3d: 60 * 24 * 3, +} + +func (e *Exchange) SupportedInterval() map[types.Interval]int { + return SupportedIntervals +} + +func (e *Exchange) IsSupportedInterval(interval types.Interval) bool { + _, ok := SupportedIntervals[interval] + return ok +} diff --git a/pkg/fixedpoint/convert.go b/pkg/fixedpoint/convert.go index 3086806f0..44f220078 100644 --- a/pkg/fixedpoint/convert.go +++ b/pkg/fixedpoint/convert.go @@ -198,18 +198,6 @@ func (v *Value) AtomicLoad() Value { } func (v *Value) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) { - var f float64 - if err = unmarshal(&f); err == nil { - *v = NewFromFloat(f) - return - } - - var i int64 - if err = unmarshal(&i); err == nil { - *v = NewFromInt(i) - return - } - var s string if err = unmarshal(&s); err == nil { nv, err2 := NewFromString(s) diff --git a/pkg/fixedpoint/dec.go b/pkg/fixedpoint/dec.go index f25c20d78..70ba35f3e 100644 --- a/pkg/fixedpoint/dec.go +++ b/pkg/fixedpoint/dec.go @@ -1032,6 +1032,10 @@ func (x Value) Compare(y Value) int { return Compare(x, y) } +func (v Value) MarshalYAML() (interface{}, error) { + return v.FormatString(8), nil +} + func (v *Value) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) { var f float64 if err = unmarshal(&f); err == nil { diff --git a/pkg/fixedpoint/dec_test.go b/pkg/fixedpoint/dec_test.go index ffbc3bc78..269904c1b 100644 --- a/pkg/fixedpoint/dec_test.go +++ b/pkg/fixedpoint/dec_test.go @@ -3,6 +3,7 @@ package fixedpoint import ( "encoding/json" "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" "math/big" "testing" ) @@ -175,6 +176,46 @@ func TestJson(t *testing.T) { _ = json.Unmarshal([]byte("6e-8"), &p) _ = json.Unmarshal([]byte("0.000062"), &q) assert.Equal(t, "0.00006194", q.Sub(p).String()) + +} + +func TestYaml(t *testing.T) { + p := MustNewFromString("0") + e, err := yaml.Marshal(p) + assert.NoError(t, err) + assert.Equal(t, "\"0.00000000\"\n", string(e)) + p = MustNewFromString("1.00000003") + e, err = yaml.Marshal(p) + assert.NoError(t, err) + assert.Equal(t, "\"1.00000003\"\n", string(e)) + p = MustNewFromString("1.000000003") + e, err = yaml.Marshal(p) + assert.NoError(t, err) + assert.Equal(t, "\"1.00000000\"\n", string(e)) + p = MustNewFromString("1.000000008") + e, err = yaml.Marshal(p) + assert.NoError(t, err) + assert.Equal(t, "\"1.00000000\"\n", string(e)) + p = MustNewFromString("0.999999999") + e, err = yaml.Marshal(p) + assert.NoError(t, err) + assert.Equal(t, "\"0.99999999\"\n", string(e)) + + p = MustNewFromString("1.2e-9") + e, err = yaml.Marshal(p) + assert.NoError(t, err) + assert.Equal(t, "0.00000000", p.FormatString(8)) + assert.Equal(t, "\"0.00000000\"\n", string(e)) + + _ = yaml.Unmarshal([]byte("0.00153917575"), &p) + assert.Equal(t, "0.00153917", p.FormatString(8)) + + q := NewFromFloat(0.00153917575) + assert.Equal(t, p, q) + _ = yaml.Unmarshal([]byte("6e-8"), &p) + _ = yaml.Unmarshal([]byte("0.000062"), &q) + assert.Equal(t, "0.00006194", q.Sub(p).String()) + } func TestNumFractionalDigits(t *testing.T) { diff --git a/pkg/types/interval.go b/pkg/types/interval.go index a06e083e5..2809ab56c 100644 --- a/pkg/types/interval.go +++ b/pkg/types/interval.go @@ -51,6 +51,9 @@ var Interval6h = Interval("6h") var Interval12h = Interval("12h") var Interval1d = Interval("1d") var Interval3d = Interval("3d") +var Interval1w = Interval("1w") +var Interval2w = Interval("2w") +var Interval1mo = Interval("1mo") var SupportedIntervals = map[Interval]int{ Interval1m: 1, @@ -64,6 +67,9 @@ var SupportedIntervals = map[Interval]int{ Interval12h: 60 * 12, Interval1d: 60 * 24, Interval3d: 60 * 24 * 3, + Interval1w: 60 * 24 * 7, + Interval2w: 60 * 24 * 14, + Interval1mo: 60 * 24 * 30, } // IntervalWindow is used by the indicators