fastquant lesson 1

Introduction to fastquant

fastquant is a python package that allows you easily access stock data with as few as 2 lines of python code.

Its goal is to promote data driven investments by making quantitative analysis in finance accessible to everyone.

Objectives and strategies

1) Raise awareness, and guide beginners to get a sufficient foundation by teaching the basics of quant analysis with lectures presented via blog posts & online lectures

2) Make quant analysis simple and easy w/ a high level python API that allows for lower level configuration for more advanced users - development of an easy to use python package to facilitate data driven investments

Top down approach to lectures and package design

We start w/ immediate usefulness and then work towards “deep” understanding later as more experience is gained. This approach will be reflected in the design of the blog posts, lectures, and the fastquant package.

Expect 1 - 2 lessons per month

medium article + notebook -> webinar

Medium article + notebook takes 1-2 weeks, while webinar will take 1-2 weeks. So the pace will generally be 1-2 webinars per month.

Webinars will be recorded and posted as content on facebook/youtube.

Course Outline (Part 1) - Backtesting Classic Technical Analysis Indicators

1. Lecture 1: Accessing PSE data in 3 lines of code
    1. Accessing PSE data in 3 lines of code
    2. Plotting a basic SMAC strategy
    3. The idea of backtesting
2. Lecture 2: Backtest your trading strategy with 5 lines of code
3. Lecture 3: Relative Strength Index (Intro + backtesting)
4. Lecture 4: Bollinger Bands (Intro + backtesting)
5. Lecture 5: Moving Average Convergence Divergence (Intro + backtesting)
6. Lecture 6: Backtesting multiple strategies at the same time

Course Outline (Part 2) [TBD] - Building Advanced Indicators w/ Statistics & Machine Learning based Approaches + other financial indicators

Setup

1. Let's make sure everyone gets Google Colab working
2. Show github and encourage people to make an account (if they want to contribute to fastquant).
3. Please star the repo if you find it useful! :)

Installation

Installing fastquant is super simple since it's on pypi!

!pip3 install fastquant
Collecting fastquant
  Downloading https://files.pythonhosted.org/packages/8b/4f/392ad2117711ed868789c9fc3a183f6079b5eabcac7e286b78651f8ac8ab/fastquant-0.1.3.14-py3-none-any.whl (5.0MB)
     |████████████████████████████████| 5.0MB 693kB/s eta 0:00:01
Requirement already satisfied: numpy==1.18.0 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.18.0)
Requirement already satisfied: pre-commit in /usr/local/lib/python3.7/site-packages (from fastquant) (2.4.0)
Requirement already satisfied: pandas==1.0.3 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.0.3)
Requirement already satisfied: python-dateutil==2.8.1 in /usr/local/lib/python3.7/site-packages (from fastquant) (2.8.1)
Requirement already satisfied: backtrader==1.9.75.123 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.9.75.123)
Requirement already satisfied: urllib3==1.25.7 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.25.7)
Requirement already satisfied: six==1.13.0 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.13.0)
Requirement already satisfied: pytz==2019.3 in /usr/local/lib/python3.7/site-packages (from fastquant) (2019.3)
Requirement already satisfied: black==19.10b0 in /usr/local/lib/python3.7/site-packages (from fastquant) (19.10b0)
Requirement already satisfied: oauthlib==3.1.0 in /usr/local/lib/python3.7/site-packages (from fastquant) (3.1.0)
Requirement already satisfied: chardet==3.0.4 in /usr/local/lib/python3.7/site-packages (from fastquant) (3.0.4)
Requirement already satisfied: matplotlib==3.1.2 in /usr/local/lib/python3.7/site-packages (from fastquant) (3.1.2)
Requirement already satisfied: lxml==4.4.2 in /usr/local/lib/python3.7/site-packages (from fastquant) (4.4.2)
Requirement already satisfied: tqdm==4.28.1 in /usr/local/lib/python3.7/site-packages (from fastquant) (4.28.1)
Requirement already satisfied: requests-oauthlib==1.3.0 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.3.0)
Requirement already satisfied: idna==2.8 in /usr/local/lib/python3.7/site-packages (from fastquant) (2.8)
Requirement already satisfied: yfinance==0.1.54 in /usr/local/lib/python3.7/site-packages (from fastquant) (0.1.54)
Requirement already satisfied: beautifulsoup4==4.8.2 in /usr/local/lib/python3.7/site-packages (from fastquant) (4.8.2)
Requirement already satisfied: PySocks==1.7.1 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.7.1)
Requirement already satisfied: certifi==2019.11.28 in /usr/local/lib/python3.7/site-packages (from fastquant) (2019.11.28)
Requirement already satisfied: bs4==0.0.1 in /usr/local/lib/python3.7/site-packages (from fastquant) (0.0.1)
Requirement already satisfied: soupsieve==1.9.5 in /usr/local/lib/python3.7/site-packages (from fastquant) (1.9.5)
Requirement already satisfied: tweepy==3.8.0 in /usr/local/lib/python3.7/site-packages (from fastquant) (3.8.0)
Requirement already satisfied: networkx==2.4 in /usr/local/lib/python3.7/site-packages (from fastquant) (2.4)
Requirement already satisfied: requests==2.22.0 in /usr/local/lib/python3.7/site-packages (from fastquant) (2.22.0)
Requirement already satisfied: identify>=1.0.0 in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (1.4.18)
Requirement already satisfied: nodeenv>=0.11.1 in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (1.3.5)
Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (5.3.1)
Requirement already satisfied: virtualenv>=20.0.8 in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (20.0.21)
Requirement already satisfied: cfgv>=2.0.0 in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (3.1.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (1.6.0)
Requirement already satisfied: toml in /usr/local/lib/python3.7/site-packages (from pre-commit->fastquant) (0.10.1)
Requirement already satisfied: appdirs in /usr/local/lib/python3.7/site-packages (from black==19.10b0->fastquant) (1.4.4)
Requirement already satisfied: regex in /usr/local/lib/python3.7/site-packages (from black==19.10b0->fastquant) (2020.5.14)
Requirement already satisfied: click>=6.5 in /usr/local/lib/python3.7/site-packages (from black==19.10b0->fastquant) (7.1.2)
Requirement already satisfied: attrs>=18.1.0 in /usr/local/lib/python3.7/site-packages (from black==19.10b0->fastquant) (19.3.0)
Requirement already satisfied: typed-ast>=1.4.0 in /usr/local/lib/python3.7/site-packages (from black==19.10b0->fastquant) (1.4.1)
Requirement already satisfied: pathspec<1,>=0.6 in /usr/local/lib/python3.7/site-packages (from black==19.10b0->fastquant) (0.8.0)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/site-packages (from matplotlib==3.1.2->fastquant) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/site-packages (from matplotlib==3.1.2->fastquant) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/site-packages (from matplotlib==3.1.2->fastquant) (1.2.0)
Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.7/site-packages (from yfinance==0.1.54->fastquant) (0.0.9)
Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.7/site-packages (from networkx==2.4->fastquant) (4.4.2)
Requirement already satisfied: distlib<1,>=0.3.0 in /usr/local/lib/python3.7/site-packages (from virtualenv>=20.0.8->pre-commit->fastquant) (0.3.0)
Requirement already satisfied: filelock<4,>=3.0.0 in /usr/local/lib/python3.7/site-packages (from virtualenv>=20.0.8->pre-commit->fastquant) (3.0.12)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->pre-commit->fastquant) (3.1.0)
Installing collected packages: fastquant
Successfully installed fastquant-0.1.3.14

