how to connecting my data from the msde sever with the database created in msde
here is my codes
it works with random numbers, but i need to to connect from the sql server
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using ChartDirector;
namespace wen_finance
{
///
/// A utility class for adding items to ComboBox
///
class ListItem
{
string m_key;
string m_value;
public ListItem(string key, string val)
{
m_key = key;
m_value = val;
}
public string Key
{
get { return m_key; }
}
public override string ToString()
{
return m_value;
}
}
public class Wenchen : System.Windows.Forms.Form
{
DataSet ds;
private ChartDirector.WinChartViewer winChartViewer1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.ComboBox TimeRange;
private System.Windows.Forms.ComboBox ChartSize;
private System.Windows.Forms.ComboBox ChartType;
private System.Windows.Forms.ComboBox Band;
private System.Windows.Forms.ComboBox AvgType1;
private System.Windows.Forms.ComboBox AvgType2;
private System.Windows.Forms.ComboBox Indicator1;
private System.Windows.Forms.ComboBox Indicator2;
private System.Windows.Forms.ComboBox Indicator3;
private System.Windows.Forms.ComboBox Indicator4;
private System.Windows.Forms.TextBox MovAvg1;
private System.Windows.Forms.TextBox MovAvg2;
private System.Windows.Forms.CheckBox MinorVGrid;
private System.Windows.Forms.CheckBox HGrid;
private System.Windows.Forms.CheckBox MajorVGrid;
private System.Windows.Forms.CheckBox Volume;
private System.Windows.Forms.CheckBox LogScale;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
//Will set to true at the end of initialization
private bool hasFinishedInitialization;
//Start date of the data points (excluding extra leading points)
private DateTime startDate;
//End date of the data points.
private DateTime endDate;
//The resolution of the data points (in sec)
private int resolution;
//The timeStamps of the data points. It can include data points that are before the
//startDate (extra leading points) to facilitate moving averages computation.
private DateTime[] timeStamps;
private double[] volData; //The volume values.
private double[] highData; //The high values.
private double[] lowData; //The low values.
private double[] openData; //The open values.
private double[] closeData; //The close values.
public Wenchen()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
private void Wenchen_Load(object sender, System.EventArgs e)
{
TimeRange.Items.Add(new ListItem("1", "1 day"));
TimeRange.Items.Add(new ListItem("3", "3 days"));
TimeRange.Items.Add(new ListItem("5", "5 days"));
TimeRange.Items.Add(new ListItem("10", "10 days"));
TimeRange.Items.Add(new ListItem("30", "1 month"));
TimeRange.Items.Add(new ListItem("60", "2 months"));
TimeRange.Items.Add(new ListItem("90", "3 months"));
TimeRange.SelectedIndex =TimeRange.Items.Add(new ListItem("1", "1 day"));
TimeRange.Items.Add(new ListItem("360", "1 year"));
TimeRange.Items.Add(new ListItem("720", "2 years"));
TimeRange.Items.Add(new ListItem("1080", "3 years"));
TimeRange.Items.Add(new ListItem("1440", "4 years"));
TimeRange.Items.Add(new ListItem("1800", "5 years"));
TimeRange.Items.Add(new ListItem("3600", "10 years"));
ChartSize.Items.Add(new ListItem("S", "Small"));
ChartSize.Items.Add(new ListItem("M", "Medium"));
ChartSize.SelectedIndex = ChartSize.Items.Add(new ListItem("R", "Regular"));
ChartSize.Items.Add(new ListItem("L", "Large"));
ChartType.Items.Add(new ListItem("None", "None"));
ChartType.SelectedIndex = ChartType.Items.Add(new ListItem("CandleStick", "CandleStick"));
ChartType.Items.Add(new ListItem("Close", "Closing Price"));
ChartType.Items.Add(new ListItem("Median", "Median Price"));
ChartType.Items.Add(new ListItem("OHLC", "OHLC"));
ChartType.Items.Add(new ListItem("TP", "Typical Price"));
ChartType.Items.Add(new ListItem("WC", "Weighted Close"));
Band.Items.Add(new ListItem("None", "None"));
Band.SelectedIndex = Band.Items.Add(new ListItem("BB", "Bollinger Band"));
Band.Items.Add(new ListItem("DC", "Donchain Channel"));
Band.Items.Add(new ListItem("Envelop", "Envelop (SMA 20 +/- 10%)"));
AvgType1.Items.Add(new ListItem("None", "None"));
AvgType1.SelectedIndex = AvgType1.Items.Add(new ListItem("SMA", "Simple"));
AvgType1.Items.Add(new ListItem("EMA", "Exponential"));
AvgType1.Items.Add(new ListItem("TMA", "Triangular"));
AvgType1.Items.Add(new ListItem("WMA", "Weighted"));
AvgType2.Items.Add(new ListItem("None", "None"));
AvgType2.SelectedIndex = AvgType2.Items.Add(new ListItem("SMA", "Simple"));
AvgType2.Items.Add(new ListItem("EMA", "Exponential"));
AvgType2.Items.Add(new ListItem("TMA", "Triangular"));
AvgType2.Items.Add(new ListItem("WMA", "Weighted"));
ListItem[] indicators =
{
new ListItem("None", "None"),
new ListItem("AccDist", "Accumulation/Distribution"),
new ListItem("AroonOsc", "Aroon Oscillator"),
new ListItem("Aroon", "Aroon Up/Down"),
new ListItem("ADX", "Avg Directional Index"),
new ListItem("ATR", "Avg True Range"),
new ListItem("BBW", "Bollinger Band Width"),
new ListItem("CMF", "Chaikin Money Flow"),
new ListItem("COscillator", "Chaikin Oscillator"),
new ListItem("CVolatility", "Chaikin Volatility"),
new ListItem("CLV", "Close Location Value"),
new ListItem("CCI", "Commodity Channel Index"),
new ListItem("DPO", "Detrended Price Osc"),
new ListItem("DCW", "Donchian Channel Width"),
new ListItem("EMV", "Ease of Movement"),
new ListItem("FStoch", "Fast Stochastic"),
new ListItem("MACD", "MACD"),
new ListItem("MDX", "Mass Index"),
new ListItem("Momentum", "Momentum"),
new ListItem("MFI", "Money Flow Index"),
new ListItem("NVI", "Neg Volume Index"),
new ListItem("OBV", "On Balance Volume"),
new ListItem("Performance", "Performance"),
new ListItem("PPO", "% Price Oscillator"),
new ListItem("PVO", "% Volume Oscillator"),
new ListItem("PVI", "Pos Volume Index"),
new ListItem("PVT", "Price Volume Trend"),
new ListItem("ROC", "Rate of Change"),
new ListItem("RSI", "RSI"),
new ListItem("SStoch", "Slow Stochastic"),
new ListItem("StochRSI", "StochRSI"),
new ListItem("TRIX", "TRIX"),
new ListItem("UO", "Ultimate Oscillator"),
new ListItem("Vol", "Volume"),
new ListItem("WilliamR", "William's %R")
};
Indicator1.Items.AddRange(indicators);
Indicator2.Items.AddRange(indicators);
Indicator3.Items.AddRange(indicators);
Indicator4.Items.AddRange(indicators);
for (int i = 0; i < indicators.Length; ++i)
{
if (indicators[i].Key == "RSI")
Indicator1.SelectedIndex = i;
else if (indicators[i].Key == "MACD")
Indicator2.SelectedIndex = i;
}
Indicator3.SelectedIndex = 0;
Indicator4.SelectedIndex = 0;
hasFinishedInitialization = true;
updateChart(winChartViewer1);
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
///
///
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.MovAvg2 = new System.Windows.Forms.TextBox();
this.AvgType2 = new System.Windows.Forms.ComboBox();
this.MovAvg1 = new System.Windows.Forms.TextBox();
this.AvgType1 = new System.Windows.Forms.ComboBox();
this.Indicator4 = new System.Windows.Forms.ComboBox();
this.Indicator3 = new System.Windows.Forms.ComboBox();
this.Indicator2 = new System.Windows.Forms.ComboBox();
this.Indicator1 = new System.Windows.Forms.ComboBox();
this.Band = new System.Windows.Forms.ComboBox();
this.ChartType = new System.Windows.Forms.ComboBox();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.MinorVGrid = new System.Windows.Forms.CheckBox();
this.HGrid = new System.Windows.Forms.CheckBox();
this.MajorVGrid = new System.Windows.Forms.CheckBox();
this.LogScale = new System.Windows.Forms.CheckBox();
this.Volume = new System.Windows.Forms.CheckBox();
this.ChartSize = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
this.TimeRange = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.winChartViewer1 = new ChartDirector.WinChartViewer();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(186)), ((System.Byte)(221)), ((System.Byte)(255)));
this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.MovAvg2,
this.AvgType2,
this.MovAvg1,
this.AvgType1,
this.Indicator4,
this.Indicator3,
this.Indicator2,
this.Indicator1,
this.Band,
this.ChartType,
this.label7,
this.label6,
this.label4,
this.label5,
this.MinorVGrid,
this.HGrid,
this.MajorVGrid,
this.LogScale,
this.Volume,
this.ChartSize,
this.label3,
this.TimeRange,
this.label2});
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(0, 24);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(160, 510);
this.panel1.TabIndex = 1;
//
// MovAvg2
//
this.MovAvg2.Location = new System.Drawing.Point(96, 360);
this.MovAvg2.Name = "MovAvg2";
this.MovAvg2.Size = new System.Drawing.Size(40, 20);
this.MovAvg2.TabIndex = 13;
this.MovAvg2.Text = "25";
this.MovAvg2.TextChanged += new System.EventHandler(this.SelectionChanged);
//
// AvgType2
//
this.AvgType2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.AvgType2.Location = new System.Drawing.Point(8, 360);
this.AvgType2.Name = "AvgType2";
this.AvgType2.Size = new System.Drawing.Size(88, 22);
this.AvgType2.TabIndex = 12;
this.AvgType2.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// MovAvg1
//
this.MovAvg1.Location = new System.Drawing.Point(96, 336);
this.MovAvg1.Name = "MovAvg1";
this.MovAvg1.Size = new System.Drawing.Size(40, 20);
this.MovAvg1.TabIndex = 11;
this.MovAvg1.Text = "10";
this.MovAvg1.TextChanged += new System.EventHandler(this.SelectionChanged);
//
// AvgType1
//
this.AvgType1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.AvgType1.Location = new System.Drawing.Point(8, 336);
this.AvgType1.Name = "AvgType1";
this.AvgType1.Size = new System.Drawing.Size(88, 22);
this.AvgType1.TabIndex = 10;
this.AvgType1.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// Indicator4
//
this.Indicator4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.Indicator4.Location = new System.Drawing.Point(8, 480);
this.Indicator4.Name = "Indicator4";
this.Indicator4.Size = new System.Drawing.Size(130, 22);
this.Indicator4.TabIndex = 17;
this.Indicator4.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// Indicator3
//
this.Indicator3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.Indicator3.Location = new System.Drawing.Point(8, 456);
this.Indicator3.Name = "Indicator3";
this.Indicator3.Size = new System.Drawing.Size(130, 22);
this.Indicator3.TabIndex = 16;
this.Indicator3.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// Indicator2
//
this.Indicator2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.Indicator2.Location = new System.Drawing.Point(8, 432);
this.Indicator2.Name = "Indicator2";
this.Indicator2.Size = new System.Drawing.Size(130, 22);
this.Indicator2.TabIndex = 15;
this.Indicator2.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// Indicator1
//
this.Indicator1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.Indicator1.Location = new System.Drawing.Point(8, 408);
this.Indicator1.Name = "Indicator1";
this.Indicator1.Size = new System.Drawing.Size(130, 22);
this.Indicator1.TabIndex = 14;
this.Indicator1.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// Band
//
this.Band.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.Band.Location = new System.Drawing.Point(8, 288);
this.Band.Name = "Band";
this.Band.Size = new System.Drawing.Size(130, 22);
this.Band.TabIndex = 9;
this.Band.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// ChartType
//
this.ChartType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ChartType.Location = new System.Drawing.Point(8, 240);
this.ChartType.Name = "ChartType";
this.ChartType.Size = new System.Drawing.Size(130, 22);
this.ChartType.TabIndex = 8;
this.ChartType.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// label7
//
this.label7.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label7.Location = new System.Drawing.Point(8, 392);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(128, 16);
this.label7.TabIndex = 13;
this.label7.Text = "Technical Indicators";
//
// label6
//
this.label6.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label6.Location = new System.Drawing.Point(8, 320);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(100, 16);
this.label6.TabIndex = 12;
this.label6.Text = "Moving Averages";
//
// label4
//
this.label4.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label4.Location = new System.Drawing.Point(8, 272);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(100, 16);
this.label4.TabIndex = 11;
this.label4.Text = "Price Band";
//
// label5
//
this.label5.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label5.Location = new System.Drawing.Point(8, 224);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(100, 16);
this.label5.TabIndex = 10;
this.label5.Text = "Chart Type";
//
// MinorVGrid
//
this.MinorVGrid.Checked = true;
this.MinorVGrid.CheckState = System.Windows.Forms.CheckState.Checked;
this.MinorVGrid.Location = new System.Drawing.Point(8, 168);
this.MinorVGrid.Name = "MinorVGrid";
this.MinorVGrid.Size = new System.Drawing.Size(144, 24);
this.MinorVGrid.TabIndex = 6;
this.MinorVGrid.Text = "Minor Vertical Grid";
this.MinorVGrid.CheckedChanged += new System.EventHandler(this.SelectionChanged);
//
// HGrid
//
this.HGrid.Checked = true;
this.HGrid.CheckState = System.Windows.Forms.CheckState.Checked;
this.HGrid.Location = new System.Drawing.Point(8, 192);
this.HGrid.Name = "HGrid";
this.HGrid.Size = new System.Drawing.Size(144, 24);
this.HGrid.TabIndex = 7;
this.HGrid.Text = "Horizontal Grid";
this.HGrid.CheckedChanged += new System.EventHandler(this.SelectionChanged);
//
// MajorVGrid
//
this.MajorVGrid.Checked = true;
this.MajorVGrid.CheckState = System.Windows.Forms.CheckState.Checked;
this.MajorVGrid.Location = new System.Drawing.Point(8, 144);
this.MajorVGrid.Name = "MajorVGrid";
this.MajorVGrid.Size = new System.Drawing.Size(144, 24);
this.MajorVGrid.TabIndex = 5;
this.MajorVGrid.Text = "Major Vertical Grid";
this.MajorVGrid.CheckedChanged += new System.EventHandler(this.SelectionChanged);
//
// LogScale
//
this.LogScale.Location = new System.Drawing.Point(8, 120);
this.LogScale.Name = "LogScale";
this.LogScale.Size = new System.Drawing.Size(144, 24);
this.LogScale.TabIndex = 4;
this.LogScale.Text = "Log Scale";
this.LogScale.CheckedChanged += new System.EventHandler(this.SelectionChanged);
//
// Volume
//
this.Volume.Checked = true;
this.Volume.CheckState = System.Windows.Forms.CheckState.Checked;
this.Volume.Location = new System.Drawing.Point(8, 96);
this.Volume.Name = "Volume";
this.Volume.Size = new System.Drawing.Size(144, 24);
this.Volume.TabIndex = 3;
this.Volume.Text = "Show Volume Bars";
this.Volume.CheckedChanged += new System.EventHandler(this.SelectionChanged);
//
// ChartSize
//
this.ChartSize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ChartSize.Location = new System.Drawing.Point(8, 72);
this.ChartSize.Name = "ChartSize";
this.ChartSize.Size = new System.Drawing.Size(130, 22);
this.ChartSize.TabIndex = 2;
this.ChartSize.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// label3
//
this.label3.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.Location = new System.Drawing.Point(8, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Chart Size";
//
// TimeRange
//
this.TimeRange.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.TimeRange.Location = new System.Drawing.Point(8, 24);
this.TimeRange.MaxDropDownItems = 20;
this.TimeRange.Name = "TimeRange";
this.TimeRange.Size = new System.Drawing.Size(130, 22);
this.TimeRange.TabIndex = 1;
this.TimeRange.SelectedIndexChanged += new System.EventHandler(this.SelectionChanged);
//
// label2
//
this.label2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(8, 8);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 0;
this.label2.Text = "Time Period";
//
// label1
//
this.label1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(136)));
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Font = new System.Drawing.Font("Arial", 9.75F, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.Color.Yellow;
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(944, 24);
this.label1.TabIndex = 2;
this.label1.Text = "Welcome to my Presentation";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// panel2
//
this.panel2.AutoScroll = true;
this.panel2.BackColor = System.Drawing.Color.White;
this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.winChartViewer1});
this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel2.Location = new System.Drawing.Point(160, 24);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(784, 510);
this.panel2.TabIndex = 3;
//
// winChartViewer1
//
this.winChartViewer1.Location = new System.Drawing.Point(0, 8);
this.winChartViewer1.Name = "winChartViewer1";
this.winChartViewer1.Size = new System.Drawing.Size(780, 496);
this.winChartViewer1.TabIndex = 1;
this.winChartViewer1.TabStop = false;
//
// wenchen program
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(944, 534);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel2,
this.panel1,
this.label1});
this.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "Neng Wen Chen";
this.Text = "Neng Wen Chen's ChartDirector Financial Chart Demonstration!Good Luck To Me:)";
this.Load += new System.EventHandler(this.Wenchen_Load);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Wenchen());
}
///
/// Get the requested data from the data source, and store the data in the member
/// array variables timeStamps, volData, highData, lowData, openData, closeData.
/// Also set the member variables startDate, endDate and resolution to reflect the
/// actual data points retrieved.
///
///
The duration in days as requested by the user.
///
The number of extra leading points requested.
///
void ConnectToData()
{
// Create the ConnectionString and create a SqlConnection.
// Change the data source value to the name of your computer.
string cString = "Persist Security Info=False;Integrated Security=SSPI;database=hourdata;server=VALUED-26C5C9DD\\INSTANCENAME";
SqlConnection myConnection = new SqlConnection(cString);
// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.TableMappings.Add("Table", "hourdata");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM hourdata",
myConnection);
myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand;
Console.WriteLine("The connection is open");
ds = new DataSet("hourdata");
myAdapter.Fill(ds);
}
private void getData(int requestedDuration, int requestedExtraPoints)
{
//
// In this program, we just assume we plot up to the latest time. So endDate is now.
endDate = DateTime.Now;
// However, if the trading day has not yet started (before 9:30am), we plot
// up to 4:00pm of the previous trading day.
if (endDate.TimeOfDay < new TimeSpan(9, 30, 0))
endDate = endDate.Date.AddDays(-1).Add(new TimeSpan(16, 0, 0));
// Remember our simulated stock change does not work on Saturday or Sunday
if (endDate.DayOfWeek == DayOfWeek.Sunday)
endDate = endDate.AddDays(-2);
else if (endDate.DayOfWeek == DayOfWeek.Saturday)
endDate = endDate.AddDays(-1);
//
// After we obtain the endDate, we get the startDate based on the user selected
// duration. It is OK to obtain more data than requested.
//
if (requestedDuration >= 30)
{
// More or equal to 30 days - so we use months as the unit
startDate = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(
-requestedDuration / 30);
}
else
{
// Less than 30 days - use day as the unit. Note that we use trading days
// below. For less than 30 days, the starting point of the axis is always at
// the start of the day.
startDate = endDate.Date.Add(new TimeSpan(9, 30, 0));
for (int i = 1; i < requestedDuration; ++i)
startDate = startDate.AddDays((startDate.DayOfWeek == DayOfWeek.Monday) ? -3 : -1);
}
//
// At this point, we have the startDate and endDate, so we can generate the
// simulated data. We generate the timeStamps first, then we fill the other
// arrays with random data.
//
ArrayList timeStampsBuffer = new ArrayList();
//
// In this program, the data resolution (that is, daily points or hourly
// points etc) is automatically determined based on the duration.
//
if (requestedDuration >= 4 * 360)
{
//4 years or more - use monthly points. Note that we need to add extra points
//by shifting the starting month backwards
resolution = 30 * 86400;
for (DateTime currentDate = startDate.AddMonths(-requestedExtraPoints);
currentDate <= endDate; currentDate = currentDate.AddMonths(1))
timeStampsBuffer.Add(currentDate);
}
else if (requestedDuration >= 360)
{
//1 year or more - use weekly points.
resolution = 7 * 86400;
//always start on Monday
while (startDate.DayOfWeek != DayOfWeek.Monday)
startDate = startDate.AddDays(-1);
//Note that we need to add extra points by shifting the starting weeks backwards
for (DateTime currentDate = startDate.AddDays(-requestedExtraPoints * 7);
currentDate <= endDate; currentDate = currentDate.AddDays(7))
timeStampsBuffer.Add(currentDate);
}
else if (requestedDuration >= 30)
{
//1 month or more - user daily points. Note that we need to add extra points
//by shifting the starting day backwards. Also, we need to multiple by 7/5
//because one week only have 5 trading days (5 points per 7 calender day).
resolution = 86400;
DateTime currentDate = startDate.Date.AddDays(- (requestedExtraPoints * 7 + 4) / 5);
//Remember to skip non-trading days
if (currentDate.DayOfWeek == DayOfWeek.Saturday)
currentDate.AddDays(-2);
else if (currentDate.DayOfWeek == DayOfWeek.Sunday)
currentDate = currentDate.AddDays(-1);
//Now add one point per day
while (currentDate <= endDate)
{
timeStampsBuffer.Add(currentDate);
currentDate = currentDate.AddDays(currentDate.DayOfWeek == DayOfWeek.Friday ? 3 : 1);
}
}
else
{
//Less than 30 days duration. In this program, if it is less than 30 days,
//the only choices are 10 days or less. So a 15-minute chart is a proper resolution.
resolution = 900;
//We need to adjust the startDate for the requestedExtraPoints by shifting the time
//backwards. We need to determine how many days to shift backwards. We assume each
//day has 6.5 hours of trading time (9:30am - 16:00pm)
double dataPointsPerDay = 6.5 * 3600 / resolution;
DateTime currentDate = startDate.Date.AddDays(
- Math.Ceiling(requestedExtraPoints / dataPointsPerDay * 7 / 5));
//Remember to skip non-trading days
if (currentDate.DayOfWeek == DayOfWeek.Saturday)
currentDate.AddDays(-2);
else if (currentDate.DayOfWeek == DayOfWeek.Sunday)
currentDate = currentDate.AddDays(-1);
//Now generate the timestamps for 9:30am to 16:00pm for trading days.
while (currentDate <= endDate)
{
timeStampsBuffer.Add(currentDate);
currentDate = currentDate.AddSeconds(resolution);
if (currentDate.TimeOfDay > new TimeSpan(16, 0, 0))
{
currentDate = currentDate.Date.Add(new TimeSpan(1, 9, 30, 0));
if (currentDate.DayOfWeek == DayOfWeek.Saturday)
currentDate = currentDate.AddDays(2);
}
}
}
//
// Now we get the simulated timestamps
//
timeStamps = (DateTime[])timeStampsBuffer.ToArray(typeof(DateTime));
***instead random, how can i use fixed data ??? // We fill in the rest with random data. We use a ChartDirector utility RanTable
// that can generate realistic HLOC data table.
dataTable datatable = new dataTable(9, 6, timeStamps.Length);
// Generate HLOC data in columns 0 to 3 of the table. Starting point is 100, and
// the maximum change between two adjacent points is 5.
datatable.setHLOCCols(0, 100, -5, 5);
// Generate random number between 500000 and 2500000 to act as volume data
rantable.setCol(4, 500000, 2500000);
// Get the data into arrays
highData = rantable.getCol(0);
lowData = rantable.getCol(1);
openData = rantable.getCol(2);
closeData = rantable.getCol(3);
volData = rantable.getCol(4);
// Currently, the Rantable generate numbers in arbitrary precision. In real life,
// the trading prices always jump in steps and with limited decimal points. To
// make the data look real, we round the data to limited precision.
for (int i = 0; i < highData.Length; ++i)
{
highData[i] = Math.Round(highData[i], 1);
lowData[i] = Math.Round(lowData[i], 1);
openData[i] = Math.Round(openData[i], 1);
closeData[i] = Math.Round(closeData[i], 1);
volData[i] = Math.Round(volData[i], 0);
}
}
///
/// Draw the chart according to user selection and display it in the WebChartViewer.
///
///
The WebChartViewer object to display the chart.
private void updateChart(WinChartViewer viewer)
{
// Use InvariantCulture to draw the chart. This ensures the fonts (mostly Arial)
// we use in this program code will work on any computer. Otherwise, if an OS uses
// special characters (eg. double-byte characters) not supported by Arial, some
// labels (eg. month labels) may not be displayed correctly.
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
//
// To draw a chart, we need data first. To retrieve data, we need the chart
// duration and the moving average period. The latter is needed because we need
// extra leading data points to compute moving averages.
//
// The duration selected by the user
int durationInDays = int.Parse(((ListItem)TimeRange.SelectedItem).Key);
// The first moving average period selected by the user.
int avgPeriod1;
try { avgPeriod1 = int.Parse(MovAvg1.Text); }
catch { avgPeriod1 = 0;}
if (avgPeriod1 < 0)
avgPeriod1 = 0;
if (avgPeriod1 > 300)
avgPeriod1 = 300;
// The second moving average period selected by the user.
int avgPeriod2;
try { avgPeriod2 = int.Parse(MovAvg2.Text); }
catch { avgPeriod2 = 0;}
if (avgPeriod2 < 0)
avgPeriod2 = 0;
if (avgPeriod2 > 300)
avgPeriod2 = 300;
// We need extra leading data points in order to compute moving averages.
int extraPoints = Math.Max(avgPeriod1, avgPeriod2);
// Get the data from the data source. This method should get the data into the
// member variables timeStamps, volData, highData, lowData, openData, closeData as
// well as providing the data range description startDate, endDate and resolution.
getData(durationInDays, extraPoints);
//
// Data points that are before the startDate are the leading data points for
// computing moving averages. This may be different from the extraPoints we
// requested (eg. the database may have no such data), so we need to detect
// the actual number of leading points.
//
extraPoints = timeStamps.Length;
for (int i = 0; i < timeStamps.Length; ++i)
{
if (timeStamps[i] >= startDate)
{
extraPoints = i;
break;
}
}
// Check if there is any valid data
if (extraPoints >= timeStamps.Length)
{
// No data - just display the no data message.
MultiChart errMsg = new MultiChart(400, 50);
errMsg.addTitle2(Chart.TopLeft, "No data available for the specified time period", "arial.ttf", 10);
viewer.Image = errMsg.makeImage();
return;
}
// In some finance chart presentation style, even if the data for the latest day
// is not fully available, the axis for the entire day will still be drawn, where
// no data will appear near the end of the axis.
if (resolution <= 86400)
{
// Add extra points to the axis until it reaches the end of the day. The end
// of day is assumed to be 4:00pm (it depends on the stock exchange).
DateTime lastTime = timeStamps[timeStamps.Length - 1];
int extraTrailingPoints = (int)((new TimeSpan(16, 0, 0) - lastTime.TimeOfDay).TotalSeconds / resolution);
if (extraTrailingPoints > 0)
{
DateTime[] extendedTimeStamps = new DateTime[timeStamps.Length + extraTrailingPoints];
Array.Copy(timeStamps, 0, extendedTimeStamps, 0, timeStamps.Length);
for (int i = 0; i < extraTrailingPoints; ++i)
extendedTimeStamps[i + timeStamps.Length] = lastTime.AddSeconds(resolution * i);
timeStamps = extendedTimeStamps;
}
}
//
// At this stage, all data is available. We can draw the chart as according to
// user input.
//
//
// Determine the Chart View. In this , user can select 4 different Chart Views.
// is the regular Chart View.
//
int width = 780; // width of chart
int mainHeight = 250; // height of main chart
int indicatorHeight = 80; // height of indicator chart
string selectedSize = ((ListItem)ChartSize.SelectedItem).Key;
if (selectedSize == "S")
{
// Small Chart View
width = 450;
mainHeight = 160;
indicatorHeight = 60;
}
else if (selectedSize == "M")
{
// Medium Chart View
width = 620;
mainHeight = 210;
indicatorHeight = 65;
}
else if (selectedSize == "L")
{
// Large Chart View
width = 1000;
mainHeight = 320;
indicatorHeight = 90;
}
// Create the chart object using the selected size
FinanceChart m = new FinanceChart(width);
// Enable using tool tips for this chart
m.enableToolTips(true, null);
// Set the data into the chart object
m.setData(timeStamps, highData, lowData, openData, closeData, volData, extraPoints);
//
// We configure the title of the chart. In this program chart design, we put the
// company name as the top line of the title with left alignment.
//
m.addPlotAreaTitle(Chart.TopLeft, "Thesis Project");
// We displays the current date as well as the data resolution on the next line.
string resolutionText = "";
if (resolution == 30 * 86400)
resolutionText = "Monthly";
else if (resolution == 7 * 86400)
resolutionText = "Weekly";
else if (resolution == 86400)
resolutionText = "Daily";
else if (resolution == 900)
resolutionText = "15-min";
m.addPlotAreaTitle(Chart.BottomLeft, "<*font=arial.ttf,size=8*>" + DateTime.Now.ToString("g")
+ " - " + resolutionText + " chart");
// A copyright message at the bottom left corner the title area
m.addPlotAreaTitle(Chart.BottomRight, "<*font=arial.ttf,size=8*>(c) NengWen Chen");
//
// Set the grid style according to user preference. In this simple program user
// interface, the user can enable/disable grid lines. The grid line colors are
// hard coded to 0xdddddd (light grey), and the plot area background color is
// hard coded to 0xffffe0 (pale yellow).
//
int majorVGridColor = MajorVGrid.Checked ? 0xdddddd : Chart.Transparent;
int minorVGridColor = MinorVGrid.Checked ? 0xdddddd : Chart.Transparent;
int hGridColor = HGrid.Checked ? 0xdddddd : Chart.Transparent;
m.setPlotAreaStyle(0xffffe0, hGridColor, majorVGridColor, hGridColor, minorVGridColor);
//
// Set log or linear scale according to user preference
//
m.setLogScale(LogScale.Checked);
//
// Add the first techical indicator according. In this program, we draw the first
// indicator on top of the main chart.
//
addIndicator(m, ((ListItem)Indicator1.SelectedItem).Key, indicatorHeight);
// Add the main chart
m.addMainChart(mainHeight);
//
// Draw the main chart depending on the chart type the user has selected
//
string selectedType = ((ListItem)ChartType.SelectedItem).Key;
if (selectedType == "Close")
m.addCloseLine(0x40);
else if (selectedType == "TP")
m.addTypicalPrice(0x40);
else if (selectedType == "WC")
m.addWeightedClose(0x40);
else if (selectedType == "Median")
m.addMedianPrice(0x40);
//
// Add moving average lines.
//
addMovingAvg(m, ((ListItem)AvgType1.SelectedItem).Key, avgPeriod1, 0x663300);
addMovingAvg(m, ((ListItem)AvgType2.SelectedItem).Key, avgPeriod2, 0x9900ff);
//
// Draw the main chart if the user has selected CandleStick or OHLC. We
// draw it here to make sure it is drawn behind the moving average lines
// (that is, the moving average lines stay on top.)
//
if (selectedType == "CandleStick")
m.addCandleStick(0x33ff33, 0xff3333);
else if (selectedType == "OHLC")
m.addHLOC(0x80);
//
// Add price band/channel/envelop to the chart according to user selection
//
string selectedBand = ((ListItem)Band.SelectedItem).Key;
if (selectedBand == "BB")
m.addBollingerBand(20, 2, 0x9999ff, unchecked((int)(0xc06666ff)));
else if (selectedBand == "DC")
m.addDonchianChannel(20, 0x9999ff, unchecked((int)(0xc06666ff)));
else if (selectedBand == "Envelop")
m.addEnvelop(20, 0.1, 0x9999ff, unchecked((int)(0xc06666ff)));
//
// Add volume bars to the main chart if necessary
//
if (Volume.Checked)
m.addVolBars(indicatorHeight, 0x99ff99, 0xff9999, 0xc0c0c0);
//
// Add additional indicators as according to user selection.
//
addIndicator(m, ((ListItem)Indicator2.SelectedItem).Key, indicatorHeight);
addIndicator(m, ((ListItem)Indicator3.SelectedItem).Key, indicatorHeight);
addIndicator(m, ((ListItem)Indicator4.SelectedItem).Key, indicatorHeight);
//
// layout and output the chart
//
m.layoutChart();
viewer.Image = m.makeImage();
viewer.ImageMap = m.getHTMLImageMap("", "", "title='{x2Label}: {value|2}'");
}
///
/// Add a moving average line to the FinanceChart object.
///
///
The FinanceChart object to add the line to.
///
The moving average type (SMA/EMA/TMA/WMA).
///
The moving average period.
///
The color of the line.
protected void addMovingAvg(FinanceChart m, string avgType, int avgPeriod, int color)
{
if (avgPeriod > 1)
{
if (avgType == "SMA")
m.addSimpleMovingAvg(avgPeriod, color);
else if (avgType == "EMA")
m.addExpMovingAvg(avgPeriod, color);
else if (avgType == "TMA")
m.addTriMovingAvg(avgPeriod, color);
else if (avgType == "WMA")
m.addWeightedMovingAvg(avgPeriod, color);
}
}
///
/// Add an indicator chart to the FinanceChart object. In this program, the indicator
/// parameters (such as the period used to compute RSI, colors of the lines, etc.) are hard
/// coded to commonly used values.
///
///
The FinanceChart object to add the line to.
///
The selected indicator.
///
Height of the chart in pixels
protected void addIndicator(FinanceChart m, string indicator, int height)
{
if (indicator == "RSI")
m.addRSI(height, 14, 0x800080, 20, 0xff6666, 0x6666ff);
else if (indicator == "StochRSI")
m.addStochRSI(height, 14, 0x800080, 30, 0xff6666, 0x6666ff);
else if (indicator == "MACD")
m.addMACD(height, 26, 12, 9, 0xff, 0xff00ff, 0x8000);
else if (indicator == "FStoch")
m.addFastStochastic(height, 14, 3, 0x6060, 0x606000);
else if (indicator == "SStoch")
m.addSlowStochastic(height, 14, 3, 0x6060, 0x606000);
else if (indicator == "ATR")
m.addATR(height, 14, 0x808080, 0xff);
else if (indicator == "ADX")
m.addADX(height, 14, 0x8000, 0x800000, 0x80);
else if (indicator == "DCW")
m.addDonchianWidth(height, 20, 0xff);
else if (indicator == "BBW")
m.addBollingerWidth(height, 20, 2, 0xff);
else if (indicator == "DPO")
m.addDPO(height, 20, 0xff);
else if (indicator == "PVT")
m.addPVT(height, 0xff);
else if (indicator == "Momentum")
m.addMomentum(height, 12, 0xff);
else if (indicator == "Performance")
m.addPerformance(height, 0xff);
else if (indicator == "ROC")
m.addROC(height, 12, 0xff);
else if (indicator == "OBV")
m.addOBV(height, 0xff);
else if (indicator == "AccDist")
m.addAccDist(height, 0xff);
else if (indicator == "CLV")
m.addCLV(height, 0xff);
else if (indicator == "WilliamR")
m.addWilliamR(height, 14, 0x800080, 30, 0xff6666, 0x6666ff);
else if (indicator == "Aroon")
m.addAroon(height, 14, 0x339933, 0x333399);
else if (indicator == "AroonOsc")
m.addAroonOsc(height, 14, 0xff);
else if (indicator == "CCI")
m.addCCI(height, 20, 0x800080, 100, 0xff6666, 0x6666ff);
else if (indicator == "EMV")
m.addEaseOfMovement(height, 9, 0x6060, 0x606000);
else if (indicator == "MDX")
m.addMassIndex(height, 0x800080, 0xff6666, 0x6666ff);
else if (indicator == "CVolatility")
m.addChaikinVolatility(height, 10, 10, 0xff);
else if (indicator == "COscillator")
m.addChaikinOscillator(height, 0xff);
else if (indicator == "CMF")
m.addChaikinMoneyFlow(height, 21, 0x8000);
else if (indicator == "NVI")
m.addNVI(height, 255, 0xff, 0x883333);
else if (indicator == "PVI")
m.addPVI(height, 255, 0xff, 0x883333);
else if (indicator == "MFI")
m.addMFI(height, 14, 0x800080, 30, 0xff6666, 0x6666ff);
else if (indicator == "PVO")
m.addPVO(height, 26, 12, 9, 0xff, 0xff00ff, 0x8000);
else if (indicator == "PPO")
m.addPPO(height, 26, 12, 9, 0xff, 0xff00ff, 0x8000);
else if (indicator == "UO")
m.addUltimateOscillator(height, 7, 14, 28, 0x800080, 20, 0xff6666, 0x6666ff);
else if (indicator == "Vol")
m.addVolIndicator(height, 0x99ff99, 0xff9999, 0xc0c0c0);
else if (indicator == "TRIX")
m.addTRIX(height, 12, 0xff);
}
///
/// In this program, the chart updates when the user selection changes. We may
/// modify the code to update the data and chart periodically for real time charts.
///
///
///
private void SelectionChanged(object sender, System.EventArgs e)
{
if (hasFinishedInitialization)
updateChart(winChartViewer1);
}
}
}