0

I am getting shares prices using the Alpha Vantage API and I am able to get the most recent price easily enough. But I want to get the price from five entries back.

This is the start of the array I am working with

{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2021-02-22",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2021-02-22": {
            "1. open": "118.5000",
            "2. high": "121.1250",
            "3. low": "118.4400",
            "4. close": "120.8600",
            "5. volume": "5838841"
        },
        "2021-02-19": {
            "1. open": "120.7500",
            "2. high": "120.7600",
            "3. low": "118.3800",
            "4. close": "118.9900",
            "5. volume": "6578741"
        },
        

and this is the clunky code I have been using,

which gets the first day,

turns it into a time,

takes seven days off (since they exchange is closed on weekends) and works with the revised date.

$getstocksapi = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=".$stock[$x]."&outputsize=compact&apikey=mycode;
$currentprices = file_get_contents($getstocksapi);
$currentprices = json_decode($currentprices,true);
$dateToCheck =($currentprices['Meta Data']['3. Last Refreshed']);
$oldDateToCheck = strtotime($dateToCheck);
$oldDateToCheck = ($oldDateToCheck  - 60 * 60 * 24 * 7);
$oldDateToCheck  = date("Y-m-d", $oldDateToCheck);
$dayEnd[$x] = ($currentprices['Time Series (Daily)'][$dateToCheck]['4. close']);
$thenDayEnd[$x] = ($currentprices['Time Series (Daily)'][$oldDateToCheck]['4. close']);

Is there a more elegant way, perhaps using array_slice that I can get the most recent closing price and then the one from five entries down?

9
  • isn't the "Time series (Daily)" already in descending order? Then you just need to get the first five elements Commented Feb 23, 2021 at 7:15
  • Thanks. Yes, they are in order. Any ideas how I can simply choose the fifth one? Commented Feb 23, 2021 at 7:21
  • 1
    yes the fifth one will have index 4 so you can use index $currentprices['Time Series (Daily)'][4]['4. close'] Commented Feb 23, 2021 at 7:23
  • $dateToCheck =($currentprices['Meta Data']['3. Last Refreshed']); $prevFithDay = date('Y-m-d', strtotime('-5 days', strtotime($dateToCheck))); echo $currentprices['Time Series (Daily)'][$prevFithDay]['4. close']; Commented Feb 23, 2021 at 7:24
  • @AlivetoDie I think op means the 5th entry, not the last 5 days? Commented Feb 23, 2021 at 7:28

1 Answer 1

1

From the comment, the number index does not work because it still does not have number index. To re-index you can use function array_values().

$currentprices = json_decode($data, true);
$timeSeriesDaily = array_values($currentprices['Time Series (Daily)']);

Then $timeSeriesDaily will be the same array but with number index instead and you can access the 5th index with the following.

echo $timeSeriesDaily[4]['4. close'];
Sign up to request clarification or add additional context in comments.

Comments

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.