-
Notifications
You must be signed in to change notification settings - Fork 11
/
symbol.go
124 lines (108 loc) · 2.97 KB
/
symbol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package future
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/NadiaSama/ccexgo/exchange"
"github.com/NadiaSama/ccexgo/exchange/huobi"
"github.com/pkg/errors"
)
type (
FutureSymbol struct {
*exchange.BaseFutureSymbol
}
FutureSymbolResp struct {
Status string `json:"status"`
Data []FSymbol `json:"data"`
}
FSymbol struct {
Symbol string `json:"symbol"`
ContractCode string `json:"contract_code"`
DeliveryDate string `json:"delivery_date"`
ContractType string `json:"contract_type"`
ContractStatus int `json:"contract_status"`
}
)
const (
ContractStatusOnline = 1
timeFmt = "20060102"
)
func (rc *RestClient) initFutureSymbol(ctx context.Context) error {
var resp FutureSymbolResp
if err := rc.Request(ctx, http.MethodGet, "/api/v1/contract_contract_info",
nil, nil, false, &resp); err != nil {
return err
}
if resp.Status != huobi.StatusOK {
ret, _ := json.Marshal(&resp)
return huobi.NewError(string(ret))
}
for _, fsym := range resp.Data {
if fsym.ContractStatus != ContractStatusOnline {
continue
}
tm := fmt.Sprintf("%s08Z", fsym.DeliveryDate)
st, err := time.Parse("2006010215Z", tm)
if err != nil {
return errors.Errorf("bad delivery date %s", tm)
}
var (
suffix string
typ exchange.FutureType
)
if fsym.ContractType == "this_week" {
typ = exchange.FutureTypeCW
suffix = "_CW"
} else if fsym.ContractType == "next_week" {
typ = exchange.FutureTypeNW
suffix = "_NW"
} else if fsym.ContractType == "quarter" {
typ = exchange.FutureTypeCQ
suffix = "_CQ"
} else if fsym.ContractType == "next_quarter" {
typ = exchange.FutureTypeNQ
suffix = "_NQ"
} else {
return errors.Errorf("unkown contract_type '%s'", fsym.ContractType)
}
sym := newFutureSymbol(fsym.Symbol, st, typ)
rc.futureSymbolMap[fmt.Sprintf("%s%s", fsym.Symbol, fsym.DeliveryDate)] = sym
rc.futureSymbolMap[fmt.Sprintf("%s%s", fsym.Symbol, suffix)] = sym
}
return nil
}
func (rc *RestClient) GetFutureSymbols(index string) []*FutureSymbol {
var ret []*FutureSymbol
for k := range rc.futureSymbolMap {
v := rc.futureSymbolMap[k]
ek := fmt.Sprintf("%s%s", index, v.SettleTime().Format(timeFmt))
if ek == k {
ret = append(ret, v)
}
}
return ret
}
func newFutureSymbol(base string, st time.Time, typ exchange.FutureType) *FutureSymbol {
return &FutureSymbol{
BaseFutureSymbol: exchange.NewBaseFutureSymbol(strings.ToUpper(base), st, typ),
}
}
func (fs *FutureSymbol) String() string {
return fmt.Sprintf("%s%s", fs.Index(), fs.SettleTime().Format(timeFmt))
}
//WSSub return symbol which used by websocket subscribe
func (fs *FutureSymbol) WSSub() string {
return fmt.Sprintf("%s_%s", fs.Index(), TypeString(fs.Type()))
}
func TypeString(ft exchange.FutureType) string {
m := map[exchange.FutureType]string{
exchange.FutureTypeCW: "CW",
exchange.FutureTypeNW: "NW",
exchange.FutureTypeCQ: "CQ",
exchange.FutureTypeNQ: "NQ",
}
return m[ft]
}