0

How to fetch "high" and "low" price from the following web api using AXIOS. Here you will find the following code, but obviously I need to change to make it work properly for all dates.

axios.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=TUVR')
.then(response => {
  console.log(response.data['Time Series (Daily)']['2018-10-18']['2. high']);
  console.log(response.data['Time Series (Daily)']['2018-10-18']['3. low']);

})
.catch(error => {
  console.log(error);
});

sample of the original data I am trying to extract

{
  "Meta Data": {
    "1. Information": "Weekly Prices (open, high, low, close) and Volumes",
      "2. Symbol": "MSFT",
        "3. Last Refreshed": "2018-10-18",
          "4. Time Zone": "US/Eastern"
  },
  "Weekly Time Series": {
    "2018-10-18": {
      "1. open": "108.9100",
        "2. high": "111.8100",
          "3. low": "106.9468",
            "4. close": "108.5000",
              "5. volume": "122020154"
    },
    "2018-10-12": {
      "1. open": "111.6600",
        "2. high": "113.0800",
          "3. low": "104.2000",
            "4. close": "109.5700",
              "5. volume": "228861873"
    },
    "2018-10-05": {
      "1. open": "114.7500",
        "2. high": "116.1800",
          "3. low": "110.6400",
            "4. close": "112.1300",
              "5. volume": "120208912"
    },
    "2018-09-28": {
      "1. open": "113.0300",
        "2. high": "115.1000",
          "3. low": "112.2175",
            "4. close": "114.3700",
              "5. volume": "110093609"
    }
  }
}
3
  • where does the key Time Series (Daily) came from Commented Oct 19, 2018 at 1:56
  • @Beginner here is the link to the web api alphavantage.co/documentation Commented Oct 19, 2018 at 1:57
  • Time Series (Daily) it means daily pricing for the stock. it comes from the original api Commented Oct 19, 2018 at 1:59

2 Answers 2

2

I did not add the axios wrapper, but you can probably translate it pretty easily from a normal fetch:

fetch('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=TUVR')
  .then(resp => resp.json())
  .then(response => {
    const highLow = Object.keys(response['Time Series (Daily)']).map(date => {
      return {
        date,
        high: response['Time Series (Daily)'][date]['2. high'],
        low: response['Time Series (Daily)'][date]['3. low']
      }
    })
    console.log(highLow);
  })
  .catch(error => {
    console.log(error);
  });

Sign up to request clarification or add additional context in comments.

Comments

1

In JS, Assign your data to some var say "myData" and Try this:

function getHighAndLow(){

  var myData = {your example data};

  var weeklyTimeSeries = myData["Weekly Time Series"];
  var series = [];

  for (var i in weeklyTimeSeries) {
      series.push(weeklyTimeSeries[i]);
  }

  var high = series.map(function (a) { return a["2. high"] });

  var low = series.map(function (a) { return a["3. low"] });
}

Hope it'll help :)

1 Comment

You don't need to worry about different dates 'cause it's already handled in the forin loop :)

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.