Problem Description This Cody problem serves as an introduction to Application P
ID: 3864485 • Letter: P
Question
Problem Description
This Cody problem serves as an introduction to Application Programming Interfaces (API's). These interfaces act as powerful tools to bridge applications to data sources such as a database. Please read the "What is an API?" discussion forum post on Edge if you have not already.
You are to write a function that returns the stock data to the user in a cell array, given some stock symbol input such as "AAPL." This function may be something a future employer would ask you to write if you are working on an analytical platform for stocks. You will be utilizing an Application Program Interface (API) from Markit to retrieve this data using the built-in function "webread." Webread makes an HTTP GET request to Markit given a URL endpoint and a key-value or multiple key-value pairs (kv).
The actual coding involved in this programming assignment is minimal and is meant to serve primarily as an introduction to some breadth topics you may find yourself using in the future.
Assign the following fields from the API response into a cell array. Your cell array should contain the following data (in order as below):
Stock Name, Stock Symbol, Last Price, % Change, and Time Stamp
Solution:
function data = stock_check(symbol)
% Help: stock_check('AAPL') returns relevant stock data about Apple.
% Markit Quote Endpoint
endpoint = 'http://dev.markitondemand.com/MODApis/Api/v2/Quote/json';
% Webread makes an HTTP GET request to Markit's Quote endpoint.
% webread(endpoint, key, value)
% The request parameter for this API call is just the symbol, these
% are in the form of 'key-value' pairs. Here the key is 'symbol' and the value
% is a stock symbol (i.e. AAPL).
resp = webread(endpoint, 'symbol', symbol);
% Run this code in MATLAB to determine the output of the API. It should
% return a data structure you are familiar with. Using the data structure,
% figure out how to extract the status, name, sign, stock price, percent change, and time stamp.
% Begin your code here:
data =
end
Explanation / Answer
endpoint = 'http://dev.markitondemand.com/MODApis/Api/v2/Quote/json';
the endpoint mentioned is the address where the API "Quote" is hosted.
resp = webread(endpoint, 'symbol', symbol);
MATLAB provides the function webread to Get data from URLs and convert it into suitable formats.
The above command makes a HTTP request and the response is a json which is converted to 1x1 struct.
data = {resp.Status , resp.Symbol , resp.LastPrice , resp.ChangePercent , resp.Timestamp};
This code extracts the required fields from the response and create a cell array out of it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.