fix: binance time sync, exchange interval query interface, yaml for fixedpoint

This commit is contained in:
zenix 2022-07-26 16:42:34 +09:00
parent 9bf48e9de4
commit 2568a81dfe
6 changed files with 121 additions and 12 deletions

View File

@ -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")

View File

@ -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
}

View File

@ -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)

View File

@ -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 {

View File

@ -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) {

View File

@ -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