基本信息
源码名称:股票,期货,现货 K线开发实例
源码大小:4.53M
文件格式:.rar
开发语言:C#
更新时间:2017-03-27
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using ImageOfStock;
using System.Drawing.Drawing2D;
using KCZXDataAccess;


namespace KCZX.Forms
{
    public partial class Form1 :  Form2   
    {
        public Form1()
        {
            InitializeComponent();
          
            CandleGraph();
            InitMainMenu();
         
        }

    
        public void InitMainMenu()
        {

            List<string[]> list = new List<string[]>();
            using (PeopleAccessClass da = new PeopleAccessClass())
            {
                DataSet ds = new DataSet();
                string x= MainForm .gudaima  ;
                string y = MainForm.mingcheng;
                string z = MainForm.k_string;
                ds = da.GETMAINdetail(x );
                if (ds.Tables[0].Rows.Count != 0)
                {
                    string[] languages = { z, x,y, "期货指数", "", "", "", "", "" };
                    list.Add(languages);
                    string[] languages1 = { "时间", "开盘价", "最高价", "最低价", "收盘价", "成交量", "成交额" };
                    list.Add(languages1);

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i )
                    {
                        string[] records = new string[8];
                        records[0] = ds.Tables[0].Rows[i][0].ToString();
                        records[1] = ds.Tables[0].Rows[i][1].ToString();
                        records[2] = ds.Tables[0].Rows[i][2].ToString();
                        records[3] = ds.Tables[0].Rows[i][3].ToString();
                        records[4] = ds.Tables[0].Rows[i][4].ToString();
                        records[5] = ds.Tables[0].Rows[i][5].ToString();
                        records[6] = ds.Tables[0].Rows[i][6].ToString();
                        records[7] = ds.Tables[0].Rows[i][7].ToString();
                        list.Add(records);
                    }

                }
                else
                {
                    return;
                }
            }
            CandleGraph();
            this.chartGraph1.ProcessBarValue = 0;
            Thread refreshData = new Thread(new ParameterizedThreadStart(UpdateDataToGraph));
            refreshData.IsBackground = true;
            refreshData.Start(list);
        }

        private void tsmiClose_Click(object sender, EventArgs e) //
        {
            this.Close();
            //Application.Exit();
        }

        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        public void CandleGraph()
        {
            this.chartGraph1.ResetNullGraph();
            this.chartGraph1.UseScrollAddSpeed = true;
            this.chartGraph1.SetXScaleField("日期");
            this.chartGraph1.CanDragSeries = true;
            this.chartGraph1.SetSrollStep(10, 10);
            this.chartGraph1.ShowLeftScale = true;
            this.chartGraph1.ShowRightScale = true;
            this.chartGraph1.LeftPixSpace = 85;
            this.chartGraph1.RightPixSpace = 85;
            //K线图 BS点
            mainPanelID = this.chartGraph1.AddChartPanel(40);
            string candleName = "K线图-1";
            this.chartGraph1.AddCandle(candleName, "OPEN", "HIGH", "LOW", "CLOSE", mainPanelID, true);
            this.chartGraph1.YMBuySellSignal(mainPanelID, candleName, "BUYEMA", "(CLOSE HIGH LOW)/3", "SELLEMA", "BUYEMA");
            this.chartGraph1.AddBollingerBands("MID", "UP", "DOWN", "CLOSE", 20, 2, mainPanelID);
            this.chartGraph1.SetYScaleField(mainPanelID, new string[] { "HIGH", "LOW" });
            //成交量
            volumePanelID = this.chartGraph1.AddChartPanel(20);
            this.chartGraph1.AddHistogram("VOL", "", candleName, volumePanelID);
            this.chartGraph1.SetHistogramStyle("VOL", Color.Red, Color.SkyBlue, 1, false);
            this.chartGraph1.AddSimpleMovingAverage("VOL-MA1", "MA5", "VOL", 5, volumePanelID);
            this.chartGraph1.SetTrendLineStyle("VOL-MA1", Color.White, Color.White, 1, DashStyle.Solid);
            this.chartGraph1.AddSimpleMovingAverage("VOL-MA2", "MA10", "VOL", 10, volumePanelID);
            this.chartGraph1.SetTrendLineStyle("VOL-MA2", Color.Yellow, Color.Yellow, 1, DashStyle.Solid);
            this.chartGraph1.AddSimpleMovingAverage("VOL-MA3", "MA20", "VOL", 20, volumePanelID);
            this.chartGraph1.SetTrendLineStyle("VOL-MA3", Color.FromArgb(255, 0, 255), Color.FromArgb(255, 0, 255), 1, DashStyle.Solid);
            this.chartGraph1.SetTick(volumePanelID, 1);
            this.chartGraph1.SetDigit(volumePanelID, 0);
            kdjPanelID = this.chartGraph1.AddChartPanel(20);
            this.chartGraph1.AddStochasticOscillator("K", "D", "J", 9, "CLOSE", "HIGH", "LOW", kdjPanelID);
            macdPanelID = this.chartGraph1.AddChartPanel(20);
            this.chartGraph1.AddMacd("MACD", "DIFF", "DEA", "CLOSE", 26, 12, 9, macdPanelID);
        }
        int mainPanelID = -1;
        int volumePanelID = -1;
        int kdjPanelID = -1;
        int macdPanelID = -1;

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomForm_Load(object sender, EventArgs e)
        {
            this.Text = "Customr";
            this.Location = new Point(0, 0);
            this.Size = new Size(Screen.GetWorkingArea(this).Width, Screen.GetWorkingArea(this).Height);
            CandleGraph();
        }

