Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This should be written in matlab code This Cody problem will likely stretch your

ID: 3687682 • Letter: T

Question

This should be written in matlab code

This Cody problem will likely stretch your current understanding of computer programming, yet will prove to be quite powerful if you choose to use this problem approach during your final project. 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're working on some kind of analytical platform for stocks. We will be utilizing an Application Program Interface (API) from Markit to retrieve this data using the built-in function "webread." Webread takes a URL string as input and returns a data structure with the information provided by the API developer. 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. Your cell array should contain the following data (in order as below): Stock Name, Stock Symbol, Last Price, % Change, and Time Stamp.

Explanation / Answer

% get_stocksymbol('searchstring','default1') takes two arguments:

%   'searchstring' is the company name to be looked up.

%   'default1' is y/n -- set to 'y' to default to the first result if

%       the search return multiple results.

%

% stocksymbol = get_stocksymbol(searchstring,default1) returns a string

% with the result of the stock symbol lookup.

%

% [stocksymbol,choices] = get_stocksymbol(searchstring,default1) returns

% the optional cell array of company name and symbol choices.

%

% The function looks up the search string using the Yahoo Finance API (but

% without requiring the use of a JSON library) and returns all possible

% stock symbols provided by Yahoo. If more than one symbol is returned,

% the user is given the option to select the most appropriate.

%

% Trevor Zink 2013

function [stocksymbol,choices] = get_stocksymbol(searchstring,default1)

fsearchstring=strrep(searchstring,' ','%20');

url = strcat('http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=',fsearchstring,'&callback=YAHOO.Finance.SymbolSuggest.ssCallback');

try

jsonread = urlread(url);

catch errmsg

    stocksymbol = 'NA';

    fprintf('No company named %s exists in the database ',searchstring)

    return

end

% search the JSON output for the desired term (in this case, ticker symbol)

% handle the case where either data not available or no symbol

no_data = strfind(jsonread, '"Result":[]}})');

if ~isempty(no_data),

    fprintf(1,'No symbol for %s. ',searchstring);

    stocksymbol = 'NA';

    return;

end

% extract the symbol(s)

% find a term in the JSON output that preceeds the stock symbol(s) (in this

% case, "symbol"):

occ = strfind(jsonread, '"symbol"');

clear choices

for i=1:length(occ)

% the next four lines find characters at the beginning and the end of

% the actual stock symbol itself

sym_lt = strfind(jsonread(occ(i):end),':"');

sym_rt = strfind(jsonread(occ(i):end),'",');

nam_lt = strfind(jsonread(occ(i):end),'"nam');

nam_rt = strfind(jsonread(occ(i):end),'","ex');

jsonshort = jsonread(occ(i):end);

% then just grab the section of the JSON output inbetween the delimiters we

% established above

choices(i,1)= {i};

choices(i,2) = cellstr(jsonshort(sym_lt+2:sym_rt-1));

choices(i,3) = cellstr(jsonshort(nam_lt+9:nam_rt-1));

end

% choose from multiple options, if available

clear choice

if or(size(choices,1)==1,default1=='y')

    stocksymbol=choices{1,2};

else

    for i=1:size(choices,1)

        % this part is a bit over the top, but formats the choices in a

        % pretty table for the user to choose from

        if size(choices{i,2},1)>6

        fprintf('%i) %s %s ',i,choices{i,2},choices{i,3})

        else

            if size(choices{i,2},1)>3 && length(choices{i,2})<=6

        fprintf('%i) %s %s ',i,choices{i,2},choices{i,3})

            else

        fprintf('%i) %s %s ',i,choices{i,2},choices{i,3})

            end

        end

    end

    fprintf('Company: %s ',searchstring)

    choice = input('Choose an option number (default 1; choose 0 for ''NA''):');

    if isempty(choice)

        choice=1;

    end

    % account for incorrect entries

    if choice > length(choices)

        valid_response = 0;

        while ~valid_response

            choice = input('Invalid selection. Choose an option number: ');

            if choice <= length(choices)

                valid_response = 1;

            end

        end

    end

        if choice==0

            stocksymbol='NA';

        else

            stocksymbol=choices{choice,2};

        end

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote