Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Talk about getting started with quantitative investing using Python

0 x00 preface

Quantitative trading, which uses computer technology (Python in this article) to help investors analyze large amounts of data to develop investment strategies, is an area at the intersection of finance and computing.

This article is intended to guide beginners to using Python for quantitative trading

0x01 Extract Data

precondition

To extract stock data, we need to use the API of Quandl, which has a lot of economic and financial data.

First we need to install Python 3 and Virtualenv and pass

virtualenv --python=/usr/bin/python3 <name of env>
Copy the code

Create a virtual environment.

Then use the

source <env_name>/bin/activate
Copy the code

Activate the virtual environment.

Install jupyter-notebook with PIP:

pip install jupyter-notebook
Copy the code

Then install the pandas, Quandl, and NUMpy packages.

Finally, run Jupyter-Notebook.

Extract the data

We import the required packages first.

import pandas as pd
import quandl as q
Copy the code

Here pandas can help with data manipulation and drawing.

Then we call the Quandl API.

Q.anpponfig. Api_key =" <API key> "MSft_data = q.get("EOD/MSFT", start_date="2010-01-01", end_date="2019-01-01") msft_data.head()Copy the code

In this code, we first set up the required API_key. You need to go to the official website for this.

The get method is then called to get the Microsoft stock price from January 1, 2010 to January 1, 2019.

Then look at the first five rows of the retrieved dataset.

0x02 Analyze Data

Let’s look at the data composition of the data set, which can be viewed through the INFO method.

As shown in the figure above, we can see that these data contain stock price information.

Such as the stock’s opening and closing prices, adjusted opening and closing prices, this adjustment takes into account actions such as dividend distributions, stock splits, as well as trading volume and the day’s high and low prices.

We can also analyze the maximum, minimum, average, and other data of a dataset using the Describe method.

We can also continue to process these data.

Let’s say we sample again. Change the interval to monthly, quarterly, or yearly.

0x04 Calculate financial return

We then calculate the financial return.

We’ll use the pct_change() method.

import numpy as np daily_close = msft_data[['Adj_Close']] daily_return = daily_close.pct_change() daily_return.fillna(0,  inplace=True) print(daily_return)Copy the code

We use the adjusted closing price to calculate returns from start time to time. Multiply that by 100 to get the percentage return.

We can also calculate the monthly rate of return over time:

mdata = msft_data.resample('M').apply(lambda x: x[-1])
monthly_return = mdata.pct_change()
monthly_return.fillna(0, inplace=True)
print(monthly_return)
Copy the code

Part of the code reference to: www.codementor.io/blog/quanti…