Archive

Posts Tagged ‘go’

招商证券股票day数据格式分析

January 8th, 2012 No comments

今天没事想把招商证券保存的股票数据提取出来, 后来发现招商证券的软件是采用是通达信做的, 这样直接通过查找通达信的数据格式就搞定了.

主要参考: http://alantop.5166.info/gpdatasoft/gpformat/gpformat.htm#_Toc224993378

通达信日线

每条数据文件占32个字节。每四个字节代表一个变量。如下所示:

 

struct stockDay
{
uint32_t date; //日期
uint32_t open; //开盘价,单位:分
uint32_t high; //最高价,单位:分
uint32_t low; //最低价,单位:分
uint32_t close; //收盘价,单位:分
float amount; //交易金额,单位:元
uint32_t vol; //成交量,单位:股
int32_t reserv; //保留,有时用来保存上一交易日收盘价
};

我分别使用C和Go写了个简单的提取程序.

C代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct stockDay
{
uint32_t date; //日期
uint32_t open; //开盘价,单位:分
uint32_t high; //最高价,单位:分
uint32_t low; //最低价,单位:分
uint32_t close; //收盘价,单位:分
float amount; //交易金额,单位:元
uint32_t vol; //成交量,单位:股
int32_t reserv; //保留,有时用来保存上一交易日收盘价
};

void printDay(struct stockDay *day)
{
printf(“日期: %d, “, day->date);
printf(“开盘价: %.2f, 最高价: %.2f, 最低价: %.2f, 收盘价: %.2f, “, day->open/100.0, day->high/100.0, day->low/100.0, day->close/100.0);
printf(“交易金额: %f, 交易量: %d\n”, day->amount, day->vol);
}

int main()
{
FILE *p;
int i = 0;
char *file = “./sh600529.day”;
struct stockDay *day = malloc(sizeof(struct stockDay));

p = fopen(file, “r”);
if (p) {
fseek(p, 0, SEEK_END);
uint64_t iFileLen = ftell(p)/(sizeof(struct stockDay));
rewind(p);
for (i = 0; i < iFileLen; i++) {
memset(day, 0, sizeof(struct stockDay));
fread(day, sizeof(struct stockDay), 1, p);
printDay(day);
}

} else {
printf(“Read File: %s Error!\n”, file);
}

free(day);
fclose(p);
return 0;
}

Go的代码:

package main

import (
“bytes”
“encoding/binary”
“fmt”
“io/ioutil”
“unsafe”
)

type StockDay struct {
date uint32 //日期
open uint32 //开盘价,单位:分
high uint32 //最高价,单位:分
low uint32 //最低价,单位:分
cls uint32 //收盘价,单位:分
amount float32 //交易金额,单位:元
vol uint32 //成交量,单位:股
reserv int32 //保留,有时用来保存上一交易日收盘价
}

func main() {
contents, _ := ioutil.ReadFile(“./sh887001.day”)
//println(string(contents));
var day StockDay
size := int(unsafe.Sizeof(day))
fmt.Printf(“sizeof: %d, days: %d\n”, size, len(contents)/size)
for i := 0; i < len(contents); i += 32 {
buf := bytes.NewBuffer(contents[i : i+4])
err := binary.Read(buf, binary.LittleEndian, &day.date)
if err != nil {
fmt.Println(“binary.Read failed: “, err)
}
buf = bytes.NewBuffer(contents[i+4 : i+8])
err = binary.Read(buf, binary.LittleEndian, &day.open)

buf = bytes.NewBuffer(contents[i+8 : i+12])
err = binary.Read(buf, binary.LittleEndian, &day.high)

buf = bytes.NewBuffer(contents[i+12 : i+16])
err = binary.Read(buf, binary.LittleEndian, &day.low)

buf = bytes.NewBuffer(contents[i+16 : i+20])
err = binary.Read(buf, binary.LittleEndian, &day.cls)

buf = bytes.NewBuffer(contents[i+20 : i+24])
err = binary.Read(buf, binary.LittleEndian, &day.amount)

buf = bytes.NewBuffer(contents[i+24 : i+28])
err = binary.Read(buf, binary.LittleEndian, &day.vol)

fmt.Printf(“%v\n”, day)
}
}

 

代码也贴在Github上: https://gist.github.com/1578661

ps: 目前5分钟线的月份和日期对不上, 不知道哪位高手搞过?

–EOF–

 

golang Byte数组转换为浮点数

January 8th, 2012 No comments

今天没事想用go从文件中提取些二进制数据,并把数据转换为相关的数据类型, 就找到一个把数组转换为float类型的数据。主要用到了encoding/binary包里面的read函数, 它可以根据你数据的大小端得到相关的数据类型。

主要代码如下:

package main

import (
“bytes”
“encoding/binary”
“fmt”
)

func main() {
var pi float64
b := []byte{0×18, 0x2d, 0×44, 0×54, 0xfb, 0×21, 0×09, 0×40}
buf := bytes.NewBuffer(b)
err := binary.Read(buf, binary.LittleEndian, &pi)
if err != nil {
fmt.Println(“binary.Read failed:”, err)
}
fmt.Println(pi)
}

下面是go里面uint32在大小端的分别实现:

bvcom@bvcomtv:~$ godoc -src encoding/binary Uint32
// A ByteOrder specifies how to convert byte sequences into
// 16-, 32-, or 64-bit unsigned integers.
type ByteOrder interface {
Uint32([]byte) uint32
// contains filtered or unexported methods
}

