C# MSChart Candle Stick & Moving Average Chart. Error: Formula error - There are not enough data points for the Period

廉价感情. 提交于 2019-12-08 05:06:39

问题


You cannot vote on your own post 0

I am trying to create a stock candle with MA(15) on daily data.

I can create a chart with OHLC bar without any problem.

But when I started usingDataManipulator.FinancialFormula for MA, I keep getting errors of "Formula error - There are not enough data points for the Period."

Can someone help me out on this? Thanks

Here is the code.

        DataSet ds = new DataSet();
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = @"Data Source=XXX;Database=Stock;Integrated Security=SSPI;";
        connection.Open();
        string sql = "Select datestamp, highprice, lowprice,openprice, closeprice from daymarketdata where tickname='GS' and datestamp>'1/1/2011' order by datestamp asc";

        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, connection);
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter sa = new SqlDataAdapter();
        sa.SelectCommand = cmd;

        sa.Fill(ds, "Cos");
        connection.Close();
        chart1.Series["Daily"].ChartType = SeriesChartType.Candlestick;
        chart1.DataSource = sa;
        chart1.DataBind();


        chart1.Series["Daily"].XValueMember = "DateStamp";
        chart1.Series["Daily"].YValueMembers = "HighPrice, LowPrice, OpenPrice, ClosePrice";
        chart1.Series["Daily"].IsXValueIndexed = true;

        chart1.Series["Daily"].BorderColor = System.Drawing.Color.Black;
        chart1.Series["Daily"].Color = System.Drawing.Color.Black;
        chart1.Series["Daily"].CustomProperties = "PriceDownColor=Green, PriceUpColor=Red";
        chart1.Series["Daily"].XValueType = ChartValueType.Date;
        chart1.ChartAreas[0].AxisY.Minimum = 100;
        chart1.ChartAreas[0].AxisY.Maximum = 180;
        chart1.DataManipulator.FinancialFormula(FinancialFormula.MovingAverage, "15", "Daily", "MA");

回答1:


Although pretty old I thought I leave my contribution since I was struggling with the very same error message and the posted code actually pointed me in the right direction.

I believe the chart1.DataBind() needs to move after setting XValueMember and YValueMembers. Like so:

    chart1.Series["Daily"].XValueMember = "DateStamp";
    chart1.Series["Daily"].YValueMembers = "HighPrice, LowPrice, OpenPrice, ClosePrice";   
    chart1.DataBind();

by the time the FinancialFormula gets applied the data will not have been loaded into the series object otherwise. You may run into other issue though ;)




回答2:


please add

Chart1.DataManipulator.IsStartFromFirst = True



来源:https://stackoverflow.com/questions/7261691/c-sharp-mschart-candle-stick-moving-average-chart-error-formula-error-ther

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!