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!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Lane Floyd
  • 73
  • 1
  • 7
  • Are you using jsoncpp by any chance? In which case this might help: https://stackoverflow.com/a/45037450/1618009 Otherwise it will depend on the JSON library you are using. – castro May 13 '20 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? – Lane Floyd May 13 '20 at 21:08
  • @castro And when i cout< – Lane Floyd May 13 '20 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 – castro May 13 '20 at 22:29
  • @castro This is the link to the .json https://sandbox.iexapis.com/v1/stock/AAPL/chart/ytd/?token=Tpk_eb10dc4b66434ecdad0c4edf1594da69 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? – Lane Floyd May 14 '20 at 00:56
  • Thanks for providing the link. It looks like some days are skipped - weekends and possibly national holidays? This at least explains why you weren't getting 132 :) you get a date for each entry so I'd suggest you parse that date and drive the logic off that. – castro May 14 '20 at 04:55
  • @castro Wow i feel extremly dumb, what would be the easiest ways to parse this date to get my x axis to show accurate time stamps for the y values? – Lane Floyd May 15 '20 at 04:28
  • No worries, happens to the best of us! I think I'd lean towards using std::get_time (though there's doubtless other options). [This post](https://stackoverflow.com/questions/4137748/c-converting-a-time-string-to-seconds-from-the-epoch) has an example of converting a string using std::get_time, then converting it to an epoch time - something along those lines would work I think. Good luck! – castro May 15 '20 at 18:38
  • @castro Thank you so much man! I really appreciate your help! – Lane Floyd May 16 '20 at 03:26

0 Answers0