C#下如何用NPlot绘制期货股票K线图(2):读取数据文件让K线图自动更新

时间:2023-03-09 06:12:28
C#下如何用NPlot绘制期货股票K线图(2):读取数据文件让K线图自动更新

[内容介绍]上一篇介绍了K线图的基本绘制方法,但很不完善,本篇增加了它直接读取数据的功能,这对于金融市场的数据量大且又需要动态刷新功能的实现很重要.

[实现方法]

1.需要一个数据文件,这里用的是直接读取由另一个CTP程序从上期交易所接收的期货合约RB1609所写的行情文件日线数据rb1609_d1.txt

文件格式如下:

日期          时间        开盘        最高          最低        收盘        成交量        持仓量

            0.100000             2555.00             2606.00             2540.00             2563.00
0.100000 2548.00 2554.00 2532.00 2539.00
0.100000 2548.00 2565.00 2461.00 2557.00
0.100000 2533.00 2546.00 2528.00 2541.00
0.100000 2533.00 2574.00 2492.00 2560.00
0.100000 2560.00 2594.00 2558.00 2568.00
0.000000 2560.00 2647.00 2558.00 2613.00
0.000000 2607.00 2626.00 2548.00 2553.00
0.000000 2550.00 2561.00 2531.00 2545.00
0.000000 2607.00 2626.00 2578.00 2601.00
0.000000 2549.00 2549.00 2525.00 2533.00
0.000000 2549.00 2549.00 2520.00 2527.00
0.000000 2549.00 2549.00 2520.00 2527.00
0.000000 2549.00 2549.00 2519.00 2519.00
0.000000 2549.00 2549.00 2519.00 2523.00
0.000000 2549.00 2549.00 2519.00 2523.00
0.000000 2549.00 2549.00 2485.00 2515.00
0.000000 2519.00 2521.00 2498.00 2509.00
0.000000 2519.00 2522.00 2462.00 2480.00
0.000000 2475.00 2480.00 2465.00 2480.00
0.000000 2475.00 2515.00 2465.00 2512.00
0.000000 2502.00 2515.00 2495.00 2498.00
0.000000 2519.00 2537.00 2466.00 2486.00
0.000000 2508.00 2509.00 2506.00 2508.00
0.000000 2508.00 2521.00 2476.00 2476.00
0.000000 2478.00 2478.00 2442.00 2453.00
0.000000 2478.00 2478.00 2442.00 2468.00
0.000000 2452.00 2484.00 2452.00 2469.00
0.000000 2452.00 2484.00 2442.00 2442.00
0.000000 2437.00 2452.00 2420.00 2425.00
0.000000 2437.00 2452.00 2420.00 2425.00
0.000000 2437.00 2452.00 2420.00 2425.00
0.000000 2437.00 2452.00 2420.00 2425.00
0.000000 2437.00 2452.00 2420.00 2424.00
0.000000 2437.00 2452.00 2405.00 2424.00
0.000000 2437.00 2452.00 2320.00 2325.00
0.000000 2325.00 2415.00 2325.00 2415.00
0.000000 2407.00 2407.00 2407.00 2407.00

2.让程序加载后默认打开这个rb1609_d1.txt文件

使用4个函数:

⑴窗体加载函数

 1       private void FormCtp_Load(object sender, EventArgs e)
2 {
3
4 OpenChartFile(file);
5
6 this.MouseWheel += new MouseEventHandler(FormCtp_MouseWheel);
7 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
8 timer.Enabled = true;
9 timer.AutoReset = true; //是否不断重复定时器操作
10
11
12 }

