{ // 日K线数据结构
public DateTime _Date; // e.g. 20100304
public decimal _Open; // *0.01 开盘价
public decimal _High; // *0.01 最高价
public decimal _Low; // *0.01 最低价
public decimal _Close; // *0.01 收盘价
public float _Amount; // 成交额
public int _Vol; // 成交量(手)
public DateTime Date
{
get { return _Date; }
set{ _Date =value ;}
}
public decimal Open
{
get { return _Open; }
set { _Open = value; }
}
public decimal High
{
get { return _High; }
set { _High = value; }
}
public decimal Low
{
get { return _Low; }
set { _Low = value; }
}
public decimal Close
{
get { return _Close; }
set { _Close = value; }
}
public float Amount
{
get { return _Amount; }
set { _Amount = value; }
}
public int Vol
{
get { return _Vol; }
set { _Vol = value; }
}
}
public class TDX
{
public string path ="";
public List<StockInfo> GetStockInfo(string code)
{
if (!File.Exists(path))
{
return null;
}
FileStream fs = File.OpenRead(path);
BinaryReader br = new BinaryReader(fs);
int days = (int)fs.Length / 32;
List<StockInfo> list = new List<StockInfo>();
for (int i = 0; i < days; i++)
{
StockInfo item = new StockInfo();
//item.DataDate =DateTime.Parse(new string(br.ReadChars(8)));
int date = br.ReadInt32();
int year = date / 10000;
int month = int.Parse(date.ToString().Substring(4, 2));
int day = int.Parse(date.ToString().Substring(6, 2));
int open = br.ReadInt32();
int high = br.ReadInt32();
int low = br.ReadInt32();
int close = br.ReadInt32();
Single amount = br.ReadSingle();
//Int32 amount = br.ReadInt32();
decimal am = Convert.ToDecimal(amount);
long amstr = Convert.ToInt64(amount);
int vol = br.ReadInt32();
int preclose = br.ReadInt32();
//Decimal open = Convert.ToDecimal(br.ReadSingle());
item.Date = new DateTime(year, month, day);
item.Open = Convert.ToDecimal(open / 100m);
item.High = Convert.ToDecimal(high / 100m);
item.Low = Convert.ToDecimal(low / 100m);
item.Close = Convert.ToDecimal(close / 100m);
item.Amount = amstr;
item.Vol = vol;
list.Add(item);
}
br.Close();
fs.Close();
return list;
}
}
private void btnReadData_Click(object sender, EventArgs e)
{
string code = txtStockCode.Text.Trim ();
if (code == "" || code.Length != 6) return;
string type;
if (code.Substring(0, 1) == "6") type = "sh";//上证指数
else type = "sz";
string path;
if (type == "sh") path = @"D:\new_ztzq_v6\vipdoc\sh\lday\";
else path =@"D:\new_ztzq_v6\vipdoc\sz\lday\";
path = path +type+ code + ".day";
// dataGridView.Rows.Clear();
TDX tdx = new TDX();
tdx.path = path;
List<StockInfo> lstStock= tdx.GetStockInfo(txtStockCode.Text,type );
dataGridView.DataSource = lstStock;
}