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是一个开放的,免费的金融数据平台,包含沪深股票数据,指数数据,基金数据,期货数据,期权数据,债券数据,外汇数据,港股数据,行业经济数据,宏观经济数据以及新闻快讯等特色数据.其中以沪深股票 ...
随机推荐
- codeforce 621D - Rat Kwesh and Cheese
题意:求表达式中最大的值. long double 128位 有效数字18-19 范围正负1.2*10^4932 注意取对数! #include<iostream> #include< ...
- HW4.9
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW2.5
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 有关gcc的扩展__attribute__((unused))
================================ Author: taoyuetao Email: tao_yuetao@yahoo.com.cn Blog: taoyuetao.cu ...
- Python队列服务 Python RQ Functions from the __main__ module cannot be processed by workers.
在使用Python队列服务 Python RQ 时候的报错: Functions from the __main__ module cannot be processed by workers. 原因 ...
- 对css中的定位属性postion刨根解牛
定位的基本思想很简单: 它允许你定义元素框相对于其正常位置应该出现的位置(relative),或者相对于父元素(absolute).另一个元素甚至浏览器窗口本身的位置(fixed).显然,这个功能非常 ...
- JavaScript----this陷阱的最全收集
原文翻译: JavaScript来自一门健全的语言,所以你可能觉得JavaScript中的this和其他面向对象的语言如java的this一样,是指存储在实例属性中的值.事实并非如此,在JavaScr ...
- Android Studio更新失败
解决方案: Windows平台下 如果是运行的Android studio是32位的需要在修改一下文件: 在andriod studio的启动目录下.找到studio.exe.vmoptions这个文 ...
- 分布式搜索elasticsearch 索引文档的增删改查 入门
1.RESTful接口使用方法 为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/ ...
- 1513:二进制中1的个数 @jobdu
题目1513:二进制中1的个数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1341 解决:455 题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: ...