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 }; }
限于篇幅,有关更多的窗口类的定义,将在下篇中说明.
C#下如何用NPlot绘制期货股票K线图(2):读取数据文件让K线图自动更新的更多相关文章
- C#下如何用NPlot绘制期货股票K线图(3):设计要显示的股票价格图表窗口并定义相应类的成员及函数
[内容简介] 上一篇介绍了要显示K线图所需要的数据结构,及要动态显示K线图,需要动态读取数据文件必需的几个功能函数.本篇介绍要显示蜡烛图所用到的窗口界面设计及对应类定义.下面分述如下: [窗口界面] ...
- C#下如何用NPlot绘制期货股票K线图(1)?
[简介] 作为一名专业程序化交易者,编程是一个程序员的基本功,本文是作者在做的一个期货CTP项目中有关K线绘图的一部分,偿试类MT4中图表 设计而写,在编写绘图时,查阅了相关资料,感觉还是用NPlot ...
- 用vue.js实现的期货,股票的实时K线
用vue.js实现的期货,股票的实时k线 项目地址:https://github.com/zhengquantao/vue-Kline vue-kline 效果图 Build Setup 本项目基于V ...
- 如何用ChemDraw绘制化学课件
近年来随着ChemDraw等多媒体技术的迅速发展,多媒体技术越来越多的应用在教学中.学会应用ChemDraw绘制化学分子结构.化学反应式和实验装置的方法,将在有机化学的教学中提供一定的帮助,进一步提高 ...
- VS2015下如何用编译、调试程序。
VS2015下如何用编译.调试程序. (通过实践给出截图) 安装步骤: 下载安装网址[VS2015下载地址](http://www.ithome.com/html/win10/164028.htm) ...
- 在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)
在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。
请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...
- CentOS下如何用nmon收集系统实时运行状况
#赋予执行权限 chmod +x nmon 执行./nmon可以查看实时的系统状态有提示的,d看磁盘,n看网络,c看cpu #如果不想看实时的,想收集系统长时间运行情况然后分析,可用这个 nohup ...
- tushare获取股票每日重要的基本面指标数据,并存入Elasticsearch
tushare是一个开放的,免费的金融数据平台,包含沪深股票数据,指数数据,基金数据,期货数据,期权数据,债券数据,外汇数据,港股数据,行业经济数据,宏观经济数据以及新闻快讯等特色数据.其中以沪深股票 ...
随机推荐
- spoj 8222 Substrings(后缀自动机+DP)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28005 [题意] 给一个字符串S,令F(x)表示S的所有长度为 ...
- Storm系列(十二)架构分析之Worker-心跳信息处理
Worker通过worker-data方法定义了一个包含很多共享数据的映射集合,Worker中很多方法都依赖它 mk-worker 功能: 创建对应的计时器.Executor.接收线程接收消息 方 ...
- 【解决】HDFS HA无法自动切换问题
[解决]HDFS HA无法自动切换问题 原因: 最早设置为root互相登录,可是zkfc服务是hdfs账号运行的,没有权限访问到root的id_rsa文件.更改为hdfs账号免密钥登录恢复正常. ...
- POJ-1981 Circle and Points 单位圆覆盖
题目链接:http://poj.org/problem?id=1981 容易想到直接枚举两个点,然后确定一个圆来枚举,算法复杂度O(n^3). 这题还有O(n^2*lg n)的算法.将每个点扩展为单位 ...
- POJ2104&&HDU2665(静态区间第K小)
题目大意 给定一个有N个数字的序列,然后又m个查询,形式如下: l r k 要求你返回区间[l,r]第k小的数是哪个 题解 终于弄懂主席树是个啥东西了,O(∩_∩)O~~,这题正是主席树的裸题,主席树 ...
- 【转】Vim 常用命令总结
使用 Vim 的时间不长,但如今已经离不开熟悉的 Vim 编辑模式了. Vim 的学习曲线是非常陡的,一开始学习的时候,面对很多的操作命令要去记住,常常望而却步. 其实,只要记住一些常用的命令,加之在 ...
- 问题-Delphi为什么不能连接oracle
问题现象:delphi 为什么不能连接oracle 问题处理:加一句OraSession1.Options.Net := True;
- linuxmint获取root
1.进入系统à点击桌面左下角的菜单à点击系统设置 2. 在系统设置里面找到登陆窗口并进去 3.打入自己设置的开机登陆密码 4. 选择选项,并把运行root登陆的勾打上 5.重启生效
- spring getbean 方法分析
spring 缺省: 1.spring用DefaultListableBeanFactory.preInstantiateSingletons()建立bean实例 2.缺省采用单例模式 在最近的项目中 ...
- vc关于文件拷贝
单个文件的拷贝 system 针对单个文件 CopyFile 针对单个文件 /** @file_extension egg: .txt .png **/ void CopyFileToDir(CS ...