{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cs224 \n", "last updated: 2020-12-30 \n", "\n", "CPython 3.6.12\n", "IPython 7.16.1\n", "\n", "numpy 1.19.1\n", "pandas 0.22.0\n", "matplotlib 3.3.1\n", "seaborn 0.11.0\n", "sklearn 0.23.2\n", "h5py 2.10.0\n", "zipline 1.4.1\n", "pyfolio 0.9.2\n", "dill 0.3.2\n", "xlrd 1.2.0\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'cs224' -u -d -v -p numpy,pandas,matplotlib,seaborn,sklearn,h5py,zipline,pyfolio,dill,xlrd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import zipline\n", "from zipline.api import future_symbol, set_commission, set_slippage, schedule_function, date_rules, time_rules, continuous_future, order_target\n", "from datetime import datetime\n", "import pytz\n", "import matplotlib.pyplot as plt\n", "import pyfolio as pf\n", "import pandas as pd\n", "import numpy as np \n", "from zipline.finance.commission import PerTrade, PerContract\n", "from zipline.finance.slippage import VolumeShareSlippage, FixedSlippage, VolatilityVolumeShare\n", "\n", "import warnings\n", "import dill" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import display, HTML\n", "\n", "from IPython.display import display_html\n", "def display_side_by_side(*args):\n", " html_str=''\n", " for df in args:\n", " if type(df) == np.ndarray:\n", " df = pd.DataFrame(df)\n", " html_str+=df.to_html()\n", " html_str = html_str.replace('table','table style=\"display:inline\"')\n", " # print(html_str)\n", " display_html(html_str,raw=True)\n", "\n", "CSS = \"\"\"\n", ".output {\n", " flex-direction: row;\n", "}\n", "\"\"\"\n", "\n", "def display_graphs_side_by_side(*args):\n", " html_str=''\n", " for g in args:\n", " html_str += ''\n", " html_str += '
'\n", " html_str += g._repr_svg_()\n", " html_str += '
'\n", " display_html(html_str,raw=True)\n", " \n", "\n", "display(HTML(\"\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* [Methods of accessing Data in zipline (For Beginners)](https://github.com/quantopian/zipline/issues/1903)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "from zipline.utils.run_algo import load_extensions\n", "\n", "load_extensions(\n", " default=True,\n", " extensions=['/home/cs/.zipline/extension.py'],\n", " strict=True,\n", " environ=os.environ,\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from zipline.data.data_portal import DataPortal\n", "from zipline.data import bundles\n", "# from zipline.utils.calendars.calendar_utils import get_calendar\n", "from trading_calendars import register_calendar, get_calendar\n", "\n", "cal = get_calendar('us_futures')\n", "\n", "bundle_name = 'csi_futures_data' # \"a bundle name\"\n", "bundle_data = bundles.load(bundle_name)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "data_por = DataPortal(bundle_data.asset_finder, \n", " cal,\n", " bundle_data.equity_daily_bar_reader.first_trading_day,\n", " equity_minute_reader=bundle_data.equity_minute_bar_reader,\n", " equity_daily_reader=bundle_data.equity_daily_bar_reader,\n", " future_daily_reader=bundle_data.equity_daily_bar_reader,\n", " adjustment_reader=bundle_data.adjustment_reader)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "base_symbol = 'CT'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Future(350 [CTV00]),\n", " Future(351 [CTZ00]),\n", " Future(352 [CTH01]),\n", " Future(353 [CTK01]),\n", " Future(354 [CTN01])]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_por.asset_finder.retrieve_all(data_por.asset_finder._get_contract_sids(base_symbol))[:5]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ContinuousFuture(91008776454275072, root_symbol='CT', offset=0, roll_style='calendar', adjustment=None)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sym = data_por.asset_finder.create_continuous_future(base_symbol, offset=0, roll_style='calendar', adjustment=None)\n", "sym" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ContinuousFuture(91008776454275072 [CT, 0, calendar, None])
2017-01-13 00:00:00+00:000.723
2017-01-16 00:00:00+00:000.723
2017-01-17 00:00:00+00:000.721
2017-01-18 00:00:00+00:000.723
2017-01-19 00:00:00+00:000.727
\n", "
" ], "text/plain": [ " ContinuousFuture(91008776454275072 [CT, 0, calendar, None])\n", "2017-01-13 00:00:00+00:00 0.723 \n", "2017-01-16 00:00:00+00:00 0.723 \n", "2017-01-17 00:00:00+00:00 0.721 \n", "2017-01-18 00:00:00+00:00 0.723 \n", "2017-01-19 00:00:00+00:00 0.727 " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "frequency='1d'\n", "bar_count=250\n", "ldf1 = data_por.get_history_window(assets=[sym],\n", " end_dt=pd.Timestamp('2018-01-01', tz='utc'),\n", " bar_count=bar_count,\n", " frequency=frequency,\n", " data_frequency='daily',\n", " field='close')\n", "ldf1.head()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ContinuousFuture(91008776454275072 [CT, 0, calendar, None])
2017-12-26 00:00:00+00:000.774
2017-12-27 00:00:00+00:000.790
2017-12-28 00:00:00+00:000.788
2017-12-29 00:00:00+00:000.786
2018-01-02 00:00:00+00:000.775
\n", "
" ], "text/plain": [ " ContinuousFuture(91008776454275072 [CT, 0, calendar, None])\n", "2017-12-26 00:00:00+00:00 0.774 \n", "2017-12-27 00:00:00+00:00 0.790 \n", "2017-12-28 00:00:00+00:00 0.788 \n", "2017-12-29 00:00:00+00:00 0.786 \n", "2018-01-02 00:00:00+00:00 0.775 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ldf1.tail()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "dt=pd.Timestamp('2018-01-01', tz='utc')\n", "simulation_dt_func = lambda: pd.Timestamp(dt)\n", "data_frequency = 'daily'\n", "restrictions = zipline.finance.asset_restrictions.NoRestrictions()\n", "data = zipline._protocol.BarData(data_por, simulation_dt_func, data_frequency, cal, restrictions)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
closevolume
2017-01-13 00:00:00+00:000.72312870.0
2017-01-16 00:00:00+00:000.72312870.0
2017-01-17 00:00:00+00:000.72116974.0
2017-01-18 00:00:00+00:000.72312813.0
2017-01-19 00:00:00+00:000.72713939.0
\n", "
" ], "text/plain": [ " close volume\n", "2017-01-13 00:00:00+00:00 0.723 12870.0\n", "2017-01-16 00:00:00+00:00 0.723 12870.0\n", "2017-01-17 00:00:00+00:00 0.721 16974.0\n", "2017-01-18 00:00:00+00:00 0.723 12813.0\n", "2017-01-19 00:00:00+00:00 0.727 13939.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fields=['close', 'volume']\n", "h = data.history([sym], fields=fields, frequency=frequency, bar_count=bar_count)\n", "ldf2 = h.xs(sym, 2)\n", "ldf2.head()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
closevolume
2017-12-26 00:00:00+00:000.7749161.0
2017-12-27 00:00:00+00:000.79019521.0
2017-12-28 00:00:00+00:000.78813448.0
2017-12-29 00:00:00+00:000.78614101.0
2018-01-02 00:00:00+00:000.77522644.0
\n", "
" ], "text/plain": [ " close volume\n", "2017-12-26 00:00:00+00:00 0.774 9161.0\n", "2017-12-27 00:00:00+00:00 0.790 19521.0\n", "2017-12-28 00:00:00+00:00 0.788 13448.0\n", "2017-12-29 00:00:00+00:00 0.786 14101.0\n", "2018-01-02 00:00:00+00:00 0.775 22644.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ldf2.tail()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.12" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }