当前位置:网站首页>C#利用开源NPlot实现K线图(蜡烛图)
C#利用开源NPlot实现K线图(蜡烛图)
2022-07-30 05:41:00 【凡梦_】
NPlot是.Net平台下开源的图表控件,包括K线图、柱状图、饼图、曲线图、散点图等。这里主要讲讲怎么利用NPlot实现股票K线图。NPlot中PlotSurface2D对象是整个NPlot的图表的容器,所有的图表都需要添加到PlotSurface2D中才能进行显示。在WinForm程序中我们引用的是Windows.PlotSurface2D类,此类集成自Control。这里利用的K线图数据来自OKEX比特币交易的日线数据。主要包括时间、开盘价、最高价、最低价、收盘价等数据。数据存储数据库中的,Nplot提供数据源绑定功能,通过数据库查询查出的DataTable对象可以直接绑定到蜡烛对象上。这里利用了NPlot中的CandlePlot蜡烛图对象。K线图效果如下:

PlotSurface2D初始化:
private void InitKLinePS()
{
KLinePS = new NPlot.Windows.PlotSurface2D();
this.KLinePS.AutoScaleAutoGeneratedAxes = true;
this.KLinePS.AutoScaleTitle = false ;
this.KLinePS.DateTimeToolTip = true;
this.KLinePS.DateTimeToolTip = true;
this.KLinePS.Legend = null;
this.KLinePS.LegendZOrder = -1;
this.KLinePS.Location = new System.Drawing.Point(0, 0);
this.KLinePS.Name = "costPS";
this.KLinePS.RightMenu = null;
this.KLinePS.Padding = 10;
//鼠标tooltips 时间+价格
this.KLinePS.ShowCoordinates = true;
this.KLinePS.Size = new System.Drawing.Size(969, 595);
this.KLinePS.Width = 1300;
this.KLinePS.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
this.KLinePS.TabIndex = 2;
this.KLinePS.Title = "123";
this.KLinePS.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
}
SetCandlePlot方法为类的外部调用方法,包括K线数据查询、CandlePlot对象创建、数据源绑定及把CandlePlot对象添加到PlotSurface2D中去,最后通过PlotSurface2D的Refresh刷新显示。
public void SetCandlePlot(string klineTable, string startTime, string endTime, Dictionary<int, GridStrategyEntity> dicGridStrategyEntity, DataTable backTestResult)
{
string sql;
dicAutoGridStrategy = dicGridStrategyEntity;
this.backTestResult = backTestResult;
backTestStartTime = startTime;
backTestEndTime = endTime;
// dealedCount = 0;
sql = "select * from " + klineTable + "kline where datetime>#" + startTime + "#" + " and datetime<#"
+ endTime + "# order by datetime asc";
DataTable dt = oleDbHelper.queryDataForDataTable(sql);
CandlePlot cp = new CandlePlot();
cp.DataSource = dt;
cp.AbscissaData = "datetime";
cp.OpenData = "open";
cp.LowData = "low";
cp.HighData = "high";
cp.CloseData = "close";
cp.Label = "蜡烛图";
//开跌色
cp.BearishColor = System.Drawing.Color.FromArgb(255, 255, 0, 0);
//看涨色
cp.BullishColor = System.Drawing.Color.FromArgb(255, 0, 255, 0);
cp.Style = CandlePlot.Styles.Filled;
//K线宽度
cp.StickWidth = 4;
this.KLinePS.Add(cp);
//隐藏日期刻度标示
this.KLinePS.XAxis1.HideTickText = true ;
this.KLinePS.Refresh();
}边栏推荐
猜你喜欢

div设置一个最小高度和最大高度,但是中间可以靠内容撑开

C语言自定义类型一网打尽(结构体、位段/位域、枚举、联合体)

php实现数据库的增删查改操作-教务管理系统
![[Net Ding Cup 2020 Qinglong Group] AreUSerialz](/img/f2/9aef8b8317eff31af2979b3a45b54c.png)
[Net Ding Cup 2020 Qinglong Group] AreUSerialz

Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)

函数解剖——深挖printf()与scanf()
CTF之misc-图片隐写

cJSON开源项目详细解剖

函数解剖——深挖getchar()与putchar()

【墨者学院】身份认证失效漏洞实战
随机推荐
Misc of CTF - other types of steganography
中间件cors三行代码解决跨域问题GET,POST跨域访问解决
通信中间件 Fast DDS 基础概念简述与通信示例
怎么判断大小端(网络字节序和主机字节序)
torch分布式训练
FastAPI Quick Start
使用Nodejs搭建Web Server(入门教程)
MySQL-Explain详解
PHP-fpm
复习 redux 总结
猜数字游戏
npm run serve starts error npm ERR Missing script "serve"
Koa2框架快速入门与基本使用
目前主流浏览器以及对应的内核
Qt对动态库(*.dll)的封装以及使用
setAttribute()方法
CTF之misc-内存分析(Volatility)
TypeError The view function did not return a valid response. The function either returned None 的解决
JDBC programming of MySQL database
sqli-labs less3/4打靶笔记