-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
374 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,374 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This exercise will require you to pull some data from the Qunadl API. Qaundl is currently the most widely used aggregator of financial market data." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"As a first step, you will need to register a free account on the http://www.quandl.com website." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"After you register, you will be provided with a unique API key, that you should store:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Store the API key as a string - according to PEP8, constants are always named in all upper case\n", | ||
"API_KEY = ''" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Qaundl has a large number of data sources, but, unfortunately, most of them require a Premium subscription. Still, there are also a good number of free datasets." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"For this mini project, we will focus on equities data from the Frankfurt Stock Exhange (FSE), which is available for free. We'll try and analyze the stock prices of a company called Carl Zeiss Meditec, which manufactures tools for eye examinations, as well as medical lasers for laser eye surgery: https://www.zeiss.com/meditec/int/home.html. The company is listed under the stock ticker AFX_X." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can find the detailed Quandl API instructions here: https://docs.quandl.com/docs/time-series" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"While there is a dedicated Python package for connecting to the Quandl API, we would prefer that you use the *requests* package, which can be easily downloaded using *pip* or *conda*. You can find the documentation for the package here: http://docs.python-requests.org/en/master/ " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Finally, apart from the *requests* package, you are encouraged to not use any third party Python packages, such as *pandas*, and instead focus on what's available in the Python Standard Library (the *collections* module might come in handy: https://pymotw.com/3/collections/ ).\n", | ||
"Also, since you won't have access to DataFrames, you are encouraged to us Python's native data structures - preferably dictionaries, though some questions can also be answered using lists.\n", | ||
"You can read more on these data structures here: https://docs.python.org/3/tutorial/datastructures.html" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Keep in mind that the JSON responses you will be getting from the API map almost one-to-one to Python's dictionaries. Unfortunately, they can be very nested, so make sure you read up on indexing dictionaries in the documentation provided above." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# First, import the relevant modules\n", | ||
"import requests\n", | ||
"import collections\n", | ||
"import json\n", | ||
"from statistics import mean\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"200" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Now, call the Quandl API and pull out a small sample of the data (only one day) to get a glimpse\n", | ||
"# into the JSON structure that will be returned\n", | ||
"dcollect= requests.get('https://www.quandl.com/api/v3/datasets/FSE/AFX_X.json?start_date=2020-01-01&end_date=2020-01-02&api_key=')\n", | ||
"\n", | ||
"dcollect.status_code\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"dataset: {'id': 10095370, 'dataset_code': 'AFX_X', 'database_code': 'FSE', 'name': 'Carl Zeiss Meditec (AFX_X)', 'description': 'Stock Prices for Carl Zeiss Meditec (2020-11-02) from the Frankfurt Stock Exchange.<br><br>Trading System: Xetra<br><br>ISIN: DE0005313704', 'refreshed_at': '2020-12-01T14:48:09.907Z', 'newest_available_date': '2020-12-01', 'oldest_available_date': '2000-06-07', 'column_names': ['Date', 'Open', 'High', 'Low', 'Close', 'Change', 'Traded Volume', 'Turnover', 'Last Price of the Day', 'Daily Traded Units', 'Daily Turnover'], 'frequency': 'daily', 'type': 'Time Series', 'premium': False, 'limit': None, 'transform': None, 'column_index': None, 'start_date': '2020-01-01', 'end_date': '2020-01-02', 'data': [['2020-01-02', 114.0, 114.9, 112.7, 113.8, None, 143464.0, 16291666.0, None, None, None]], 'collapse': None, 'order': None, 'database_id': 6129}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Inspect the JSON structure of the object you created, and take note of how nested it is,\n", | ||
"# as well as the overall structure\n", | ||
"\n", | ||
"\n", | ||
"dcol_dict=dcollect.json()\n", | ||
"\n", | ||
"for k in dcol_dict.keys():\n", | ||
" print(k + ': ', dcol_dict[k])\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'The highest opening price for 2017 was: 53.11 and The lowest opening price for 2017 was: 34.0'" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#Collect data from the Franfurt Stock Exchange, for the ticker AFX_X, for the whole year 2017\n", | ||
"\n", | ||
"dyear=requests.get('https://www.quandl.com/api/v3/datasets/FSE/AFX_X.json?start_date=2017-01-01&end_date=2017-12-31&api_key=')\n", | ||
"\n", | ||
"dyear.status_code\n", | ||
"\n", | ||
"#Convert the returned JSON object into a Python dictionary.\n", | ||
"dyeardict=dyear.json()\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"# highest and lowest opening prices were for the stock in this period\n", | ||
"\n", | ||
"openlist = []\n", | ||
"\n", | ||
"for item in dyeardict['dataset']['data']:\n", | ||
" \n", | ||
" if item[1] != None:\n", | ||
" \n", | ||
" openlist.append((item[1]))\n", | ||
" \n", | ||
"\n", | ||
"openlistso = sorted(openlist, key = float) \n", | ||
"\n", | ||
"\"The highest opening price for 2017 was: \" + str(max(openlistso)) + \" and\" + \" The lowest opening price for 2017 was: \" + str(min(openlistso))\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
" \n", | ||
"\n", | ||
" \n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'The largest change in any one day was: 2.8100000000000023'" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#What was the largest change in any one day (based on High and Low price)?\n", | ||
"\n", | ||
"changelist=[]\n", | ||
"\n", | ||
"for item in dyeardict['dataset']['data']:\n", | ||
" \n", | ||
" change=item[2]-item[3]\n", | ||
" changelist.append(change)\n", | ||
"\n", | ||
"changeso= sorted(changelist, key = float)\n", | ||
"\n", | ||
"\n", | ||
"\"The largest change in any one day was: \" + str(max(changeso))\n", | ||
" \n", | ||
" \n", | ||
" \n", | ||
" \n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'The largest change between any two days (based on Closing Price) was: 19.03'" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#What was the largest change between any two days (based on Closing Price)?\n", | ||
"\n", | ||
"listforchange=[]\n", | ||
"for item in dyeardict['dataset']['data']:\n", | ||
" \n", | ||
" listforchange.append(item[4])\n", | ||
" \n", | ||
" \n", | ||
"listforchangeso = (sorted(listforchange, key=float, reverse=True))\n", | ||
"\n", | ||
"\n", | ||
"change= (max(listforchangeso) - min(listforchangeso))\n", | ||
"\n", | ||
"\"The largest change between any two days (based on Closing Price) was: \" + str(change)\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'The average daily trading volume during this year was: 89124.33725490196'" | ||
] | ||
}, | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#What was the average daily trading volume during this year?\n", | ||
"\n", | ||
"traded=[]\n", | ||
"\n", | ||
"for item in dyeardict['dataset']['data']:\n", | ||
" \n", | ||
" traded.append(item[6])\n", | ||
" \n", | ||
"\"The average daily trading volume during this year was: \" + str(mean(traded))\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The median trading volume during this year was: 76286.0\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#What was the median trading volume during this year?\n", | ||
"\n", | ||
"tradedso=sorted(traded, key=float)\n", | ||
"#print(tradedso)\n", | ||
"\n", | ||
"#print(len(tradedso))\n", | ||
"\n", | ||
"mid=int(len(tradedso)/2)\n", | ||
"\n", | ||
"print(\"The median trading volume during this year was: \" + str(tradedso[mid]))\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"These are your tasks for this mini project:\n", | ||
"\n", | ||
"1. Collect data from the Franfurt Stock Exchange, for the ticker AFX_X, for the whole year 2017 (keep in mind that the date format is YYYY-MM-DD).\n", | ||
"2. Convert the returned JSON object into a Python dictionary.\n", | ||
"3. Calculate what the highest and lowest opening prices were for the stock in this period.\n", | ||
"4. What was the largest change in any one day (based on High and Low price)?\n", | ||
"5. What was the largest change between any two days (based on Closing Price)?\n", | ||
"6. What was the average daily trading volume during this year?\n", | ||
"7. (Optional) What was the median trading volume during this year. (Note: you may need to implement your own function for calculating the median.)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |