Skip to content

Instantly share code, notes, and snippets.

@border
Created January 8, 2012 15:15
Show Gist options
  • Save border/1578661 to your computer and use it in GitHub Desktop.
Save border/1578661 to your computer and use it in GitHub Desktop.
通达信股票天和5分钟数据格式分析, 参考: http://alantop.5166.info/gpdatasoft/gpformat/gpformat.htm
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct stock5Min
{
unsigned short monthDay; // 月日
unsigned short hourMin; // 小时, 分钟
float open; //开盘价,单位:分
float high; //最高价,单位:分
float low; //最低价,单位:分
float close; //收盘价,单位:分
float amount; //交易金额,单位:元
uint32_t vol; //成交量,单位:股
uint32_t reserv; //保留,有时用来保存上一交易日收盘价
};
void printDay(struct stock5Min *day)
{
printf("日期: %d-%d %02d:%02d, ", day->monthDay/100, day->monthDay%100, day->hourMin/60, day->hourMin%60);
printf("开盘价: %.2f, 最高价: %.2f, 最低价: %.2f, 收盘价: %.2f, ", day->open, day->high, day->low, day->close);
printf("交易金额: %f, 交易量: %d\n", day->amount, day->vol);
}
int main()
{
FILE *p;
int i = 0;
char *file = "./sh600004.lc5";
struct stock5Min *day = malloc(sizeof(struct stock5Min));
p = fopen(file, "r");
if (p) {
fseek(p, 0, SEEK_END);
uint64_t iFileLen = ftell(p)/(sizeof(struct stock5Min));
rewind(p);
for (i = 0; i < iFileLen; i++) {
memset(day, 0, sizeof(struct stock5Min));
fread(day, sizeof(struct stock5Min), 1, p);
printDay(day);
}
} else {
printf("Read File: %s Error!\n", file);
}
printf("size: %d, month: %d, Day: %d\n", sizeof(struct stock5Min), 205/100, 205%100);
printf("size: %d, month: %d, Day: %d\n", sizeof(struct stock5Min), 205/100, 205%100);
free(day);
fclose(p);
return 0;
}
#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;
}
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment