UWSC Date to Float Data Files & Floating Point Array Programming Exercises
Question Description
This comma-separated value file hold various daily values for the S&P500 stock index for all trading days from Jan 3, 1950 to Oct 7, 2016. Column headers in the file are self-explanatory, except the final column: The “Adj Close” column is the close price adjusted for dividends and splits. This file was downloaded from: http://finance.yahoo.com/quote/%5EGSPC/history?p=%5EGSPC (Links to an external site.) (accessed Oct 8, 2016; note that versions of the original data file that are currently available online may have slightly different formatting of the fields).
Remember, a comma-separated value file is a plain text file that follows certain conventions: commas separate columns, quotation marks set off fields, etc.
Please be aware of the following:
- If you have code that doesn’t work in your notebook or source code file, but you have later code in your file that does work, put your answers into different files so the code errors in your answers to the earlier questions don’t prevent the file from running for the later questions.
- For questions that ask you to create images, your code must have a
show
(at the end of the entire file, not the end of each applicable question) orsavefig
call (for each applicable question) so that we can see the plots you have made.
Using the above data, write a program that does the following:
- Write a function
convert_date_to_float
that takes a string of the format yyyy-mm-dd and converts it to a floating point number. Thus,'1950-01-31'
as input results in a return value of 19500131.0 - Reads the contents of the data file into a floating point array
data2
without usinggenfromtxt
or any other pre-written file-reading function (read
,readlines
, etc. are not pre-written file-reading functions but methods of a file object). Thus, you have to create a file object, use string methods to access, convert, etc. Make the first column be yyyy-mm-dd as a floating point number. You can use the function you wrote for #1 to do this. For this task, you do not have to write a function. - Reads the contents of the data file into a floating point array
data3
usinggenfromtxt
. Using what we’ve covered in this class, the first column of the array this call togenfromtxt
returns will be filled with “nan”s (i.e., “not-a-number” values). That’s okay. For this task, you do not have to write a function. - Compare all values (except the first column) in
data2
anddata3
to confirm they are “equal” to each other. Since they’re all floating point values, you’re checking that they are “close” to each other. Hint: There’s a function in NumPy calledallclose
that will help here. For this task, you do not have to write a function. - Create an x–y plot of the adjusted close (y-axis) versus trading day (x-axis). You don’t need to plot a date on the x-axis; “days since Jan 3, 1950” would be fine. For this task, you do not have to write a function. Your graph should look similar to this one:
- Create an x–y plot of the difference between the high and low value for the day (y-axis) versus trading day (x-axis). You don’t need to plot a date on the x-axis; “days since Jan 3, 1950” would be fine. For this task, you do not have to write a function. Your graph should look similar to this one:
NB regarding Tasks 2 and 3: Your goal is to read in the text data from the file and convert it into floating point values. You’ll then store the floating point values in an array. Once you’ve done that, the rest of the tasks are much easier to do. Do not store the text data as a string array; taking that approach will probably make things much more difficult for you. Also, if you find yourself writing nested for
loops, you’re probably doing something wrong; it’s not supposed to be that hard.
Your code should be adequately commented in the sense that I can see which parts correspond to which tasks. Also, any functions you write should have adequately written docstrings. As a reminder, while you can talk and work with each other on the homework, you have to write-up your own solution.
Whenever I talk about a “stock index” above, I do not mean the location of a value in a list or array but the value of a stock market benchmark like the S&P500 or DJIA stock index.
Deliverables
Your Python code file (you can name it anything you want, as long as it ends with the .py or .ipynb suffix). Please do not submit your image files. Please use comments to label the question numbers in your code.
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."