func (littleEndian) Uint32(b []byte) uint32 {
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
}

func (bigEndian) Uint32(b []byte) uint32 {
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
}

参考:
Convert 8 byte array to int64? Options:  http://groups.google.com/group/golang-nuts/browse_thread/thread/7f541090fd7d1bd4

Reading byte array as another datatype Options: http://groups.google.com/group/golang-nuts/browse_thread/thread/fc00115bf5b35757

http://golang.org/src/pkg/encoding/binary/binary.go?s=3435:3501#L117

–EOF–

Go 1 will be

January 1st, 2012 No comments

在golang-nuts邮件列表里面, 有人问了有关Go 1的情况, golang的主要开发者Brad Fitzpatrick说: Go 1 相关的库和包, 都已经完成, 节后只修复相关的Bug, 修复完后就会发布Go 1.
如果你想使用可以使用最新Weekly版本2011-12-22, 这个基本上就是Go1 版本.

原文如下:
All the major Go 1 language & library changes are now in. Bug fixing &
polish begins after the holidays, then some release candidates will start
going out. The latest weekly, 2011-12-22 is basically what Go 1 will be,
if you want to start playing.

参考: http://groups.google.com/group/golang-n … 3abf4b868b

Categories: golang Tags: , , , ,

Two Go Talks: “Lexical Scanning in Go” and “Cuddle: an App Engine Demo”

September 3rd, 2011 No comments

Rob’s talk, “Lexical Scanning in Go“, discusses the design of a particularly interesting and idiomatic piece of Go code, the lexer component of the new template package.
Lexical Scanning in Go – Rob Pike

The slides are available here. The new template package is available as exp/template in Go release r59. In a future release it will replace the old template package.

Andrew’s talk, “Cuddle: an App Engine Demo“, describes the construction of a simple real-time chat application that uses App Engine’s DatastoreChannel, and Memcache APIs. It also includes a question and answer session that covers Go for App Engine and Go more generally.

Cuddle, A go AppEngine Demo – Andrew Gerrand

The slides are available here. The code is available at the cuddle Google Code project.
From: http://blog.golang.org/2011/09/two-go-talks-lexical-scanning-in-go-and.html

Google App Engine支持Go语言

May 12th, 2011 No comments

在昨天的Google I/O大会上, Google App Engine宣布支持Go语言, 对于go来说是个非常好的消息, 现在Google App支持Python, Java 和Go. 如果Go的性能不错的话可能取代Java.

文章是Go官方blog上面的, 简单翻译如下:
原文: http://blog.golang.org/2011/05/go-and-google-app-engine.html
翻译: Bian Jiang (@b0rder)

Google的APP Engine 为Web应用提供了一个稳定, 可扩展, 易于部署和发布的平台.
已经有超过10万活跃的应用部署在appspot.com以及用户自己定制的App Engine.
从最开始的Python到2009年增加对Java的支持. 在今天的Google I/O开发者大会大会上, 我们很高兴的宣布要支持Go语言, 虽然现在只是初期版本, 但无论是对App Engine还是Go开发团队都是一个令人兴奋的时刻, 有里程碑的意义.

现在可以从这下载App Engine SDK For Go语言, 很快就可以把Go开发的应用部署到App Engine中.
现在可以通过SDK学习如何去写web应用, 如何去使用API, 在你的本地测试Web APP.
只要一开通就可以很容易的把你的应用部署到Google的云服务器中.

如果你不想等待那么长时间, 可以在这里申请测试帐号. 在测试过后, 我们将对所有人开发, 虽然还只是试验阶段.

这里提供了一个简单的方法来进行开发Go Web应用, 你甚至不用安装Go, 因为SDK里面已经包含了所有内容. 你只需要下载SDK并解压, 就可以开始的Go web App之旅. 并且SDK提供了一个App Server它会自动编译你的代码.

在SDK中提供基本上提供了标准的App Engine API, 比如: Datastore, Blobstore, URL Fetch, Mail, Users等. 更多的API也在增加的开发环境中. GAE用的Go运行环境是当前最新版本的Go Release r57.1, 基本包含了Go所有的基本库, 当然为了安全起见, 在APP环境中去掉了一些系统调用(syscall)和不安全(unsafe)的包.

虽然都提供了goroutines和channels, 但是目前Go APP只支持单线程. 也就是说所有的goroutines都跑在一个系统线程里面, 目前不提供并行处理模式, 但是
这个限制在不久的将来会取消.

尽管有些小的限制, 但是不影响go的强大. Go的代码是以源代码的形式发布, 编译都采用64位的x86平台, 确保Go App Engine高效运行.

在这里查看更多信息.
一个Go APP的Demo:http://moustach-io.appspot.com/
代码在这里: https://code.google.com/p/appengine-go/source/browse/example/moustachio
SDK的源码可以从这里下载
我们创建了google-appengine-go 邮件列表. 有关APP Engine的疑问可以发到这个邮件列表里面.
如果发现了Bug可以提交到这里: http://code.google.com/p/googleappengine/issues/list

目前Go App Engine SDK 只支持Linux和Mac OSX(10.5或以上)版本. 我们希望很快推出Windows版本.

We’d like to offer our thanks for all the help and enthusiasm we received from Google’s App Engine team in making this happen.

- David Symonds, Nigel Tao, Andrew Gerrand, and the rest of the Go Team.

Categories: golang Tags: , , , , , ,