        /// <summary>
        /// 更新进度条
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateProcessBar(object obj)
        {
            int[] values = obj as int[];
            int total = values[0];
            int current = values[1];
            int processValue = Convert.ToInt32((double)current / (double)total * 100);
            if (processValue > this.chartGraph1.ProcessBarValue)
            {
                this.chartGraph1.ProcessBarValue = processValue;
            }
            if (current == total - 2)
            {
                this.chartGraph1.ProcessBarValue = 100;
                //this.tsmiImport.Enabled = true;
                this.chartGraph1.RefreshGraph();
            }
        }

        private delegate void UpdateProcessBarDelegate(object obj);

        /// <summary>
        /// 更新数据到图像
        /// </summary>
        /// <param name="obj"></param>
        public void UpdateDataToGraph(object obj)
        {
            List<string[]> list = obj as List<string[]>;
            string[] str = list[0];
            this.chartGraph1.SetTitle(mainPanelID, str[2] "(" str[1] ") " str[0]);
            this.chartGraph1.SetTitle(kdjPanelID, "KDJ(9,3,3)");
            this.chartGraph1.SetTitle(volumePanelID, "VOL(5,10,20)");
            this.chartGraph1.SetTitle(macdPanelID, "MACD(12,26,9)");
            string lineType = str[0];
            switch (lineType)
            {
                case "5分钟":
                    this.chartGraph1.SetIntervalType(mainPanelID, ChartGraph.IntervalType.Second );
                    this.chartGraph1.SetIntervalType(volumePanelID, ChartGraph.IntervalType.Second);
                    this.chartGraph1.SetIntervalType(kdjPanelID, ChartGraph.IntervalType.Second);
                    break;
                case "15分钟":
                case "30分钟":
                case "60分钟":
                    this.chartGraph1.SetIntervalType(mainPanelID, ChartGraph.IntervalType.Minute);
                    this.chartGraph1.SetIntervalType(volumePanelID, ChartGraph.IntervalType.Minute);
                    this.chartGraph1.SetIntervalType(kdjPanelID, ChartGraph.IntervalType.Minute);
                    break;
                case "日线":
                    this.chartGraph1.SetIntervalType(mainPanelID, ChartGraph.IntervalType.Day);
                    this.chartGraph1.SetIntervalType(volumePanelID, ChartGraph.IntervalType.Day);
                    this.chartGraph1.SetIntervalType(kdjPanelID, ChartGraph.IntervalType.Day);
                    break;
                case "周线":
                    this.chartGraph1.SetIntervalType(mainPanelID, ChartGraph.IntervalType.Week);
                    this.chartGraph1.SetIntervalType(volumePanelID, ChartGraph.IntervalType.Week);
                    this.chartGraph1.SetIntervalType(kdjPanelID, ChartGraph.IntervalType.Week);
                    break;
                case "月线":
                    this.chartGraph1.SetIntervalType(mainPanelID, ChartGraph.IntervalType.Month);
                    this.chartGraph1.SetIntervalType(volumePanelID, ChartGraph.IntervalType.Month);
                    this.chartGraph1.SetIntervalType(kdjPanelID, ChartGraph.IntervalType.Month);
                    break;
            }
            this.chartGraph1.RefreshGraph();
            for (int i = list.Count - 1; i >= 2; i--)
            {
                string[] records = list[i];
                string timeKey = records[0];
                int year = 1970;
                int month = 1;
                int day = 1;
                int hour = 0;
                int minute = 0;
                switch (lineType)
                {
                    case "5分钟":
                    case "15分钟":
                    case "30分钟":
                    case "60分钟": 
                        month = Convert.ToInt32(timeKey.Substring(4,2));
                        day = Convert.ToInt32(timeKey.Substring(6, 2));
                        hour = 11;
                        minute = 22;
                        //hour = Convert.ToInt32(timeKey.Substring(3, 2));
                        //minute = Convert.ToInt32(timeKey.Substring(5, 2));
                        break;
                    case "日线":
                    case "周线":
                        year = Convert.ToInt32(timeKey.Substring(0, 4));
                        month = Convert.ToInt32(timeKey.Substring(4, 2));
                        day = Convert.ToInt32(timeKey.Substring(6, 2));
                        break;
                    case "月线":
                        year = Convert.ToInt32(timeKey.Substring(0, 4));
                        month = Convert.ToInt32(timeKey.Substring(4, 2));
                        break;
                }
                DateTime dt = new DateTime(year, month, day, hour, minute, 0);
                this.chartGraph1.SetValue("OPEN", records[1], dt);
                this.chartGraph1.SetValue("HIGH", records[2], dt);
                this.chartGraph1.SetValue("LOW", records[3], dt);
                this.chartGraph1.SetValue("CLOSE", records[4], dt);
                this.chartGraph1.SetValue("VOL", records[6], dt);
                double ymValue = (Convert.ToDouble(records[4]) Convert.ToDouble(records[3]) Convert.ToDouble(records[2])) / 3;
                this.chartGraph1.SetValue("(CLOSE HIGH LOW)/3", ymValue, dt);
                this.BeginInvoke(new UpdateProcessBarDelegate(UpdateProcessBar), new int[] { list.Count, list.Count - i });
            }
            this.chartGraph1.Enabled = true;
        }

