Yahoo Finance Error "Error using yahoo/fetch (line 44) Security list must be cell array of strings."

1 回表示 (過去 30 日間)
Hi All,
I am trying to build out the current market value for a portfolio where the # of securities is dynamic, and the user enters the number of securities and portfolio weights each time the code runs (This all works fine).
I am running into issues when trying to pull data back from yahoo for multiple securities, see the below code. i should be the value in the array "Securities".
Also, once I get this working, how would I be able to have it constantly run and update the market value during market hours, so I can have a close to up to date market value streaming real time (less the 15 yahoo delay).
numsec = ('Enter the number of securities in Portfolio: ');
numsec = input(numsec)
Security = cell(numsec,1);
SecurityWt = nan(numsec,1);
for ns = 1:numsec
Security{ns} = input(['Enter Security ',num2str(ns),' Ticker: '],'s');
SecurityWt(ns) = input(['Enter Security ',num2str(ns),' Weight (ex: .25): ']);
end
if sum(SecurityWt) ~=1
display('Error - The sum of the security weights must be equal to 1! ')
else
pie(SecurityWt,Security)
% Begin Pulling in market prices to contruct portfolio Market Value
c = yahoo;
yhoo_fld = 'Last';
for i=1:size(Security)
fetch(c,i,yhoo_fld)
end

採用された回答

Walter Roberson
Walter Roberson 2015 年 10 月 14 日
You are trying to fetch information about the number stored in i rather than about the i'th security. fetch(c, Security{i}, yhoo_fld)
Alternately you can use fetch(c, Security, yhoo_fld) to get information on all of them at once without a loop.
  3 件のコメント
P_Alpha
P_Alpha 2015 年 10 月 14 日
編集済み: P_Alpha 2015 年 10 月 14 日
I actually figured it out using your method without the loop. Once I got the prices. My question now is how do I tie together my prices to my securities? PriList is converting the the 1x1 structure into a column of prices.
numsec = ('Enter the number of securities in Portfolio: ');
numsec = input(numsec)
Security = cell(numsec,1);
SecurityQty = nan(numsec,1);
for ns = 1:numsec
Security{ns} = input(['Enter Security ',num2str(ns),' Ticker: '],'s');
SecurityQty(ns) = input(['Enter Security ',num2str(ns),' Quantity: ']);
end
c = yahoo;
yhoo_fld = 'Last';
prices = fetch(c,Security,yhoo_fld);
PriList = prices.Last
Walter Roberson
Walter Roberson 2015 年 10 月 14 日
Sorry I do not have that toolbox to test with.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFinancial Data についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by