最近行情火爆,想利用go语言导入通达信软件的日线数据,做一些数据分析。初步完成的代码如下:
通达信日K线资料是存在 C:\new_gxzq_v6\Vipdoc\sh\lday(上海股票)
C:\new_gxzq_v6\Vipdoc\sz\lday (深圳股票)两个地方,文件是以DAY为扩展名的。文件是以二进制格式存放。
每天的记录为32个字节。
* 以深发展1997年1月2日的数据为例:
* 00000000h: 36 B8 30 01 72 06 00 00 86 06 00 00 60 06 00 00 ;
* 00000010h: 72 06 00 00 77 69 D4 4C 68 FE 66 00 74 06 00 00 ;
* 以下是分解 00000000h:|36 B8 30 01|72 06 00 00|86 06 00 00|60 06 00 00|;
* [36 B8 30 01] = * 0x0130B836 = 19970102 日期[unsigned long]
* [72 06 00 00] = 0x00000672 = 1650/100 = 16.50 开盘[unsigned long]
* [86 06 00 00] = 0x00000686 = 1670/100 = 16.70 最高[unsigned long]
* [60 06 00 00] = 0x00000660 = 1632/100 = 16.32 最低[unsigned long]
* 00000010h:|72 06 00 00|77 69 D4 4C|68 FE 66 00|74 06 00 00|;
* [72 06 00 00] = 0x00000672 = 1650/100 = 16.50 收盘[unsigned long]
* [77 69 D4 4C] = 0x4CD46977 = 111365048.0 成交额[float]
* [68 FE 66 00] = 0x0066FE68 = 6749800 成交量[unsigned long]
* [74 06 00 00] = 0x00000674 = 1652/100 = 16.52 上日收盘[unsigned long](保留)
废话少讲,直接给出程式,不明的请留言。
```go
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
)
type StockFile struct {
Date, Open, High, Low, Close int32
Amount float32
Vol int32
Preclose int32
}
type StockData struct {
Date int32
Open, High, Low, Close, Amount float32
Vol, Preclose int32
}
func main() {
//buf, err := ioutil.ReadFile("D:\\新建文本文档.txt") windown下面的文件打开路径
f, err := os.OpenFile("../src/hangqing/sz000012.day", os.O_RDONLY, 0666)
if err != nil {
fmt.Println(err.Error())
return
}
buf := make([]byte, 32)
/*重置文件指针,否则读不到内容的。*/
f.Seek(0, os.SEEK_SET)
for {
n, ferr := f.Read(buf)
if ferr != nil && ferr != io.EOF {
fmt.Println(ferr.Error())
break
}
if n == 0 {
break
}
//str += string(buf[0:n])
b_buf := bytes.NewBuffer(buf[0:32])
var x StockFile
var y StockData
binary.Read(b_buf, binary.LittleEndian, &x) //binary.LittleEndian 是内存中的字节序的概念,就是把低字节的放到了后面。网络传输一般用BigEndian,内存字节序和cpu有关,编程时要转化。
y.Date = x.Date
y.Open = float32(x.Open) / 100
y.High = float32(x.High) / 100
y.Low = float32(x.Low) / 100
y.Close = float32(x.Close) / 100
y.Preclose = x.Preclose
y.Amount = x.Amount
y.Vol = x.Vol
fmt.Println(y)
}
f.Close()
}
```
说明:1、该代码没用用目录遍历把所有日线数据都下载,有需要的继续完善。
个人想依托go语言,开发一个互联网证券的项目,有此想法的同道可以一起合作。mail:764238714@qq.com
重新调整过,采用了结构体的代码
<code>
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
)
type StockFile struct {
Date, Open, High, Low, Close int32
Amount float32
Vol int32
Preclose int32
}
type StockData struct {
Date int32
Open, High, Low, Close, Amount float32
Vol, Preclose int32
}
func main() {
//buf, err := ioutil.ReadFile("D:\\新建文本文档.txt") windown下面的文件打开路径
f, err := os.OpenFile("../src/hangqing/sz000012.day", os.O_RDONLY, 0666)
if err != nil {
fmt.Println(err.Error())
return
}
buf := make([]byte, 32)
/*重置文件指针,否则读不到内容的。*/
f.Seek(0, os.SEEK_SET)
for {
n, ferr := f.Read(buf)
if ferr != nil && ferr != io.EOF {
fmt.Println(ferr.Error())
break
}
if n == 0 {
break
}
//str += string(buf[0:n])
b_buf := bytes.NewBuffer(buf[0:n])
var x StockFile
var y StockData
binary.Read(b_buf, binary.LittleEndian, &x) //binary.LittleEndian 是内存中的字节序的概念,就是把低字节的放到了后面。网络传输一般用BigEndian,内存字节序和cpu有关,编程时要转化。
y.Date = x.Date
y.Open = float32(x.Open) / 100
y.High = float32(x.High) / 100
y.Low = float32(x.Low) / 100
y.Close = float32(x.Close) / 100
y.Preclose = x.Preclose
y.Amount = x.Amount
y.Vol = x.Vol
fmt.Println(y)
}
f.Close()
}
</code>
#7
更多评论
package main
import (
"bytes"
"encoding/binary"
// "encoding/gob"
"fmt"
"io"
// "log"
"os"
)
type StockData struct {
Date, Open, High, Low, Close, Amount, Vol, Reservation int
}
func main() {
//buf, err := ioutil.ReadFile("D:\\新建文本文档.txt") windown下面的文件打开路径
//buf, err := ioutil.ReadFile("../src/hangqing/sh000001.day")
f, err := os.OpenFile("../src/hangqing/sh000001.day", os.O_RDONLY, 0666)
if err != nil {
fmt.Println(err.Error())
return
}
buf := make([]byte, 32)
// var str string
#2