Skip to content

Instantly share code, notes, and snippets.

@pranjalchaubey
Last active September 22, 2019 12:13
Show Gist options
  • Save pranjalchaubey/6389f23ea63efbc1c26d7569bd5c85a4 to your computer and use it in GitHub Desktop.
Save pranjalchaubey/6389f23ea63efbc1c26d7569bd5c85a4 to your computer and use it in GitHub Desktop.
Selecting Liquid/Illiquid Stocks based on Trading Volume
# Load packages
import cvxpy as cvx
import numpy as np
import pandas as pd
import time
import project_tests
import project_helper
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = (14, 8)
import os
import project_helper
from zipline.data import bundles
from zipline.pipeline import Pipeline
from zipline.pipeline.factors import AverageDollarVolume
from zipline.utils.calendars import get_calendar
# Data Bundle
# Using Zipline to handle the data
os.environ['ZIPLINE_ROOT'] = os.path.join(os.getcwd(), '..', '..', 'data', 'US_eod_data')
ingest_func = bundles.csvdir.csvdir_equities(['daily'], project_helper.EOD_BUNDLE_NAME)
bundles.register(project_helper.EOD_BUNDLE_NAME, ingest_func)
print(os.getcwd())
print('Data Registered')
# Build the pipeline engine
universe = AverageDollarVolume(window_length=120).top(500)
trading_calendar = get_calendar('NYSE')
bundle_data = bundles.load(project_helper.EOD_BUNDLE_NAME)
engine = project_helper.build_pipeline_engine(bundle_data, trading_calendar)
# Extract the most liquid 500 stocks in the last 120 days
universe_end_date = pd.Timestamp('2016-01-05', tz='UTC')
universe_tickers = engine\
.run_pipeline(
Pipeline(screen=universe),
universe_end_date,
universe_end_date)\
.index.get_level_values(1)\
.values.tolist()
print('List of the 500 most liquid stocks in the last 120 days: ')
print(universe_tickers)
@pranjalchaubey
Copy link
Author

You need to have a csv file called, 'US_eod_data.csv', kept inside a 'data' folder for this snippet to work.
US_eod_data should have dates as its index, with stock symbols as its columns. It is supposed to have 'End of Day' or closing prices for each stock on each date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment