This tutorial explains how to load financial data from a CSV file and display it using LightningChart StockSeries. Stock series are used to visualize stock exchange data in candlestick and stock bars formats. The tutorial assumes that you have created a new chart with LightningChart on a WinForms or WPF application. If not, please follow our Simple 2D Chart on creating an application.
// Configure X- and Y-axes.
// X-axis configuration.
var axisX = chart.ViewXY.XAxes[0];
axisX.Title.Text = "Date";
axisX.ValueType = AxisValueType.DateTime;
axisX.LabelsAngle = 90;
axisX.MajorDiv = 24 * 60 * 60; // Major division is one day in seconds.
// Y-axis configuration.
var axisY = chart.ViewXY.YAxes[0];
axisY.Title.Text = "Price";
// Create a new StockSeries.
var stockSeries = new StockSeries(chart.ViewXY, xAxis, yAxis);
chart.ViewXY.StockSeries.Add(stockSeries);
// Configure the stock plot.
stockSeries.Style = StockStyle.OptimizedCandleStick;
stockSeries.FillBorder.Width = 1;
stockSeries.Title.Text = "Example Inc.";
Load the data from a CSV file into the series data points using series.LoadFromCSV(string fileName, SeparatorCSV separator)
. The data has to be organized in columns in the following order:
Date | Open | Close | High | Low | Value | Transaction |
---|---|---|---|---|---|---|
DateTime | double | double | double | double | int | double |
Series values can be written into a file using series.SaveToCSV
, which is a pair function for LoadFromCSV.
stockSeries.LoadFromCSV("../../../data/data.csv", SeparatorCSV.Semicolon);
// Create a reference to the loaded data points.
var stockData = stockSeries.DataPoints;
// Generate data for series, which matches closed values.
var closeData = new SeriesPoint[stockData.Length];
for (var i = 0; i < stockData.Length; i++)
{
closeData[i] = new SeriesPoint()
{
X = xAxis.DateTimeToAxisValue(stockData[i].Date),
Y = stockData[i].Close
};
}
// Create a new PointLineSeries to show the dynamic in closed values on Stock Exchange.
var lineSeries = new PointLineSeries();
lineSeries.Title.Text = "Example Inc.";
lineSeries.Points = closeData;
chart.ViewXY.PointLineSeries.Add(lineSeries);
//Auto-scale X- and Y-axes.
chart.ViewXY.ZoomToFit();