如果自己想写一个股票分析的小软件,如果获取历史日线数据是个问题,今天读到http://www.doc88.com/p-97936884038.html 文章以及
http://bbs.csdn.net/topics/350004443
这两片文章,清楚了同花顺历史数据文件的格式,我去,这对于我做软件的不是小菜一碟,打开vs2010,建一个c#工程,很快搞定,哈哈
主要代码如下:
string filePath =
@"C:\同花顺软件\同花顺\history\sznse\day\000001.day";//同花顺目录下history目录是历史日线数据,我例子打开的是平安银行000001的
byte[] stockFileBytes = System.IO.File.ReadAllBytes(filePath);
int recordStartPos = readByteToInt(stockFileBytes, 10,
2);//记录开始位置
int recordLength = readByteToInt(stockFileBytes, 12,
2);//记录长度
int recordCount = readByteToInt(stockFileBytes, 14,
2);//文件中记录条数
int fileBytesLength=stockFileBytes.Length;
int pos=recordStartPos;
List stockDayList = new List();//日线数据暂时读到List中
do
{
StockDay sd = new StockDay();
sd.DateInt = readByteToInt(stockFileBytes, pos,
4);//时间,整形表示的,可转为日期型
sd.OpenPrice = readByteToInt(stockFileBytes, pos+4,
2)*0.001f;//开盘价
sd.HighPrice = readByteToInt(stockFileBytes, pos + 8, 2) *
0.001f;//最高价
sd.LowPrice = readByteToInt(stockFileBytes, pos + 12, 2) *
0.001f;//最低价
sd.ClosePrice = readByteToInt(stockFileBytes, pos + 16, 2) *
0.001f;//收盘价
sd.VolumeValue = readByteToInt(stockFileBytes, pos + 20,
4);//成交额
sd.Volume = readByteToInt(stockFileBytes, pos + 24, 4);//成交量
stockDayList.Add(sd);
pos = pos + recordLength;
} while (pos < fileBytesLength);
用到了如下两个方法:
///
/// 读取某位置开始的byte转换为16进制字符串
///
///
///
///
private string readByteToHex(byte[] stockFileBytes,int startPos,int
length) {
string r = "";
for (int i = startPos + length - 1; i >= startPos;
i--) {
r += stockFileBytes[i].ToString("X2");
}
return r;
}
///
/// 读取某位置开始的byte转换为16进制字符串
///
///
///
///
private int readByteToInt(byte[] stockFileBytes, int startPos, int
length)
{
string r = readByteToHex(stockFileBytes,startPos,length);
int v = Convert.ToInt32(r, 16);
return v;
}
每一天的日K线封装为 类
public class StockDay
{
int dateInt;
public int DateInt
{
get { return dateInt; }
set { dateInt = value; }
}
DateTime date;//日期
public DateTime Date
{
get { return date; }
set { date = value; }
}
float openPrice;//开盘价
public float OpenPrice
{
get { return openPrice; }
set { openPrice = value; }
}
float closePrice;//收盘价
public float ClosePrice
{
get { return closePrice; }
set { closePrice = value; }
}
float highPrice;//最高价
public float HighPrice
{
get { return highPrice; }
set { highPrice = value; }
}
float lowPrice;//最低价
public float LowPrice
{
get { return lowPrice; }
set { lowPrice = value; }
}
float volume;//成交量
public float Volume
{
get { return volume; }
set { volume = value; }
}
float volumeValue;//成交额
public float VolumeValue
{
get { return volumeValue; }
set { volumeValue = value; }
}
}
有了日线数据,就可以写自己的股票分析工具了,灵活性大啊,哈哈
加载中,请稍候......