Chart for .NET / User's Guide / Chart Types / Stock / Stock Candle

In This Topic
    Stock Candle
    In This Topic

    Candle charts are used to plot daily stock data. Each data point resembles a candle, where the candle body is formed by the open and close prices for the day. The high and low prices are displayed by vertical lines above and below the candle body. The body fill color depends on whether the stock price goes up (the closing price is higher than the opening price) or down (the closing price is lower than the opening price). The following figure displays a Candle Stock Chart.




    Figure 1.

     Creating a stock series

    Stock charts are represented by the NStockSeries type. An instance of this type must be added to the series collection of a Cartesian chart.

    C#
    Copy Code
    // obtain a reference to the Cartesian chart that is created by default
    NCartesianChart chart = (NCartesianChart)chartControl.Charts[0];
    
    NStockSeries stock = (NStockSeries)chart.Series.Add(SeriesType.Stock);
    
    Visual Basic
    Copy Code
    ' obtain a reference to the Cartesian chart that is created by default
    Dim chart As NCartesianChart = chartControl.Charts(0)
    
    Dim stock As NStockSeries = chart.Series.Add(SeriesType.Stock)
    

    The CandleStyle property of the NStockSeries object should be set to Bar.

    C#
    Copy Code
    stock.CandleStyle = CandleStyle.Bar;
    
    Visual Basic
    Copy Code
    stock.CandleStyle = CandleStyle.Bar
    
     Passing Data

    Once the stock series is created you can add some data in it. Stock series use the Values data series for the high values and the XValues data series for the x positions of the data points. For consistency the Values data series can also be accessed through the HighValues property.

    In addition to the HighValues and XValues data series the stock series provides several data series of type Double, which hold the low, open and close values. They are accessible respectively through the LowValues, OpenValues and CloseValues properties of NStockSeries object.

    You can either manipulate directly these data series, or use the data point interface to supply data to the stock series. The NStockDataPoint class provides several constructors that can be very helpful when you add stock data points. Please refer to the Working with Data Points topic for more information.

     Controlling the candle size

    The width of the candles is specified by the CandleWidth property, which is of type NLength. The depth of a candle is specified in percents of the floor grid cell it occupies. This value is specified through the CandleDepthPercent property. By default it is set to 50. The following code sets the candle width to 2 percent of the chart width and the candle depth to 70 precent of the Z axis cells:

    C#
    Copy Code
    stock.CandleWidth = new NLength(2, NRelativeUnit.ParentPercentage);
    stock.CandleDepthPercent = 70;
    
    Visual Basic
    Copy Code
    stock.CandleWidth = New NLength(2, NRelativeUnit.ParentPercentage)
    stock.CandleDepthPercent = 70
    
     Controlling the High - Low line appearance

    Stock charts displays a line connecting the high and low values. Its visibility is controlled by the ShowHighLow property which is by default set to true. The line stroke style is specified through the HighLowStrokeStyle property. The following code will display the high - low line in red with width 2 pixels:

    C#
    Copy Code
    stock.HighLowStrokeStyle = new NStrokeStyle(2, Color.Red);
    
    Visual Basic
    Copy Code
    stock.HighLowStrokeStyle = New NStrokeStyle(2, Color.Red)
    
     Controlling the candles appearance

    A candle is considered with up orientation if its open value is smaller than its close value. Otherwise it is considered with down orientation. Up candles are displayed with the styles specified in the UpFillStyle and UpStrokeStyle properties. Down candles are displayed with the styles specified in the DownFillStyle and DownStrokeStyle properties. The following code will display the up candles in blue and the down candles in green:

    C#
    Copy Code
    stock.UpFillStyle = new NColorFillStyle(Color.Blue);
    stock.DownFillStyle = new NColorFillStyle(Color.Green);
    
    Visual Basic
    Copy Code
    stock.UpFillStyle = New NColorFillStyle(Color.Blue)
    stock.DownFillStyle = New NColorFillStyle(Color.Green)
    
     Formatting Commands

    The NStockSeries class extends the formatting commands set with the following formatting commands:

    <open> - the current data point open value (extracted from the OpenValues data series)
    <high> - the current data point high value (extracted from the HighValues data series)
    <low> - the current data point low value (extracted from the LowValues data series)
    <close> - the current data point close value (extracted from the CloseValues data series)
    <up_label> - in the case of a candle with up orientation it is replaced with string specified by the UpLabel property
    <down_label> - in the case of a candle with down orientation it is replaced with string specified by the DownLabel property

    For example if you want to display the open and close values in the legend you must use the following code:

    C#
    Copy Code
    stock.Legend.Mode = SeriesLegendMode.DataPoints;
    stock.Legend.Format = "<open> <close>";
    
    Visual Basic
    Copy Code
    stock.Legend.Mode = SeriesLegendMode.DataPoints
    stock.Legend.Format = "<open> <close>"
    

    In a scenario of a date time stock chart you may want to display the dates in the legend:

    C#
    Copy Code
    stock.Legend.Mode = SeriesLegendMode.DataPoints;
    stock.Legend.Format = "<xvalue>";
    stock.XValues.ValueFormatter = new NDateTimeValueFormatter();
    
    Visual Basic
    Copy Code
    stock.Legend.Mode = SeriesLegendMode.DataPoints
    stock.Legend.Format = "<xvalue>"
    stock.XValues.ValueFormatter = New NDateTimeValueFormatter()
    
     Stock Legend

    When the Mode property of the NSeriesLegend object (accessible trough the Legend property) is set to SeriesLogic the stock series will add two items in the legend - for the up and down oriented candles. The legend marks will be filled with the up and down fill styles respectively. The UpLabel string is associated with the up legend items while the DownLabel string is associated with the down legend items.

    C#
    Copy Code
    stock.Legend.Mode = SeriesLegendMode.SeriesLogic;
    
    Visual Basic
    Copy Code
    stock.Legend.Mode = SeriesLegendMode.SeriesLogic
    

    See the Legend Data topic for more information.

     Related Examples
    Windows forms: Chart Gallery\Stock\Candle
    Windows forms: Chart Gallery\Stock\DateTime Stock
    Windows forms: Chart Gallery\Combo Charts\Financial chart example
    See Also