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

    [内容简介]

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

    [窗口界面] 见下图

    上图中对应各周期功能按钮和标签进行相应拖放操作就是了, 不再多述 .倒是图表区为一个Nplot控件,命名:myPlot.

    [窗口类定义]

    1. 界面部分自动生成的代码文件:

    ///<ctp1.Designer.cs>

    ///K线图窗口界面代码此代码由VS自动生成。

    //------------------------------------------------------------------------------

    namespace Ctp

    {

    partialclassFormCtpThread

    {

    ///<summary>

    /// Required designer variable.

    ///</summary>

    private System.ComponentModel.IContainer components = null;

    ///<summary>

    /// Clean up any resources being used.

    ///</summary>

    ///<param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

    protectedoverridevoid Dispose(bool disposing)

    {

    if (disposing && (components != null))

    {

    components.Dispose();

    }

    base.Dispose(disposing);

    }

    #region Windows Form Designer generated code

    ///<summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    ///</summary>

    privatevoid InitializeComponent()

    {

    this.menuStrip1 = new System.Windows.Forms.MenuStrip();

    this.M5 = new System.Windows.Forms.Button();

    this.myPlot = new NPlot.Windows.PlotSurface2D();

    this.M1 = new System.Windows.Forms.Button();

    this.M15 = new System.Windows.Forms.Button();

    this.M30 = new System.Windows.Forms.Button();

    this.H1 = new System.Windows.Forms.Button();

    this.D1 = new System.Windows.Forms.Button();

    this.W1 = new System.Windows.Forms.Button();

    this.Mn1 = new System.Windows.Forms.Button();

    this.label_symbol = new System.Windows.Forms.Label();

    this.label1 = new System.Windows.Forms.Label();

    this.textBox1 = new System.Windows.Forms.TextBox();

    this.enlarge = new System.Windows.Forms.Button();

    this.shrink = new System.Windows.Forms.Button();

    this.labelTime = new System.Windows.Forms.Label();

    this.button3 = new System.Windows.Forms.Button();

    this.buttonRoll = new System.Windows.Forms.Button();

    this.textBox2 = new System.Windows.Forms.TextBox();

    this.buttonMove = new System.Windows.Forms.Button();

    this.SuspendLayout();

    //

    // menuStrip1

    //

    this.menuStrip1.Location = new System.Drawing.Point(0, 0);

    this.menuStrip1.Name = "menuStrip1";

    this.menuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;

    this.menuStrip1.Size = new System.Drawing.Size(1049, 24);

    this.menuStrip1.TabIndex = 1;

    this.menuStrip1.Text = "menuStrip1";

    //

    // M5

    //

    this.M5.AutoSize = true;

    this.M5.CausesValidation = false;

    this.M5.Location = new System.Drawing.Point(337, 0);

    this.M5.Name = "M5";

    this.M5.Size = new System.Drawing.Size(39, 24);

    this.M5.TabIndex = 2;

    this.M5.TabStop = false;

    this.M5.Text = "5";

    this.M5.UseVisualStyleBackColor = true;

    this.M5.Click += new System.EventHandler(this.M5_Click);

    //

    // myPlot

    //

    this.myPlot.AutoScaleAutoGeneratedAxes = false;

    this.myPlot.AutoScaleTitle = false;

    this.myPlot.BackColor = System.Drawing.Color.White;

    this.myPlot.DateTimeToolTip = true;

    this.myPlot.Dock = System.Windows.Forms.DockStyle.Fill;

    this.myPlot.ForeColor = System.Drawing.Color.White;

    this.myPlot.Legend = null;

    this.myPlot.LegendZOrder = -1;

    this.myPlot.Location = new System.Drawing.Point(0, 24);

    this.myPlot.Name = "myPlot";

    this.myPlot.RightMenu = null;

    this.myPlot.ShowCoordinates = true;

    this.myPlot.Size = new System.Drawing.Size(1049, 414);

    this.myPlot.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

    this.myPlot.TabIndex = 1;

    this.myPlot.Text = "myPlotSurface2D1";

    this.myPlot.Title = "";

    this.myPlot.TitleFont = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

    this.myPlot.XAxis1 = null;

    this.myPlot.XAxis2 = null;

    this.myPlot.YAxis1 = null;

    this.myPlot.YAxis2 = null;

    //

    // M1

    //

    this.M1.AutoSize = true;

    this.M1.CausesValidation = false;

    this.M1.Location = new System.Drawing.Point(301, 0);

    this.M1.Name = "M1";

    this.M1.Size = new System.Drawing.Size(39, 24);

    this.M1.TabIndex = 4;

    this.M1.TabStop = false;

    this.M1.Text = "1";

    this.M1.UseVisualStyleBackColor = true;

    this.M1.Click += new System.EventHandler(this.M1_Click);

    //

    // M15

    //

    this.M15.AutoSize = true;

    this.M15.CausesValidation = false;

    this.M15.Location = new System.Drawing.Point(373, 0);

    this.M15.Name = "M15";

    this.M15.Size = new System.Drawing.Size(39, 24);

    this.M15.TabIndex = 6;

    this.M15.TabStop = false;

    this.M15.Text = "15";

    this.M15.UseVisualStyleBackColor = true;

    this.M15.Click += new System.EventHandler(this.M15_Click);

    //

    // M30

    //

    this.M30.AutoSize = true;

    this.M30.CausesValidation = false;

    this.M30.Location = new System.Drawing.Point(410, 0);

    this.M30.Name = "M30";

    this.M30.Size = new System.Drawing.Size(39, 24);

    this.M30.TabIndex = 5;

    this.M30.TabStop = false;

    this.M30.Text = "30";

    this.M30.UseVisualStyleBackColor = true;

    this.M30.Click += new System.EventHandler(this.M30_Click);

    //

    // H1

    //

    this.H1.AutoSize = true;

    this.H1.CausesValidation = false;

    this.H1.Location = new System.Drawing.Point(448, 0);

    this.H1.Name = "H1";

    this.H1.Size = new System.Drawing.Size(39, 24);

    this.H1.TabIndex = 8;

    this.H1.TabStop = false;

    this.H1.Text = "1";

    this.H1.UseVisualStyleBackColor = true;

    this.H1.Click += new System.EventHandler(this.H1_Click);

    //

    // D1

    //

    this.D1.AutoSize = true;

    this.D1.CausesValidation = false;

    this.D1.Location = new System.Drawing.Point(486, 0);

    this.D1.Name = "D1";

    this.D1.Size = new System.Drawing.Size(39, 24);

    this.D1.TabIndex = 7;

    this.D1.TabStop = false;

    this.D1.Text = "1";

    this.D1.UseVisualStyleBackColor = true;

    this.D1.Click += new System.EventHandler(this.D1_Click);

    //

    // W1

    //

    this.W1.AutoSize = true;

    this.W1.CausesValidation = false;

    this.W1.Location = new System.Drawing.Point(524, 0);

    this.W1.Name = "W1";

    this.W1.Size = new System.Drawing.Size(39, 24);

    this.W1.TabIndex = 9;

    this.W1.TabStop = false;

    this.W1.Text = "1";

    this.W1.UseVisualStyleBackColor = true;

    this.W1.Click += new System.EventHandler(this.W1_Click);

    //

    // Mn1

    //

    this.Mn1.AutoSize = true;

    this.Mn1.CausesValidation = false;

    this.Mn1.Location = new System.Drawing.Point(561, 0);

    this.Mn1.Name = "Mn1";

    this.Mn1.Size = new System.Drawing.Size(39, 24);

    this.Mn1.TabIndex = 10;

    this.Mn1.TabStop = false;

    this.Mn1.Text = "1";

    this.Mn1.UseVisualStyleBackColor = true;

    this.Mn1.Click += new System.EventHandler(this.Mn1_Click);

    //

    // label_symbol

    //

    this.label_symbol.AutoSize = true;

    this.label_symbol.Location = new System.Drawing.Point(60, 6);

    this.label_symbol.Name = "label_symbol";

    this.label_symbol.Size = new System.Drawing.Size(41, 12);

    this.label_symbol.TabIndex = 11;

    this.label_symbol.Text = "label1";

    //

    // label1

    //

    this.label1.AutoSize = true;

    this.label1.Location = new System.Drawing.Point(5, 6);

    this.label1.Name = "label1";

    this.label1.Size = new System.Drawing.Size(29, 12);

    this.label1.TabIndex = 12;

    this.label1.Text = "商品";

    //

    // textBox1

    //

    this.textBox1.Location = new System.Drawing.Point(742, 2);

    this.textBox1.Name = "textBox1";

    this.textBox1.Size = new System.Drawing.Size(104, 21);

    this.textBox1.TabIndex = 13;

    //

    // enlarge

    //

    this.enlarge.FlatStyle = System.Windows.Forms.FlatStyle.System;

    this.enlarge.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

    this.enlarge.Location = new System.Drawing.Point(601, 0);

    this.enlarge.Name = "enlarge";

    this.enlarge.Size = new System.Drawing.Size(24, 23);

    this.enlarge.TabIndex = 14;

    this.enlarge.TabStop = false;

    this.enlarge.Text = "+";

    this.enlarge.UseVisualStyleBackColor = true;

    this.enlarge.Click += new System.EventHandler(this.enlarge_Click);

    //

    // shrink

    //

    this.shrink.FlatStyle = System.Windows.Forms.FlatStyle.System;

    this.shrink.Font = new System.Drawing.Font("Arial", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

    this.shrink.Location = new System.Drawing.Point(625, 0);

    this.shrink.Name = "shrink";

    this.shrink.Size = new System.Drawing.Size(24, 23);

    this.shrink.TabIndex = 15;

    this.shrink.TabStop = false;

    this.shrink.Text = "-";

    this.shrink.UseVisualStyleBackColor = true;

    this.shrink.Click += new System.EventHandler(this.shrink_Click);

    //

    // labelTime

    //

    this.labelTime.AutoSize = true;

    this.labelTime.Location = new System.Drawing.Point(182, 6);

    this.labelTime.Name = "labelTime";

    this.labelTime.Size = new System.Drawing.Size(41, 12);

    this.labelTime.TabIndex = 16;

    this.labelTime.Text = "label2";

    //

    // button3

    //

    this.button3.Location = new System.Drawing.Point(974, 0);

    this.button3.Name = "button3";

    this.button3.Size = new System.Drawing.Size(75, 23);

    this.button3.TabIndex = 17;

    this.button3.Text = "button3";

    this.button3.UseVisualStyleBackColor = true;

    this.button3.Click += new System.EventHandler(this.button3_Click);

    //

    // buttonRoll

    //

    this.buttonRoll.FlatStyle = System.Windows.Forms.FlatStyle.System;

    this.buttonRoll.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

    this.buttonRoll.Location = new System.Drawing.Point(689, 0);

    this.buttonRoll.Name = "buttonRoll";

    this.buttonRoll.Size = new System.Drawing.Size(38, 23);

    this.buttonRoll.TabIndex = 19;

    this.buttonRoll.TabStop = false;

    this.buttonRoll.Text = "滚动";

    this.buttonRoll.UseVisualStyleBackColor = true;

    this.buttonRoll.Click += new System.EventHandler(this.buttonRoll_Click);

    //

    // textBox2

    //

    this.textBox2.Location = new System.Drawing.Point(862, 2);

    this.textBox2.Name = "textBox2";

    this.textBox2.Size = new System.Drawing.Size(100, 21);

    this.textBox2.TabIndex = 20;

    this.textBox2.Visible = false;

    //

    // buttonMove

    //

    this.buttonMove.Location = new System.Drawing.Point(651, 0);

    this.buttonMove.Name = "buttonMove";

    this.buttonMove.Size = new System.Drawing.Size(37, 23);

    this.buttonMove.TabIndex = 21;

    this.buttonMove.Text = "平移";

    this.buttonMove.UseVisualStyleBackColor = true;

    this.buttonMove.Click += new System.EventHandler(this.buttonMove_Click);

    //

    // FormCtpThread

    //

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

    this.ClientSize = new System.Drawing.Size(1049, 438);

    this.Controls.Add(this.buttonMove);

    this.Controls.Add(this.textBox2);

    this.Controls.Add(this.buttonRoll);

    this.Controls.Add(this.button3);

    this.Controls.Add(this.labelTime);

    this.Controls.Add(this.shrink);

    this.Controls.Add(this.enlarge);

    this.Controls.Add(this.textBox1);

    this.Controls.Add(this.label1);

    this.Controls.Add(this.label_symbol);

    this.Controls.Add(this.Mn1);

    this.Controls.Add(this.W1);

    this.Controls.Add(this.H1);

    this.Controls.Add(this.D1);

    this.Controls.Add(this.M15);

    this.Controls.Add(this.M30);

    this.Controls.Add(this.M1);

    this.Controls.Add(this.myPlot);

    this.Controls.Add(this.M5);

    this.Controls.Add(this.menuStrip1);

    this.KeyPreview = true;

    this.Name = "FormCtpThread";

    this.Text = "FormCtpThread";

    this.Load += new System.EventHandler(this.FormCtp_Load);

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormCtp_KeyDown);

    this.ResumeLayout(false);

    this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.MenuStrip menuStrip1;

    private System.Windows.Forms.Button M5;

    private NPlot.Windows.PlotSurface2D myPlot;

    private System.Windows.Forms.Button M1;

    private System.Windows.Forms.Button M15;

    private System.Windows.Forms.Button M30;

    private System.Windows.Forms.Button H1;

    private System.Windows.Forms.Button D1;

    private System.Windows.Forms.Button W1;

    private System.Windows.Forms.Button Mn1;

    private System.Windows.Forms.Label label_symbol;

    private System.Windows.Forms.Label label1;

    private System.Windows.Forms.TextBox textBox1;

    private System.Windows.Forms.Button enlarge;

    private System.Windows.Forms.Button shrink;

    private System.Windows.Forms.Label labelTime;

    private System.Windows.Forms.Button button3;

    private System.Windows.Forms.Button buttonRoll;

    private System.Windows.Forms.TextBox textBox2;

    private System.Windows.Forms.Button buttonMove;

    //private NPlot.Windows.PlotSurface2D myplot;

    }

    }

    1. 蜡烛图表类的显示部分代码:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Text;

    using System.IO;

    using System.Data;

    using System.Drawing;

    using System.Globalization;

    using System.Drawing.Drawing2D;

    using System.Linq;

    using System.Collections;

    using System.Threading.Tasks;

    using System.Windows.Forms;

    using System.Runtime.Remoting;

    using StockAnalyse;

    using INIRead;

    using NPlot;

    using System.Runtime.InteropServices;

    using System.Threading;

    /////////////////////////////////////////////////////////////////////////

    ///20160806        觉海QQ:806935610创建该文件

    ///@brief 蜡烛图显示类的定义文件: ///@ctp.cs

    ///类定义名称:class FormCtpThread

    ///20160806

    ///此版本已经可以自动更新价格,但不能滚动K线

    ///20160904

    ///已经实现K价格图表自动更新

    /////////////////////////////////////////////////////////////////////////

    namespace Ctp

    {

    publicpartialclassFormCtpThread : Form

    {

    //使用多线程计时器

    private System.Timers.Timer timer = new System.Timers.Timer(1000);

    ///<summary>

    /// /////////////////////////////成员

    ///</summary>

    publicList<QuotesInfo> DataSymbol { get; set; }

    publicList<StockInfo> DataStock { get; set; }

    publicList<StockInfo> DataStockReverse { get; set; }

    String period;

    StockAnalyse.TimeFrame timeFrame = StockAnalyse.TimeFrame.D1;

    String stockName;

    double xMin, xMax, yMin, yMax;

    int xInitShowBars = 300;

    int xShowBars = 300;//k线最大数

    CandlePlot cp = newCandlePlot();

    string pathCtp { get; set; }

    string file { get; set; }

    public FormCtpThread()

    {

    InitializeComponent();

    //MessageBox.Show("PK");

    }

    public FormCtpThread(string _file)

    {

    InitializeComponent();

    file = _file;

    //this.label_symbol.Text = file;

    //MessageBox.Show("PK");

    }

    privatevoid FormCtp_Load(object sender, EventArgs e)

    {

    OpenChartFile(file);

    this.MouseWheel += newMouseEventHandler(FormCtp_MouseWheel);

    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

    timer.Enabled = true;

    timer.AutoReset = true; //是否不断重复定时器操作

    }

    void FormCtp_MouseWheel(object sender, MouseEventArgs e)

    {

    if (e.Delta > 0)

    {

    // this.Text = "正在向上滚动滑轮";用于放大K线蜡烛图;

    ChangeSize(1);

    }

    elseif (e.Delta < 0)

    {

    // this.Text = "正在向下滚动滑轮"用于缩小K蜡烛图;

    ChangeSize(-1);

    }

    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

    {

    //用于实现K线图表自动读取文件数据,并定时刷新显示

    labelTime.Text = DateTime.Now.ToString();//调用内容,并用lable1显示出来。。。

    OpenChartFileLastBar(file);

    if (!isMove)

    KMove(ref xBegin, ref xEnd, ref xShowBars, 0);

    }

    }

     

    上面的窗口类定义文件,包含了本程序的主要代码,加入定时器用于实现K线图表自动读取文件数据,并定时刷新显示,时间为1000ms

    以下为K线图自动更新代码:

    void ReFreshMe(refint xBegin, refint xEnd, refint xShowBars, int opMode)

    {

    int xDisLen = xBegin - xEnd;

    textBox1.Text = xDisLen.ToString();

    int n, m;

    if (opMode > 0)

    {

    n = xBegin - xEnd; //(int)(xShowBars) / 1;

    //MessageBox.Show(n.ToString());

    m = n / 2;

    m = Math.Max(2, m);

    xBegin = xDisLen = Math.Min(m, DataStock.Count - 1);

    xEnd = 0;// Math.Max(xEnd - m, 0);

    }

    elseif (opMode < 0)

    {

    n = xBegin - xEnd;

    m = n * 2;

    xBegin = xDisLen = Math.Min(m, DataStock.Count - 1);

    xEnd = 0;// Math.Max(xEnd - m, 0);

    }

    else//首次加载

    {

    n = xInitShowBars;

    xBegin = xDisLen = Math.Min(n, DataStock.Count - 1);

    xEnd = 0;// Math.Max(xEnd - m, 0);

    }

    //判定要显示的数据数量

    List<Double> DataOpen = newList<Double>();

    List<Double> DataHigh = newList<Double>();

    List<Double> DataLow = newList<Double>();

    List<Double> DataClose = newList<Double>();

    List<DateTime> DataTime = newList<DateTime>();

    List<int> Time = newList<int>();

    for (int i = xBegin - 1; i >= xEnd; i--)

    {

    DataHigh.Add(DataStock[i].high);

    DataLow.Add(DataStock[i].low);

    DataClose.Add(DataStock[i].close);

    DataOpen.Add(DataStock[i].open);

    DataTime.Add(DataStock[i].date);

    //Time.Add(i);

    }

    //写横坐标轴数据

    for (int i = 00; i < xDisLen + 5; i++) Time.Add(i);

    cp.OpenData = DataOpen.ToArray();

    cp.CloseData = DataClose.ToArray();

    cp.HighData = DataHigh.ToArray();

    cp.LowData = DataLow.ToArray();

    cp.AbscissaData = Time.ToArray();

    PlotCandle(cp, DataTime, timeFrame);

    }

    下面为蜡烛K线图形缩放代码和K线图绘制代码:

    ///放大缩小

    privatevoid ChangeSize(int opMode)

    {

    ReFreshMe(ref xBegin, ref xEnd, ref xShowBars, opMode);

    }

    /////////K线图绘制//////////

    publicvoid PlotCandle(CandlePlot cp, List<DateTime> dates, StockAnalyse.TimeFrame timeframe)

    {

    try

    {

    myPlot.Clear();

    // --- Grid Code ---

    Grid mygrid = newGrid();

    mygrid.HorizontalGridType = Grid.GridType.Fine;

    mygrid.VerticalGridType = Grid.GridType.Fine;

    myPlot.Add(mygrid);

    cp.BullishColor = Color.Red;

    //cp.Color = Color.Black;

    cp.Centered = false;

    cp.BearishColor = Color.Green;

    cp.Style = CandlePlot.Styles.Filled;

    this.myPlot.Add(cp);

    //////这里需要先添加图形后设置坐标轴

    /////字符坐标轴加入后会隐藏原来x坐标轴

    LabelAxis la1 = newLabelAxis(this.myPlot.XAxis1);

    for (int i = 00; i < dates.Count; i++)

    {

    switch (timeframe)

    {

    case StockAnalyse.TimeFrame.M1:

    if (Math.IEEERemainder(i, 30) == 0)

    la1.AddLabel(dates[i].ToShortTimeString() + @" " + dates[i].ToShortDateString(), i);

    break;

    case StockAnalyse.TimeFrame.M5:

    if (Math.IEEERemainder(i, 12) == 0)

    la1.AddLabel(dates[i].ToShortTimeString() + @" " + dates[i].ToShortDateString(), i + 00);

    break;

    default:

    if (Math.IEEERemainder(i, 5) == 0)

    la1.AddLabel(dates[i].ToShortTimeString() + @" " + dates[i].ToShortDateString(), i + 00);

    break;

    }

    }

    //la1.Label = "时间";

    la1.TickTextFont = newFont("Courier New", 8);

    la1.TicksBetweenText = false;

    this.myPlot.XAxis1 = la1;

    //////让日期斜45度。

    //myPlot.XAxis1.TicksLabelAngle = 45;

    myPlot.Refresh();

    }

    catch (Exception e)

    {

    //Console.WriteLine("{0} Exception caught.", e);

    //MessageBox.Show(e.ToString());

    }

    }

    崇尚代码即法律,做一个智能合约开发者,重点在于合约的交易逻辑设计,交流请访问微信yyy99966
  • 相关阅读:
    数据库mysql基础语言--各模式的含义
    Linux下判断磁盘是SSD还是HDD的几种方法
    linux解压大全
    RedHat Linux RHEL6配置本地YUM源
    利用ssh传输文件-服务器之间传输文件
    深入理解asp.net里的HttpModule机制
    WPF(一)
    JS中caller和callee
    Vue-Methods中使用Filter
    c#值类型与引用类型区别
  • 原文地址:https://www.cnblogs.com/juehai/p/5840798.html
Copyright © 2011-2022 走看看