Let's start by grabbing some data. For the purposes of this chapter, we'll use data from Quandl, a longtime favorite of technically adept independent investors. Quandl makes data available on many stocks using a number of mechanisms. One easy mechanism is via a URL API. To get stock data on, say, Google Stock, we can click on https://www.quandl.com/api/v3/datasets/WIKI/GOOG/data.json. Similarly, we can replace GOOG with other index codes to grab data on other stocks.
This is fairly easy to automate via Python; we will use the following code to do so:
import requests API_KEY = '<your_api_key>' start_date = '2010-01-01' end_date = '2015-01-01' order = 'asc' column_index = 4 stock_exchange = 'WIKI' index = 'GOOG' data_specs = 'start_date={}&end_date={}&order={}&column_index={}&api_key={}' .format(start_date, end_date, order, column_index, API_KEY) base_url = "https://www.quandl.com/api/v3/datasets/{}/{}/data.json?" + data_specs stock_data = requests.get(base_url.format(stock_exchange, index)).json()
So, here, in the stock_data variable, you'll have the stock data variable from WIKI/GOOG into the stock_data variable, downloaded from the formatted URL between the dates 2010-01-01 and 2015-01-01. The column_index = 4 variables is telling the server to get only the closing values from the history.
So, what are these closing values? Well, stock prices fluctuate every day. They open with a certain value and they reach a certain high value and a certain low value within a day, and, at the end of the day, they close with a certain value. The following image shows how the stock prices change within each day:
So, after the stock opens, you can invest in them and buy shares. At the end of the day, you'll have either a profit or a loss, depending on the closing values of those shares bought. Investors use different techniques to predict which stocks have the potential to rise on a particular day, and, depending on their analysis, they invest in shares.