.NET Framework Bookmark and Share   
 index > Chart Controls for .NET Framework > Financial Formula
 

Financial Formula

Hello,
I wrote a code that should display AccumulationDistribution formula on the chart:
public void Ad()
        {
            DateTime date = DateTime.Now;
            System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection();
            connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
            connection.Open();
            string sql = @"SELECT
    a.[Date], 
    CASE WHEN a.[Date] >= @StartDate THEN a.[Open] END AS [Open], 
    CASE WHEN a.[Date] >= @StartDate THEN a.[Low] END AS [Low], 
    CASE WHEN a.[Date] >= @StartDate THEN a.[Close] END AS [Close], 
    a.[High], 
    a.[Volume]
FROM StockQuotes a
WHERE
    a.Ticker = @Ticker
    AND a.[Date] >= @Chaos
    AND a.[Date] < @EndDate;";
            
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@StartDate", SqlDbType.DateTime);
            cmd.Parameters["@StartDate"].Value = DateTime.Now.Date.AddDays(-100);
            cmd.Parameters.Add("@EndDate", SqlDbType.DateTime);
            cmd.Parameters["@EndDate"].Value = DateTime.Now.Date.AddDays(0);
            cmd.Parameters.Add("@Chaos", SqlDbType.DateTime);
            cmd.Parameters["@Chaos"].Value = DateTime.Now.Date.AddDays(-101);
            
            cmd.Parameters.Add("@Ticker", SqlDbType.VarChar, 20);
            cmd.Parameters["@Ticker"].Value = listBox1.SelectedItem;
            cmd.Parameters.Add("@Date", SqlDbType.DateTime, 50);
            cmd.Parameters["@Date"].Value = date;
            cmd.CommandType = CommandType.Text;
            DataSet ds = new DataSet();
            SqlDataAdapter sa = new SqlDataAdapter();
           
            sa.SelectCommand = cmd;
           
            sa.Fill(ds, "StockQuotes");
           
            connection.Close();
            chart3.DataSource = sa;
           
            chart3.DataBind();
            chart3.Series["High"].XValueMember = "Date";
            chart3.Series["High"].YValueMembers = "High";
            
            chart3.Series["Low"].YValueMembers = "Low";
           
            chart3.Series["Open"].YValueMembers = "Open";
            
            chart3.Series["Close"].YValueMembers = "Close";
            chart3.Series["Volume"].XValueMember = "Date";
            chart3.Series["Volume"].YValueMembers = "Volume";
            
            chart3.Series[0].XValueMember = "Date";
            
            chart3.DataManipulator.FinancialFormula(FinancialFormula.AccumulationDistribution, "High:Y, Low:Y, Close:Y, Volume", "Series6:Y");
            
            chart3.Series[0].XValueType = ChartValueType.Date;
            chart3.Series[0].ChartType = SeriesChartType.Line;
            chart3.Series[0].BorderWidth = 1;
            chart3.Series[0].ShadowOffset = 1;
            chart3.Series[0].Color = Color.Chartreuse;
           

        }
but I get error:
System.InvalidOperationException: Invalid Period specified for the ACCUMULATIONDISTRIBUTION formula. Increase the number of data points, or decrease the Period.

Could somebody tell me how to resolve that, please.
  •  
Katarzyna_Biskupska
Please help me, I've tried everything and nothing :(
Katarzyna_Biskupska

I don't quite get why you have the CASE WHEN in the SELECT part of your statement. It looks like the @StartDateshould be used in the where clause instead - I think this one causes NULLS in the Open, Low, Close values and as a result you get the exception (certanly the exception message cannot be less helpfull).

If the querydata is correct - just dump (obfuscate if necessary) it here. That would make iteasiear to undertand your scenario.

Thanks,
Victor.

Victor Arakcheev - MSFT

You can use google to search for other answers

Custom Search

More Threads

• How can I change the fonts of Legend on pie chart?
• problem of path of ChartImg.axd
• Chart Controls Can't Display in iframe tag (different Domain)
• outlook warning
• How to auto Fix XAxis
• Multiple cursors
• asp.net Chart Controls on a user control in MVC
• how to display lots of data on chart over 10 series
• How Can I get a series's color
• Legend position calculation problem