Get stock data from stock in 2 lines of code!

Here I demonstrate how to get the stock data of Jollibee (JFC is the stock symbol) from January 1, 2018 to January 1, 2019.

Please ensure that your date strings are in the format YYYY-MM-DD.

You can find a list of company names and PSE stock symbols here, and Yahoo Finance symbols here

from fastquant import get_stock_data
df = get_stock_data('JFC', '2018-01-01', '2019-01-01')
df.head()
close
dt
2018-01-03 255.4
2018-01-04 255.0
2018-01-05 255.0
2018-01-08 256.0
2018-01-09 255.8
help(get_stock_data)
Help on function get_stock_data in module fastquant.fastquant:

get_stock_data(symbol, start_date, end_date, source='phisix', format='c')
    Returns pricing data for a specified stock and source.
    
    Parameters
    ----------
    symbol : str
        Symbol of the stock in the PSE or Yahoo.
        You can refer to these links:
        PHISIX: https://www.pesobility.com/stock
        YAHOO: https://www.nasdaq.com/market-activity/stocks/screener?exchange=nasdaq
    start_date : str
        Starting date (YYYY-MM-DD) of the period that you want to get data on
    end_date : str
        Ending date (YYYY-MM-DD) of the period you want to get data on
    source : str
        First source to query from ("pse", "yahoo").
        If the stock is not found in the first source,
        the query is run on the other source.
    format : str
        Format of the output data
    
    Returns
    -------
    pandas.DataFrame
        Stock data (in the specified `format`) for the specified company and date range

Plot daily closing prices

For this part, we'll want to import a plotting package, matplotlib, to be able to visualize the closing prices of Jollibee. Don't worry if you don't understand how this works - for now, just take the code as is.

from matplotlib import pyplot as plt
df.close.plot(figsize=(10, 6))
plt.title("Daily Closing Prices of JFC\nfrom 2018-01-01 to 2019-01-01", fontsize=20)
Text(0.5, 1.0, 'Daily Closing Prices of JFC\nfrom 2018-01-01 to 2019-01-01')

Analyze with a simple moving average crossover (SMAC) trading strategy

Now that we have the data ready, let's visualize a sample SMAC strategy.

First, let's import the pandas library (general purpose library for handling tabular data).

import pandas as pd

Next, we calculate the 30 day moving average of Jollibee's closing price and combine it with the original closing price data.

ma30 = df.close.rolling(30).mean()
close_ma30 = pd.concat([df.close, ma30], axis=1).dropna()
close_ma30.columns = ['Closing Price', 'Simple Moving Average (30 day)']
ma30.dropna()
dt
2018-02-13    276.586667
2018-02-14    277.340000
2018-02-15    278.340000
2018-02-19    279.340000
2018-02-20    280.340000
                 ...    
2018-12-20    287.446667
2018-12-21    288.193333
2018-12-26    288.660000
2018-12-27    289.360000
2018-12-28    289.993333
Name: close, Length: 214, dtype: float64
ma30.dropna().plot(figsize=(10, 6))
<matplotlib.axes._subplots.AxesSubplot at 0x114e25890>

Lastly, we plot the trends for the daily closing prices and the 30 day SMA together

close_ma30.plot(figsize=(10, 6))
plt.title("Daily Closing Prices vs 30 day SMA of JFC\nfrom 2018-01-01 to 2019-01-01", fontsize=20)
Text(0.5, 1.0, 'Daily Closing Prices vs 30 day SMA of JFC\nfrom 2018-01-01 to 2019-01-01')