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是一个开放的,免费的金融数据平台,包含沪深股票数据,指数数据,基金数据,期货数据,期权数据,债券数据,外汇数据,港股数据,行业经济数据,宏观经济数据以及新闻快讯等特色数据.其中以沪深股票 ...
随机推荐
- 【CSS】Beginner2:Selectors, Properties, and Values
1.Whereas HTML has tags,CSS has selectors. 2.Selector{ properties:value; properties2:value2; } 3 ...
- Dynamic Vertex Buffers
ynamic vertex buffers on the other hand allow us to manipulate the information inside the vertex buf ...
- 转移python
这段时间一直学python,工作需要做一个基于python的web管理系统,恶补Django. 之前一直觉得开发人员只需要掌握了某个技术就OK了,没有重视总结学习的知识,最近经历的事情让我改变了之前的 ...
- 【前端】CSS3实现弹出效果
36氪这个网站上的登录框弹出的时候挺帅气的,想知道它是怎么做的 .. 今天通过问新爷再加上自己琢磨琢磨写出一个小小Demo - 上代码 <!DOCTYPE html> <html&g ...
- HW2.6
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 局域网内Linux服务器时间同步
局域网内Linux服务器时间同步 1.将一台能够上网的服务器作为时间服务器: # /usr/bin/rdate -s time-b.timefreq.bldrdoc.gov //将时间服务器与互 ...
- redis的hash, list, set类型相关命令
hash相关命令: 1. hset HSET key field value 将哈希表key中的域field的值设为value.如果key不存在,一个新的哈希表被创建并进行hset操作.如果域fiel ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- snowflake算法(java版)
转自:http://www.cnblogs.com/haoxinyue/p/5208136.html 1. 数据库自增长序列或字段 最常见的方式.利用数据库,全数据库唯一. 优点: 1)简单,代码方 ...
- 三目运算符 改变<a>标签的class属性
<s:iterator value="funcList" status="status" id="bean"> <a id ...