C#利用开源NPlot实现K线图(蜡烛图)

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


整个代码分为PlotSurface2D声明、初始化、查询K线数据、创建CandlePlot对象并绑定数据,最后把CandlePlot对象添加到PlotSurface2D容器中进行刷新显示。具体代码如下:

PlotSurface2D声明,这里为类对象:
private NPlot.Windows. PlotSurface2D KLinePS;

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();            
        }

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凡梦_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值