NEEDS TO BE IN PYTHON Your mission will be to do the following: 1. Write a funct
ID: 664421 • Letter: N
Question
NEEDS TO BE IN PYTHON
Your mission will be to do the following:
1. Write a function getStockData(company) that takes a company symbol as a string argument and returns these values: the last trade, change, date, open, and previous close. The default value for company should be “GOOG”. You will want to use http://finance.yahoo.com/q?s=GOOG OR http://quote.yahoo.com/d/quotes.csv?s=GOOG&f=sl1d1t1c1ohgvj1pp2owern&e=.csv
but you need to modify it such that the URL can change with the argument company.
2. Now write a loop to call the function with ten company symbols of your
choice (Search for companies here: http://finance.yahoo.com/) and print out the
returned values.
3. Use string slicing, split and datetime module to convert dates like “2/28/2012” to Feb 28, 2012 and dates like “12/12/2011” to Dec 12, 2011.
Here is the output for one company:
The last trade for GOOG was $583.22 and the change was -$1.51 on July
7, 2014. The open was $583.76 and the previous close was $584.73 .
Explanation / Answer
CONCEPT:
Spread Sheet
This part of code helps you to read company symbol and company name from spreasheet.
CODE:
public static XDocument FetchQuote(string smbl)
{
smbl = smbl.Trim();
smbl = smbl.Replace(" ", "&stock=");
smbl = smbl.Replace(",", "&stock=");
string url = "https://www.google.com/ig/api?stock=" + (smbl);
return XDocument.Load(url);
}
public static List<Stock> getValidstcks(XDocument doc)
{
List<Stock> stcks = new List<Stock>();
foreach (var root in doc.Root.Elements("finance"))
{
if (root.Element("last") != null && root.Element(
"last").Attribute("data").Value != null && root.Element(
"last").Attribute("data").Value.Equals("0.00") == false)
{
stcks.Add(Stock_quotes.createNewStock(root));
}
else
{
System.Windows.Forms.MessageBox.Show(root.Element("smbl").Attribute(
"data").Value + " is not a valid stock smbl");
}
}
return stcks;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.