0

Basically I am using an API to retrieve stock data to me in the form of a ytd so it will return data of the closing price of the stock everyday from january until now. At first I was simply using a for loop and reading until i < json.size() but after figuring out the .size() does not properly return what i need for it to work i am again stuck on this. My code is below

    //Retrieves json format of data
    Json::Value chartData = IEX::stocks::chartYtd(symbol_std);

    //Stores x and y values
    QVector<double> time(365), closePrice(365);

    //Intialize first vector to first values
    closePrice[0] = chartData[0]["close"].asDouble();
    time[0] = startYearTime;

    //Finds max and min for range
    float maxAvg = closePrice[0];
    float minAvg = closePrice[0];

    //Reads in data from json(historical data 1 day delayed)
    for(int i = 1; ; i++)
    {
        time[i] = startYearTime + 86400*i;
        closePrice[i] = (chartData[i]["close"].asDouble());

        if((closePrice[i] == 0) && (time[i] != chartData.size() - 1))
        {
            closePrice[i] = closePrice[i-1];
        }

        if(closePrice[i] > maxAvg)
        {
            maxAvg = closePrice[i];
        }

        else if(closePrice[i] < minAvg)
        {
            minAvg = closePrice[i];
        }

    }

The json file looks like this

what can i do to have my code store the "close" value in the json file until there is no more "close" value to read in and then in which it stops, thank you in advance as im a new developer!

9
  • Are you using jsoncpp by any chance? In which case this might help: stackoverflow.com/a/45037450/1618009 Otherwise it will depend on the JSON library you are using. Commented May 13, 2020 at 20:43
  • @castro I am actually using jsoncpp, in the example you gave is root.size() the same as chartData.size() for me? Also why use .pushBack instead of just doing closePrice[i] = chartData[i]["close"].asDouble? Commented May 13, 2020 at 21:08
  • @castro And when i cout<<chartData.size I get 99, when there should be 133 being 133 days has passed since january, so if I use i!=chartData.size() it will only run 99 times instead of the needed 133. Commented May 13, 2020 at 21:13
  • Apologies, missed the call to size when I first looked. In the for loop there's no stopping condition so it will carry on until you get an access violation I'd have thought. In the posted code you're comparing time[i] with chartData.size(), I think you want to compare index i instead? And unless there's a reason not to, this condition should be what stops the for loop. With size not returning what you expect, I would double check the data you are receiving, that there is an entry for every day of the year. Failing all of that it might be worth writing the JSON to a file and attaching it here Commented May 13, 2020 at 22:29
  • @castro This is the link to the .json sandbox.iexapis.com/v1/stock/AAPL/chart/ytd/… As you can tell there is dates from January 2nd - May 12th which is 132 days i guess, so shouldnt the chartData.size() be 132 so i can read in one data value per day? Commented May 14, 2020 at 0:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.