        private void chartGraph1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                using (PeopleAccessClass da = new PeopleAccessClass())
                {
                    string riqi1 = "";
                    string riqi2 = "";
                    if (DateTime.Parse(new AutoOIDManager().GetSystemDateTime().ToShortTimeString().ToString()) < DateTime.Parse("21:00"))
                    {
                        riqi1 = new AutoOIDManager().GetSystemDateTime().AddDays(-1).ToString("yyyy-MM-dd") " 21:00:00";
                        riqi2 = new AutoOIDManager().GetSystemDateTime().ToString("yyyy-MM-dd") " 15:00:00";
                    }
                    else
                    {
                        riqi1 = new AutoOIDManager().GetSystemDateTime().ToString("yyyy-MM-dd") " 21:00:00";
                        riqi2 = new AutoOIDManager().GetSystemDateTime().AddDays(1).ToString("yyyy-MM-dd") " 15:00:00";
                    }
                    DataSet dsd = new DataSet();
                    dsd = da.getshuju(MainForm.gudaima, riqi1, riqi2);  //获得从9:00 开始到 15 dian 看有没有数据。
                    if (dsd.Tables[0].Rows.Count != 0)
                    {
                        FormLine ChildForm = new FormLine();
                        //ChildForm.MdiParent = this;
                        ChildForm.Show();
                    }
                    else
                    {
                        MessageBox.Show("网络连接异常,请检查数据连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }

            if (e.KeyChar == (char)27)
            {
                Close();
            }


        }

        private void chartGraph1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            using (PeopleAccessClass da = new PeopleAccessClass())
            {
                string riqi1 = "";
                string riqi2 = "";
                if (DateTime.Parse(new AutoOIDManager().GetSystemDateTime().ToShortTimeString().ToString()) < DateTime.Parse("21:00"))
                {
                     riqi1 = new AutoOIDManager().GetSystemDateTime().AddDays (-1).ToString("yyyy-MM-dd") " 21:00:00";
                     riqi2 = new AutoOIDManager().GetSystemDateTime().ToString("yyyy-MM-dd") " 15:00:00";
                }
                else
                {
                     riqi1 = new AutoOIDManager().GetSystemDateTime().ToString("yyyy-MM-dd") " 21:00:00";
                     riqi2 = new AutoOIDManager().GetSystemDateTime().AddDays (1).ToString("yyyy-MM-dd") " 15:00:00";
                }
                DataSet dsd = new DataSet();
                dsd = da.getshuju(MainForm.gudaima, riqi1, riqi2);  //获得从9:00 开始到 15 dian 看有没有数据。
                if (dsd.Tables[0].Rows.Count != 0)
                {
                    FormLine ChildForm = new FormLine();
                    //ChildForm.MdiParent = this;
                    ChildForm.Show();
                }
                else
                {
                    MessageBox.Show("网络连接异常,请检查数据连接!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
         
        }

    }
}