⑵打开图表函数,获得合约名称,同时调用读取文件数据函数

      private void OpenChartFile(string fileName)
{
this.textBox1.Focus();
if (!String.IsNullOrEmpty(fileName))
{
//获取股票合约代码名称
int m1 = fileName.LastIndexOf(@"\");
int m2 = fileName.LastIndexOf(@"_");
int m3 = fileName.IndexOf(@".txt");
pathCtp = file.Substring(, file.LastIndexOf(@"\"));
// string[] subLines = pathCtp.Split('\\');
stockName = fileName.Substring(m1 + , m2 - m1 - );
period = fileName.Substring(m2 + , m3 - m2 - );
label_symbol.Text = stockName + @" 周期: " + period;
file = fileName;
//MessageBox.Show(fileName);
DataStock = LoadCtpInfo(file);//读取文件数据到listp
DataStock.Reverse();//反转list容器上元素
xEnd = ;
xBegin = Math.Min(xInitShowBars, DataStock.Count - );
xShowBars = xInitShowBars;
ReFreshMe(ref xBegin, ref xEnd, ref xShowBars, );
}
}

⑶读取文件数据函数:

      private List<StockInfo> LoadCtpInfo(string fileName)
{
//MessageBox.Show(fileName);
using (Stream resourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader reader = new StreamReader(resourceStream, Encoding.GetEncoding("GB2312")))
{
//一次读入所有行到一个string[]
var strings = reader.ReadToEnd().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
textBox1.Text = strings.Length.ToString();
//根据数据文件行数动态构建一个list
var res = new List<StockInfo>(strings.Length);
//List<Double> DataOpen = new List<Double>(strings.Length);
//List<Double> DataHigh = new List<Double>(strings.Length);
//List<Double> DataLow = new List<Double>(strings.Length);
//List<Double> DataClose = new List<Double>(strings.Length);
////DataVolume = new List<Double>(strings.Length);
////DataHold = new List<Double>(strings.Length);
//List<DateTime> DataTime = new List<DateTime>(strings.Length);
//List<int> Time = new List<int>(strings.Length); //针对每一行文本按<StockInfo>结构添加成股票图数据
for (int i = ; i < strings.Length; i++)
{
//string line = strings[i];
string[] subLines = strings[i].Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
CtpInfo data = new CtpInfo();
//对每一行文本按<StockInfo>结构进行数据转换
CtpTxt2AmCharts(subLines, out data);
DateTime date = data.date;
Double open = data.open;
Double high = data.high;
Double low = data.low;
Double close = data.close;
Double volume = data.volume;
Double hold = data.hold;
//MessageBox.Show(DataOpen[i].ToString());
//DataHigh.Add(high);
//DataLow.Add(low);
//DataClose.Add(close);
//DataOpen.Add(open);
//DataTime.Add(date);
//Time.Add(i);
res.Add(
new StockInfo
{
date = date,
open = open,
high = high,
low = low,
close = close,
volume = volume
});
}
return res;
}
}
}

⑷对每一行文本按<StockInfo>结构进行数据转换函数

       void CtpTxt2AmCharts(string[] vitems, out CtpInfo data)
{
//vitems[0]=20160101;
data = new CtpInfo();
data.open = Double.Parse(vitems[].Trim());
data.high = Double.Parse(vitems[].Trim());
data.low = Double.Parse(vitems[].Trim());
data.close = Double.Parse(vitems[].Trim());
data.volume = Double.Parse(vitems[].Trim());
data.hold = Double.Parse(vitems[].Trim());
int year = Int32.Parse(vitems[].Trim().Substring(, ));
int month = Int32.Parse(vitems[].Trim().Substring(, ));
int day = Int32.Parse(vitems[].Trim().Substring(, ));
//vitems[1]=0.121212; int time = (int)(Double.Parse(vitems[].Trim()) * );
int h = time / ;
// MessageBox.Show(vitems[1].Trim());
int m = Int32.Parse(vitems[].Trim().Substring(, ));
// MessageBox.Show(vitems[1].Trim().Substring(4, 2));
// MessageBox.Show(vitems[1].Trim().Substring(6, 2));
int s = Int32.Parse(vitems[].Trim().Substring(, ));
DateTime date1 = new DateTime(year, month, day, h, m, s);
//DateTime date0 = new DateTime(1970, 1, 1, 0, 0, 0);
//System.TimeSpan timeSpan = date1 - date0;
//int timeSign = (int)timeSpan.TotalMinutes;
data.date = date1;
}

3.上述程序需要使用的一个数据结构文件

   using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///<数据结构文件>
///stockinfo.cs
///<> namespace StockAnalyse
{
/// <summary>
/// 股票数据结构信息
/// </summary>
public class StockInfo
{
/// <summary>
/// 时间
/// </summary>
public DateTime date { get; set; } /// <summary>
/// 开盘价
/// </summary>
public double open { get; set; }
/// <summary>
/// 最高价
/// </summary>
public double high { get; set; }
/// <summary>
/// 最低价
/// </summary>
public double low { get; set; }
/// <summary>
/// 收盘价
/// </summary>
public double close { get; set; } /// <summary>
/// 成交量
/// </summary>
public double volume { get; set; }
}
/// <summary>
/// 商品合约列表结构信息
/// </summary>
public class QuotesInfo
{
/////////////////////////////
/// 商品名称
///////////////////////
public string symbol { get; set; }
/// <summary>
/// 时间
/// </summary>
public DateTime date { get; set; }
public string time { get; set; }
/// <summary>
/// 开盘价
/// </summary>
public double open { get; set; }
/// <summary>
/// 最高价
/// </summary>
public double high { get; set; }
/// <summary>
/// 最低价
/// </summary>
public double low { get; set; }
/// <summary>
/// 收盘价
/// </summary>
public double close { get; set; } /// <summary>
/// 成交量
/// </summary>
public double volume { get; set; }
}
/// <summary>
/// Ctp期货数据结构
/// </summary>
public class CtpInfo
{
/// <summary>
/// 时间
/// </summary>
public DateTime date { get; set; } /// <summary>
/// 开盘价
/// </summary>
public double open { get; set; }
/// <summary>
/// 最高价
/// </summary>
public double high { get; set; }
/// <summary>
/// 最低价
/// </summary>
public double low { get; set; }
/// <summary>
/// 收盘价
/// </summary>
public double close { get; set; } /// <summary>
/// 成交量
/// </summary>
public double volume { get; set; }
/// <summary>
/// 持仓量
/// </summary>
public double hold { get; set; }
}
public enum TimeFrame { tick = , M1, M5, M15, M30, H1, D1, W1, Mn1, Y1 }; }

限于篇幅,有关更多的窗口类的定义,将在下篇中说明.