Search code examples
c#.netchartsmschartstochastic

How to use MS Stochastic Indicator


I am working on drawing a stochastic indicator, using double arrays containing open high low close data, adjusted to time periods of 5,10,15 and 30 minutes.

chart1.DataManipulator.FinancialFormula(FinancialFormula.StochasticIndicator, 
"21,3",
"highPriceArray:Y,lowPriceArray:Y2,closePriceArray:Y4"
, "percentage_K_values:Y,percentage_D_values:Y");

The function above is the one I'm using from the MSDN library,

  • How can I convert my double arrays of OHLC values to a time series format so I can, input them to this function and obtain %K and %D values afterward in return, and proceed to plot the chart?

                openPriceArray[ii] = obj[0];
                highPriceArray[ii] = obj[1];
                lowPriceArray[ii] = obj[2];
                closePriceArray[ii] = obj[3];
    

Thank you in advance.


Solution

  • This sample code, with comments, should get you going. You can also refer to the MSDN Docs.

    enter image description here

    private void Form1_Load(object sender, EventArgs e)
    {
        // load your data into a List<DataPoint>
        List<DataPoint> data = GetData();
    
        /*
         * dp.XValue - DateTime
         * dp.YValues[0] - High
         * dp.YValues[1] - Low
         * dp.YValues[2] - Open
         * dp.YValues[3] - Close
         */
    
        foreach (DataPoint dp in data)
            chart1.Series[0].Points.Add(dp);
    
        chart1.ChartAreas[0].AxisY.Maximum = chart1.Series[0].Points.Max(p => p.YValues[0]);
        chart1.ChartAreas[0].AxisY.Minimum = chart1.Series[0].Points.Min(p => p.YValues[1]);
    
        chart1.DataManipulator.FinancialFormula(FinancialFormula.StochasticIndicator,
        "10,10",
        "HLOC-Price:Y,HLOC-Price:Y2,HLOC-Price:Y4",
        "K-Indicator,D-Indicator");
    
        /*
         * HLOC-Price - Candlestick Series[0], primary Y-axis
         * K-Indicator - Line Series[1], secondary Y-axis
         * D-Indicator - Line Series[2], secondary Y-axis
         */
    }