using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApplication1 { public partial class CandleChartTest : Form { private int _nDays = 60; public CandleChartTest() { InitializeComponent(); nDaysTextBox.Text = _nDays.ToString(); cbShowLine.Checked = true; } private void CandleChartTest_Load(object sender, EventArgs e) { // Populate series data FillData(); } private void FillData() { Random rand; // Use a number to calculate a starting value for // the pseudo-random number sequence rand = new Random(); // The number of days for stock data int period = _nDays; try { period = Convert.ToInt32(nDaysTextBox.Text); } catch { // bad data entered use default data and reset textbox data period = _nDays; nDaysTextBox.Text = _nDays.ToString(); } // The first High value double high = rand.NextDouble() * 40; // The first Close value double close = high - rand.NextDouble(); // Close can not be less than zero if (close < 0.0) close = 0.0; // The first Low value double low = close - rand.NextDouble(); // Low can not be less than zero if (low < 0.0) low = 0.0; // The first Open value double open = (high - low) * rand.NextDouble() + low; // The first day X and Y values chart1.Series["Price"].Points.AddXY(DateTime.Parse("1/2/2002"), high); chart1.Series["Price"].Points[0].YValues[1] = low; // The Open value is not used. chart1.Series["Price"].Points[0].YValues[2] = open; chart1.Series["Price"].Points[0].YValues[3] = close; // Begin with the line chart chart1.Series["Close"].Points.AddXY(DateTime.Parse("1/2/2002"), close); // Days loop for (int day = 1; day <= period; day++) { // Calculate High, Low and Close values high = chart1.Series["Price"].Points[day - 1].YValues[2] + rand.NextDouble(); close = high - rand.NextDouble(); // Close can not be less than zero if (close < 0.0) close = 0.0; low = close - rand.NextDouble(); // Low can not be less than zero if (low < 0.0) low = 0.0; open = (high - low) * rand.NextDouble() + low; // The low cannot be less than yesterday close value. //if (low > chart1.Series["Price"].Points[day - 1].YValues[2]) // low = chart1.Series["Price"].Points[day - 1].YValues[2]; // Set data points values chart1.Series["Price"].Points.AddXY(day, high); chart1.Series["Price"].Points[day].XValue = chart1.Series["Price"].Points[day - 1].XValue + 1; chart1.Series["Price"].Points[day].YValues[1] = low; chart1.Series["Price"].Points[day].YValues[2] = open; chart1.Series["Price"].Points[day].YValues[3] = close; // Set data point for line chart chart1.Series["Close"].Points.AddXY(day, close); chart1.Series["Close"].Points[day].XValue = chart1.Series["Close"].Points[day - 1].XValue + 1; } } private void button1_Click(object sender, EventArgs e) { // Clear the populate series data chart1.Series["Price"].Points.Clear(); chart1.Series["Close"].Points.Clear(); FillData(); } private void cbShowLine_CheckedChanged(object sender, EventArgs e) { if (cbShowLine.Checked) { chart1.Series["Close"].Enabled = true; } else { chart1.Series["Close"].Enabled = false; } } private void button2_Click(object sender, EventArgs e) { (new InteractiveChart()).Show(); } } }