- Joined
- Apr 8, 2023
- Messages
- 1
- Reaction score
- 0
Hi!
I am currently working on a program in C# that loads stock data saved on my computer. It populates a candlestick chart in a new window based on the ticker selected in a combo box. I'd like it to load the chart window again once another ticker is selected in the combo box, but that's my problem. It bugs out the second time I try to load a ticker. The first time works fine. Here's the code.
Anyone have any idea why this happens? Its like the "listOfCandlesticks" is null the second time it tries to run the method....
I am currently working on a program in C# that loads stock data saved on my computer. It populates a candlestick chart in a new window based on the ticker selected in a combo box. I'd like it to load the chart window again once another ticker is selected in the combo box, but that's my problem. It bugs out the second time I try to load a ticker. The first time works fine. Here's the code.
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Stock_Project_Loader_Final
{
/// <summary>
/// create the StockLoader class
/// </summary>
public partial class StockLoader : Form
{
aCandlestickReader candlestickReader = null;
List<aCandlestick> listOfCandlesticks = null;
/// <summary>
/// Required method for designing the program
/// </summary>
public StockLoader()
{
InitializeComponent();
listOfCandlesticks = new List<aCandlestick>(512);
}
private void comboBoxTicker_SelectedIndexChanged(object sender, EventArgs e)
{
//set listOfCandlesticks to null
listOfCandlesticks = null;
String period = String.Empty;
String seriesName = String.Empty;
candlestickReader = new aCandlestickReader();
if (comboBoxTicker.Text != String.Empty)
{
//chatGPT inspired code to separate out the ticker and period from comboBoxTicker
string comboBoxText = comboBoxTicker.Text;
string[] parts = comboBoxText.Split('-');
string ticker = parts[0];
int hyphenIndex = comboBoxText.IndexOf('-');
int dotIndex = comboBoxText.IndexOf('.');
period = comboBoxText.Substring(hyphenIndex + 1, dotIndex - hyphenIndex - 1);
List<aCandlestick> listOfCandlesticks = candlestickReader.readStock(ticker, period, dateTimePickerStart.Value, dateTimePickerEnd.Value);
//THE LINE BELOW THIS IS WHERE THE ERROR OCCURS!
chartCandlestick1.DataSource = listOfCandlesticks;
chartCandlestick1.DataBind();
// clear the grid
chartCandlestick1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
chartCandlestick1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
// set the series name (day, week, month)
chartCandlestick1.Series[0].Name = seriesName;
// set date values on x-axis
chartCandlestick1.Series[0].XValueMember = "Date";
// set high, low, open and close values on y axis
chartCandlestick1.Series[0].YValueMembers = "High, Low, Open, Close";
chartCandlestick1.Series[0].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chartCandlestick1.Series[0].CustomProperties = "PriceDownColor=Red, PriceUpColor=Green";
// open close style
chartCandlestick1.Series[0]["OpenCloseStyle"] = "Triangle";
chartCandlestick1.Series[0]["ShowOpenClose"] = "Both";
// set the y range based off the highest high and lowest low values
aCandlestickReader extremes = new aCandlestickReader();
chartCandlestick1.ChartAreas[0].AxisY.Minimum = extremes.getLowestLow() - 5;
chartCandlestick1.ChartAreas[0].AxisY.Maximum = extremes.getHighestHigh() + 5;
chartCandlestick1.ChartAreas[0].AxisY.Interval = 10;
Form formChart = new Form();
formChart.Show();
//display chartCandlestick1 in formChart
formChart.Controls.Add(chartCandlestick1);
//change size of formChart when it displays
formChart.Size = new Size(800, 600);
//clear listOfCandlesticks after user closes formChart
}
}
}
}
Anyone have any idea why this happens? Its like the "listOfCandlesticks" is null the second time it tries to run the method....