[内容介绍]上一篇介绍了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线图自动更新的更多相关文章

  1. C#下如何用NPlot绘制期货股票K线图(3):设计要显示的股票价格图表窗口并定义相应类的成员及函数

    [内容简介] 上一篇介绍了要显示K线图所需要的数据结构,及要动态显示K线图,需要动态读取数据文件必需的几个功能函数.本篇介绍要显示蜡烛图所用到的窗口界面设计及对应类定义.下面分述如下: [窗口界面] ...

  2. C#下如何用NPlot绘制期货股票K线图(1)?

    [简介] 作为一名专业程序化交易者,编程是一个程序员的基本功,本文是作者在做的一个期货CTP项目中有关K线绘图的一部分,偿试类MT4中图表 设计而写,在编写绘图时,查阅了相关资料,感觉还是用NPlot ...

  3. 用vue.js实现的期货,股票的实时K线

    用vue.js实现的期货,股票的实时k线 项目地址:https://github.com/zhengquantao/vue-Kline vue-kline 效果图 Build Setup 本项目基于V ...

  4. 如何用ChemDraw绘制化学课件

    近年来随着ChemDraw等多媒体技术的迅速发展,多媒体技术越来越多的应用在教学中.学会应用ChemDraw绘制化学分子结构.化学反应式和实验装置的方法,将在有机化学的教学中提供一定的帮助,进一步提高 ...

  5. VS2015下如何用编译、调试程序。

    VS2015下如何用编译.调试程序. (通过实践给出截图) 安装步骤: 下载安装网址[VS2015下载地址](http://www.ithome.com/html/win10/164028.htm) ...

  6. 在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)

    在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...

  7. 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。

    请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...

  8. CentOS下如何用nmon收集系统实时运行状况

    #赋予执行权限 chmod +x nmon 执行./nmon可以查看实时的系统状态有提示的,d看磁盘,n看网络,c看cpu #如果不想看实时的,想收集系统长时间运行情况然后分析,可用这个 nohup ...

  9. tushare获取股票每日重要的基本面指标数据,并存入Elasticsearch

    tushare是一个开放的,免费的金融数据平台,包含沪深股票数据,指数数据,基金数据,期货数据,期权数据,债券数据,外汇数据,港股数据,行业经济数据,宏观经济数据以及新闻快讯等特色数据.其中以沪深股票 ...

随机推荐

  1. vijosP1016 北京2008的挂钟

    vijosP1016 北京2008的挂钟 题目链接:https://vijos.org/p/1016 [思路] Dfs. 对操作搜索更加优秀,所以采用搜索每一个操作的使用次数,因为操作数为4则相当于没 ...

  2. 输入A和B,计算并输出A+B

    EOF是一个预定义的常量,等于-1. 输入A和B,计算并输出A+B Sample input: 1    5 10  20 Sample output: 6 30 #include <iostr ...

  3. 整合maven,jetty,jrebel进行debug调试

    整合maven,jetty,jrebel进行调试 maven配置 这个网上有很多,验证mvn是否配置正确: Jrebel配置 解压至目录,不建议目录名有空格 ,破解包下载 参考: http://zer ...

  4. Extjs中grid行的上移和下移

    一.将up和down按钮放到tbar中,然后选中grid行即可实现上移和下移 var up = new Ext.Action({ text : 'Up', icon : 'up.png',//或者添加 ...

  5. 【Java基础】基本类型的包装类作为参数传递是值传递还是引用传递

    突然想到这个问题,然后做了下实验,下面以Integer来讲解,其他的忽略: import java.util.Iterator; /** * Created by lili on 15/9/24. * ...

  6. 为静态Checkbox动态地添加checked属性

    1.ASP.NET HTML Code: 嵌套在repeater中 " ? "checked" : "" %> /> *** 关键代码: ...

  7. SQL2005以上行变行简单实现

    用语法PIVOT参照:http://technet.microsoft.com/zh-cn/library/ms177410(v=sql.105).aspx

  8. light oj 1354 - IP Checking

    1354 - IP Checking   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB An I ...

  9. android开源项目学习

    FBReaderJ FBReaderJ用于Android平台的电子书阅读器,它支持多种电子书籍格式包括:oeb.ePub和fb2.此外还支持直接读取zip.tar和gzip等压缩文档. 项目地址:ht ...

  10. Android执行shell命令

    一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/bin/cat", &q ...