Help me fix this bug in my program! Displaying chart data

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.

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....
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
The issue with your code might be that you are declaring a new List<aCandlestick> with the same name as the instance variable listOfCandlesticks inside the comboBoxTicker_SelectedIndexChanged method. This creates a new local variable that shadows the instance variable, and any modifications made to the local variable will not affect the instance variable. Therefore, the DataSource property of the chartCandlestick1 control is not being set to the new list of candlesticks.

To fix this, you can remove the local declaration of listOfCandlesticks inside the comboBoxTicker_SelectedIndexChanged method and modify the instance variable directly:
C#:
private void comboBoxTicker_SelectedIndexChanged(object sender, EventArgs e)
{
    // Remove this line to modify the instance variable directly
    // listOfCandlesticks = null;

    String period = String.Empty;
    String seriesName = String.Empty;
    candlestickReader = new aCandlestickReader();

    if (comboBoxTicker.Text != String.Empty)
    {
        
        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);

        listOfCandlesticks = candlestickReader.readStock(ticker, period, dateTimePickerStart.Value, dateTimePickerEnd.Value);

        chartCandlestick1.DataSource = listOfCandlesticks;
        chartCandlestick1.DataBind();

        // rest of the code...
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top