-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtdx.go
More file actions
213 lines (175 loc) · 4.69 KB
/
Copy pathtdx.go
File metadata and controls
213 lines (175 loc) · 4.69 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package tdx
import (
"encoding/binary"
"fmt"
"os"
"path"
"time"
)
var tz = time.FixedZone("UTC+8", int(8*time.Hour/time.Second))
// Bar provides a generic representation of bar data.
type Bar interface {
Time() time.Time
Open() float32
High() float32
Low() float32
Close() float32
Volume() uint32
Turnover() float32
}
// A dayBar is a single record in end-of-day bar (.day) files.
type dayBar struct {
RawDate uint32 // yyyymmdd
RawOpen uint32 // in cents
RawHigh uint32 // in cents
RawLow uint32 // in cents
RawClose uint32 // in cents
RawTurnover float32 // in yuan
RawVolume uint32 // in shares
Reserved [4]byte // unknown
}
func (bar *dayBar) Time() time.Time {
mmdd := int(bar.RawDate % 10000)
return time.Date(int(bar.RawDate/10000), time.Month(mmdd/100), mmdd%100, 15, 0, 0, 0, tz)
}
func (bar *dayBar) Open() float32 {
return float32(bar.RawOpen) / 100
}
func (bar *dayBar) High() float32 {
return float32(bar.RawHigh) / 100
}
func (bar *dayBar) Low() float32 {
return float32(bar.RawLow) / 100
}
func (bar *dayBar) Close() float32 {
return float32(bar.RawClose) / 100
}
func (bar *dayBar) Volume() uint32 {
return bar.RawVolume
}
func (bar *dayBar) Turnover() float32 {
return bar.RawTurnover
}
// A fiveBar in a single record in five-minute bar (.5) files.
type fiveBar struct {
RawDate uint16 // higher 5 bits: years since 2004, higher 11 bits: mmdd
RawTime uint16 // minutes since 00:00:00 (UTC+8)
RawOpen uint32 // in cents
RawHigh uint32 // in cents
RawLow uint32 // in cents
RawClose uint32 // in cents
RawTurnover float32 // in yuan
RawVolume uint32 // in shares
Reserved [4]byte // unknown
}
func (bar *fiveBar) Time() time.Time {
mmdd := int(bar.RawDate & (^uint16(0) >> (16 - 11)))
return time.Date(2004+int(bar.RawDate>>11), time.Month(mmdd/100), mmdd%100, int(bar.RawTime/60), int(bar.RawTime%60), 0, 0, tz)
}
func (bar *fiveBar) Open() float32 {
return float32(bar.RawOpen) / 100
}
func (bar *fiveBar) High() float32 {
return float32(bar.RawHigh) / 100
}
func (bar *fiveBar) Low() float32 {
return float32(bar.RawLow) / 100
}
func (bar *fiveBar) Close() float32 {
return float32(bar.RawClose) / 100
}
func (bar *fiveBar) Volume() uint32 {
return bar.RawVolume
}
func (bar *fiveBar) Turnover() float32 {
return bar.RawTurnover
}
// A lcnBar is a single record in minute bar (.lc5/.lc1) files.
type lcnBar struct {
RawDate uint16 // higher 5 bits: years since 2004, higher 11 bits: mmdd
RawTime uint16 // minutes since 00:00:00 (UTC+8)
RawOpen float32 // in yuan
RawHigh float32 // in yuan
RawLow float32 // in yuan
RawClose float32 // in yuan
RawTurnover float32 // in yuan
RawVolume uint32 // in shares
Reserved [4]byte // unknown
}
func (bar *lcnBar) Time() time.Time {
mmdd := int(bar.RawDate & (^uint16(0) >> (16 - 11)))
return time.Date(2004+int(bar.RawDate>>11), time.Month(mmdd/100), mmdd%100, int(bar.RawTime/60), int(bar.RawTime%60), 0, 0, tz)
}
func (bar *lcnBar) Open() float32 {
return bar.RawOpen
}
func (bar *lcnBar) High() float32 {
return bar.RawHigh
}
func (bar *lcnBar) Low() float32 {
return bar.RawLow
}
func (bar *lcnBar) Close() float32 {
return bar.RawClose
}
func (bar *lcnBar) Volume() uint32 {
return bar.RawVolume
}
func (bar *lcnBar) Turnover() float32 {
return bar.RawTurnover
}
// Dataset is a collection of bars with metadata.
type Dataset struct {
Market string // Market identification code (ISO 10383)
Symbol string // Local exchange ticker symbol
BarSize uint // Length of the bar defined in minutes
Bars []Bar
}
// DecodeFile decodes a bar data file that has been encoded in any of
// the supported formats. It detects file format by the file extension.
func DecodeFile(filepath string) (*Dataset, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
market, symbol, ext := path.Base(filepath)[0:2], path.Base(filepath)[2:8], path.Base(filepath)[8:]
switch market {
case "sh":
market = "XSHG"
case "sz":
market = "XSHE"
default:
return nil, fmt.Errorf("unknown market: %s", market)
}
var barSize uint
bars := make([]Bar, fi.Size()/32)
for i := 0; i < len(bars); i++ {
var bar Bar
switch ext {
case ".day":
bar = new(dayBar)
barSize = 1440
case ".5":
bar = new(fiveBar)
barSize = 5
case ".lc5":
bar = new(lcnBar)
barSize = 5
case ".lc1":
bar = new(lcnBar)
barSize = 1
default:
return nil, fmt.Errorf("unsupported file: %s", filepath)
}
err = binary.Read(f, binary.LittleEndian, bar)
if err != nil {
return nil, err
}
bars[i] = bar
}
return &Dataset{market, symbol, barSize, bars}, nil
}