├── AUTHORS ├── LICENSE ├── MANIFEST.in ├── README.rst ├── Vagrantfile ├── _config.yml ├── appveyor.yml ├── ci ├── appveyor │ ├── install.ps1 │ ├── run_with_env.cmd │ └── vcvars64.bat └── make_conda_packages.py ├── conda ├── README.md ├── bcolz │ ├── bld.bat │ ├── build.sh │ └── meta.yaml ├── cyordereddict │ ├── bld.bat │ ├── build.sh │ └── meta.yaml ├── logbook │ ├── bld.bat │ ├── build.sh │ └── meta.yaml ├── numexpr │ ├── bld.bat │ ├── build.sh │ └── meta.yaml ├── setuptools_scm │ ├── bld.bat │ ├── build.sh │ └── meta.yaml ├── ta-lib │ ├── bld.bat │ ├── build.sh │ └── meta.yaml └── zipline │ ├── bld.bat │ ├── build.sh │ └── meta.yaml ├── docs ├── .gitignore ├── CNAME ├── Makefile ├── deploy.py ├── make.bat ├── notebooks │ └── tutorial.ipynb └── source │ ├── appendix.rst │ ├── beginner-tutorial.rst │ ├── conf.py │ ├── dev-doc-message.txt │ ├── index.rst │ ├── install.rst │ ├── release-process.rst │ ├── releases.rst │ ├── tutorial_files │ ├── tutorial_11_2.png │ └── tutorial_22_1.png │ └── whatsnew │ ├── 0.6.1.txt │ ├── 0.7.0.txt │ ├── 0.8.0.txt │ ├── 0.8.3.txt │ ├── 0.8.4.txt │ ├── 0.9.0.txt │ └── skeleton.txt ├── etc ├── conda_build_matrix.py ├── create_authors_file.sh ├── git-hooks │ └── pre-commit ├── goodies.txt ├── ordered_pip.sh ├── requirements.txt ├── requirements_blaze.txt ├── requirements_dev.txt ├── requirements_docs.txt └── requirements_talib.txt ├── requirements.txt ├── scripts ├── generate_new_sample_saved_state.py └── run_algo.py ├── setup.cfg ├── setup.py ├── tests ├── __init__.py ├── data │ ├── __init__.py │ ├── test_minute_bars.py │ └── test_us_equity_pricing.py ├── finance │ ├── __init__.py │ └── test_slippage.py ├── history_cases.py ├── pipeline │ ├── __init__.py │ ├── base.py │ ├── test_adjusted_array.py │ ├── test_adjustment.py │ ├── test_blaze.py │ ├── test_buyback_auth.py │ ├── test_classifier.py │ ├── test_column.py │ ├── test_dividends.py │ ├── test_earnings.py │ ├── test_engine.py │ ├── test_events.py │ ├── test_factor.py │ ├── test_filter.py │ ├── test_frameload.py │ ├── test_numerical_expression.py │ ├── test_pipeline.py │ ├── test_pipeline_algo.py │ ├── test_term.py │ └── test_us_equity_pricing_loader.py ├── resources │ ├── pipeline_inputs │ │ ├── AAPL.csv │ │ ├── BRK-A.csv │ │ ├── MSFT.csv │ │ └── generate.py │ └── saved_state_archive │ │ ├── zipline.finance.blotter.Blotter │ │ └── State_Version_1 │ │ ├── zipline.finance.blotter.Order │ │ └── State_Version_1 │ │ ├── zipline.finance.commission.PerDollar │ │ └── State_Version_1 │ │ ├── zipline.finance.commission.PerShare │ │ └── State_Version_1 │ │ ├── zipline.finance.commission.PerTrade │ │ └── State_Version_1 │ │ ├── zipline.finance.performance.period.PerformancePeriod │ │ ├── State_Version_1 │ │ └── State_Version_2 │ │ ├── zipline.finance.performance.position.Position │ │ └── State_Version_1 │ │ ├── zipline.finance.performance.position_tracker.PositionTracker │ │ └── State_Version_1 │ │ ├── zipline.finance.performance.tracker.PerformanceTracker │ │ └── State_Version_3 │ │ ├── zipline.finance.risk.cumulative.RiskMetricsCumulative │ │ └── State_Version_2 │ │ ├── zipline.finance.risk.period.RiskMetricsPeriod │ │ └── State_Version_2 │ │ ├── zipline.finance.risk.report.RiskReport │ │ └── State_Version_1 │ │ ├── zipline.finance.slippage.FixedSlippage │ │ └── State_Version_1 │ │ ├── zipline.finance.slippage.Transaction │ │ └── State_Version_1 │ │ ├── zipline.finance.slippage.VolumeShareSlippage │ │ └── State_Version_1 │ │ ├── zipline.protocol.Account │ │ └── State_Version_1 │ │ ├── zipline.protocol.Portfolio │ │ └── State_Version_1 │ │ └── zipline.protocol.Position │ │ └── State_Version_1 ├── risk │ ├── AnswerKeyAnnotations.ipynb │ ├── AnswerKeyLink.ipynb │ ├── __init__.py │ ├── annotation_utils.py │ ├── answer_key.py │ ├── risk-answer-key-checksums │ ├── test_risk_cumulative.py │ ├── test_risk_period.py │ └── upload_answer_key.py ├── serialization_cases.py ├── test_algorithm.py ├── test_algorithm_gen.py ├── test_assets.py ├── test_batchtransform.py ├── test_blotter.py ├── test_cli.py ├── test_doctests.py ├── test_events_through_risk.py ├── test_examples.py ├── test_exception_handling.py ├── test_execution_styles.py ├── test_finance.py ├── test_history.py ├── test_memoize.py ├── test_munge.py ├── test_perf_tracking.py ├── test_pickle_serialization.py ├── test_rolling_panel.py ├── test_security_list.py ├── test_serialization.py ├── test_sources.py ├── test_testing.py ├── test_tradesimulation.py ├── test_tradingcalendar.py ├── test_transforms.py ├── test_transforms_talib.py ├── test_versioning.py └── utils │ ├── __init__.py │ ├── test_argcheck.py │ ├── test_cache.py │ ├── test_events.py │ ├── test_factory.py │ ├── test_final.py │ ├── test_numpy_utils.py │ ├── test_preprocess.py │ └── test_sentinel.py ├── vagrant_init.sh ├── versioneer.py └── zipline ├── __init__.py ├── _version.py ├── algorithm.py ├── api.py ├── assets ├── __init__.py ├── _assets.pyx ├── asset_db_migrations.py ├── asset_db_schema.py ├── asset_writer.py ├── assets.py └── futures.py ├── data ├── __init__.py ├── _adjustments.pyx ├── _equities.pyx ├── benchmarks.py ├── constants.py ├── data_portal.py ├── future_pricing.py ├── loader.py ├── minute_bars.py ├── mongodb.py ├── paths.py ├── treasuries.py ├── treasuries_can.py ├── us_equity_pricing.py └── xlsx │ ├── 2002.xlsx │ ├── 2003.xlsx │ ├── 2004.xlsx │ ├── 2005.xlsx │ ├── 2006.xlsx │ ├── 2007.xlsx │ ├── 2008.xlsx │ ├── 2009.xlsx │ ├── 2010.xlsx │ ├── 2011.xlsx │ ├── 2012.xlsx │ ├── 2013.xlsx │ ├── 2014.xlsx │ ├── 2015.xlsx │ ├── 2016.xlsx │ └── __init__.py ├── errors.py ├── examples ├── buy_and_hold.py ├── doubleMA.py └── stock_select.py ├── finance ├── __init__.py ├── blotter.py ├── commission.py ├── constants.py ├── controls.py ├── execution.py ├── order.py ├── performance │ ├── __init__.py │ ├── period.py │ ├── position.py │ ├── position_tracker.py │ └── tracker.py ├── risk │ ├── __init__.py │ ├── cumulative.py │ ├── period.py │ ├── report.py │ └── risk.py ├── slippage.py ├── trading.py └── transaction.py ├── gens ├── __init__.py ├── composites.py ├── tradesimulation.py └── utils.py ├── history ├── __init__.py ├── history.py └── history_container.py ├── lib ├── __init__.py ├── _float64window.pyx ├── _int64window.pyx ├── _uint8window.pyx ├── _windowtemplate.pxi ├── adjusted_array.py ├── adjustment.pyx ├── normalize.py ├── quantiles.py └── rank.pyx ├── pipeline ├── __init__.py ├── classifiers │ ├── __init__.py │ └── classifier.py ├── common.py ├── data │ ├── __init__.py │ ├── buyback_auth.py │ ├── dataset.py │ ├── dividends.py │ ├── earnings.py │ ├── equity_pricing.py │ └── testing.py ├── engine.py ├── expression.py ├── factors │ ├── __init__.py │ ├── events.py │ ├── factor.py │ └── technical.py ├── filters │ ├── __init__.py │ └── filter.py ├── graph.py ├── loaders │ ├── __init__.py │ ├── base.py │ ├── blaze │ │ ├── __init__.py │ │ ├── buyback_auth.py │ │ ├── core.py │ │ ├── dividends.py │ │ ├── earnings.py │ │ └── events.py │ ├── buyback_auth.py │ ├── dividends.py │ ├── earnings.py │ ├── equity_pricing_loader.py │ ├── events.py │ ├── frame.py │ ├── synthetic.py │ ├── testing.py │ └── utils.py ├── mixins.py ├── pipeline.py ├── term.py └── visualize.py ├── protocol.py ├── resources └── security_lists │ └── leveraged_etf_list │ └── 20020103 │ ├── 20120913 │ ├── add │ └── delete │ ├── 20120919 │ ├── add │ └── delete │ ├── 20121012 │ ├── add │ └── delete │ ├── 20130605 │ ├── add │ └── delete │ ├── 20130916 │ ├── add │ └── delete │ ├── 20131002 │ ├── add │ └── delete │ ├── 20131009 │ ├── add │ └── delete │ ├── 20131121 │ ├── add │ └── delete │ ├── 20131227 │ ├── add │ └── delete │ ├── 20140410 │ ├── add │ └── delete │ ├── 20140923 │ ├── add │ └── delete │ ├── 20141119 │ ├── add │ └── delete │ ├── 20141226 │ ├── add │ └── delete │ └── 20150123 │ ├── add │ └── delete ├── sources ├── __init__.py ├── data_frame_source.py ├── data_source.py ├── simulated.py └── test_source.py ├── test_algorithms.py ├── testing ├── __init__.py ├── core.py └── fixtures.py ├── transforms ├── __init__.py ├── batch_transform.py └── ta.py └── utils ├── __init__.py ├── algo_instance.py ├── api_support.py ├── argcheck.py ├── cache.py ├── cli.py ├── context_tricks.py ├── control_flow.py ├── data.py ├── data_source_tables_gen.py ├── deprecate.py ├── enum.py ├── events.py ├── factory.py ├── final.py ├── functional.py ├── input_validation.py ├── math_utils.py ├── memoize.py ├── munge.py ├── numpy_utils.py ├── pandas_utils.py ├── preprocess.py ├── security_list.py ├── sentinel.py ├── serialization_utils.py ├── simfactory.py ├── tradingcalendar.py ├── tradingcalendar_bmf.py ├── tradingcalendar_china.py ├── tradingcalendar_lse.py └── tradingcalendar_tse.py /AUTHORS: -------------------------------------------------------------------------------- 1 | Eddie Hebert 2 | fawce 3 | Thomas Wiecki 4 | Stephen Diehl 5 | scottsanderson 6 | Scott Sanderson 7 | Richard Frank 8 | Jonathan Kamens 9 | twiecki 10 | Joe Jevnik 11 | Delaney Granizo-Mackenzie 12 | Tobias Brandt 13 | Ben McCann 14 | John Ricklefs 15 | Jenkins T. Quantopian, III 16 | Jeremiah Lowin 17 | jbredeche 18 | Brian Fink 19 | David Edwards 20 | Matti Hanninen 21 | Ryan Day 22 | llllllllll 23 | David Stephens 24 | Tim 25 | Dale Jung 26 | Jamie Kirkpatrick 27 | Jean Bredeche 28 | Wes McKinney 29 | jikamens 30 | Aidan 31 | Colin Alexander 32 | Elektra58 33 | Jason Kölker 34 | Jeremi Joslin 35 | Luke Schiefelbein 36 | Martin Dengler 37 | Mete Atamel 38 | Michael Schatzow 39 | Moises Trovo 40 | Nicholas Pezolano 41 | Pankaj Garg 42 | Paolo Bernardi 43 | Peter Cawthron 44 | Philipp Kosel 45 | Suminda Dharmasena 46 | The Gitter Badger 47 | Tony Lambiris 48 | Tony Worm 49 | stanh 50 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | 3 | include etc/requirements*.txt 4 | recursive-include zipline/resources *.* 5 | recursive-include zipline *.pyx 6 | recursive-include zipline *.pxi 7 | include versioneer.py 8 | include zipline/_version.py 9 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Zipline-一个正在成长的项目 2 | ======= 3 | Zipline:当前在线的量化平台基本都是基于zipline进行开发,使用这些平台,首先,自己的 4 | 策略会泄密,其次,这些平台速度慢,而且不够灵活。 5 | 然而,A股并不能直接使用zipline,需要对数据,基准,交易日期,手续费等部分做修改。 6 | 本项目修改zipline平台,以使得其能适用于A股市场。 7 | 8 | 9 | 项目文档: 10 | 11 | https://github.com/zhanghan1990/zipline/wiki 12 | 13 | 安装方法 14 | ======== 15 | 运行环境:linux, OSX,建议不要使用windows,因为zipline涉及到gcc的编译,windows可能编译过程中有一些问题。 16 | 17 | (1)windows 用户可以下载virtual box,在这个镜像中,集成了数据,和开发环境,以及ipython notebook,virtual box 虚拟机地址: https://pan.baidu.com/s/1bp5roxL 18 | 19 | 虚拟机密码为:zipline 20 | 21 | 具体使用方法:打开virtual box ,输入以下命令 22 | 23 | - service mongodb start 24 | 25 | - source zipline/zip_env/bin/activate 26 | 27 | - sudo jupyter notebook 28 | 29 | - ifconfig 30 | 31 | 得到虚拟机的IP 地址,例如IP为:192.168.1.120,则在windows浏览器输入 192.168.1.120:8888 32 | 33 | 34 | (2)对于本地安装,以ubuntu 为例: 35 | 36 | - git clone https://github.com/zhanghan1990/zipline 37 | - cd zipline 38 | - sudo apt-get install python-pip 39 | - sudo apt-get install mongodb 40 | - sudo pip install virtualenv 41 | - sudo apt-get install python-tk 42 | - virtualenv zipline_env 43 | - source zipline_env/bin/activate 44 | - pip install -r requirements.txt 45 | - python setup.py install 46 | - sudo service mongodb start 47 | - pip install xlrd 48 | - pip3 install jupyter 49 | 50 | 51 | 52 | 53 | version_1.0 完成的主要工作 54 | ======================= 55 | 56 | - 交易日历纠正,从1990年开始的所有有效交易日都包含其中,剔除非交易时段 57 | - A股数据源,把数据写入mongodb中,每次从mongodb中读取需要的数据 58 | - benchmark,使用A股的几个标准(HS300指数等) 59 | - return 计算,计算alpha和beta当前使用中国国债作为基准 60 | - 手续费模型设定 61 | 62 | 63 | 关于数据 64 | ======== 65 | 66 | - 您可以使用自己的数据,也可以使用我配置的数据源,数据源我已经配置好,如果自己配置,需要修改文件 data/constants.py 下的IP和PORT 67 | - 我的机器的IP为166.111.68.233 PORT为27017 68 | - 本版本的数据源,只更新到2017.02.28,后面我会每天更新数据 69 | 70 | 本地数据导入 71 | =========== 72 | - 交易数据地址: 73 | http://pan.baidu.com/s/1i4GZWFF 74 | 75 | - 关于数据导入: 76 | 脚本 https://github.com/zhanghan1990/zipline/blob/master/zipline/data/mongodb.py 提供数据导入,修改line 29为您数据解压缩位置 77 | 然后执行脚本python mongodb.py 78 | 79 | 80 | 关于例子 81 | ======== 82 | 83 | -在examples下面有3个例子,这3个例子可以满足基本的回测需求,这三个例子我和joinquant做了比对,差距很小(ps,完全一样还是很难,手续费那里有问题,我会继续修改) 84 | 85 | 联系方式 86 | ======== 87 | 88 | 欢迎感兴趣的朋友加入到这个项目来,有问题请给我发邮件: 89 | zganghanhan@foxmail.com 90 | 91 | 加入我们 92 | ======= 93 | 欢迎有兴趣的朋友伙伴加入我们的开源讨论群: 94 | 95 | 96 | QQ群:556125593 97 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | Vagrant.configure("2") do |config| 5 | config.vm.box = "precise64" 6 | config.vm.box_url = "http://files.vagrantup.com/precise64.box" 7 | config.vm.provider :virtualbox do |vb| 8 | vb.customize ["modifyvm", :id, "--memory", 2048, "--cpus", 2] 9 | end 10 | config.vm.provision "shell", path: "vagrant_init.sh" 11 | end 12 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-time-machine -------------------------------------------------------------------------------- /ci/appveyor/install.ps1: -------------------------------------------------------------------------------- 1 | # Sample script to install Miniconda under Windows 2 | # Authors: Olivier Grisel, Jonathan Helmus and Kyle Kastner, Robert McGibbon 3 | # License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/ 4 | 5 | $MINICONDA_URL = "http://repo.continuum.io/miniconda/" 6 | 7 | 8 | function DownloadMiniconda ($python_version, $platform_suffix) { 9 | $webclient = New-Object System.Net.WebClient 10 | if ($python_version -match "3.4") { 11 | $filename = "Miniconda3-latest-Windows-" + $platform_suffix + ".exe" 12 | } else { 13 | $filename = "Miniconda-latest-Windows-" + $platform_suffix + ".exe" 14 | } 15 | $url = $MINICONDA_URL + $filename 16 | 17 | $basedir = $pwd.Path + "\" 18 | $filepath = $basedir + $filename 19 | if (Test-Path $filename) { 20 | Write-Host "Reusing" $filepath 21 | return $filepath 22 | } 23 | 24 | # Download and retry up to 3 times in case of network transient errors. 25 | Write-Host "Downloading" $filename "from" $url 26 | $retry_attempts = 2 27 | for($i=0; $i -lt $retry_attempts; $i++){ 28 | try { 29 | $webclient.DownloadFile($url, $filepath) 30 | break 31 | } 32 | Catch [Exception]{ 33 | Start-Sleep 1 34 | } 35 | } 36 | if (Test-Path $filepath) { 37 | Write-Host "File saved at" $filepath 38 | } else { 39 | # Retry once to get the error message if any at the last try 40 | $webclient.DownloadFile($url, $filepath) 41 | } 42 | return $filepath 43 | } 44 | 45 | 46 | function InstallMiniconda ($python_version, $architecture, $python_home) { 47 | Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home 48 | if (Test-Path $python_home) { 49 | Write-Host $python_home "already exists, skipping." 50 | return $false 51 | } 52 | if ($architecture -match "32") { 53 | $platform_suffix = "x86" 54 | } else { 55 | $platform_suffix = "x86_64" 56 | } 57 | 58 | $filepath = DownloadMiniconda $python_version $platform_suffix 59 | Write-Host "Installing" $filepath "to" $python_home 60 | $install_log = $python_home + ".log" 61 | $args = "/S /D=$python_home" 62 | Write-Host $filepath $args 63 | Start-Process -FilePath $filepath -ArgumentList $args -Wait -Passthru 64 | if (Test-Path $python_home) { 65 | Write-Host "Python $python_version ($architecture) installation complete" 66 | } else { 67 | Write-Host "Failed to install Python in $python_home" 68 | Get-Content -Path $install_log 69 | Exit 1 70 | } 71 | } 72 | 73 | 74 | function InstallCondaPackages ($python_home, $spec) { 75 | $conda_path = $python_home + "\Scripts\conda.exe" 76 | $args = "install --yes " + $spec 77 | Write-Host ("conda " + $args) 78 | Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru 79 | } 80 | 81 | function UpdateConda ($python_home) { 82 | $conda_path = $python_home + "\Scripts\conda.exe" 83 | Write-Host "Updating conda..." 84 | $args = "update --yes conda" 85 | Write-Host $conda_path $args 86 | Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru 87 | } 88 | 89 | 90 | function main () { 91 | InstallMiniconda $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON 92 | UpdateConda $env:PYTHON 93 | InstallCondaPackages $env:PYTHON "conda-build jinja2 anaconda-client" 94 | } 95 | 96 | main 97 | -------------------------------------------------------------------------------- /ci/appveyor/vcvars64.bat: -------------------------------------------------------------------------------- 1 | CALL "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 2 | -------------------------------------------------------------------------------- /ci/make_conda_packages.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | import subprocess 4 | 5 | 6 | def get_immediate_subdirectories(a_dir): 7 | return [name for name in os.listdir(a_dir) 8 | if os.path.isdir(os.path.join(a_dir, name))] 9 | 10 | 11 | def iter_stdout(cmd): 12 | p = subprocess.Popen(cmd, 13 | stdout=subprocess.PIPE, 14 | stderr=subprocess.STDOUT) 15 | 16 | try: 17 | for line in iter(p.stdout.readline, b''): 18 | yield line.decode().rstrip() 19 | finally: 20 | retcode = p.wait() 21 | if retcode: 22 | raise subprocess.CalledProcessError(retcode, cmd[0]) 23 | 24 | 25 | PKG_PATH_PATTERN = re.compile(".* anaconda upload (?P.+)$") 26 | 27 | 28 | def main(env, do_upload): 29 | for recipe in get_immediate_subdirectories('conda'): 30 | cmd = ["conda", "build", os.path.join('conda', recipe), 31 | "--python", env['CONDA_PY'], 32 | "--numpy", env['CONDA_NPY'], 33 | "--skip-existing", 34 | "-c", "quantopian", 35 | "-c", "https://conda.anaconda.org/quantopian/label/ci"] 36 | 37 | output = None 38 | 39 | for line in iter_stdout(cmd): 40 | print(line) 41 | 42 | if not output: 43 | match = PKG_PATH_PATTERN.match(line) 44 | if match: 45 | output = match.group('pkg_path') 46 | 47 | if output and os.path.exists(output) and do_upload: 48 | cmd = ["anaconda", "-t", env['ANACONDA_TOKEN'], 49 | "upload", output, "-u", "quantopian", "--label", "ci"] 50 | 51 | for line in iter_stdout(cmd): 52 | print(line) 53 | 54 | 55 | if __name__ == '__main__': 56 | env = os.environ.copy() 57 | main(env, 58 | do_upload=(env.get('ANACONDA_TOKEN') and 59 | env.get('APPVEYOR_REPO_BRANCH') == 'master') and 60 | 'APPVEYOR_PULL_REQUEST_NUMBER' not in env) 61 | -------------------------------------------------------------------------------- /conda/README.md: -------------------------------------------------------------------------------- 1 | conda build files 2 | ================= 3 | 4 | [conda](http://docs.continuum.io/conda/intro.html) is a 5 | Python package management system by Continuum that provides 6 | easy installation of binary packages. 7 | 8 | The files in this directory provide instructions for how 9 | to create these binary packages. After installing conda and 10 | conda-build you should be able to: 11 | 12 | ``` 13 | conda build ta-lib 14 | conda build logbook 15 | conda build cyordereddict 16 | conda build zipline 17 | ``` 18 | 19 | You can then upload these binary packages to your own 20 | channel at [binstar](https://binstar.org). 21 | 22 | Windows 23 | ------- 24 | 25 | Building ta-lib on Windows requires Visual Studio (Express) and 26 | the [compiled ta-lib](ta-lib-0.4.0-msvc.zip) which you have to 27 | unzip to C:\ta-lib. 28 | -------------------------------------------------------------------------------- /conda/bcolz/bld.bat: -------------------------------------------------------------------------------- 1 | "%PYTHON%" setup.py install 2 | if errorlevel 1 exit 1 3 | 4 | :: Add more build steps here, if they are necessary. 5 | 6 | :: See 7 | :: http://docs.continuum.io/conda/build.html 8 | :: for a list of environment variables that are set during the build process. 9 | -------------------------------------------------------------------------------- /conda/bcolz/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | $PYTHON setup.py install --single-version-externally-managed --record=record.txt 4 | 5 | # Add more build steps here, if they are necessary. 6 | 7 | # See 8 | # http://docs.continuum.io/conda/build.html 9 | # for a list of environment variables that are set during the build process. 10 | -------------------------------------------------------------------------------- /conda/bcolz/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: bcolz 3 | version: "0.12.1" 4 | 5 | source: 6 | fn: bcolz-0.12.1.tar.gz 7 | url: https://pypi.python.org/packages/source/b/bcolz/bcolz-0.12.1.tar.gz 8 | md5: 43deb659e1f926976be3850e01844360 9 | # patches: 10 | # List any patch files here 11 | # - fix.patch 12 | 13 | # build: 14 | # noarch_python: True 15 | # preserve_egg_dir: True 16 | # entry_points: 17 | # Put any entry points (scripts to be generated automatically) here. The 18 | # syntax is module:function. For example 19 | # 20 | # - bcolz = bcolz:main 21 | # 22 | # Would create an entry point called bcolz that calls bcolz.main() 23 | 24 | 25 | # If this is a new build for the same version, increment the build 26 | # number. If you do not include this key, it defaults to 0. 27 | # number: 1 28 | 29 | requirements: 30 | build: 31 | - python 32 | - setuptools 33 | - numpy x.x 34 | - setuptools_scm 35 | - cython ==0.22.1 36 | 37 | run: 38 | - python 39 | - numpy x.x 40 | 41 | test: 42 | # Python imports 43 | imports: 44 | - bcolz 45 | # - bcolz.tests 46 | 47 | # commands: 48 | # You can put test commands to be run here. Use this to test that the 49 | # entry points work. 50 | 51 | 52 | # You can also put a file called run_test.py in the recipe that will be run 53 | # at test time. 54 | 55 | # requires: 56 | # - mock 57 | # - unittest2 ; python_version < 2.7 58 | # Put any additional test requirements here. For example 59 | # - nose 60 | 61 | about: 62 | home: https://github.com/Blosc/bcolz 63 | license: BSD License 64 | summary: 'columnar and compressed data containers.' 65 | 66 | # See 67 | # http://docs.continuum.io/conda/build.html for 68 | # more information about meta.yaml 69 | -------------------------------------------------------------------------------- /conda/cyordereddict/bld.bat: -------------------------------------------------------------------------------- 1 | "%PYTHON%" setup.py install 2 | if errorlevel 1 exit 1 3 | 4 | :: Add more build steps here, if they are necessary. 5 | 6 | :: See 7 | :: http://docs.continuum.io/conda/build.html 8 | :: for a list of environment variables that are set during the build process. 9 | -------------------------------------------------------------------------------- /conda/cyordereddict/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | $PYTHON setup.py install 4 | 5 | # Add more build steps here, if they are necessary. 6 | 7 | # See 8 | # http://docs.continuum.io/conda/build.html 9 | # for a list of environment variables that are set during the build process. 10 | -------------------------------------------------------------------------------- /conda/cyordereddict/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: cyordereddict 3 | version: "0.2.2" 4 | 5 | source: 6 | fn: cyordereddict-0.2.2.tar.gz 7 | url: https://pypi.python.org/packages/source/c/cyordereddict/cyordereddict-0.2.2.tar.gz 8 | md5: 6279eb0bf9819f0293ad5315b2d484d0 9 | # patches: 10 | # List any patch files here 11 | # - fix.patch 12 | 13 | # build: 14 | # noarch_python: True 15 | # preserve_egg_dir: True 16 | # entry_points: 17 | # Put any entry points (scripts to be generated automatically) here. The 18 | # syntax is module:function. For example 19 | # 20 | # - cyordereddict = cyordereddict:main 21 | # 22 | # Would create an entry point called cyordereddict that calls cyordereddict.main() 23 | 24 | 25 | # If this is a new build for the same version, increment the build 26 | # number. If you do not include this key, it defaults to 0. 27 | # number: 1 28 | 29 | requirements: 30 | build: 31 | - python 32 | 33 | run: 34 | - python 35 | 36 | test: 37 | # Python imports 38 | imports: 39 | - cyordereddict 40 | - cyordereddict.benchmark 41 | 42 | # commands: 43 | # You can put test commands to be run here. Use this to test that the 44 | # entry points work. 45 | 46 | 47 | # You can also put a file called run_test.py in the recipe that will be run 48 | # at test time. 49 | 50 | # requires: 51 | # Put any additional test requirements here. For example 52 | # - nose 53 | 54 | about: 55 | home: https://github.com/shoyer/cyordereddict 56 | license: BSD License 57 | summary: "Cython implementation of Python's collections.OrderedDict" 58 | 59 | # See 60 | # http://docs.continuum.io/conda/build.html for 61 | # more information about meta.yaml 62 | -------------------------------------------------------------------------------- /conda/logbook/bld.bat: -------------------------------------------------------------------------------- 1 | "%PYTHON%" setup.py install 2 | if errorlevel 1 exit 1 3 | 4 | :: Add more build steps here, if they are necessary. 5 | 6 | :: See 7 | :: http://docs.continuum.io/conda/build.html 8 | :: for a list of environment variables that are set during the build process. 9 | -------------------------------------------------------------------------------- /conda/logbook/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | $PYTHON setup.py install 4 | 5 | # Add more build steps here, if they are necessary. 6 | 7 | # See 8 | # http://docs.continuum.io/conda/build.html 9 | # for a list of environment variables that are set during the build process. 10 | -------------------------------------------------------------------------------- /conda/logbook/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: logbook 3 | version: "0.12.5" 4 | 5 | source: 6 | fn: Logbook-0.12.5.tar.gz 7 | url: https://pypi.python.org/packages/source/L/Logbook/Logbook-0.12.5.tar.gz 8 | md5: 1bf64289b9b4cada5a61817c63dd9e82 9 | # patches: 10 | # List any patch files here 11 | # - fix.patch 12 | 13 | # build: 14 | # noarch_python: True 15 | # preserve_egg_dir: True 16 | # entry_points: 17 | # Put any entry points (scripts to be generated automatically) here. The 18 | # syntax is module:function. For example 19 | # 20 | # - logbook = logbook:main 21 | # 22 | # Would create an entry point called logbook that calls logbook.main() 23 | 24 | 25 | # If this is a new build for the same version, increment the build 26 | # number. If you do not include this key, it defaults to 0. 27 | # number: 1 28 | 29 | requirements: 30 | build: 31 | - python 32 | - setuptools 33 | 34 | run: 35 | - python 36 | 37 | test: 38 | # Python imports 39 | imports: 40 | - logbook 41 | 42 | # commands: 43 | # You can put test commands to be run here. Use this to test that the 44 | # entry points work. 45 | 46 | 47 | # You can also put a file called run_test.py in the recipe that will be run 48 | # at test time. 49 | 50 | # requires: 51 | # Put any additional test requirements here. For example 52 | # - nose 53 | 54 | about: 55 | home: http://logbook.pocoo.org/ 56 | license: BSD 57 | summary: 'A logging replacement for Python' 58 | 59 | # See 60 | # http://docs.continuum.io/conda/build.html for 61 | # more information about meta.yaml 62 | -------------------------------------------------------------------------------- /conda/numexpr/bld.bat: -------------------------------------------------------------------------------- 1 | "%PYTHON%" setup.py install 2 | if errorlevel 1 exit 1 3 | 4 | :: Add more build steps here, if they are necessary. 5 | 6 | :: See 7 | :: http://docs.continuum.io/conda/build.html 8 | :: for a list of environment variables that are set during the build process. 9 | -------------------------------------------------------------------------------- /conda/numexpr/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | $PYTHON setup.py install 4 | 5 | # Add more build steps here, if they are necessary. 6 | 7 | # See 8 | # http://docs.continuum.io/conda/build.html 9 | # for a list of environment variables that are set during the build process. 10 | -------------------------------------------------------------------------------- /conda/numexpr/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: numexpr 3 | version: "2.4.6" 4 | 5 | source: 6 | fn: numexpr-2.4.6.tar.gz 7 | url: https://pypi.python.org/packages/source/n/numexpr/numexpr-2.4.6.tar.gz 8 | md5: 17ac6fafc9ea1ce3eb970b9abccb4fbd 9 | 10 | requirements: 11 | build: 12 | - python 13 | - numpy x.x 14 | 15 | run: 16 | - python 17 | - numpy x.x 18 | 19 | test: 20 | # Python imports 21 | imports: 22 | - numexpr 23 | -------------------------------------------------------------------------------- /conda/setuptools_scm/bld.bat: -------------------------------------------------------------------------------- 1 | "%PYTHON%" setup.py install 2 | if errorlevel 1 exit 1 3 | 4 | :: Add more build steps here, if they are necessary. 5 | 6 | :: See 7 | :: http://docs.continuum.io/conda/build.html 8 | :: for a list of environment variables that are set during the build process. 9 | -------------------------------------------------------------------------------- /conda/setuptools_scm/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | $PYTHON setup.py install 4 | 5 | # Add more build steps here, if they are necessary. 6 | 7 | # See 8 | # http://docs.continuum.io/conda/build.html 9 | # for a list of environment variables that are set during the build process. 10 | -------------------------------------------------------------------------------- /conda/setuptools_scm/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: setuptools_scm 3 | version: "1.10.1" 4 | 5 | source: 6 | fn: setuptools_scm-1.10.1.tar.bz2 7 | url: https://pypi.python.org/packages/source/s/setuptools_scm/setuptools_scm-1.10.1.tar.bz2 8 | md5: 99823e2cd564b996f18820a065f0a974 9 | # patches: 10 | # List any patch files here 11 | # - fix.patch 12 | 13 | build: 14 | # noarch_python: True 15 | preserve_egg_dir: True 16 | # entry_points: 17 | # Put any entry points (scripts to be generated automatically) here. The 18 | # syntax is module:function. For example 19 | # 20 | # - setuptools_scm = setuptools_scm:main 21 | # 22 | # Would create an entry point called setuptools_scm that calls setuptools_scm.main() 23 | 24 | 25 | # If this is a new build for the same version, increment the build 26 | # number. If you do not include this key, it defaults to 0. 27 | # number: 1 28 | 29 | requirements: 30 | build: 31 | - python 32 | - setuptools 33 | 34 | run: 35 | - python 36 | - setuptools 37 | 38 | test: 39 | # Python imports 40 | imports: 41 | - setuptools_scm 42 | 43 | # commands: 44 | # You can put test commands to be run here. Use this to test that the 45 | # entry points work. 46 | 47 | 48 | # You can also put a file called run_test.py in the recipe that will be run 49 | # at test time. 50 | 51 | # requires: 52 | # Put any additional test requirements here. For example 53 | # - nose 54 | 55 | about: 56 | home: https://github.com/pypa/setuptools_scm/ 57 | license: MIT License 58 | summary: 'the blessed package to manage your versions by scm tags' 59 | 60 | # See 61 | # http://docs.continuum.io/conda/build.html for 62 | # more information about meta.yaml 63 | -------------------------------------------------------------------------------- /conda/ta-lib/bld.bat: -------------------------------------------------------------------------------- 1 | rmdir /s /q "C:\ta-lib\" 2 | powershell -Command "(New-Object Net.WebClient).DownloadFile('http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-msvc.zip', 'ta-lib-0.4.0-msvc.zip')" 3 | IF %ERRORLEVEL% == 1; exit 1 4 | powershell -Command "Add-Type -AssemblyName System.IO.Compression.FileSystem;[System.IO.Compression.ZipFile]::ExtractToDirectory('ta-lib-0.4.0-msvc.zip', 'C:\')" 5 | IF %ERRORLEVEL% == 1; exit 1 6 | pushd C:\ta-lib\c\ 7 | pushd make\cdd\win32\msvc 8 | nmake 9 | IF %ERRORLEVEL% == 1; exit 1 10 | popd 11 | pushd make\cdr\win32\msvc 12 | nmake 13 | IF %ERRORLEVEL% == 1; exit 1 14 | popd 15 | pushd make\cmd\win32\msvc 16 | nmake 17 | IF %ERRORLEVEL% == 1; exit 1 18 | popd 19 | pushd make\cmr\win32\msvc 20 | nmake 21 | IF %ERRORLEVEL% == 1; exit 1 22 | popd 23 | pushd make\csd\win32\msvc 24 | nmake 25 | IF %ERRORLEVEL% == 1; exit 1 26 | popd 27 | pushd make\csr\win32\msvc 28 | nmake 29 | IF %ERRORLEVEL% == 1; exit 1 30 | popd 31 | popd 32 | del ta-lib-0.4.0-msvc.zip 33 | 34 | python setup.py build --compiler msvc 35 | python setup.py install --prefix=%PREFIX% 36 | -------------------------------------------------------------------------------- /conda/ta-lib/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz 3 | tar xvfz ta-lib-0.4.0-src.tar.gz 4 | pushd ta-lib 5 | ./configure --prefix=$PREFIX 6 | make 7 | make install 8 | popd 9 | rm ta-lib-0.4.0-src.tar.gz 10 | rm -r ta-lib 11 | 12 | export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PREFIX/lib 13 | export TA_INCLUDE_PATH=$PREFIX/include 14 | export TA_LIBRARY_PATH=$PREFIX/lib 15 | python setup.py build 16 | python setup.py install --prefix=$PREFIX 17 | -------------------------------------------------------------------------------- /conda/ta-lib/meta.yaml: -------------------------------------------------------------------------------- 1 | package: 2 | name: ta-lib 3 | version: !!str 0.4.9 4 | 5 | source: 6 | fn: TA_Lib-0.4.9.tar.gz 7 | url: https://github.com/mrjbq7/ta-lib/archive/TA_Lib-0.4.9.tar.gz 8 | md5: f2f6ec8b7d552ff96d53d56ffb7b4e97 9 | 10 | requirements: 11 | build: 12 | - python 13 | - cython 14 | - numpy x.x 15 | run: 16 | - python 17 | - numpy x.x 18 | 19 | test: 20 | imports: 21 | - talib 22 | 23 | about: 24 | home: http://github.com/mrjbq7/ta-lib 25 | license: BSD License 26 | -------------------------------------------------------------------------------- /conda/zipline/bld.bat: -------------------------------------------------------------------------------- 1 | "%PYTHON%" setup.py install --single-version-externally-managed --record=record.txt 2 | if errorlevel 1 exit 1 3 | 4 | :: Add more build steps here, if they are necessary. 5 | 6 | :: See 7 | :: http://docs.continuum.io/conda/build.html 8 | :: for a list of environment variables that are set during the build process. 9 | -------------------------------------------------------------------------------- /conda/zipline/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | $PYTHON setup.py install --single-version-externally-managed --record=record.txt 4 | 5 | # Add more build steps here, if they are necessary. 6 | 7 | # See 8 | # http://docs.continuum.io/conda/build.html 9 | # for a list of environment variables that are set during the build process. 10 | -------------------------------------------------------------------------------- /conda/zipline/meta.yaml: -------------------------------------------------------------------------------- 1 | {% set data = load_setuptools() %} 2 | 3 | package: 4 | name: zipline 5 | version: {{ GIT_DESCRIBE_TAG|replace('v', '') }} 6 | 7 | build: 8 | number: {{ GIT_DESCRIBE_NUMBER|int }} 9 | string: np{{ NPY_VER|replace('.', '') }}py{{ PY_VER|replace('.', '') }}_{{ ( 10 | GIT_BUILD_STR if GIT_DESCRIBE_NUMBER|int != 0 else '0' 11 | ) }} 12 | 13 | source: 14 | git_url: ../../ 15 | 16 | requirements: 17 | build: 18 | - python {{ PY_VER }}* 19 | {% for req in data.get('build_requires', []) -%} 20 | - {{req}} 21 | {% endfor %} 22 | run: 23 | - python 24 | {% for req in data.get('install_requires', []) -%} 25 | - {{req}} 26 | {% endfor %} 27 | 28 | test: 29 | {# When we include the tests module in the zipline package, we can use this: 30 | requires: 31 | {% for req in data.get('extras_require', {}).get('dev', []) -%} 32 | - {{req}} 33 | {% endfor %} 34 | #} 35 | # Python imports 36 | imports: 37 | - zipline 38 | 39 | about: 40 | home: https://zipline.io 41 | license: Apache Software License 42 | 43 | # See 44 | # http://docs.continuum.io/conda/build.html for 45 | # more information about meta.yaml 46 | -------------------------------------------------------------------------------- /docs/.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | notebooks/test.nc 3 | log 4 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | www.zipline.io 2 | -------------------------------------------------------------------------------- /docs/deploy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from __future__ import print_function 3 | from contextlib import contextmanager 4 | from glob import glob 5 | from path import path 6 | import os 7 | from os.path import abspath, basename, dirname, exists, isfile 8 | from shutil import move, rmtree 9 | from subprocess import check_call 10 | 11 | HERE = dirname(abspath(__file__)) 12 | ZIPLINE_ROOT = dirname(HERE) 13 | TEMP_LOCATION = '/tmp/zipline-doc' 14 | TEMP_LOCATION_GLOB = TEMP_LOCATION + '/*' 15 | 16 | 17 | @contextmanager 18 | def removing(path): 19 | try: 20 | yield 21 | finally: 22 | rmtree(path) 23 | 24 | 25 | def ensure_not_exists(path): 26 | if not exists(path): 27 | return 28 | if isfile(path): 29 | os.unlink(path) 30 | else: 31 | rmtree(path) 32 | 33 | 34 | def main(): 35 | print("Moving to %s." % HERE) 36 | with path(HERE): 37 | print("Building docs with 'make html'") 38 | check_call(['make', 'html']) 39 | 40 | print("Clearing temp location '%s'" % TEMP_LOCATION) 41 | rmtree(TEMP_LOCATION, ignore_errors=True) 42 | 43 | with removing(TEMP_LOCATION): 44 | print("Copying built files to temp location.") 45 | move('build/html', TEMP_LOCATION) 46 | 47 | print("Moving to '%s'" % ZIPLINE_ROOT) 48 | os.chdir(ZIPLINE_ROOT) 49 | 50 | print("Checking out gh-pages branch.") 51 | check_call( 52 | [ 53 | 'git', 'branch', '-f', 54 | '--track', 'gh-pages', 'origin/gh-pages' 55 | ] 56 | ) 57 | check_call(['git', 'checkout', 'gh-pages']) 58 | check_call(['git', 'reset', '--hard', 'origin/gh-pages']) 59 | 60 | print("Copying built files:") 61 | for file_ in glob(TEMP_LOCATION_GLOB): 62 | base = basename(file_) 63 | 64 | print("%s -> %s" % (file_, base)) 65 | ensure_not_exists(base) 66 | move(file_, '.') 67 | 68 | print() 69 | print("Updated documentation branch in directory %s" % ZIPLINE_ROOT) 70 | print("If you are happy with these changes, commit and push to gh-pages.") 71 | 72 | if __name__ == '__main__': 73 | main() 74 | -------------------------------------------------------------------------------- /docs/source/appendix.rst: -------------------------------------------------------------------------------- 1 | API Reference 2 | ------------- 3 | 4 | Algorithm API 5 | ~~~~~~~~~~~~~ 6 | 7 | The following methods are available for use in the ``initialize``, 8 | ``handle_data``, and ``before_trading_start`` API functions. 9 | 10 | In all listed functions, the ``self`` argument is implicitly the 11 | currently-executing :class:`~zipline.algorithm.TradingAlgorithm` instance. 12 | 13 | .. automodule:: zipline.api 14 | :members: 15 | 16 | .. autoclass:: zipline.algorithm.TradingAlgorithm 17 | 18 | Pipeline API 19 | ~~~~~~~~~~~~ 20 | 21 | .. autoclass:: zipline.pipeline.Pipeline 22 | :members: 23 | :member-order: groupwise 24 | 25 | .. autoclass:: zipline.pipeline.CustomFactor 26 | :members: 27 | :member-order: groupwise 28 | 29 | .. autoclass:: zipline.pipeline.factors.Factor 30 | :members: top, bottom, rank, percentile_between, isnan, notnan, isfinite, 31 | eq, __add__, __sub__, __mul__, __div__, __mod__, __pow__, __lt__, 32 | __le__, __ne__, __ge__, __gt__ 33 | :exclude-members: dtype 34 | :member-order: bysource 35 | 36 | .. autoclass:: zipline.pipeline.factors.Latest 37 | :members: 38 | 39 | .. autoclass:: zipline.pipeline.factors.MaxDrawdown 40 | :members: 41 | 42 | .. autoclass:: zipline.pipeline.factors.Returns 43 | :members: 44 | 45 | .. autoclass:: zipline.pipeline.factors.RSI 46 | :members: 47 | 48 | .. autoclass:: zipline.pipeline.factors.BusinessDaysUntilNextEarnings 49 | :members: 50 | 51 | .. autoclass:: zipline.pipeline.factors.BusinessDaysSincePreviousEarnings 52 | :members: 53 | 54 | .. autoclass:: zipline.pipeline.factors.SimpleMovingAverage 55 | :members: 56 | 57 | .. autoclass:: zipline.pipeline.factors.VWAP 58 | :members: 59 | 60 | .. autoclass:: zipline.pipeline.factors.WeightedAverageValue 61 | :members: 62 | 63 | .. autoclass:: zipline.pipeline.factors.ExponentialWeightedMovingAverage 64 | :members: 65 | 66 | .. autoclass:: zipline.pipeline.factors.ExponentialWeightedMovingStdDev 67 | :members: 68 | 69 | .. autoclass:: zipline.pipeline.factors.AverageDollarVolume 70 | :members: 71 | 72 | .. autoclass:: zipline.pipeline.filters.Filter 73 | :members: __and__, __or__ 74 | :exclude-members: dtype 75 | 76 | .. autoclass:: zipline.pipeline.data.EarningsCalendar 77 | :members: next_announcement, previous_announcement 78 | :undoc-members: 79 | 80 | .. autoclass:: zipline.pipeline.data.USEquityPricing 81 | :members: open, high, low, close, volume 82 | :undoc-members: 83 | 84 | 85 | Asset Metadata 86 | ~~~~~~~~~~~~~~ 87 | 88 | .. autoclass:: zipline.assets.assets.Asset 89 | :members: 90 | 91 | .. autoclass:: zipline.assets.assets.Equity 92 | :members: 93 | 94 | .. autoclass:: zipline.assets.assets.Future 95 | :members: 96 | 97 | .. autoclass:: zipline.assets.assets.AssetFinder 98 | :members: 99 | 100 | .. autoclass:: zipline.assets.assets.AssetFinderCachedEquities 101 | :members: 102 | 103 | .. autoclass:: zipline.assets.asset_writer.AssetDBWriter 104 | :members: 105 | 106 | .. autoclass:: zipline.assets.assets.AssetConvertible 107 | :members: 108 | 109 | Data API 110 | ~~~~~~~~ 111 | 112 | .. autoclass:: zipline.data.minute_bars.BcolzMinuteBarWriter 113 | :members: 114 | -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | 4 | from zipline import __version__ as version 5 | 6 | # If extensions (or modules to document with autodoc) are in another directory, 7 | # add these directories to sys.path here. If the directory is relative to the 8 | # documentation root, use os.path.abspath to make it absolute, like shown here. 9 | sys.path.insert(0, os.path.abspath('.')) 10 | sys.path.insert(0, os.path.abspath('..')) 11 | 12 | extensions = [ 13 | 'sphinx.ext.autodoc', 14 | 'sphinx.ext.intersphinx', 15 | 'sphinx.ext.doctest', 16 | 'sphinx.ext.extlinks', 17 | 'sphinx.ext.autosummary', 18 | 'sphinx.ext.viewcode', 19 | 'sphinx.ext.todo', 20 | ] 21 | 22 | 23 | extlinks = { 24 | 'issue': ('https://github.com/quantopian/zipline/issues/%s', '#'), 25 | 'commit': ('https://github.com/quantopian/zipline/commit/%s', ''), 26 | } 27 | 28 | # -- Docstrings --------------------------------------------------------------- 29 | 30 | extensions += ['numpydoc'] 31 | numpydoc_show_class_members = False 32 | 33 | # Add any paths that contain templates here, relative to this directory. 34 | templates_path = ['.templates'] 35 | 36 | # The suffix of source filenames. 37 | source_suffix = '.rst' 38 | 39 | # The master toctree document. 40 | master_doc = 'index' 41 | 42 | # General information about the project. 43 | project = u'Zipline' 44 | copyright = u'2016, Quantopian Inc.' 45 | 46 | # The full version, including alpha/beta/rc tags. 47 | release = version 48 | 49 | # List of patterns, relative to source directory, that match files and 50 | # directories to ignore when looking for source files. 51 | exclude_patterns = [] 52 | 53 | on_rtd = os.environ.get('READTHEDOCS', None) == 'True' 54 | if not on_rtd: # only import and set the theme if we're building docs locally 55 | try: 56 | import sphinx_rtd_theme 57 | except ImportError: 58 | html_theme = 'default' 59 | html_theme_path = [] 60 | else: 61 | html_theme = 'sphinx_rtd_theme' 62 | html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] 63 | 64 | # The name of the Pygments (syntax highlighting) style to use. 65 | highlight_language = 'python' 66 | 67 | # The name of an image file (within the static path) to use as favicon of the 68 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 69 | # pixels large. 70 | # html_favicon = os.path.join('svg', 'zipline.ico') 71 | 72 | # Add any paths that contain custom static files (such as style sheets) here, 73 | # relative to this directory. They are copied after the builtin static files, 74 | # so a file named "default.css" will overwrite the builtin "default.css". 75 | html_static_path = ['.static'] 76 | 77 | # If false, no index is generated. 78 | html_use_index = True 79 | 80 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 81 | html_show_sphinx = True 82 | 83 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 84 | html_show_copyright = True 85 | 86 | # Output file base name for HTML help builder. 87 | htmlhelp_basename = 'ziplinedoc' 88 | 89 | intersphinx_mapping = { 90 | 'http://docs.python.org/dev': None, 91 | 'numpy': ('http://docs.scipy.org/doc/numpy/', None), 92 | 'scipy': ('http://docs.scipy.org/doc/scipy/reference/', None), 93 | 'pandas': ('http://pandas.pydata.org/pandas-docs/stable/', None), 94 | } 95 | 96 | doctest_global_setup = "import zipline" 97 | 98 | todo_include_todos = True 99 | -------------------------------------------------------------------------------- /docs/source/dev-doc-message.txt: -------------------------------------------------------------------------------- 1 | .. note:: 2 | 3 | This page is intended for developers of zipline. 4 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../../README.rst 2 | 3 | .. toctree:: 4 | :maxdepth: 1 5 | 6 | install 7 | beginner-tutorial 8 | releases 9 | appendix 10 | release-process 11 | -------------------------------------------------------------------------------- /docs/source/releases.rst: -------------------------------------------------------------------------------- 1 | ============= 2 | Release Notes 3 | ============= 4 | 5 | .. include:: whatsnew/0.8.5.txt 6 | 7 | .. include:: whatsnew/0.8.4.txt 8 | 9 | .. include:: whatsnew/0.8.3.txt 10 | 11 | .. include:: whatsnew/0.8.0.txt 12 | 13 | .. include:: whatsnew/0.7.0.txt 14 | 15 | .. include:: whatsnew/0.6.1.txt 16 | -------------------------------------------------------------------------------- /docs/source/tutorial_files/tutorial_11_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/docs/source/tutorial_files/tutorial_11_2.png -------------------------------------------------------------------------------- /docs/source/tutorial_files/tutorial_22_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/docs/source/tutorial_files/tutorial_22_1.png -------------------------------------------------------------------------------- /docs/source/whatsnew/0.8.3.txt: -------------------------------------------------------------------------------- 1 | Release 0.8.3 2 | ------------- 3 | 4 | :Release: 0.8.3 5 | :Date: November 6, 2015 6 | 7 | 8 | .. note:: 9 | 10 | We advanced the version to ``0.8.3`` to fix a source distribution issue with 11 | pypi. There are no code changes in this version. 12 | -------------------------------------------------------------------------------- /docs/source/whatsnew/skeleton.txt: -------------------------------------------------------------------------------- 1 | Development 2 | ----------- 3 | 4 | :Release: x.x.x 5 | :Date: TBD 6 | 7 | .. warning:: 8 | This release is still under active development. All changes listed are 9 | subject to change at any time. 10 | 11 | 12 | Highlights 13 | ~~~~~~~~~~ 14 | 15 | None 16 | 17 | Enhancements 18 | ~~~~~~~~~~~~ 19 | 20 | None 21 | 22 | Experimental Features 23 | ~~~~~~~~~~~~~~~~~~~~~ 24 | 25 | .. warning:: 26 | 27 | Experimental features are subject to change. 28 | 29 | None 30 | 31 | Bug Fixes 32 | ~~~~~~~~~ 33 | 34 | None 35 | 36 | Performance 37 | ~~~~~~~~~~~ 38 | 39 | None 40 | 41 | Maintenance and Refactorings 42 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 43 | 44 | None 45 | 46 | Build 47 | ~~~~~ 48 | 49 | None 50 | 51 | Documentation 52 | ~~~~~~~~~~~~~ 53 | 54 | None 55 | 56 | Miscellaneous 57 | ~~~~~~~~~~~~~ 58 | 59 | None 60 | -------------------------------------------------------------------------------- /etc/create_authors_file.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | git shortlog -ns master | awk '$1 >= $THRESHOLD {$1="";print $0}' | \ 3 | cut -d" " -f2- > AUTHORS 4 | -------------------------------------------------------------------------------- /etc/git-hooks/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # An hook script to verify linting and passing unit tests. 4 | # 5 | # Called by "git commit" with no arguments. The hook should 6 | # exit with non-zero status after issuing an appropriate message if 7 | # it wants to stop the commit. 8 | # 9 | # To enable this hook, copy or symlink to your repo's 10 | # ".git/hooks/pre-commit". 11 | # 12 | # Please read the following as it will execute on your machine on each commit. 13 | 14 | set -e 15 | 16 | # stash everything that wasn't just staged 17 | # so that we are only testing the staged code 18 | stash_result=$(git stash --keep-index) 19 | 20 | # Run flake8 linting 21 | flake8 zipline tests 22 | # Run unit tests 23 | nosetests -x 24 | 25 | # restore unstaged code 26 | # N.B. this won't run if linting or unit tests fail 27 | # But if either fail, it's probably best to have only the offending 28 | # staged commits 'active', anyway. 29 | stash_result=$(git stash --keep-index) 30 | if [ "$stash_result" != "No local changes to save" ] 31 | then 32 | git stash pop -q 33 | fi 34 | -------------------------------------------------------------------------------- /etc/goodies.txt: -------------------------------------------------------------------------------- 1 | # Extra modules, goodies for algorithms. 2 | 3 | matplotlib==1.3.0 4 | scipy==0.12.0 5 | scikit-learn==0.13.1 6 | statsmodels==0.5.0 7 | patsy==0.1.0 8 | -------------------------------------------------------------------------------- /etc/ordered_pip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | a=0 4 | while read line 5 | do 6 | if [[ -n "$line" && "$line" != \#* ]] ; then 7 | # forward to pip any args after the reqs filename 8 | pip install --exists-action w $line "${@:2}" 9 | ((a = a + 1)) 10 | fi 11 | done < $1 12 | echo "$0: Final package count is $a"; 13 | -------------------------------------------------------------------------------- /etc/requirements.txt: -------------------------------------------------------------------------------- 1 | # Incompatible with earlier PIP versions 2 | pip>=7.1.0 3 | # bcolz fails to install if this is not in the build_requires. 4 | setuptools>18.0 5 | 6 | # Logging 7 | Logbook==0.12.5 8 | 9 | # Scientific Libraries 10 | 11 | pytz==2015.4 12 | numpy==1.9.2 13 | 14 | # scipy and pandas are required for statsmodels, 15 | # statsmodels in turn is required for some pandas packages 16 | scipy==0.15.1 17 | pandas==0.16.1 18 | # Needed for parts of pandas.stats 19 | patsy==0.4.0 20 | statsmodels==0.6.1 21 | 22 | python-dateutil==2.4.2 23 | six==1.10.0 24 | 25 | # For fetching remote data 26 | requests==2.9.1 27 | 28 | Cython==0.22.1 29 | 30 | # faster OrderedDict 31 | cyordereddict==0.2.2 32 | 33 | # faster array ops. 34 | bottleneck==1.0.0 35 | 36 | contextlib2==0.4.0 37 | 38 | # networkx requires decorator 39 | decorator==4.0.0 40 | # Graph algorithms used by zipline.pipeline 41 | networkx==1.9.1 42 | 43 | # NumericalExpression pipeline terms. 44 | numexpr==2.4.6 45 | 46 | # On disk storage format for pipeline data. 47 | bcolz==0.12.1 48 | 49 | # Command line interface helper 50 | click==4.0.0 51 | 52 | # FUNctional programming utilities 53 | toolz==0.7.4 54 | 55 | # Asset writer and finder 56 | sqlalchemy==1.0.8 57 | -------------------------------------------------------------------------------- /etc/requirements_blaze.txt: -------------------------------------------------------------------------------- 1 | -e git://github.com/quantopian/datashape.git@9bd8fb970a0fc55e866a0b46b5101c9aa47e24ed#egg=datashape-dev 2 | -e git://github.com/quantopian/odo.git@4f7f45fb039d89ea101803b95da21fc055901d66#egg=odo-dev 3 | -e git://github.com/quantopian/blaze.git@32f39b1dadefc1e686c45dc23db0ecb56e753938#egg=blaze-dev 4 | -------------------------------------------------------------------------------- /etc/requirements_dev.txt: -------------------------------------------------------------------------------- 1 | # Testing 2 | coverage==4.0.3 3 | nose==1.3.7 4 | nose-parameterized==0.5.0 5 | nose-ignore-docstring==0.2 6 | nose-timer==0.5.0 7 | xlrd==0.9.4 8 | 9 | # These are required by mock or its dependencies 10 | MarkupSafe==0.23 11 | Jinja2==2.7.3 12 | funcsigs==0.4 13 | Pygments==2.0.2 14 | alabaster==0.7.6 15 | babel==1.3 16 | docutils==0.12 17 | snowballstemmer==1.2.0 18 | sphinx-rtd-theme==0.1.8 19 | sphinx==1.3.4 20 | pbr==1.3.0 21 | 22 | mock==1.3.0 23 | 24 | # Temp Directories for testing 25 | testfixtures==4.1.2 26 | 27 | # Linting 28 | 29 | flake8==2.4.1 30 | mccabe==0.3.1 31 | # Note: Old version of pep8 required by flake8 2.4.1 32 | pep8==1.5.7 33 | # Note: Old version of pyflakes required by flake8 2.4.1 34 | pyflakes==0.8.1 35 | 36 | # Documentation Conversion 37 | 38 | pyandoc==0.0.1 39 | docopt==0.6.2 40 | numpydoc==0.5 41 | mistune==0.7 42 | 43 | # Example scripts that are run during unit tests use the following: 44 | 45 | # Required by tornado 46 | backports.ssl-match-hostname==3.4.0.2;python_version<'3.0' 47 | certifi==2015.4.28 48 | 49 | # tornado and pyparsing are required by matplotlib 50 | tornado==4.2.1 51 | pyparsing==2.0.3 52 | 53 | matplotlib==1.4.3 54 | 55 | Markdown==2.6.2 56 | 57 | # Checking for old PIP packages 58 | futures==3.0.5 59 | requests-futures==0.9.7 60 | piprot==0.9.6 61 | 62 | # For asset db management 63 | alembic==0.7.7 64 | -------------------------------------------------------------------------------- /etc/requirements_docs.txt: -------------------------------------------------------------------------------- 1 | Sphinx>=1.3.2 2 | numpydoc>=0.5.0 3 | -------------------------------------------------------------------------------- /etc/requirements_talib.txt: -------------------------------------------------------------------------------- 1 | TA-Lib==0.4.9 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | bottleneck==1.0.0 2 | scipy==0.15.1 3 | pymongo==3.4.0 4 | pandas==0.16.1 5 | matplotlib==2.0.0 6 | lxml==3.7.3 7 | tushare==0.7.4 8 | -------------------------------------------------------------------------------- /scripts/run_algo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # Copyright 2014 Quantopian, Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | import logbook 18 | import sys 19 | 20 | from zipline.utils import parse_args, run_pipeline 21 | 22 | if __name__ == "__main__": 23 | logbook.StderrHandler().push_application() 24 | parsed = parse_args(sys.argv[1:]) 25 | run_pipeline(**parsed) 26 | sys.exit(0) 27 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [nosetests] 2 | verbosity=2 3 | detailed-errors=1 4 | with-ignore-docstrings=1 5 | with-timer=1 6 | timer-top-n=15 7 | with-coverage=1 8 | cover-package=zipline 9 | 10 | [metadata] 11 | description-file = README.rst 12 | 13 | # See the docstring in versioneer.py for instructions. Note that you must 14 | # re-run 'versioneer.py setup' after changing this section, and commit the 15 | # resulting files. 16 | [versioneer] 17 | VCS=git 18 | style=pep440 19 | versionfile_source=zipline/_version.py 20 | versionfile_build=zipline/_version.py 21 | tag_prefix= 22 | parentdir_prefix= zipline- 23 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/tests/__init__.py -------------------------------------------------------------------------------- /tests/data/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/tests/data/__init__.py -------------------------------------------------------------------------------- /tests/finance/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/tests/finance/__init__.py -------------------------------------------------------------------------------- /tests/pipeline/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/tests/pipeline/__init__.py -------------------------------------------------------------------------------- /tests/pipeline/test_adjustment.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests for zipline.lib.adjustment 3 | """ 4 | from unittest import TestCase 5 | from nose_parameterized import parameterized 6 | 7 | from zipline.lib import adjustment as adj 8 | from zipline.utils.numpy_utils import make_datetime64ns 9 | 10 | 11 | class AdjustmentTestCase(TestCase): 12 | 13 | @parameterized.expand([ 14 | ('add', adj.ADD), 15 | ('multiply', adj.MULTIPLY), 16 | ('overwrite', adj.OVERWRITE), 17 | ]) 18 | def test_make_float_adjustment(self, name, adj_type): 19 | expected_types = { 20 | 'add': adj.Float64Add, 21 | 'multiply': adj.Float64Multiply, 22 | 'overwrite': adj.Float64Overwrite, 23 | } 24 | result = adj.make_adjustment_from_indices( 25 | 1, 2, 3, 4, 26 | adjustment_kind=adj_type, 27 | value=0.5, 28 | ) 29 | expected = expected_types[name]( 30 | first_row=1, 31 | last_row=2, 32 | first_col=3, 33 | last_col=4, 34 | value=0.5, 35 | ) 36 | self.assertEqual(result, expected) 37 | 38 | def test_make_datetime_adjustment(self): 39 | overwrite_dt = make_datetime64ns(0) 40 | result = adj.make_adjustment_from_indices( 41 | 1, 2, 3, 4, 42 | adjustment_kind=adj.OVERWRITE, 43 | value=overwrite_dt, 44 | ) 45 | expected = adj.Datetime64Overwrite( 46 | first_row=1, 47 | last_row=2, 48 | first_col=3, 49 | last_col=4, 50 | value=overwrite_dt, 51 | ) 52 | self.assertEqual(result, expected) 53 | 54 | def test_unsupported_type(self): 55 | class SomeClass(object): 56 | pass 57 | 58 | with self.assertRaises(TypeError) as e: 59 | adj.make_adjustment_from_indices( 60 | 1, 2, 3, 4, 61 | adjustment_kind=adj.OVERWRITE, 62 | value=SomeClass(), 63 | ) 64 | 65 | exc = e.exception 66 | expected_msg = ( 67 | "Don't know how to make overwrite adjustments for values of type " 68 | "%r." % SomeClass 69 | ) 70 | self.assertEqual(str(exc), expected_msg) 71 | -------------------------------------------------------------------------------- /tests/pipeline/test_column.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests BoundColumn attributes and methods. 3 | """ 4 | from contextlib2 import ExitStack 5 | from unittest import TestCase 6 | 7 | from pandas import date_range, DataFrame 8 | from pandas.util.testing import assert_frame_equal 9 | 10 | from zipline.pipeline import Pipeline 11 | from zipline.pipeline.data.testing import TestingDataSet as TDS 12 | from zipline.testing import chrange, temp_pipeline_engine 13 | 14 | 15 | class LatestTestCase(TestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | cls._stack = stack = ExitStack() 20 | cls.calendar = cal = date_range('2014', '2015', freq='D', tz='UTC') 21 | cls.sids = list(range(5)) 22 | cls.engine = stack.enter_context( 23 | temp_pipeline_engine( 24 | cal, 25 | cls.sids, 26 | random_seed=100, 27 | symbols=chrange('A', 'E'), 28 | ), 29 | ) 30 | cls.assets = cls.engine._finder.retrieve_all(cls.sids) 31 | 32 | @classmethod 33 | def tearDownClass(cls): 34 | cls._stack.close() 35 | 36 | def expected_latest(self, column, slice_): 37 | loader = self.engine.get_loader(column) 38 | return DataFrame( 39 | loader.values(column.dtype, self.calendar, self.sids)[slice_], 40 | index=self.calendar[slice_], 41 | columns=self.assets, 42 | ) 43 | 44 | def test_latest(self): 45 | columns = TDS.columns 46 | pipe = Pipeline( 47 | columns={c.name: c.latest for c in columns}, 48 | ) 49 | 50 | cal_slice = slice(20, 40) 51 | dates_to_test = self.calendar[cal_slice] 52 | result = self.engine.run_pipeline( 53 | pipe, 54 | dates_to_test[0], 55 | dates_to_test[-1], 56 | ) 57 | for column in columns: 58 | float_result = result[column.name].unstack() 59 | expected_float_result = self.expected_latest(column, cal_slice) 60 | assert_frame_equal(float_result, expected_float_result) 61 | -------------------------------------------------------------------------------- /tests/resources/pipeline_inputs/generate.py: -------------------------------------------------------------------------------- 1 | """ 2 | Quick and dirty script to generate test case inputs. 3 | """ 4 | from __future__ import print_function 5 | from os.path import ( 6 | dirname, 7 | join, 8 | ) 9 | from pandas.io.data import get_data_yahoo 10 | 11 | here = join(dirname(__file__)) 12 | 13 | 14 | def main(): 15 | symbols = ['AAPL', 'MSFT', 'BRK-A'] 16 | # Specifically chosen to include the AAPL split on June 9, 2014. 17 | for symbol in symbols: 18 | data = get_data_yahoo(symbol, start='2014-03-01', end='2014-09-01') 19 | data.rename( 20 | columns={ 21 | 'Open': 'open', 22 | 'High': 'high', 23 | 'Low': 'low', 24 | 'Close': 'close', 25 | 'Volume': 'volume', 26 | }, 27 | inplace=True, 28 | ) 29 | del data['Adj Close'] 30 | 31 | dest = join(here, symbol + '.csv') 32 | print("Writing %s -> %s" % (symbol, dest)) 33 | data.to_csv(dest, index_label='day') 34 | 35 | if __name__ == '__main__': 36 | main() 37 | -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.blotter.Blotter/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'_stateversion_' 6 | p3 7 | I1 8 | sS'new_orders' 9 | p4 10 | (lp5 11 | sS'orders' 12 | p6 13 | (dp7 14 | sS'open_orders' 15 | p8 16 | (dp9 17 | ssS'initargs' 18 | p10 19 | NsS'newargs' 20 | p11 21 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.blotter.Order/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'direction' 6 | p3 7 | F1.0 8 | sS'_stateversion_' 9 | p4 10 | I1 11 | sS'_status' 12 | p5 13 | I0 14 | sS'created' 15 | p6 16 | cdatetime 17 | datetime 18 | p7 19 | (S'\x07\xdd\x06\x13\x00\x00\x00\x00\x00\x00' 20 | p8 21 | tp9 22 | Rp10 23 | sS'limit_reached' 24 | p11 25 | I00 26 | sS'stop' 27 | p12 28 | NsS'reason' 29 | p13 30 | NsS'stop_reached' 31 | p14 32 | I00 33 | sS'commission' 34 | p15 35 | NsS'amount' 36 | p16 37 | I100 38 | sS'limit' 39 | p17 40 | NsS'sid' 41 | p18 42 | I8554 43 | sS'dt' 44 | p19 45 | g10 46 | sS'type' 47 | p20 48 | I6 49 | sS'id' 50 | p21 51 | S'e837d6193375414eb1594c8adb068a34' 52 | p22 53 | sS'filled' 54 | p23 55 | I0 56 | ssS'initargs' 57 | p24 58 | NsS'newargs' 59 | p25 60 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.commission.PerDollar/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'cost' 6 | p3 7 | F0.0015 8 | sS'_stateversion_' 9 | p4 10 | I1 11 | ssS'initargs' 12 | p5 13 | NsS'newargs' 14 | p6 15 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.commission.PerShare/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'min_trade_cost' 6 | p3 7 | NsS'cost' 8 | p4 9 | F0.03 10 | sS'_stateversion_' 11 | p5 12 | I1 13 | ssS'initargs' 14 | p6 15 | NsS'newargs' 16 | p7 17 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.commission.PerTrade/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'cost' 6 | p3 7 | F5.0 8 | sS'_stateversion_' 9 | p4 10 | I1 11 | ssS'initargs' 12 | p5 13 | NsS'newargs' 14 | p6 15 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.performance.period.PerformancePeriod/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'_account_store' 6 | p3 7 | ccopy_reg 8 | _reconstructor 9 | p4 10 | (czipline.protocol 11 | Account 12 | p5 13 | c__builtin__ 14 | object 15 | p6 16 | Ntp7 17 | Rp8 18 | (dp9 19 | S'regt_margin' 20 | p10 21 | Finf 22 | sS'maintenance_margin_requirement' 23 | p11 24 | F0.0 25 | sS'day_trades_remaining' 26 | p12 27 | Finf 28 | sS'buying_power' 29 | p13 30 | Finf 31 | sS'net_leverage' 32 | p14 33 | F0.0 34 | sS'settled_cash' 35 | p15 36 | F0.0 37 | sS'cushion' 38 | p16 39 | F0.0 40 | sS'_stateversion_' 41 | p17 42 | I1 43 | sS'leverage' 44 | p18 45 | F0.0 46 | sS'regt_equity' 47 | p19 48 | F0.0 49 | sS'excess_liquidity' 50 | p20 51 | F0.0 52 | sS'available_funds' 53 | p21 54 | F0.0 55 | sS'equity_with_loan' 56 | p22 57 | F0.0 58 | sS'initial_margin_requirement' 59 | p23 60 | F0.0 61 | sS'net_liquidation' 62 | p24 63 | F0.0 64 | sS'total_positions_value' 65 | p25 66 | F0.0 67 | sS'accrued_interest' 68 | p26 69 | F0.0 70 | sbsS'orders_by_modified' 71 | p27 72 | (dp28 73 | sS'keep_transactions' 74 | p29 75 | I01 76 | sS'ending_cash' 77 | p30 78 | F10000.0 79 | sS'_positions_store' 80 | p31 81 | (dp32 82 | sS'positions' 83 | p33 84 | (dp34 85 | sS'processed_transactions' 86 | p35 87 | (dp36 88 | sS'ending_value' 89 | p37 90 | cnumpy.core.multiarray 91 | scalar 92 | p38 93 | (cnumpy 94 | dtype 95 | p39 96 | (S'f8' 97 | p40 98 | I0 99 | I1 100 | tp41 101 | Rp42 102 | (I3 103 | S'<' 104 | p43 105 | NNNI-1 106 | I-1 107 | I0 108 | tp44 109 | bS'\x00\x00\x00\x00\x00\x00\x00\x00' 110 | p45 111 | tp46 112 | Rp47 113 | sS'loc_map' 114 | p48 115 | (dp49 116 | sS'starting_cash' 117 | p50 118 | I10000 119 | sS'returns' 120 | p51 121 | g38 122 | (g42 123 | S'\x00\x00\x00\x00\x00\x00\x00\x00' 124 | p52 125 | tp53 126 | Rp54 127 | sg17 128 | I1 129 | sS'pnl' 130 | p55 131 | g38 132 | (g42 133 | S'\x00\x00\x00\x00\x00\x00\x00\x00' 134 | p56 135 | tp57 136 | Rp58 137 | sS'period_cash_flow' 138 | p59 139 | F0.0 140 | sS'serialize_positions' 141 | p60 142 | I01 143 | sS'keep_orders' 144 | p61 145 | I00 146 | sS'_portfolio_store' 147 | p62 148 | g4 149 | (czipline.protocol 150 | Portfolio 151 | p63 152 | g6 153 | Ntp64 154 | Rp65 155 | (dp66 156 | g17 157 | I1 158 | sS'portfolio_value' 159 | p67 160 | F0.0 161 | sS'cash' 162 | p68 163 | F0.0 164 | sg50 165 | F0.0 166 | sg51 167 | F0.0 168 | sS'capital_used' 169 | p69 170 | F0.0 171 | sg55 172 | F0.0 173 | sg33 174 | (dp70 175 | sS'positions_value' 176 | p71 177 | F0.0 178 | sS'start_date' 179 | p72 180 | NsbsS'starting_value' 181 | p73 182 | F0.0 183 | sS'period_open' 184 | p74 185 | NsS'period_close' 186 | p75 187 | NsS'orders_by_id' 188 | p76 189 | (dp77 190 | ssS'initargs' 191 | p78 192 | NsS'newargs' 193 | p79 194 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.performance.period.PerformancePeriod/State_Version_2: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'_account_store' 6 | p3 7 | ccopy_reg 8 | _reconstructor 9 | p4 10 | (czipline.protocol 11 | Account 12 | p5 13 | c__builtin__ 14 | object 15 | p6 16 | Ntp7 17 | Rp8 18 | (dp9 19 | S'regt_margin' 20 | p10 21 | Finf 22 | sS'maintenance_margin_requirement' 23 | p11 24 | F0.0 25 | sS'day_trades_remaining' 26 | p12 27 | Finf 28 | sS'buying_power' 29 | p13 30 | Finf 31 | sS'net_leverage' 32 | p14 33 | F0.0 34 | sS'settled_cash' 35 | p15 36 | F0.0 37 | sS'cushion' 38 | p16 39 | F0.0 40 | sS'_stateversion_' 41 | p17 42 | I1 43 | sS'leverage' 44 | p18 45 | F0.0 46 | sS'regt_equity' 47 | p19 48 | F0.0 49 | sS'excess_liquidity' 50 | p20 51 | F0.0 52 | sS'available_funds' 53 | p21 54 | F0.0 55 | sS'equity_with_loan' 56 | p22 57 | F0.0 58 | sS'initial_margin_requirement' 59 | p23 60 | F0.0 61 | sS'net_liquidation' 62 | p24 63 | F0.0 64 | sS'total_positions_value' 65 | p25 66 | F0.0 67 | sS'accrued_interest' 68 | p26 69 | F0.0 70 | sbsS'orders_by_modified' 71 | p27 72 | (dp28 73 | sS'keep_transactions' 74 | p29 75 | I01 76 | sS'ending_cash' 77 | p30 78 | I10000 79 | sS'processed_transactions' 80 | p31 81 | (dp32 82 | sS'ending_value' 83 | p33 84 | F0.0 85 | sS'starting_cash' 86 | p34 87 | I10000 88 | sg17 89 | I2 90 | sS'pnl' 91 | p35 92 | F0.0 93 | sS'period_cash_flow' 94 | p36 95 | F0.0 96 | sS'serialize_positions' 97 | p37 98 | I01 99 | sS'keep_orders' 100 | p38 101 | I00 102 | sS'_portfolio_store' 103 | p39 104 | g4 105 | (czipline.protocol 106 | Portfolio 107 | p40 108 | g6 109 | Ntp41 110 | Rp42 111 | (dp43 112 | g17 113 | I1 114 | sS'portfolio_value' 115 | p44 116 | F0.0 117 | sS'cash' 118 | p45 119 | F0.0 120 | sg34 121 | F0.0 122 | sS'returns' 123 | p46 124 | F0.0 125 | sS'capital_used' 126 | p47 127 | F0.0 128 | sg35 129 | F0.0 130 | sS'positions' 131 | p48 132 | (dp49 133 | sS'positions_value' 134 | p50 135 | F0.0 136 | sS'start_date' 137 | p51 138 | NsbsS'starting_value' 139 | p52 140 | F0.0 141 | sS'period_open' 142 | p53 143 | NsS'period_close' 144 | p54 145 | NsS'orders_by_id' 146 | p55 147 | (dp56 148 | ssS'initargs' 149 | p57 150 | NsS'newargs' 151 | p58 152 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.performance.position.Position/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'_stateversion_' 6 | p3 7 | I1 8 | sS'cost_basis' 9 | p4 10 | F0.0 11 | sS'amount' 12 | p5 13 | I0 14 | sS'last_sale_price' 15 | p6 16 | F0.0 17 | sS'sid' 18 | p7 19 | I8554 20 | sS'last_sale_date' 21 | p8 22 | NssS'initargs' 23 | p9 24 | NsS'newargs' 25 | p10 26 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.performance.position_tracker.PositionTracker/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'positions' 6 | p3 7 | (dp4 8 | sS'unpaid_dividends' 9 | p5 10 | ccopy_reg 11 | _reconstructor 12 | p6 13 | (cpandas.core.frame 14 | DataFrame 15 | p7 16 | c__builtin__ 17 | object 18 | p8 19 | Ntp9 20 | Rp10 21 | g6 22 | (cpandas.core.internals 23 | BlockManager 24 | p11 25 | g8 26 | Ntp12 27 | Rp13 28 | ((lp14 29 | cnumpy.core.multiarray 30 | _reconstruct 31 | p15 32 | (cpandas.core.index 33 | Index 34 | p16 35 | (I0 36 | tp17 37 | S'b' 38 | p18 39 | tp19 40 | Rp20 41 | ((I1 42 | (I4 43 | tp21 44 | cnumpy 45 | dtype 46 | p22 47 | (S'O8' 48 | p23 49 | I0 50 | I1 51 | tp24 52 | Rp25 53 | (I3 54 | S'|' 55 | p26 56 | NNNI-1 57 | I-1 58 | I63 59 | tp27 60 | bI00 61 | (lp28 62 | S'id' 63 | p29 64 | aS'payment_sid' 65 | p30 66 | aS'cash_amount' 67 | p31 68 | aS'share_count' 69 | p32 70 | atp33 71 | (Ntp34 72 | tp35 73 | bag15 74 | (g16 75 | (I0 76 | tp36 77 | g18 78 | tp37 79 | Rp38 80 | ((I1 81 | (I0 82 | tp39 83 | g25 84 | I00 85 | (lp40 86 | tp41 87 | (Ntp42 88 | tp43 89 | ba(lp44 90 | g15 91 | (cnumpy 92 | ndarray 93 | p45 94 | (I0 95 | tp46 96 | g18 97 | tp47 98 | Rp48 99 | (I1 100 | (I4 101 | I0 102 | tp49 103 | g25 104 | I00 105 | (lp50 106 | tp51 107 | ba(lp52 108 | g15 109 | (g16 110 | (I0 111 | tp53 112 | g18 113 | tp54 114 | Rp55 115 | ((I1 116 | (I4 117 | tp56 118 | g25 119 | I00 120 | (lp57 121 | g29 122 | ag30 123 | ag31 124 | ag32 125 | atp58 126 | (Ntp59 127 | tp60 128 | batp61 129 | bbsS'_stateversion_' 130 | p62 131 | I1 132 | ssS'initargs' 133 | p63 134 | NsS'newargs' 135 | p64 136 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.slippage.FixedSlippage/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'spread' 6 | p3 7 | F0.0 8 | sS'_stateversion_' 9 | p4 10 | I1 11 | ssS'initargs' 12 | p5 13 | NsS'newargs' 14 | p6 15 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.slippage.Transaction/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'commission' 6 | p3 7 | NsS'amount' 8 | p4 9 | I10 10 | sS'_stateversion_' 11 | p5 12 | I1 13 | sS'sid' 14 | p6 15 | I8554 16 | sS'order_id' 17 | p7 18 | S'0000' 19 | p8 20 | sS'price' 21 | p9 22 | I100 23 | sS'type' 24 | p10 25 | I5 26 | sS'dt' 27 | p11 28 | cdatetime 29 | datetime 30 | p12 31 | (S'\x07\xdd\x06\x13\x00\x00\x00\x00\x00\x00' 32 | p13 33 | tp14 34 | Rp15 35 | ssS'initargs' 36 | p16 37 | NsS'newargs' 38 | p17 39 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.finance.slippage.VolumeShareSlippage/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'price_impact' 6 | p3 7 | F0.1 8 | sS'volume_limit' 9 | p4 10 | F0.25 11 | sS'_stateversion_' 12 | p5 13 | I1 14 | ssS'initargs' 15 | p6 16 | NsS'newargs' 17 | p7 18 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.protocol.Account/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'regt_margin' 6 | p3 7 | Finf 8 | sS'maintenance_margin_requirement' 9 | p4 10 | F0.0 11 | sS'day_trades_remaining' 12 | p5 13 | Finf 14 | sS'buying_power' 15 | p6 16 | Finf 17 | sS'net_leverage' 18 | p7 19 | F0.0 20 | sS'settled_cash' 21 | p8 22 | F0.0 23 | sS'cushion' 24 | p9 25 | F0.0 26 | sS'_stateversion_' 27 | p10 28 | I1 29 | sS'leverage' 30 | p11 31 | F0.0 32 | sS'regt_equity' 33 | p12 34 | F0.0 35 | sS'excess_liquidity' 36 | p13 37 | F0.0 38 | sS'available_funds' 39 | p14 40 | F0.0 41 | sS'equity_with_loan' 42 | p15 43 | F0.0 44 | sS'initial_margin_requirement' 45 | p16 46 | F0.0 47 | sS'net_liquidation' 48 | p17 49 | F0.0 50 | sS'total_positions_value' 51 | p18 52 | F0.0 53 | sS'accrued_interest' 54 | p19 55 | F0.0 56 | ssS'initargs' 57 | p20 58 | NsS'newargs' 59 | p21 60 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.protocol.Portfolio/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'_stateversion_' 6 | p3 7 | I1 8 | sS'portfolio_value' 9 | p4 10 | F0.0 11 | sS'cash' 12 | p5 13 | F0.0 14 | sS'starting_cash' 15 | p6 16 | F0.0 17 | sS'returns' 18 | p7 19 | F0.0 20 | sS'capital_used' 21 | p8 22 | F0.0 23 | sS'pnl' 24 | p9 25 | F0.0 26 | sS'positions' 27 | p10 28 | (dp11 29 | sS'positions_value' 30 | p12 31 | F0.0 32 | sS'start_date' 33 | p13 34 | NssS'initargs' 35 | p14 36 | NsS'newargs' 37 | p15 38 | Ns. -------------------------------------------------------------------------------- /tests/resources/saved_state_archive/zipline.protocol.Position/State_Version_1: -------------------------------------------------------------------------------- 1 | (dp0 2 | S'obj_state' 3 | p1 4 | (dp2 5 | S'_stateversion_' 6 | p3 7 | I1 8 | sS'amount' 9 | p4 10 | I0 11 | sS'last_sale_price' 12 | p5 13 | F0.0 14 | sS'cost_basis' 15 | p6 16 | F0.0 17 | sS'sid' 18 | p7 19 | I8554 20 | ssS'initargs' 21 | p8 22 | NsS'newargs' 23 | p9 24 | Ns. -------------------------------------------------------------------------------- /tests/risk/AnswerKeyAnnotations.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "AnswerKeyAnnotations" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "code", 12 | "collapsed": false, 13 | "input": [ 14 | "#\n", 15 | "# Copyright 2013 Quantopian, Inc.\n", 16 | "#\n", 17 | "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", 18 | "# you may not use this file except in compliance with the License.\n", 19 | "# You may obtain a copy of the License at\n", 20 | "#\n", 21 | "# http://www.apache.org/licenses/LICENSE-2.0\n", 22 | "#\n", 23 | "# Unless required by applicable law or agreed to in writing, software\n", 24 | "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", 25 | "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", 26 | "# See the License for the specific language governing permissions and\n", 27 | "# limitations under the License." 28 | ], 29 | "language": "python", 30 | "outputs": [] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "collapsed": false, 35 | "input": [ 36 | "%load_ext autoreload\n", 37 | "%autoreload 2" 38 | ], 39 | "language": "python", 40 | "outputs": [] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "collapsed": false, 45 | "input": [ 46 | "import datetime\n", 47 | "import pandas as pd\n", 48 | "from IPython.display import HTML\n", 49 | "\n", 50 | "import answer_key\n", 51 | "ANSWER_KEY = answer_key.ANSWER_KEY" 52 | ], 53 | "language": "python", 54 | "outputs": [] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "collapsed": false, 59 | "input": [ 60 | "print 'Period Returns Index'\n", 61 | "print ANSWER_KEY.RETURNS" 62 | ], 63 | "language": "python", 64 | "outputs": [] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "collapsed": false, 69 | "input": [ 70 | "HTML(answer_key.RETURNS_DATA.to_html())" 71 | ], 72 | "language": "python", 73 | "outputs": [] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "collapsed": false, 78 | "input": [ 79 | "ANSWER_KEY.ALGORITHM_CUMULATIVE_SHARPE" 80 | ], 81 | "language": "python", 82 | "outputs": [] 83 | } 84 | ] 85 | } 86 | ] 87 | } -------------------------------------------------------------------------------- /tests/risk/AnswerKeyLink.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "" 4 | }, 5 | "nbformat": 3, 6 | "nbformat_minor": 0, 7 | "worksheets": [ 8 | { 9 | "cells": [ 10 | { 11 | "cell_type": "code", 12 | "collapsed": true, 13 | "input": [ 14 | "#\n", 15 | "# Copyright 2014 Quantopian, Inc.\n", 16 | "#\n", 17 | "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", 18 | "# you may not use this file except in compliance with the License.\n", 19 | "# You may obtain a copy of the License at\n", 20 | "#\n", 21 | "# http://www.apache.org/licenses/LICENSE-2.0\n", 22 | "#\n", 23 | "# Unless required by applicable law or agreed to in writing, software\n", 24 | "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", 25 | "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", 26 | "# See the License for the specific language governing permissions and\n", 27 | "# limitations under the License.\n", 28 | "\n", 29 | "from annotation_utils import Markdown\n", 30 | "import answer_key" 31 | ], 32 | "language": "python", 33 | "outputs": [], 34 | "prompt_number": 1 35 | }, 36 | { 37 | "cell_type": "code", 38 | "collapsed": false, 39 | "input": [ 40 | "Markdown(\"\"\"\n", 41 | "Download link for latest answer key: [{latest_answer_key_url}]({latest_answer_key_url})\n", 42 | "\"\"\".format(latest_answer_key_url=answer_key.LATEST_ANSWER_KEY_URL))" 43 | ], 44 | "language": "python", 45 | "outputs": [ 46 | { 47 | "html": [ 48 | "

Download link for latest answer key: https://s3.amazonaws.com/zipline-test-data/risk/79d117cd4849745bf72ee1fd7442ef89/risk-answer-key.xlsx

" 49 | ], 50 | "output_type": "pyout", 51 | "prompt_number": 2, 52 | "text": [ 53 | "'\\nDownload link for latest answer key: [https://s3.amazonaws.com/zipline-test-data/risk/79d117cd4849745bf72ee1fd7442ef89/risk-answer-key.xlsx](https://s3.amazonaws.com/zipline-test-data/risk/79d117cd4849745bf72ee1fd7442ef89/risk-answer-key.xlsx)\\n'" 54 | ] 55 | } 56 | ], 57 | "prompt_number": 2 58 | } 59 | ] 60 | } 61 | ] 62 | } -------------------------------------------------------------------------------- /tests/risk/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/tests/risk/__init__.py -------------------------------------------------------------------------------- /tests/risk/annotation_utils.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import markdown 17 | 18 | 19 | # Inspired by: 20 | # http://catherinedevlin.blogspot.com/2013/06/\ 21 | # easy-html-output-in-ipython-notebook.html 22 | class Markdown(str): 23 | """ 24 | Markdown wrapper to allow dynamic Markdown cells. 25 | """ 26 | def _repr_html_(self): 27 | return markdown.markdown(self) 28 | -------------------------------------------------------------------------------- /tests/risk/risk-answer-key-checksums: -------------------------------------------------------------------------------- 1 | 3ac0773c4be4e9e5bacd9c6fa0e03e15 2 | 3a5fae958c8bac684f1773fa8dff7810 3 | 19d580890e211a122e9e746f07c80cbc 4 | 70cfe3677a0ff401c801b8628e125d8f 5 | 99b3855ef1b8963163c3cb8f7e05cb70 6 | 97dfb557c3501179504926e4079e6446 7 | cc507b6fca18aabadac69657181edd4e 8 | 5b48e6a70181d73ecb7f07df5a3092e2 9 | 3343940379161143630503413627a53a 10 | 820235c4157a3c55474836438019ef2e 11 | 75c1b1441efbc2431215835a5079ccc6 12 | 37e3ea4a1788f1aa6f3ee0986bc625ae 13 | 651e611e723e2a58b1ded91d0cd39b66 14 | d62fce39ec78f032165d8f356bba5c2c 15 | 97632f6f64dfc4a2de09882419a79421 16 | 79d117cd4849745bf72ee1fd7442ef89 17 | -------------------------------------------------------------------------------- /tests/risk/upload_answer_key.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | """ 17 | Utility script for maintainer use to upload current version of the answer key 18 | spreadsheet to S3. 19 | """ 20 | import hashlib 21 | 22 | import boto 23 | 24 | from . import answer_key 25 | 26 | BUCKET_NAME = 'zipline-test-data' 27 | 28 | 29 | def main(): 30 | with open(answer_key.ANSWER_KEY_PATH, 'r') as f: 31 | md5 = hashlib.md5() 32 | while True: 33 | buf = f.read(1024) 34 | if not buf: 35 | break 36 | md5.update(buf) 37 | local_hash = md5.hexdigest() 38 | 39 | s3_conn = boto.connect_s3() 40 | 41 | bucket = s3_conn.get_bucket(BUCKET_NAME) 42 | key = boto.s3.key.Key(bucket) 43 | 44 | key.key = "risk/{local_hash}/risk-answer-key.xlsx".format( 45 | local_hash=local_hash) 46 | key.set_contents_from_filename(answer_key.ANSWER_KEY_PATH) 47 | key.set_acl('public-read') 48 | 49 | download_link = "http://s3.amazonaws.com/{bucket_name}/{key}".format( 50 | bucket_name=BUCKET_NAME, 51 | key=key.key) 52 | 53 | print("Uploaded to key: {key}".format(key=key.key)) 54 | print("Download link: {download_link}".format(download_link=download_link)) 55 | 56 | # Now update checksum file with the recently added answer key. 57 | # checksum file update will be then need to be commited via git. 58 | with open(answer_key.ANSWER_KEY_CHECKSUMS_PATH, 'a') as checksum_file: 59 | checksum_file.write(local_hash) 60 | checksum_file.write("\n") 61 | 62 | if __name__ == "__main__": 63 | main() 64 | -------------------------------------------------------------------------------- /tests/test_cli.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import os 17 | from unittest import TestCase 18 | from six import iteritems 19 | 20 | from zipline.utils import parse_args 21 | from zipline.utils import cli 22 | 23 | 24 | class TestParseArgs(TestCase): 25 | def test_defaults(self): 26 | args = parse_args([]) 27 | for k, v in iteritems(cli.DEFAULTS): 28 | self.assertEqual(v, args[k]) 29 | 30 | def write_conf_file(self): 31 | conf_str = """ 32 | [Defaults] 33 | algofile=test.py 34 | symbols=test_symbols 35 | start=1990-1-1 36 | """ 37 | 38 | with open('test.conf', 'w') as fd: 39 | fd.write(conf_str) 40 | 41 | def test_conf_file(self): 42 | self.write_conf_file() 43 | try: 44 | args = parse_args(['-c', 'test.conf']) 45 | 46 | self.assertEqual(args['algofile'], 'test.py') 47 | self.assertEqual(args['symbols'], 'test_symbols') 48 | self.assertEqual(args['start'], '1990-1-1') 49 | self.assertEqual(args['data_frequency'], 50 | cli.DEFAULTS['data_frequency']) 51 | finally: 52 | os.remove('test.conf') 53 | 54 | def test_overwrite(self): 55 | self.write_conf_file() 56 | 57 | try: 58 | args = parse_args(['-c', 'test.conf', '--start', '1992-1-1', 59 | '--algofile', 'test2.py']) 60 | 61 | # Overwritten values 62 | self.assertEqual(args['algofile'], 'test2.py') 63 | self.assertEqual(args['start'], '1992-1-1') 64 | # Non-overwritten values 65 | self.assertEqual(args['symbols'], 'test_symbols') 66 | # Default values 67 | self.assertEqual(args['data_frequency'], 68 | cli.DEFAULTS['data_frequency']) 69 | finally: 70 | os.remove('test.conf') 71 | -------------------------------------------------------------------------------- /tests/test_doctests.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import sys 3 | import doctest 4 | from unittest import TestCase 5 | 6 | from zipline import testing 7 | from zipline.lib import adjustment, normalize 8 | from zipline.pipeline import ( 9 | engine, 10 | expression, 11 | ) 12 | from zipline.utils import ( 13 | cache, 14 | data, 15 | functional, 16 | input_validation, 17 | memoize, 18 | numpy_utils, 19 | preprocess, 20 | ) 21 | 22 | 23 | class DoctestTestCase(TestCase): 24 | 25 | @classmethod 26 | def setUpClass(cls): 27 | import pdb 28 | # Workaround for the issue addressed by this (unmerged) PR to pdbpp: 29 | # https://bitbucket.org/antocuni/pdb/pull-request/40/fix-ensure_file_can_write_unicode/diff # noqa 30 | if '_pdbpp_path_hack' in pdb.__file__: 31 | cls._skip = True 32 | else: 33 | cls._skip = False 34 | cls.flags = doctest.REPORT_CDIFF | doctest.IGNORE_EXCEPTION_DETAIL 35 | 36 | def _check_docs(self, module): 37 | if self._skip: 38 | # Printing this directly to __stdout__ so that it doesn't get 39 | # captured by nose. 40 | print("Warning: Skipping doctests for %s because " 41 | "pdbpp is installed." % module.__name__, file=sys.__stdout__) 42 | return 43 | try: 44 | doctest.testmod( 45 | module, 46 | verbose=True, 47 | raise_on_error=True, 48 | optionflags=self.flags, 49 | ) 50 | except doctest.UnexpectedException as e: 51 | raise e.exc_info[1] 52 | except doctest.DocTestFailure as e: 53 | print("Got:") 54 | print(e.got) 55 | raise 56 | 57 | def test_adjustment_docs(self): 58 | self._check_docs(adjustment) 59 | 60 | def test_expression_docs(self): 61 | self._check_docs(expression) 62 | 63 | def test_engine_docs(self): 64 | self._check_docs(engine) 65 | 66 | def test_memoize_docs(self): 67 | self._check_docs(memoize) 68 | 69 | def test_testing_docs(self): 70 | self._check_docs(testing) 71 | 72 | def test_preprocess_docs(self): 73 | self._check_docs(preprocess) 74 | 75 | def test_input_validation_docs(self): 76 | self._check_docs(input_validation) 77 | 78 | def test_cache_docs(self): 79 | self._check_docs(cache) 80 | 81 | def test_numpy_utils_docs(self): 82 | self._check_docs(numpy_utils) 83 | 84 | def test_data_docs(self): 85 | self._check_docs(data) 86 | 87 | def test_functional_docs(self): 88 | self._check_docs(functional) 89 | 90 | def test_normalize_docs(self): 91 | self._check_docs(normalize) 92 | -------------------------------------------------------------------------------- /tests/test_examples.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # This code is based on a unittest written by John Salvatier: 17 | # https://github.com/pymc-devs/pymc/blob/pymc3/tests/test_examples.py 18 | 19 | import glob 20 | import matplotlib 21 | from nose_parameterized import parameterized 22 | import os 23 | import runpy 24 | from unittest import TestCase 25 | 26 | from zipline.utils import parse_args, run_pipeline 27 | 28 | # Otherwise the next line sometimes complains about being run too late. 29 | _multiprocess_can_split_ = False 30 | 31 | matplotlib.use('Agg') 32 | 33 | 34 | def example_dir(): 35 | import zipline 36 | d = os.path.dirname(zipline.__file__) 37 | return os.path.join(os.path.abspath(d), 'examples') 38 | 39 | 40 | class ExamplesTests(TestCase): 41 | # Test algorithms as if they are executed directly from the command line. 42 | @parameterized.expand(((os.path.basename(f).replace('.', '_'), f) for f in 43 | glob.glob(os.path.join(example_dir(), '*.py')))) 44 | def test_example(self, name, example): 45 | runpy.run_path(example, run_name='__main__') 46 | 47 | # Test algorithm as if scripts/run_algo.py is being used. 48 | def test_example_run_pipline(self): 49 | example = os.path.join(example_dir(), 'buyapple.py') 50 | confs = ['-f', example, '--start', '2011-1-1', '--end', '2012-1-1'] 51 | parsed_args = parse_args(confs) 52 | run_pipeline(**parsed_args) 53 | -------------------------------------------------------------------------------- /tests/test_memoize.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests for zipline.utils.memoize. 3 | """ 4 | from unittest import TestCase 5 | 6 | from zipline.utils.memoize import remember_last 7 | 8 | 9 | class TestRememberLast(TestCase): 10 | 11 | def test_remember_last(self): 12 | 13 | # Store the count in a list so we can mutate it from inside `func`. 14 | call_count = [0] 15 | 16 | @remember_last 17 | def func(x): 18 | call_count[0] += 1 19 | return x 20 | 21 | self.assertEqual((func(1), call_count[0]), (1, 1)) 22 | 23 | # Calling again with the same argument should just re-use the old 24 | # value, which means func shouldn't get called again. 25 | self.assertEqual((func(1), call_count[0]), (1, 1)) 26 | self.assertEqual((func(1), call_count[0]), (1, 1)) 27 | 28 | # Calling with a new value should increment the counter. 29 | self.assertEqual((func(2), call_count[0]), (2, 2)) 30 | self.assertEqual((func(2), call_count[0]), (2, 2)) 31 | 32 | # Calling the old value should still increment the counter. 33 | self.assertEqual((func(1), call_count[0]), (1, 3)) 34 | self.assertEqual((func(1), call_count[0]), (1, 3)) 35 | -------------------------------------------------------------------------------- /tests/test_munge.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | import random 16 | 17 | import pandas as pd 18 | import numpy as np 19 | from numpy.testing import assert_almost_equal 20 | from unittest import TestCase 21 | from zipline.utils.munge import bfill, ffill 22 | 23 | 24 | class MungeTests(TestCase): 25 | def test_bfill(self): 26 | # test ndim=1 27 | N = 100 28 | s = pd.Series(np.random.randn(N)) 29 | mask = random.sample(range(N), 10) 30 | s.iloc[mask] = np.nan 31 | 32 | correct = s.bfill().values 33 | test = bfill(s.values) 34 | assert_almost_equal(correct, test) 35 | 36 | # test ndim=2 37 | df = pd.DataFrame(np.random.randn(N, N)) 38 | df.iloc[mask] = np.nan 39 | correct = df.bfill().values 40 | test = bfill(df.values) 41 | assert_almost_equal(correct, test) 42 | 43 | def test_ffill(self): 44 | # test ndim=1 45 | N = 100 46 | s = pd.Series(np.random.randn(N)) 47 | mask = random.sample(range(N), 10) 48 | s.iloc[mask] = np.nan 49 | 50 | correct = s.ffill().values 51 | test = ffill(s.values) 52 | assert_almost_equal(correct, test) 53 | 54 | # test ndim=2 55 | df = pd.DataFrame(np.random.randn(N, N)) 56 | df.iloc[mask] = np.nan 57 | correct = df.ffill().values 58 | test = ffill(df.values) 59 | assert_almost_equal(correct, test) 60 | -------------------------------------------------------------------------------- /tests/test_pickle_serialization.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from zipline.utils.serialization_utils import ( 17 | loads_with_persistent_ids, dumps_with_persistent_ids 18 | ) 19 | 20 | from nose_parameterized import parameterized 21 | from unittest import TestCase 22 | 23 | from .serialization_cases import ( 24 | object_serialization_cases, 25 | assert_dict_equal, 26 | cases_env, 27 | ) 28 | 29 | 30 | class PickleSerializationTestCase(TestCase): 31 | 32 | @parameterized.expand(object_serialization_cases()) 33 | def test_object_serialization(self, 34 | _, 35 | cls, 36 | initargs, 37 | di_vars, 38 | comparison_method='dict'): 39 | 40 | obj = cls(*initargs) 41 | for k, v in di_vars.items(): 42 | setattr(obj, k, v) 43 | state = dumps_with_persistent_ids(obj) 44 | 45 | obj2 = loads_with_persistent_ids(state, env=cases_env) 46 | for k, v in di_vars.items(): 47 | setattr(obj2, k, v) 48 | 49 | if comparison_method == 'repr': 50 | self.assertEqual(obj.__repr__(), obj2.__repr__()) 51 | elif comparison_method == 'to_dict': 52 | assert_dict_equal(obj.to_dict(), obj2.to_dict()) 53 | else: 54 | assert_dict_equal(obj.__dict__, obj2.__dict__) 55 | -------------------------------------------------------------------------------- /tests/test_serialization.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from nose_parameterized import parameterized 17 | from unittest import TestCase 18 | from zipline.finance.trading import TradingEnvironment 19 | 20 | from .serialization_cases import ( 21 | object_serialization_cases, 22 | assert_dict_equal 23 | ) 24 | 25 | from six import iteritems 26 | 27 | 28 | def gather_bad_dicts(state): 29 | bad = [] 30 | for k, v in iteritems(state): 31 | if not isinstance(v, dict): 32 | continue 33 | if type(v) != dict: 34 | bad.append((k, v)) 35 | bad.extend(gather_bad_dicts(v)) 36 | return bad 37 | 38 | 39 | class SerializationTestCase(TestCase): 40 | @classmethod 41 | def setUpClass(cls): 42 | cls.env = TradingEnvironment() 43 | 44 | @classmethod 45 | def tearDownClass(cls): 46 | del cls.env 47 | 48 | @parameterized.expand(object_serialization_cases()) 49 | def test_object_serialization(self, 50 | _, 51 | cls, 52 | initargs, 53 | di_vars, 54 | comparison_method='dict'): 55 | 56 | obj = cls(*initargs) 57 | for k, v in di_vars.items(): 58 | setattr(obj, k, v) 59 | 60 | state = obj.__getstate__() 61 | 62 | bad_dicts = gather_bad_dicts(state) 63 | bad_template = "type({0}) == {1}".format 64 | bad_msgs = [bad_template(k, type(v)) for k, v in bad_dicts] 65 | msg = "Only support bare dicts. " + ', '.join(bad_msgs) 66 | self.assertEqual(len(bad_dicts), 0, msg) 67 | # no state should have a dict subclass. Only regular PyDict 68 | 69 | if hasattr(obj, '__getinitargs__'): 70 | initargs = obj.__getinitargs__() 71 | else: 72 | initargs = None 73 | if hasattr(obj, '__getnewargs__'): 74 | newargs = obj.__getnewargs__() 75 | else: 76 | newargs = None 77 | 78 | if newargs is not None: 79 | obj2 = cls.__new__(cls, *newargs) 80 | else: 81 | obj2 = cls.__new__(cls) 82 | if initargs is not None: 83 | obj2.__init__(*initargs) 84 | obj2.__setstate__(state) 85 | 86 | for k, v in di_vars.items(): 87 | setattr(obj2, k, v) 88 | 89 | if comparison_method == 'repr': 90 | self.assertEqual(obj.__repr__(), obj2.__repr__()) 91 | elif comparison_method == 'to_dict': 92 | assert_dict_equal(obj.to_dict(), obj2.to_dict()) 93 | else: 94 | assert_dict_equal(obj.__dict__, obj2.__dict__) 95 | -------------------------------------------------------------------------------- /tests/test_testing.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests for our testing utilities. 3 | """ 4 | from itertools import product 5 | from unittest import TestCase 6 | 7 | from zipline.testing import parameter_space 8 | 9 | 10 | class TestParameterSpace(TestCase): 11 | 12 | x_args = [1, 2] 13 | y_args = [3, 4] 14 | 15 | @classmethod 16 | def setUpClass(cls): 17 | cls.xy_invocations = [] 18 | cls.yx_invocations = [] 19 | 20 | @classmethod 21 | def tearDownClass(cls): 22 | # This is the only actual test here. 23 | assert cls.xy_invocations == list(product(cls.x_args, cls.y_args)) 24 | assert cls.yx_invocations == list(product(cls.y_args, cls.x_args)) 25 | 26 | @parameter_space(x=x_args, y=y_args) 27 | def test_xy(self, x, y): 28 | self.xy_invocations.append((x, y)) 29 | 30 | @parameter_space(x=x_args, y=y_args) 31 | def test_yx(self, y, x): 32 | # Ensure that product is called with args in the order that they appear 33 | # in the function's parameter list. 34 | self.yx_invocations.append((y, x)) 35 | 36 | def test_nothing(self): 37 | # Ensure that there's at least one "real" test in the class, or else 38 | # our {setUp,tearDown}Class won't be called if, for example, 39 | # `parameter_space` returns None. 40 | pass 41 | -------------------------------------------------------------------------------- /tests/test_tradesimulation.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | import pandas as pd 16 | 17 | from nose_parameterized import parameterized 18 | from six.moves import range 19 | from unittest import TestCase 20 | from zipline import TradingAlgorithm 21 | from zipline.test_algorithms import NoopAlgorithm 22 | from zipline.utils import factory 23 | 24 | 25 | class BeforeTradingAlgorithm(TradingAlgorithm): 26 | def __init__(self, *args, **kwargs): 27 | self.before_trading_at = [] 28 | super(BeforeTradingAlgorithm, self).__init__(*args, **kwargs) 29 | 30 | def before_trading_start(self, data): 31 | self.before_trading_at.append(self.datetime) 32 | 33 | 34 | FREQUENCIES = {'daily': 0, 'minute': 1} # daily is less frequent than minute 35 | 36 | 37 | class TestTradeSimulation(TestCase): 38 | 39 | def test_minutely_emissions_generate_performance_stats_for_last_day(self): 40 | params = factory.create_simulation_parameters(num_days=1, 41 | data_frequency='minute', 42 | emission_rate='minute') 43 | algo = NoopAlgorithm(sim_params=params) 44 | algo.run(source=[], overwrite_sim_params=False) 45 | self.assertEqual(algo.perf_tracker.day_count, 1.0) 46 | 47 | @parameterized.expand([('%s_%s_%s' % (num_days, freq, emission_rate), 48 | num_days, freq, emission_rate) 49 | for freq in FREQUENCIES 50 | for emission_rate in FREQUENCIES 51 | for num_days in range(1, 4) 52 | if FREQUENCIES[emission_rate] <= FREQUENCIES[freq]]) 53 | def test_before_trading_start(self, test_name, num_days, freq, 54 | emission_rate): 55 | params = factory.create_simulation_parameters( 56 | num_days=num_days, data_frequency=freq, 57 | emission_rate=emission_rate) 58 | 59 | algo = BeforeTradingAlgorithm(sim_params=params) 60 | algo.run(source=[], overwrite_sim_params=False) 61 | 62 | self.assertEqual(algo.perf_tracker.day_count, num_days) 63 | self.assertTrue(params.trading_days.equals( 64 | pd.DatetimeIndex(algo.before_trading_at)), 65 | "Expected %s but was %s." 66 | % (params.trading_days, algo.before_trading_at)) 67 | -------------------------------------------------------------------------------- /tests/test_versioning.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import os 17 | import pickle 18 | 19 | from nose_parameterized import parameterized 20 | from unittest import TestCase, skip 21 | 22 | from zipline.finance.blotter import Order 23 | 24 | from .serialization_cases import ( 25 | object_serialization_cases, 26 | assert_dict_equal 27 | ) 28 | 29 | base_state_dir = 'tests/resources/saved_state_archive' 30 | 31 | BASE_STATE_DIR = os.path.join( 32 | os.path.dirname(__file__), 33 | 'resources', 34 | 'saved_state_archive') 35 | 36 | 37 | class VersioningTestCase(TestCase): 38 | 39 | def load_state_from_disk(self, cls): 40 | state_dir = cls.__module__ + '.' + cls.__name__ 41 | 42 | full_dir = BASE_STATE_DIR + '/' + state_dir 43 | 44 | state_files = \ 45 | [f for f in os.listdir(full_dir) if 'State_Version_' in f] 46 | 47 | for f_name in state_files: 48 | f = open(full_dir + '/' + f_name, 'r') 49 | yield pickle.load(f) 50 | 51 | # Only test versioning in minutely mode right now 52 | @parameterized.expand(object_serialization_cases(skip_daily=True)) 53 | @skip 54 | def test_object_serialization(self, 55 | _, 56 | cls, 57 | initargs, 58 | di_vars, 59 | comparison_method='dict'): 60 | 61 | # Make reference object 62 | obj = cls(*initargs) 63 | for k, v in di_vars.items(): 64 | setattr(obj, k, v) 65 | 66 | # Fetch state 67 | state_versions = self.load_state_from_disk(cls) 68 | 69 | for version in state_versions: 70 | 71 | # For each version inflate a new object and ensure that it 72 | # matches the original. 73 | 74 | newargs = version['newargs'] 75 | initargs = version['initargs'] 76 | state = version['obj_state'] 77 | 78 | if newargs is not None: 79 | obj2 = cls.__new__(cls, *newargs) 80 | else: 81 | obj2 = cls.__new__(cls) 82 | if initargs is not None: 83 | obj2.__init__(*initargs) 84 | 85 | obj2.__setstate__(state) 86 | for k, v in di_vars.items(): 87 | setattr(obj2, k, v) 88 | 89 | # The ObjectId generated on instantiation of Order will 90 | # not be the same as the one loaded from saved state. 91 | if cls == Order: 92 | obj.__dict__['id'] = obj2.__dict__['id'] 93 | 94 | if comparison_method == 'repr': 95 | self.assertEqual(obj.__repr__(), obj2.__repr__()) 96 | elif comparison_method == 'to_dict': 97 | assert_dict_equal(obj.to_dict(), obj2.to_dict()) 98 | else: 99 | assert_dict_equal(obj.__dict__, obj2.__dict__) 100 | -------------------------------------------------------------------------------- /tests/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/tests/utils/__init__.py -------------------------------------------------------------------------------- /tests/utils/test_cache.py: -------------------------------------------------------------------------------- 1 | from unittest import TestCase 2 | 3 | from pandas import Timestamp, Timedelta 4 | 5 | from zipline.utils.cache import CachedObject, Expired 6 | 7 | 8 | class CachedObjectTestCase(TestCase): 9 | 10 | def test_cached_object(self): 11 | expiry = Timestamp('2014') 12 | before = expiry - Timedelta('1 minute') 13 | after = expiry + Timedelta('1 minute') 14 | 15 | obj = CachedObject(1, expiry) 16 | 17 | self.assertEqual(obj.unwrap(before), 1) 18 | self.assertEqual(obj.unwrap(expiry), 1) # Unwrap on expiry is allowed. 19 | with self.assertRaises(Expired) as e: 20 | obj.unwrap(after) 21 | self.assertEqual(e.exception.args, (expiry,)) 22 | -------------------------------------------------------------------------------- /tests/utils/test_factory.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from unittest import TestCase 17 | 18 | import pandas as pd 19 | import pytz 20 | import numpy as np 21 | 22 | from zipline.utils.factory import (load_from_yahoo, 23 | load_bars_from_yahoo) 24 | 25 | 26 | class TestFactory(TestCase): 27 | def test_load_from_yahoo(self): 28 | stocks = ['AAPL', 'GE'] 29 | start = pd.datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc) 30 | end = pd.datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) 31 | data = load_from_yahoo(stocks=stocks, start=start, end=end) 32 | 33 | assert data.index[0] == pd.Timestamp('1993-01-04 00:00:00+0000') 34 | assert data.index[-1] == pd.Timestamp('2001-12-31 00:00:00+0000') 35 | for stock in stocks: 36 | assert stock in data.columns 37 | 38 | np.testing.assert_raises( 39 | AssertionError, load_from_yahoo, stocks=stocks, 40 | start=end, end=start 41 | ) 42 | 43 | def test_load_bars_from_yahoo(self): 44 | stocks = ['AAPL', 'GE'] 45 | start = pd.datetime(1993, 1, 1, 0, 0, 0, 0, pytz.utc) 46 | end = pd.datetime(2002, 1, 1, 0, 0, 0, 0, pytz.utc) 47 | data = load_bars_from_yahoo(stocks=stocks, start=start, end=end) 48 | 49 | assert data.major_axis[0] == pd.Timestamp('1993-01-04 00:00:00+0000') 50 | assert data.major_axis[-1] == pd.Timestamp('2001-12-31 00:00:00+0000') 51 | for stock in stocks: 52 | assert stock in data.items 53 | 54 | for ohlc in ['open', 'high', 'low', 'close', 'volume', 'price']: 55 | assert ohlc in data.minor_axis 56 | 57 | np.testing.assert_raises( 58 | AssertionError, load_bars_from_yahoo, stocks=stocks, 59 | start=end, end=start 60 | ) 61 | -------------------------------------------------------------------------------- /tests/utils/test_numpy_utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests for zipline.utils.numpy_utils. 3 | """ 4 | from datetime import datetime 5 | from six import itervalues 6 | from unittest import TestCase 7 | 8 | from numpy import ( 9 | array, 10 | float16, 11 | float32, 12 | float64, 13 | int16, 14 | int32, 15 | int64, 16 | ) 17 | from pandas import Timestamp 18 | from toolz import concat, keyfilter 19 | from toolz import curry 20 | from toolz.curried.operator import ne 21 | 22 | from zipline.utils.functional import mapall as lazy_mapall 23 | from zipline.utils.numpy_utils import ( 24 | is_float, 25 | is_int, 26 | is_datetime, 27 | make_datetime64D, 28 | make_datetime64ns, 29 | NaTns, 30 | NaTD, 31 | ) 32 | 33 | 34 | def mapall(*args): 35 | "Strict version of mapall." 36 | return list(lazy_mapall(*args)) 37 | 38 | 39 | @curry 40 | def make_array(dtype, value): 41 | return array([value], dtype=dtype) 42 | 43 | 44 | CASES = { 45 | int: mapall( 46 | (int, int16, int32, int64, make_array(int)), 47 | [0, 1, -1] 48 | ), 49 | float: mapall( 50 | (float16, float32, float64, float, make_array(float)), 51 | [0., 1., -1., float('nan'), float('inf'), -float('inf')], 52 | ), 53 | datetime: mapall( 54 | ( 55 | make_datetime64D, 56 | make_datetime64ns, 57 | Timestamp, 58 | make_array('datetime64[ns]'), 59 | ), 60 | [0, 1, 2], 61 | ) + [NaTD, NaTns], 62 | } 63 | 64 | 65 | def everything_but(k, d): 66 | """ 67 | Return iterator of all values in d except the values in k. 68 | """ 69 | assert k in d 70 | return concat(itervalues(keyfilter(ne(k), d))) 71 | 72 | 73 | class TypeCheckTestCase(TestCase): 74 | 75 | def test_is_float(self): 76 | for good_value in CASES[float]: 77 | self.assertTrue(is_float(good_value)) 78 | 79 | for bad_value in everything_but(float, CASES): 80 | self.assertFalse(is_float(bad_value)) 81 | 82 | def test_is_int(self): 83 | for good_value in CASES[int]: 84 | self.assertTrue(is_int(good_value)) 85 | 86 | for bad_value in everything_but(int, CASES): 87 | self.assertFalse(is_int(bad_value)) 88 | 89 | def test_is_datetime(self): 90 | for good_value in CASES[datetime]: 91 | self.assertTrue(is_datetime(good_value)) 92 | 93 | for bad_value in everything_but(datetime, CASES): 94 | self.assertFalse(is_datetime(bad_value)) 95 | -------------------------------------------------------------------------------- /tests/utils/test_sentinel.py: -------------------------------------------------------------------------------- 1 | from copy import copy, deepcopy 2 | from pickle import loads, dumps 3 | from unittest import TestCase 4 | from weakref import ref 5 | 6 | from zipline.utils.sentinel import sentinel 7 | 8 | 9 | class SentinelTestCase(TestCase): 10 | def tearDown(self): 11 | sentinel._cache.clear() # don't pollute cache. 12 | 13 | def test_name(self): 14 | self.assertEqual(sentinel('a').__name__, 'a') 15 | 16 | def test_doc(self): 17 | self.assertEqual(sentinel('a', 'b').__doc__, 'b') 18 | 19 | def test_doc_differentiates(self): 20 | a = sentinel('sentinel-name', 'original-doc') 21 | with self.assertRaises(ValueError) as e: 22 | sentinel(a.__name__, 'new-doc') 23 | 24 | msg = str(e.exception) 25 | self.assertIn(a.__name__, msg) 26 | self.assertIn(a.__doc__, msg) 27 | 28 | def test_memo(self): 29 | self.assertIs(sentinel('a'), sentinel('a')) 30 | 31 | def test_copy(self): 32 | a = sentinel('a') 33 | self.assertIs(copy(a), a) 34 | 35 | def test_deepcopy(self): 36 | a = sentinel('a') 37 | self.assertIs(deepcopy(a), a) 38 | 39 | def test_repr(self): 40 | self.assertEqual( 41 | repr(sentinel('a')), 42 | "sentinel('a')", 43 | ) 44 | 45 | def test_new(self): 46 | with self.assertRaises(TypeError): 47 | type(sentinel('a'))() 48 | 49 | def test_pickle_roundtrip(self): 50 | a = sentinel('a') 51 | self.assertIs(loads(dumps(a)), a) 52 | 53 | def test_weakreferencable(self): 54 | ref(sentinel('a')) 55 | -------------------------------------------------------------------------------- /vagrant_init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # This script will be run by Vagrant to 4 | # set up everything necessary to use Zipline. 5 | 6 | # Because this is intended be a disposable dev VM setup, 7 | # no effort is made to use virtualenv/virtualenvwrapper 8 | 9 | # It is assumed that you have "vagrant up" 10 | # from the root of the zipline github checkout. 11 | # This will put the zipline code in the 12 | # /vagrant folder in the system. 13 | 14 | VAGRANT_LOG="/home/vagrant/vagrant.log" 15 | 16 | # Need to "hold" grub-pc so that it doesn't break 17 | # the rest of the package installs (in case of a "apt-get upgrade") 18 | # (grub-pc will complain that your boot device changed, probably 19 | # due to something that vagrant did, and break your console) 20 | 21 | echo "Obstructing updates to grub-pc..." 22 | apt-mark hold grub-pc 2>&1 >> "$VAGRANT_LOG" 23 | 24 | # Run a full apt-get update first. 25 | echo "Updating apt-get caches..." 26 | apt-get -y update 2>&1 >> "$VAGRANT_LOG" 27 | 28 | # Install required packages 29 | echo "Installing required packages..." 30 | apt-get -y install python-pip python-dev g++ make libfreetype6-dev libpng-dev libopenblas-dev liblapack-dev gfortran 2>&1 >> "$VAGRANT_LOG" 31 | 32 | # Add ta-lib 33 | echo "Installing ta-lib integration..." 34 | wget http://switch.dl.sourceforge.net/project/ta-lib/ta-lib/0.4.0/ta-lib-0.4.0-src.tar.gz 2>&1 "$VAGRANT_LOG" 35 | tar -xvzf ta-lib-0.4.0-src.tar.gz 2>&1 >> "$VAGRANT_LOG" 36 | cd ta-lib/ 37 | ./configure --prefix=/usr 2>&1 >> "$VAGRANT_LOG" 38 | make 2>&1 >> "$VAGRANT_LOG" 39 | sudo make install 2>&1 >> "$VAGRANT_LOG" 40 | cd ../ 41 | 42 | # Add Zipline python dependencies 43 | echo "Installing python package dependencies..." 44 | /vagrant/etc/ordered_pip.sh /vagrant/etc/requirements.txt 2>&1 >> "$VAGRANT_LOG" 45 | # Add scipy next (if it's not done now, breaks installing of statsmodels for some reason ??) 46 | echo "Installing scipy..." 47 | pip install scipy==0.12.0 2>&1 >> "$VAGRANT_LOG" 48 | echo "Installing zipline dev python dependencies..." 49 | pip install --exists-action w -r /vagrant/etc/requirements_dev.txt 2>&1 >> "$VAGRANT_LOG" 50 | echo "Finished!" 51 | -------------------------------------------------------------------------------- /zipline/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | 17 | # This is *not* a place to dump arbitrary classes/modules for convenience, 18 | # it is a place to expose the public interfaces. 19 | from . import data 20 | from . import finance 21 | from . import gens 22 | from . import utils 23 | from . import transforms 24 | from ._version import get_versions 25 | # These need to happen after the other imports. 26 | from . algorithm import TradingAlgorithm 27 | from . import api 28 | 29 | __version__ = get_versions()['version'] 30 | del get_versions 31 | 32 | try: 33 | ip = get_ipython() # flake8: noqa 34 | except NameError: 35 | pass 36 | else: 37 | ip.register_magic_function(utils.parse_cell_magic, "line_cell", "zipline") 38 | del ip 39 | 40 | __all__ = [ 41 | 'data', 42 | 'finance', 43 | 'gens', 44 | 'utils', 45 | 'transforms', 46 | 'api', 47 | 'TradingAlgorithm', 48 | ] 49 | -------------------------------------------------------------------------------- /zipline/api.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # Note that part of the API is implemented in TradingAlgorithm as 17 | # methods (e.g. order). These are added to this namespace via the 18 | # decorator `api_methods` inside of algorithm.py. 19 | 20 | import zipline 21 | from .finance import (commission, slippage) 22 | from .utils import math_utils, events 23 | 24 | from zipline.finance.slippage import ( 25 | FixedSlippage, 26 | VolumeShareSlippage, 27 | ) 28 | 29 | from zipline.utils.events import ( 30 | date_rules, 31 | time_rules 32 | ) 33 | 34 | batch_transform = zipline.transforms.BatchTransform 35 | 36 | 37 | __all__ = [ 38 | 'slippage', 39 | 'commission', 40 | 'events', 41 | 'math_utils', 42 | 'batch_transform', 43 | 'FixedSlippage', 44 | 'VolumeShareSlippage', 45 | 'date_rules', 46 | 'time_rules' 47 | ] 48 | -------------------------------------------------------------------------------- /zipline/assets/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from ._assets import ( 17 | Asset, 18 | Equity, 19 | Future, 20 | make_asset_array, 21 | CACHE_FILE_TEMPLATE 22 | ) 23 | from .assets import ( 24 | AssetFinder, 25 | AssetConvertible, 26 | AssetFinderCachedEquities 27 | ) 28 | 29 | __all__ = [ 30 | 'Asset', 31 | 'Equity', 32 | 'Future', 33 | 'AssetFinder', 34 | 'AssetFinderCachedEquities', 35 | 'AssetConvertible', 36 | 'make_asset_array', 37 | 'CACHE_FILE_TEMPLATE' 38 | ] 39 | -------------------------------------------------------------------------------- /zipline/data/__init__.py: -------------------------------------------------------------------------------- 1 | from . import loader 2 | from .loader import ( 3 | load_from_yahoo, load_bars_from_yahoo, load_prices_from_csv, 4 | load_prices_from_csv_folder 5 | ) 6 | 7 | __all__ = ['loader', 'load_from_yahoo', 'load_bars_from_yahoo', 8 | 'load_prices_from_csv', 'load_prices_from_csv_folder'] 9 | -------------------------------------------------------------------------------- /zipline/data/benchmarks.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | import pandas as pd 16 | 17 | from six.moves.urllib_parse import urlencode 18 | 19 | import constants 20 | 21 | from .mongodb import LoadDataCVS 22 | 23 | 24 | 25 | def format_yahoo_index_url(symbol, start_date, end_date): 26 | """ 27 | Format a URL for querying Yahoo Finance for Index data. 28 | """ 29 | return ( 30 | 'http://ichart.finance.yahoo.com/table.csv?' + urlencode({ 31 | 's': symbol, 32 | # start_date month, zero indexed 33 | 'a': start_date.month - 1, 34 | # start_date day 35 | 'b': start_date.day, 36 | # start_date year 37 | 'c': start_date.year, 38 | # end_date month, zero indexed 39 | 'd': end_date.month - 1, 40 | # end_date day 41 | 'e': end_date.day, 42 | # end_date year 43 | 'f': end_date.year, 44 | # daily frequency 45 | 'g': 'd', 46 | }) 47 | ) 48 | 49 | 50 | def get_benchmark_returns(symbol, start_date, end_date): 51 | """ 52 | Get a Series of benchmark returns from Yahoo. 53 | 54 | Returns a Series with returns from (start_date, end_date]. 55 | 56 | start_date is **not** included because we need the close from day N - 1 to 57 | compute the returns for day N. 58 | """ 59 | l=LoadDataCVS(constants.IP,constants.PORT) 60 | l.Conn() 61 | data=l.getBenchamark(symbol,start_date,end_date) 62 | l.Close() 63 | return data 64 | -------------------------------------------------------------------------------- /zipline/data/constants.py: -------------------------------------------------------------------------------- 1 | IP="127.0.0.1" 2 | PORT=27017 3 | -------------------------------------------------------------------------------- /zipline/data/future_pricing.py: -------------------------------------------------------------------------------- 1 | # Copyright 2015 Quantopian, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | class FutureDailyReader(object): 17 | """ 18 | Stubbed out. Currently unimplemented. 19 | """ 20 | pass 21 | 22 | 23 | class FutureMinuteReader(object): 24 | 25 | def __init__(self, rootdir, sid_path_func=None): 26 | self.rootdir = rootdir 27 | self.sid_path_func = sid_path_func 28 | -------------------------------------------------------------------------------- /zipline/data/paths.py: -------------------------------------------------------------------------------- 1 | """ 2 | Canonical path locations for zipline data. 3 | 4 | Paths are rooted at $ZIPLINE_ROOT if that environment variable is set. 5 | Otherwise default to expanduser(~/.zipline) 6 | """ 7 | import os 8 | from os.path import ( 9 | expanduser, 10 | join, 11 | ) 12 | 13 | 14 | def zipline_root(environ=None): 15 | """ 16 | Get the root directory for all zipline-managed files. 17 | 18 | For testing purposes, this accepts a dictionary to interpret as the os 19 | environment. 20 | 21 | Parameters 22 | ---------- 23 | environ : dict, optional 24 | A dict to interpret as the os environment. 25 | 26 | Returns 27 | ------- 28 | root : string 29 | Path to the zipline root dir. 30 | """ 31 | if environ is None: 32 | environ = os.environ.copy() 33 | 34 | root = environ.get('ZIPLINE_ROOT', None) 35 | if root is None: 36 | root = expanduser('~/.zipline') 37 | 38 | return root 39 | 40 | 41 | def zipline_root_path(path, environ=None): 42 | """ 43 | Get a path relative to the zipline root. 44 | 45 | Parameters 46 | ---------- 47 | path : str 48 | The requested path. 49 | environ : dict, optional 50 | An environment dict to forward to zipline_root. 51 | 52 | Returns 53 | ------- 54 | newpath : str 55 | The requested path joined with the zipline root. 56 | """ 57 | return join(zipline_root(environ=environ), path) 58 | 59 | 60 | def data_root(environ=None): 61 | """ 62 | The root directory for zipline data files. 63 | 64 | Parameters 65 | ---------- 66 | environ : dict, optional 67 | An environment dict to forward to zipline_root. 68 | 69 | Returns 70 | ------- 71 | data_root : str 72 | The zipline data root. 73 | """ 74 | return zipline_root_path('data', environ=environ) 75 | 76 | 77 | def cache_root(environ=None): 78 | """ 79 | The root directory for zipline cache files. 80 | 81 | Parameters 82 | ---------- 83 | environ : dict, optional 84 | An environment dict to forward to zipline_root. 85 | 86 | Returns 87 | ------- 88 | cache_root : str 89 | The zipline cache root. 90 | """ 91 | return zipline_root_path('cache', environ=environ) 92 | -------------------------------------------------------------------------------- /zipline/data/xlsx/2002.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2002.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2003.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2003.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2004.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2004.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2005.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2005.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2006.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2006.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2007.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2007.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2008.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2008.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2009.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2009.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2010.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2010.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2011.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2011.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2012.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2012.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2013.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2013.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2014.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2014.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2015.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2015.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/2016.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/2016.xlsx -------------------------------------------------------------------------------- /zipline/data/xlsx/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/data/xlsx/__init__.py -------------------------------------------------------------------------------- /zipline/examples/buy_and_hold.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | import pandas as pd 16 | from zipline import TradingAlgorithm 17 | from zipline.api import order, sid,get_datetime 18 | from zipline.data.loader import load_data 19 | from zipline.api import order_target, record, symbol, history, add_history,symbol,set_commission,order_percent,set_long_only,get_open_orders 20 | from zipline.finance.commission import OrderCost 21 | from pylab import * 22 | mpl.rcParams['font.sans-serif'] = ['SimHei'] 23 | mpl.rcParams['axes.unicode_minus'] = False 24 | 25 | 26 | 27 | # loading the data 28 | input_data = load_data( 29 | stockList=['002057'], 30 | start="2015-11-04", 31 | end="2016-01-16" 32 | ) 33 | def analyze(context=None, results=None): 34 | import matplotlib.pyplot as plt 35 | 36 | # Plot the portfolio and asset data. 37 | ax1 = plt.subplot(211) 38 | results.algorithm_period_return.plot(ax=ax1,color='blue',legend=u'策略收益') 39 | ax1.set_ylabel(u'收益') 40 | results.benchmark_period_return.plot(ax=ax1,color='red',legend=u'基准收益') 41 | 42 | # Show the plot. 43 | plt.gcf().set_size_inches(18, 8) 44 | plt.show() 45 | 46 | 47 | 48 | def initialize(context): 49 | context.has_ordered = False 50 | set_commission(OrderCost(open_tax=0,close_tax=0.001,open_commission=0.0003,close_commission=0.0003,close_today_commission=0,min_commission=5)) 51 | set_long_only() 52 | 53 | def handle_data(context, data): 54 | 55 | #输出每天持仓情况 56 | 57 | if not context.has_ordered: 58 | for stock in data: 59 | #openprice=history(3, '1d', 'open') 60 | closeprice=history(5,'1d','close') 61 | #-2:昨天,-3 前天.-4 大前天 62 | print get_datetime(),closeprice[sid(stock)][0],closeprice[sid(stock)][1],closeprice[sid(stock)][2],closeprice[sid(stock)][3],closeprice[sid(stock)][4] 63 | #print closeprice,closeprice[sid(stock)][1] 64 | if closeprice[sid(stock)][-2]>closeprice[sid(stock)][-3] and closeprice[sid(stock)][-3]>closeprice[sid(stock)][-4]: 65 | print "buy",get_datetime() 66 | order(stock, 300) 67 | elif closeprice[sid(stock)][-2] ma10[sid(symbol(context.security))]: 46 | order_value(symbol(context.security), cash) 47 | # 如果五日均线小于十日均线,并且目前有头寸 48 | elif ma5[sid(symbol(context.security))] < ma10[sid(symbol(context.security))]: 49 | # 全部卖出 50 | order_target(symbol(context.security), 0) 51 | # 记录这次卖出 52 | #log.info("Selling %s" % (context.security)) 53 | 54 | #record(short_mavg=ma) 55 | 56 | # # 绘制五日均线价格 57 | # record(ma5=ma5) 58 | # # 绘制十日均线价格 59 | # record(ma10=ma10) 60 | def analyze(context=None, results=None): 61 | import matplotlib.pyplot as plt 62 | import logbook 63 | logbook.StderrHandler().push_application() 64 | log = logbook.Logger('Algorithm') 65 | 66 | fig = plt.figure() 67 | ax1 = fig.add_subplot(211) 68 | 69 | results.algorithm_period_return.plot(ax=ax1,color='blue',legend=u'策略收益') 70 | ax1.set_ylabel(u'收益') 71 | results.benchmark_period_return.plot(ax=ax1,color='red',legend=u'基准收益') 72 | 73 | plt.show() 74 | 75 | # capital_base is the base value of capital 76 | # 77 | algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data,capital_base=10000,benchmark='000300') 78 | 79 | #print input_data 80 | #api: print all the api function 81 | #print algo.all_api_methods() 82 | results = algo.run(input_data) 83 | #print results['benchmark_period_return'],results['portfolio_value'] 84 | analyze(results=results) 85 | 86 | -------------------------------------------------------------------------------- /zipline/examples/stock_select.py: -------------------------------------------------------------------------------- 1 | #encoding:utf-8 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | import pandas as pd 16 | from zipline import TradingAlgorithm 17 | from zipline.api import order, sid,get_datetime 18 | from zipline.data.loader import load_data 19 | from zipline.api import order_target, record, symbol, history, add_history,symbol,set_commission,order_percent,set_long_only,get_open_orders,run_monthly,run_weekly 20 | from zipline.finance.commission import OrderCost 21 | from pylab import * 22 | mpl.rcParams['font.sans-serif'] = ['SimHei'] 23 | mpl.rcParams['axes.unicode_minus'] = False 24 | 25 | 26 | 27 | def analyze(context=None, results=None): 28 | import matplotlib.pyplot as plt 29 | 30 | # Plot the portfolio and asset data. 31 | ax1 = plt.subplot(211) 32 | results.algorithm_period_return.plot(ax=ax1,color='blue',legend=u'策略收益') 33 | ax1.set_ylabel(u'收益') 34 | results.benchmark_period_return.plot(ax=ax1,color='red',legend=u'基准收益') 35 | 36 | # Show the plot. 37 | plt.gcf().set_size_inches(18, 8) 38 | plt.show() 39 | 40 | 41 | 42 | # loading the data 43 | input_data = load_data( 44 | stockList= ['000001','000002','000004','000005'], 45 | start="2013-11-01", 46 | end="2016-01-16" 47 | ) 48 | 49 | 50 | def initialize(context): 51 | # 初始化此策略 52 | # 设置我们要操作的股票池 53 | context.stocks = ['000001','000002','000004','000005'] 54 | 55 | 56 | 57 | # 每个单位时间(如果按天回测,则每天调用一次,如果按分钟,则每分钟调用一次)调用一次 58 | def handle_data(context, data): 59 | 60 | # context.i+=1 61 | # if context.i<=5: 62 | # return 63 | # 循环每只股票 64 | 65 | closeprice= history(5,'1d','close') 66 | for security in context.stocks: 67 | vwap=(closeprice[symbol(security)][-2]+closeprice[symbol(security)][-3]+closeprice[symbol(security)][-4])/3 68 | price = closeprice[symbol(security)][-2] 69 | print get_datetime(),security,vwap,price 70 | # # 如果上一时间点价格小于三天平均价*0.995,并且持有该股票,卖出 71 | if price < vwap * 0.995: 72 | # 下入卖出单 73 | order(symbol(security),-300) 74 | print get_datetime(),("Selling %s" % (security)) 75 | # 记录这次卖出 76 | #log.info("Selling %s" % (security)) 77 | # 如果上一时间点价格大于三天平均价*1.005,并且有现金余额,买入 78 | elif price > vwap * 1.005: 79 | # 下入买入单 80 | order(symbol(security),300) 81 | # 记录这次买入 82 | print get_datetime(),("Buying %s" % (security)) 83 | #log.info("Buying %s" % (security)) 84 | 85 | 86 | algo = TradingAlgorithm(initialize=initialize, handle_data=handle_data,capital_base=100000,benchmark='000300') 87 | 88 | #print input_data 89 | #api: print all the api function 90 | #print algo.all_api_methods() 91 | results = algo.run(input_data) 92 | #print results['benchmark_period_return'],results['portfolio_value'] 93 | analyze(results=results) 94 | 95 | 96 | -------------------------------------------------------------------------------- /zipline/finance/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from . import execution, trading 17 | 18 | __all__ = [ 19 | 'trading', 20 | 'execution' 21 | ] 22 | -------------------------------------------------------------------------------- /zipline/finance/constants.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2012 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | TRADING_DAYS_IN_YEAR = 250 17 | TRADING_HOURS_IN_DAY = 6.5 18 | MINUTES_IN_HOUR = 60 19 | 20 | ANNUALIZER = {'daily': TRADING_DAYS_IN_YEAR, 21 | 'hourly': TRADING_DAYS_IN_YEAR * TRADING_HOURS_IN_DAY, 22 | 'minute': TRADING_DAYS_IN_YEAR * TRADING_HOURS_IN_DAY * 23 | MINUTES_IN_HOUR} 24 | -------------------------------------------------------------------------------- /zipline/finance/performance/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from . tracker import PerformanceTracker 17 | from . period import PerformancePeriod 18 | from . position import Position 19 | from . position_tracker import PositionTracker 20 | 21 | __all__ = [ 22 | 'PerformanceTracker', 23 | 'PerformancePeriod', 24 | 'Position', 25 | 'PositionTracker', 26 | ] 27 | -------------------------------------------------------------------------------- /zipline/finance/risk/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from . report import RiskReport 17 | from . period import RiskMetricsPeriod 18 | from . cumulative import RiskMetricsCumulative 19 | 20 | 21 | __all__ = [ 22 | 'RiskReport', 23 | 'RiskMetricsPeriod', 24 | 'RiskMetricsCumulative', 25 | ] 26 | -------------------------------------------------------------------------------- /zipline/finance/transaction.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | from __future__ import division 16 | 17 | from copy import copy 18 | 19 | from zipline.protocol import DATASOURCE_TYPE 20 | from zipline.utils.serialization_utils import ( 21 | VERSION_LABEL 22 | ) 23 | 24 | 25 | class Transaction(object): 26 | 27 | def __init__(self, sid, amount, dt, price, order_id, commission=None): 28 | self.sid = sid 29 | self.amount = amount 30 | self.dt = dt 31 | self.price = price 32 | self.order_id = order_id 33 | self.commission = commission 34 | self.type = DATASOURCE_TYPE.TRANSACTION 35 | 36 | def __getitem__(self, name): 37 | return self.__dict__[name] 38 | 39 | def to_dict(self): 40 | py = copy(self.__dict__) 41 | del py['type'] 42 | return py 43 | 44 | def __getstate__(self): 45 | 46 | state_dict = copy(self.__dict__) 47 | 48 | STATE_VERSION = 1 49 | state_dict[VERSION_LABEL] = STATE_VERSION 50 | 51 | return state_dict 52 | 53 | def __setstate__(self, state): 54 | 55 | OLDEST_SUPPORTED_STATE = 1 56 | version = state.pop(VERSION_LABEL) 57 | 58 | if version < OLDEST_SUPPORTED_STATE: 59 | raise BaseException("Transaction saved state is too old.") 60 | 61 | self.__dict__.update(state) 62 | 63 | 64 | def create_transaction(event, order, price, amount): 65 | 66 | # floor the amount to protect against non-whole number orders 67 | # TODO: Investigate whether we can add a robust check in blotter 68 | # and/or tradesimulation, as well. 69 | amount_magnitude = int(abs(amount)) 70 | 71 | if amount_magnitude < 1: 72 | raise Exception("Transaction magnitude must be at least 1.") 73 | 74 | transaction = Transaction( 75 | sid=event.sid, 76 | amount=int(amount), 77 | dt=event.dt, 78 | price=price, 79 | order_id=order.id 80 | ) 81 | 82 | return transaction 83 | -------------------------------------------------------------------------------- /zipline/gens/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/gens/__init__.py -------------------------------------------------------------------------------- /zipline/gens/composites.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import heapq 17 | 18 | 19 | def _decorate_source(source): 20 | for message in source: 21 | yield ((message.dt, message.source_id), message) 22 | 23 | 24 | def date_sorted_sources(*sources): 25 | """ 26 | Takes an iterable of sources, generating namestrings and 27 | piping their output into date_sort. 28 | """ 29 | sorted_stream = heapq.merge(*(_decorate_source(s) for s in sources)) 30 | 31 | # Strip out key decoration 32 | for _, message in sorted_stream: 33 | yield message 34 | -------------------------------------------------------------------------------- /zipline/gens/utils.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | 17 | import pytz 18 | import numbers 19 | 20 | from hashlib import md5 21 | from datetime import datetime 22 | from zipline.protocol import DATASOURCE_TYPE 23 | 24 | from six import iteritems, b 25 | 26 | 27 | def hash_args(*args, **kwargs): 28 | """Define a unique string for any set of representable args.""" 29 | arg_string = '_'.join([str(arg) for arg in args]) 30 | kwarg_string = '_'.join([str(key) + '=' + str(value) 31 | for key, value in iteritems(kwargs)]) 32 | combined = ':'.join([arg_string, kwarg_string]) 33 | 34 | hasher = md5() 35 | hasher.update(b(combined)) 36 | return hasher.hexdigest() 37 | 38 | 39 | def assert_datasource_protocol(event): 40 | """Assert that an event meets the protocol for datasource outputs.""" 41 | 42 | assert event.type in DATASOURCE_TYPE 43 | 44 | # Done packets have no dt. 45 | if not event.type == DATASOURCE_TYPE.DONE: 46 | assert isinstance(event.dt, datetime) 47 | assert event.dt.tzinfo == pytz.utc 48 | 49 | 50 | def assert_trade_protocol(event): 51 | """Assert that an event meets the protocol for datasource TRADE outputs.""" 52 | assert_datasource_protocol(event) 53 | 54 | assert event.type == DATASOURCE_TYPE.TRADE 55 | assert isinstance(event.price, numbers.Real) 56 | assert isinstance(event.volume, numbers.Integral) 57 | assert isinstance(event.dt, datetime) 58 | 59 | 60 | def assert_datasource_unframe_protocol(event): 61 | """Assert that an event is valid output of zp.DATASOURCE_UNFRAME.""" 62 | assert event.type in DATASOURCE_TYPE 63 | -------------------------------------------------------------------------------- /zipline/history/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from . history import ( 17 | HistorySpec, 18 | Frequency, 19 | ) 20 | 21 | from . import history_container 22 | 23 | __all__ = [ 24 | 'HistorySpec', 25 | 'history_container', 26 | 'Frequency', 27 | ] 28 | -------------------------------------------------------------------------------- /zipline/lib/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/lib/__init__.py -------------------------------------------------------------------------------- /zipline/lib/_float64window.pyx: -------------------------------------------------------------------------------- 1 | """ 2 | float specialization of AdjustedArrayWindow 3 | """ 4 | from numpy cimport float64_t as ctype 5 | include "_windowtemplate.pxi" 6 | -------------------------------------------------------------------------------- /zipline/lib/_int64window.pyx: -------------------------------------------------------------------------------- 1 | """ 2 | datetime specialization of AdjustedArrayWindow 3 | """ 4 | from numpy cimport int64_t as ctype 5 | include "_windowtemplate.pxi" 6 | -------------------------------------------------------------------------------- /zipline/lib/_uint8window.pyx: -------------------------------------------------------------------------------- 1 | """ 2 | bool specialization of AdjustedArrayWindow 3 | """ 4 | from numpy cimport uint8_t as ctype 5 | include "_windowtemplate.pxi" 6 | -------------------------------------------------------------------------------- /zipline/lib/_windowtemplate.pxi: -------------------------------------------------------------------------------- 1 | """ 2 | Template for AdjustedArray windowed iterators. 3 | 4 | This file is intended to be used by inserting it via a Cython include into a 5 | file that's define a type symbol named `ctype` and string constant named 6 | `dtype`. 7 | 8 | See Also 9 | -------- 10 | zipline.lib._floatwindow 11 | zipline.lib._intwindow 12 | zipline.lib._datewindow 13 | """ 14 | from numpy cimport ndarray 15 | from numpy import asarray 16 | 17 | ctypedef ctype[:, :] databuffer 18 | 19 | 20 | cdef class AdjustedArrayWindow: 21 | """ 22 | An iterator representing a moving view over an AdjustedArray. 23 | 24 | Concrete subtypes should subclass this and provide a `data` attribute for 25 | specific types. 26 | 27 | This object stores a copy of the data from the AdjustedArray over which 28 | it's iterating. At each step in the iteration, it mutates its copy to 29 | allow us to show different data when looking back over the array. 30 | 31 | The arrays yielded by this iterator are always views over the underlying 32 | data. 33 | """ 34 | cdef: 35 | # ctype must be defined by the file into which this is being copied. 36 | databuffer data 37 | object viewtype 38 | readonly Py_ssize_t window_length 39 | Py_ssize_t anchor, max_anchor, next_adj 40 | dict adjustments 41 | list adjustment_indices 42 | 43 | def __cinit__(self, 44 | databuffer data not None, 45 | object viewtype not None, 46 | dict adjustments not None, 47 | Py_ssize_t offset, 48 | Py_ssize_t window_length): 49 | 50 | self.data = data 51 | self.viewtype = viewtype 52 | self.adjustments = adjustments 53 | self.adjustment_indices = sorted(adjustments, reverse=True) 54 | self.window_length = window_length 55 | self.anchor = window_length + offset 56 | self.max_anchor = data.shape[0] 57 | 58 | self.next_adj = self.pop_next_adj() 59 | 60 | cdef pop_next_adj(self): 61 | """ 62 | Pop the index of the next adjustment to apply from self.adjustment_indices. 63 | """ 64 | if len(self.adjustment_indices) > 0: 65 | return self.adjustment_indices.pop() 66 | else: 67 | return self.max_anchor 68 | 69 | def __iter__(self): 70 | return self 71 | 72 | def __next__(self): 73 | cdef: 74 | ndarray out 75 | object adjustment 76 | Py_ssize_t start, anchor 77 | 78 | anchor = self.anchor 79 | if anchor > self.max_anchor: 80 | raise StopIteration() 81 | 82 | # Apply any adjustments that occured before our current anchor. 83 | # Equivalently, apply any adjustments known **on or before** the date 84 | # for which we're calculating a window. 85 | while self.next_adj < anchor: 86 | 87 | for adjustment in self.adjustments[self.next_adj]: 88 | adjustment.mutate(self.data) 89 | 90 | self.next_adj = self.pop_next_adj() 91 | 92 | start = anchor - self.window_length 93 | out = asarray(self.data[start:self.anchor]).view(self.viewtype) 94 | out.setflags(write=False) 95 | 96 | self.anchor += 1 97 | return out 98 | 99 | def __repr__(self): 100 | return "<%s: window_length=%d, anchor=%d, max_anchor=%d, dtype=%r>" % ( 101 | type(self).__name__, 102 | self.window_length, 103 | self.anchor, 104 | self.max_anchor, 105 | self.viewtype, 106 | ) 107 | -------------------------------------------------------------------------------- /zipline/lib/normalize.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def naive_grouped_rowwise_apply(data, group_labels, func, out=None): 5 | """ 6 | Simple implementation of grouped row-wise function application. 7 | 8 | Parameters 9 | ---------- 10 | data : ndarray[ndim=2] 11 | Input array over which to apply a grouped function. 12 | group_labels : ndarray[ndim=2, dtype=int64] 13 | Labels to use to bucket inputs from array. 14 | Should be the same shape as array. 15 | func : function[ndarray[ndim=1]] -> function[ndarray[ndim=1]] 16 | Function to apply to pieces of each row in array. 17 | out : ndarray, optional 18 | Array into which to write output. If not supplied, a new array of the 19 | same shape as ``data`` is allocated and returned. 20 | 21 | Example 22 | ------- 23 | >>> data = np.array([[1., 2., 3.], 24 | ... [2., 3., 4.], 25 | ... [5., 6., 7.]]) 26 | >>> labels = np.array([[0, 0, 1], 27 | ... [0, 1, 0], 28 | ... [1, 0, 2]]) 29 | >>> naive_grouped_rowwise_apply(data, labels, lambda row: row - row.min()) 30 | array([[ 0., 1., 0.], 31 | [ 0., 0., 2.], 32 | [ 0., 0., 0.]]) 33 | >>> naive_grouped_rowwise_apply(data, labels, lambda row: row / row.sum()) 34 | array([[ 0.33333333, 0.66666667, 1. ], 35 | [ 0.33333333, 1. , 0.66666667], 36 | [ 1. , 1. , 1. ]]) 37 | """ 38 | if out is None: 39 | out = np.empty_like(data) 40 | 41 | for (row, label_row, out_row) in zip(data, group_labels, out): 42 | for label in np.unique(label_row): 43 | locs = (label_row == label) 44 | out_row[locs] = func(row[locs]) 45 | return out 46 | -------------------------------------------------------------------------------- /zipline/lib/quantiles.py: -------------------------------------------------------------------------------- 1 | """ 2 | Algorithms for computing quantiles on numpy arrays. 3 | """ 4 | from numpy.lib import apply_along_axis 5 | from pandas import qcut 6 | 7 | 8 | def quantiles(data, nbins_or_partition_bounds): 9 | """ 10 | Compute rowwise array quantiles on an input. 11 | """ 12 | return apply_along_axis( 13 | qcut, 14 | 1, 15 | data, 16 | q=nbins_or_partition_bounds, labels=False, 17 | ) 18 | -------------------------------------------------------------------------------- /zipline/lib/rank.pyx: -------------------------------------------------------------------------------- 1 | """ 2 | Functions for ranking and sorting. 3 | """ 4 | cimport cython 5 | from cpython cimport bool 6 | from numpy cimport ( 7 | float64_t, 8 | import_array, 9 | intp_t, 10 | ndarray, 11 | NPY_DOUBLE, 12 | NPY_MERGESORT, 13 | PyArray_ArgSort, 14 | PyArray_DIMS, 15 | PyArray_EMPTY, 16 | ) 17 | from numpy import apply_along_axis, float64, isnan, nan 18 | from scipy.stats import rankdata 19 | 20 | from zipline.utils.numpy_utils import ( 21 | is_float, 22 | float64_dtype, 23 | int64_dtype, 24 | datetime64ns_dtype, 25 | ) 26 | 27 | 28 | import_array() 29 | 30 | 31 | cpdef ismissing(ndarray data, object missing_value): 32 | """ 33 | Generic ismissing function that handles quirks with NaN. 34 | """ 35 | if is_float(data) and isnan(missing_value): 36 | return isnan(data) 37 | return (data == missing_value) 38 | 39 | 40 | def masked_rankdata_2d(ndarray data, 41 | ndarray mask, 42 | object missing_value, 43 | str method, 44 | bool ascending): 45 | """ 46 | Compute masked rankdata on data on float64, int64, or datetime64 data. 47 | """ 48 | cdef str dtype_name = data.dtype.name 49 | if dtype_name not in ('float64', 'int64', 'datetime64[ns]'): 50 | raise TypeError( 51 | "Can't compute rankdata on array of dtype %r." % dtype_name 52 | ) 53 | 54 | cdef ndarray missing_locations = (~mask | ismissing(data, missing_value)) 55 | 56 | # Interpret the bytes of integral data as floats for sorting. 57 | data = data.copy().view(float64) 58 | data[missing_locations] = nan 59 | if not ascending: 60 | data = -data 61 | 62 | # OPTIMIZATION: Fast path the default case with our own specialized 63 | # Cython implementation. 64 | if method == 'ordinal': 65 | result = rankdata_2d_ordinal(data) 66 | else: 67 | # FUTURE OPTIMIZATION: 68 | # Write a less general "apply to rows" method that doesn't do all 69 | # the extra work that apply_along_axis does. 70 | result = apply_along_axis(rankdata, 1, data, method=method) 71 | 72 | # On SciPy >= 0.17, rankdata returns integers for any method except 73 | # average. 74 | if result.dtype.name != 'float64': 75 | result = result.astype('float64') 76 | 77 | # rankdata will sort missing values into last place, but we want our nans 78 | # to propagate, so explicitly re-apply. 79 | result[missing_locations] = nan 80 | return result 81 | 82 | 83 | @cython.boundscheck(False) 84 | @cython.wraparound(False) 85 | @cython.embedsignature(True) 86 | cpdef rankdata_2d_ordinal(ndarray[float64_t, ndim=2] array): 87 | """ 88 | Equivalent to: 89 | 90 | numpy.apply_over_axis(scipy.stats.rankdata, 1, array, method='ordinal') 91 | """ 92 | cdef: 93 | int nrows, ncols 94 | ndarray[intp_t, ndim=2] sort_idxs 95 | ndarray[float64_t, ndim=2] out 96 | 97 | nrows = array.shape[0] 98 | ncols = array.shape[1] 99 | 100 | # scipy.stats.rankdata explicitly uses MERGESORT instead of QUICKSORT for 101 | # the ordinal branch. c.f. commit ab21d2fee2d27daca0b2c161bbb7dba7e73e70ba 102 | sort_idxs = PyArray_ArgSort(array, 1, NPY_MERGESORT) 103 | 104 | # Roughly, "out = np.empty_like(array)" 105 | out = PyArray_EMPTY(2, PyArray_DIMS(array), NPY_DOUBLE, False) 106 | 107 | cdef intp_t i, j 108 | for i in range(nrows): 109 | for j in range(ncols): 110 | out[i, sort_idxs[i, j]] = j + 1.0 111 | 112 | return out 113 | -------------------------------------------------------------------------------- /zipline/pipeline/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from zipline.assets import AssetFinder 3 | 4 | from .classifiers import Classifier, CustomClassifier 5 | from .engine import SimplePipelineEngine 6 | from .factors import Factor, CustomFactor 7 | from .filters import Filter, CustomFilter 8 | from .term import Term 9 | from .graph import TermGraph 10 | from .pipeline import Pipeline 11 | from .loaders import USEquityPricingLoader 12 | 13 | 14 | def engine_from_files(daily_bar_path, 15 | adjustments_path, 16 | asset_db_path, 17 | calendar, 18 | warmup_assets=False): 19 | """ 20 | Construct a SimplePipelineEngine from local filesystem resources. 21 | 22 | Parameters 23 | ---------- 24 | daily_bar_path : str 25 | Path to pass to `BcolzDailyBarReader`. 26 | adjustments_path : str 27 | Path to pass to SQLiteAdjustmentReader. 28 | asset_db_path : str 29 | Path to pass to `AssetFinder`. 30 | calendar : pd.DatetimeIndex 31 | Calendar to use for the loader. 32 | warmup_assets : bool, optional 33 | Whether or not to populate AssetFinder caches. This can speed up 34 | initial latency on subsequent pipeline runs, at the cost of extra 35 | memory consumption. Default is False 36 | """ 37 | loader = USEquityPricingLoader.from_files(daily_bar_path, adjustments_path) 38 | 39 | if not asset_db_path.startswith("sqlite:"): 40 | asset_db_path = "sqlite:///" + asset_db_path 41 | asset_finder = AssetFinder(asset_db_path) 42 | if warmup_assets: 43 | results = asset_finder.retrieve_all(asset_finder.sids) 44 | print("Warmed up %d assets." % len(results)) 45 | 46 | return SimplePipelineEngine( 47 | lambda _: loader, 48 | calendar, 49 | asset_finder, 50 | ) 51 | 52 | 53 | __all__ = ( 54 | 'Classifier', 55 | 'CustomFactor', 56 | 'CustomFilter', 57 | 'CustomClassifier', 58 | 'engine_from_files', 59 | 'Factor', 60 | 'Filter', 61 | 'Pipeline', 62 | 'SimplePipelineEngine', 63 | 'Term', 64 | 'TermGraph', 65 | ) 66 | -------------------------------------------------------------------------------- /zipline/pipeline/classifiers/__init__.py: -------------------------------------------------------------------------------- 1 | from .classifier import ( 2 | Classifier, 3 | CustomClassifier, 4 | Quantiles, 5 | Everything, 6 | Latest, 7 | ) 8 | 9 | __all__ = [ 10 | 'Classifier', 11 | 'CustomClassifier', 12 | 'Everything', 13 | 'Latest', 14 | 'Quantiles', 15 | ] 16 | -------------------------------------------------------------------------------- /zipline/pipeline/common.py: -------------------------------------------------------------------------------- 1 | """ 2 | Common constants for Pipeline. 3 | """ 4 | AD_FIELD_NAME = 'asof_date' 5 | ANNOUNCEMENT_FIELD_NAME = 'announcement_date' 6 | CASH_FIELD_NAME = 'cash' 7 | CASH_AMOUNT_FIELD_NAME = 'cash_amount' 8 | BUYBACK_ANNOUNCEMENT_FIELD_NAME = 'buyback_date' 9 | DAYS_SINCE_PREV = 'days_since_prev' 10 | DAYS_SINCE_PREV_DIVIDEND_ANNOUNCEMENT = 'days_since_prev_dividend_announcement' 11 | DAYS_SINCE_PREV_EX_DATE = 'days_since_prev_ex_date' 12 | DAYS_TO_NEXT = 'days_to_next' 13 | DAYS_TO_NEXT_EX_DATE = 'days_to_next_ex_date' 14 | EX_DATE_FIELD_NAME = 'ex_date' 15 | NEXT_AMOUNT = 'next_amount' 16 | NEXT_ANNOUNCEMENT = 'next_announcement' 17 | NEXT_EX_DATE = 'next_ex_date' 18 | NEXT_PAY_DATE = 'next_pay_date' 19 | PAY_DATE_FIELD_NAME = 'pay_date' 20 | PREVIOUS_AMOUNT = 'previous_amount' 21 | PREVIOUS_ANNOUNCEMENT = 'previous_announcement' 22 | PREVIOUS_BUYBACK_ANNOUNCEMENT = 'previous_buyback_announcement' 23 | PREVIOUS_BUYBACK_CASH = 'previous_buyback_cash' 24 | PREVIOUS_BUYBACK_SHARE_COUNT = 'previous_buyback_share_count' 25 | PREVIOUS_EX_DATE = 'previous_ex_date' 26 | PREVIOUS_PAY_DATE = 'previous_pay_date' 27 | SHARE_COUNT_FIELD_NAME = 'share_count' 28 | SID_FIELD_NAME = 'sid' 29 | TS_FIELD_NAME = 'timestamp' 30 | -------------------------------------------------------------------------------- /zipline/pipeline/data/__init__.py: -------------------------------------------------------------------------------- 1 | from .buyback_auth import CashBuybackAuthorizations, ShareBuybackAuthorizations 2 | from .dividends import ( 3 | DividendsByAnnouncementDate, 4 | DividendsByExDate, 5 | DividendsByPayDate, 6 | ) 7 | from .earnings import EarningsCalendar 8 | from .equity_pricing import USEquityPricing 9 | from .dataset import DataSet, Column, BoundColumn 10 | 11 | __all__ = [ 12 | 'BoundColumn', 13 | 'CashBuybackAuthorizations', 14 | 'Column', 15 | 'DataSet', 16 | 'DividendsByAnnouncementDate', 17 | 'DividendsByExDate', 18 | 'DividendsByPayDate', 19 | 'EarningsCalendar', 20 | 'ShareBuybackAuthorizations', 21 | 'USEquityPricing', 22 | ] 23 | -------------------------------------------------------------------------------- /zipline/pipeline/data/buyback_auth.py: -------------------------------------------------------------------------------- 1 | """ 2 | Datasets representing dates of recently announced buyback authorizations. 3 | """ 4 | from zipline.utils.numpy_utils import datetime64ns_dtype, float64_dtype 5 | 6 | from .dataset import Column, DataSet 7 | 8 | 9 | class CashBuybackAuthorizations(DataSet): 10 | """ 11 | Dataset representing dates of recently announced cash buyback 12 | authorizations. 13 | """ 14 | cash_amount = Column(float64_dtype) 15 | announcement_date = Column(datetime64ns_dtype) 16 | 17 | 18 | class ShareBuybackAuthorizations(DataSet): 19 | """ 20 | Dataset representing dates of recently announced share buyback 21 | authorizations. 22 | """ 23 | share_count = Column(float64_dtype) 24 | announcement_date = Column(datetime64ns_dtype) 25 | -------------------------------------------------------------------------------- /zipline/pipeline/data/dividends.py: -------------------------------------------------------------------------------- 1 | """ 2 | Dataset representing dates of upcoming dividends. 3 | """ 4 | from zipline.utils.numpy_utils import datetime64ns_dtype, float64_dtype 5 | 6 | from .dataset import Column, DataSet 7 | 8 | 9 | class DividendsByExDate(DataSet): 10 | next_date = Column(datetime64ns_dtype) 11 | previous_date = Column(datetime64ns_dtype) 12 | next_amount = Column(float64_dtype) 13 | previous_amount = Column(float64_dtype) 14 | 15 | 16 | class DividendsByPayDate(DataSet): 17 | next_date = Column(datetime64ns_dtype) 18 | previous_date = Column(datetime64ns_dtype) 19 | next_amount = Column(float64_dtype) 20 | previous_amount = Column(float64_dtype) 21 | 22 | 23 | class DividendsByAnnouncementDate(DataSet): 24 | previous_announcement_date = Column(datetime64ns_dtype) 25 | previous_amount = Column(float64_dtype) 26 | -------------------------------------------------------------------------------- /zipline/pipeline/data/earnings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Dataset representing dates of upcoming earnings. 3 | """ 4 | from zipline.utils.numpy_utils import datetime64ns_dtype 5 | 6 | from .dataset import Column, DataSet 7 | 8 | 9 | class EarningsCalendar(DataSet): 10 | """ 11 | Dataset representing dates of upcoming or recently announced earnings. 12 | """ 13 | next_announcement = Column(datetime64ns_dtype) 14 | previous_announcement = Column(datetime64ns_dtype) 15 | 16 | # TODO: Provide categorical columns for when during the day the 17 | # announcement occurred. 18 | -------------------------------------------------------------------------------- /zipline/pipeline/data/equity_pricing.py: -------------------------------------------------------------------------------- 1 | """ 2 | Dataset representing OHLCV data. 3 | """ 4 | from zipline.utils.numpy_utils import float64_dtype 5 | 6 | from .dataset import Column, DataSet 7 | 8 | 9 | class USEquityPricing(DataSet): 10 | """ 11 | Dataset representing daily trading prices and volumes. 12 | """ 13 | open = Column(float64_dtype) 14 | high = Column(float64_dtype) 15 | low = Column(float64_dtype) 16 | close = Column(float64_dtype) 17 | volume = Column(float64_dtype) 18 | -------------------------------------------------------------------------------- /zipline/pipeline/data/testing.py: -------------------------------------------------------------------------------- 1 | """ 2 | Datasets for testing use. 3 | 4 | Loaders for datasets in this file can be found in 5 | zipline.pipeline.data.testing. 6 | """ 7 | from .dataset import Column, DataSet 8 | from zipline.utils.numpy_utils import ( 9 | bool_dtype, 10 | float64_dtype, 11 | datetime64ns_dtype, 12 | int64_dtype, 13 | ) 14 | 15 | 16 | class TestingDataSet(DataSet): 17 | # Tell nose this isn't a test case. 18 | __test__ = False 19 | 20 | bool_col = Column(dtype=bool_dtype, missing_value=False) 21 | bool_col_default_True = Column(dtype=bool_dtype, missing_value=True) 22 | float_col = Column(dtype=float64_dtype) 23 | datetime_col = Column(dtype=datetime64ns_dtype) 24 | int_col = Column(dtype=int64_dtype, missing_value=0) 25 | -------------------------------------------------------------------------------- /zipline/pipeline/factors/__init__.py: -------------------------------------------------------------------------------- 1 | from .factor import ( 2 | CustomFactor, 3 | Factor, 4 | Latest 5 | ) 6 | from .events import ( 7 | BusinessDaysSinceCashBuybackAuth, 8 | BusinessDaysSinceDividendAnnouncement, 9 | BusinessDaysUntilNextExDate, 10 | BusinessDaysSincePreviousExDate, 11 | BusinessDaysUntilNextEarnings, 12 | BusinessDaysSincePreviousEarnings, 13 | BusinessDaysSinceShareBuybackAuth, 14 | ) 15 | from .technical import ( 16 | AverageDollarVolume, 17 | EWMA, 18 | EWMSTD, 19 | ExponentialWeightedMovingAverage, 20 | ExponentialWeightedMovingStdDev, 21 | MaxDrawdown, 22 | RSI, 23 | Returns, 24 | SimpleMovingAverage, 25 | VWAP, 26 | WeightedAverageValue, 27 | ) 28 | 29 | __all__ = [ 30 | 'BusinessDaysSinceCashBuybackAuth', 31 | 'BusinessDaysSinceDividendAnnouncement', 32 | 'BusinessDaysUntilNextExDate', 33 | 'BusinessDaysSincePreviousExDate', 34 | 'BusinessDaysUntilNextEarnings', 35 | 'BusinessDaysSincePreviousEarnings', 36 | 'BusinessDaysSinceShareBuybackAuth', 37 | 'CustomFactor', 38 | 'AverageDollarVolume', 39 | 'EWMA', 40 | 'EWMSTD', 41 | 'ExponentialWeightedMovingAverage', 42 | 'ExponentialWeightedMovingStdDev', 43 | 'Factor', 44 | 'Latest', 45 | 'MaxDrawdown', 46 | 'RSI', 47 | 'Returns', 48 | 'SimpleMovingAverage', 49 | 'VWAP', 50 | 'WeightedAverageValue', 51 | ] 52 | -------------------------------------------------------------------------------- /zipline/pipeline/filters/__init__.py: -------------------------------------------------------------------------------- 1 | from .filter import ( 2 | CustomFilter, 3 | Filter, 4 | Latest, 5 | NullFilter, 6 | NumExprFilter, 7 | PercentileFilter, 8 | ) 9 | 10 | __all__ = [ 11 | 'CustomFilter', 12 | 'Filter', 13 | 'Latest', 14 | 'NullFilter', 15 | 'NumExprFilter', 16 | 'PercentileFilter', 17 | ] 18 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/__init__.py: -------------------------------------------------------------------------------- 1 | from .earnings import EarningsCalendarLoader 2 | from .buyback_auth import ( 3 | CashBuybackAuthorizationsLoader, 4 | ShareBuybackAuthorizationsLoader 5 | ) 6 | from .dividends import ( 7 | DividendsByAnnouncementDateLoader, 8 | DividendsByExDateLoader, 9 | DividendsByPayDateLoader, 10 | ) 11 | from .equity_pricing_loader import USEquityPricingLoader 12 | 13 | __all__ = [ 14 | 'CashBuybackAuthorizationsLoader', 15 | 'DividendsByAnnouncementDateLoader', 16 | 'DividendsByExDateLoader', 17 | 'DividendsByPayDateLoader', 18 | 'EarningsCalendarLoader', 19 | 'ShareBuybackAuthorizationsLoader', 20 | 'USEquityPricingLoader', 21 | ] 22 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/base.py: -------------------------------------------------------------------------------- 1 | """ 2 | Base class for Pipeline API data loaders. 3 | """ 4 | from abc import ( 5 | ABCMeta, 6 | abstractmethod, 7 | ) 8 | 9 | 10 | from six import with_metaclass 11 | 12 | 13 | class PipelineLoader(with_metaclass(ABCMeta)): 14 | """ 15 | ABC for classes that can load data for use with zipline.pipeline APIs. 16 | 17 | TODO: DOCUMENT THIS MORE! 18 | """ 19 | @abstractmethod 20 | def load_adjusted_array(self, columns, dates, assets, mask): 21 | pass 22 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/blaze/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from .buyback_auth import ( 3 | BlazeCashBuybackAuthorizationsLoader, 4 | BlazeShareBuybackAuthorizationsLoader 5 | ) 6 | from .core import ( 7 | BlazeLoader, 8 | NoDeltasWarning, 9 | from_blaze, 10 | global_loader, 11 | ) 12 | from .dividends import ( 13 | BlazeDividendsByAnnouncementDateLoader, 14 | BlazeDividendsByExDateLoader, 15 | BlazeDividendsByPayDateLoader 16 | ) 17 | from .earnings import ( 18 | BlazeEarningsCalendarLoader, 19 | ) 20 | 21 | __all__ = ( 22 | 'BlazeCashBuybackAuthorizationsLoader', 23 | 'BlazeDividendsByAnnouncementDateLoader', 24 | 'BlazeDividendsByExDateLoader', 25 | 'BlazeDividendsByPayDateLoader', 26 | 'BlazeEarningsCalendarLoader', 27 | 'BlazeLoader', 28 | 'BlazeShareBuybackAuthorizationsLoader', 29 | 'from_blaze', 30 | 'global_loader', 31 | 'NoDeltasWarning', 32 | ) 33 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/blaze/earnings.py: -------------------------------------------------------------------------------- 1 | from zipline.pipeline.common import ( 2 | ANNOUNCEMENT_FIELD_NAME, 3 | SID_FIELD_NAME, 4 | TS_FIELD_NAME, 5 | ) 6 | from zipline.pipeline.data import EarningsCalendar 7 | from zipline.pipeline.loaders import EarningsCalendarLoader 8 | from .events import BlazeEventsLoader 9 | 10 | 11 | class BlazeEarningsCalendarLoader(BlazeEventsLoader): 12 | """A pipeline loader for the ``EarningsCalendar`` dataset that loads 13 | data from a blaze expression. 14 | 15 | Parameters 16 | ---------- 17 | expr : Expr 18 | The expression representing the data to load. 19 | resources : dict, optional 20 | Mapping from the loadable terms of ``expr`` to actual data resources. 21 | odo_kwargs : dict, optional 22 | Extra keyword arguments to pass to odo when executing the expression. 23 | data_query_time : time, optional 24 | The time to use for the data query cutoff. 25 | data_query_tz : tzinfo or str 26 | The timezeone to use for the data query cutoff. 27 | dataset: DataSet 28 | The DataSet object for which this loader loads data. 29 | 30 | Notes 31 | ----- 32 | The expression should have a tabular dshape of:: 33 | 34 | Dim * {{ 35 | {SID_FIELD_NAME}: int64, 36 | {TS_FIELD_NAME}: datetime, 37 | {ANNOUNCEMENT_FIELD_NAME}: ?datetime, 38 | }} 39 | 40 | Where each row of the table is a record including the sid to identify the 41 | company, the timestamp where we learned about the announcement, and the 42 | date when the earnings will be announced. 43 | 44 | If the '{TS_FIELD_NAME}' field is not included it is assumed that we 45 | start the backtest with knowledge of all announcements. 46 | """ 47 | 48 | __doc__ = __doc__.format( 49 | TS_FIELD_NAME=TS_FIELD_NAME, 50 | SID_FIELD_NAME=SID_FIELD_NAME, 51 | ANNOUNCEMENT_FIELD_NAME=ANNOUNCEMENT_FIELD_NAME, 52 | ) 53 | 54 | _expected_fields = frozenset({ 55 | TS_FIELD_NAME, 56 | SID_FIELD_NAME, 57 | ANNOUNCEMENT_FIELD_NAME, 58 | }) 59 | 60 | concrete_loader = EarningsCalendarLoader 61 | default_dataset = EarningsCalendar 62 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/buyback_auth.py: -------------------------------------------------------------------------------- 1 | """ 2 | Reference implementation for buyback auth loaders. 3 | """ 4 | 5 | from ..data import ( 6 | CashBuybackAuthorizations, 7 | ShareBuybackAuthorizations 8 | ) 9 | from .events import EventsLoader 10 | from zipline.pipeline.common import ( 11 | BUYBACK_ANNOUNCEMENT_FIELD_NAME, 12 | CASH_FIELD_NAME, 13 | SHARE_COUNT_FIELD_NAME 14 | ) 15 | from zipline.utils.memoize import lazyval 16 | 17 | 18 | class CashBuybackAuthorizationsLoader(EventsLoader): 19 | """ 20 | Reference loader for 21 | :class:`zipline.pipeline.data.CashBuybackAuthorizations`. 22 | 23 | events_by_sid: dict[sid -> pd.DataFrame(knowledge date, 24 | event date, cash value)] 25 | 26 | """ 27 | expected_cols = frozenset([BUYBACK_ANNOUNCEMENT_FIELD_NAME, 28 | CASH_FIELD_NAME]) 29 | 30 | def __init__(self, 31 | all_dates, 32 | events_by_sid, 33 | infer_timestamps=False, 34 | dataset=CashBuybackAuthorizations): 35 | super(CashBuybackAuthorizationsLoader, self).__init__( 36 | all_dates, 37 | events_by_sid, 38 | infer_timestamps=infer_timestamps, 39 | dataset=dataset, 40 | ) 41 | 42 | @lazyval 43 | def cash_amount_loader(self): 44 | return self._previous_event_value_loader( 45 | self.dataset.cash_amount, 46 | BUYBACK_ANNOUNCEMENT_FIELD_NAME, 47 | CASH_FIELD_NAME 48 | ) 49 | 50 | @lazyval 51 | def announcement_date_loader(self): 52 | return self._previous_event_date_loader( 53 | self.dataset.announcement_date, 54 | BUYBACK_ANNOUNCEMENT_FIELD_NAME, 55 | ) 56 | 57 | 58 | class ShareBuybackAuthorizationsLoader(EventsLoader): 59 | """ 60 | Reference loader for 61 | :class:`zipline.pipeline.data.ShareBuybackAuthorizations`. 62 | 63 | Does not currently support adjustments to the dates of known buyback 64 | authorizations. 65 | 66 | events_by_sid: dict[sid -> pd.DataFrame(knowledge date, 67 | event date, share value)] 68 | 69 | """ 70 | expected_cols = frozenset([BUYBACK_ANNOUNCEMENT_FIELD_NAME, 71 | SHARE_COUNT_FIELD_NAME]) 72 | 73 | def __init__(self, 74 | all_dates, 75 | events_by_sid, 76 | infer_timestamps=False, 77 | dataset=ShareBuybackAuthorizations): 78 | super(ShareBuybackAuthorizationsLoader, self).__init__( 79 | all_dates, 80 | events_by_sid, 81 | infer_timestamps=infer_timestamps, 82 | dataset=dataset, 83 | ) 84 | 85 | @lazyval 86 | def share_count_loader(self): 87 | return self._previous_event_value_loader( 88 | self.dataset.share_count, 89 | BUYBACK_ANNOUNCEMENT_FIELD_NAME, 90 | SHARE_COUNT_FIELD_NAME 91 | ) 92 | 93 | @lazyval 94 | def announcement_date_loader(self): 95 | return self._previous_event_date_loader( 96 | self.dataset.announcement_date, 97 | BUYBACK_ANNOUNCEMENT_FIELD_NAME, 98 | ) 99 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/earnings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Reference implementation for EarningsCalendar loaders. 3 | """ 4 | 5 | from ..data import EarningsCalendar 6 | from .events import EventsLoader 7 | from zipline.pipeline.common import ANNOUNCEMENT_FIELD_NAME 8 | from zipline.utils.memoize import lazyval 9 | 10 | 11 | class EarningsCalendarLoader(EventsLoader): 12 | 13 | expected_cols = frozenset([ANNOUNCEMENT_FIELD_NAME]) 14 | 15 | def __init__(self, all_dates, events_by_sid, 16 | infer_timestamps=False, 17 | dataset=EarningsCalendar): 18 | super(EarningsCalendarLoader, self).__init__( 19 | all_dates, events_by_sid, infer_timestamps, dataset=dataset, 20 | ) 21 | 22 | @lazyval 23 | def next_announcement_loader(self): 24 | return self._next_event_date_loader(self.dataset.next_announcement, 25 | ANNOUNCEMENT_FIELD_NAME) 26 | 27 | @lazyval 28 | def previous_announcement_loader(self): 29 | return self._previous_event_date_loader( 30 | self.dataset.previous_announcement, 31 | ANNOUNCEMENT_FIELD_NAME 32 | ) 33 | -------------------------------------------------------------------------------- /zipline/pipeline/loaders/testing.py: -------------------------------------------------------------------------------- 1 | """ 2 | Loaders for zipline.pipeline.data.testing datasets. 3 | """ 4 | from .synthetic import EyeLoader, SeededRandomLoader 5 | from ..data.testing import TestingDataSet 6 | 7 | 8 | def make_eye_loader(dates, sids): 9 | """ 10 | Make a PipelineLoader that emits np.eye arrays for the columns in 11 | ``TestingDataSet``. 12 | """ 13 | return EyeLoader(TestingDataSet.columns, dates, sids) 14 | 15 | 16 | def make_seeded_random_loader(seed, dates, sids): 17 | """ 18 | Make a PipelineLoader that emits random arrays seeded with `seed` for the 19 | columns in ``TestingDataSet``. 20 | """ 21 | return SeededRandomLoader(seed, TestingDataSet.columns, dates, sids) 22 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20120913/add: -------------------------------------------------------------------------------- 1 | ROSA 2 | SFSA 3 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20120913/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20120913/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20120919/add: -------------------------------------------------------------------------------- 1 | RTSA 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20120919/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20120919/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20121012/add: -------------------------------------------------------------------------------- 1 | VZZB 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20121012/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20121012/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20130605/add: -------------------------------------------------------------------------------- 1 | LSKY 2 | SSDL 3 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20130605/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20130605/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20130916/add: -------------------------------------------------------------------------------- 1 | MFSA 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20130916/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20130916/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131002/add: -------------------------------------------------------------------------------- 1 | DSTJ 2 | DSXJ 3 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131002/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20131002/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131009/add: -------------------------------------------------------------------------------- 1 | JFT 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131009/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20131009/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131121/add: -------------------------------------------------------------------------------- 1 | FSA 2 | FSE 3 | FSG 4 | FSU 5 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131121/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20131121/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131227/add: -------------------------------------------------------------------------------- 1 | IPLT 2 | LPLT 3 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20131227/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20131227/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20140410/add: -------------------------------------------------------------------------------- 1 | BXDB 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20140410/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20140410/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20140923/add: -------------------------------------------------------------------------------- 1 | BRZS 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20140923/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20140923/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20141119/add: -------------------------------------------------------------------------------- 1 | BXUB 2 | BXUC 3 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20141119/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20141119/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20141226/add: -------------------------------------------------------------------------------- 1 | BARS 2 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20141226/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20141226/delete -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20150123/add: -------------------------------------------------------------------------------- 1 | AGA 2 | AGQ 3 | BAR 4 | BDCL 5 | BDD 6 | BIB 7 | BIS 8 | BOIL 9 | BOM 10 | BRZU 11 | BUNT 12 | BZQ 13 | CEFL 14 | CMD 15 | CROC 16 | CSMB 17 | CURE 18 | DAG 19 | DDM 20 | DEE 21 | DGAZ 22 | DGLD 23 | DGP 24 | DGZ 25 | DIG 26 | DPK 27 | DRN 28 | DRR 29 | DRV 30 | DSLV 31 | DTO 32 | DUG 33 | DUST 34 | DVHL 35 | DVYL 36 | DWTI 37 | DXD 38 | DYY 39 | DZK 40 | DZZ 41 | EDC 42 | EDZ 43 | EET 44 | EEV 45 | EFO 46 | EFU 47 | EMLB 48 | EMSA 49 | EPV 50 | ERX 51 | ERY 52 | EUO 53 | EURL 54 | EWV 55 | EZJ 56 | FAS 57 | FAZ 58 | FBG 59 | FBGX 60 | FEEU 61 | FIBG 62 | FIEG 63 | FIEU 64 | FIGY 65 | FINU 66 | FINZ 67 | FLGE 68 | FXP 69 | GASL 70 | GDAY 71 | GLL 72 | HDLV 73 | IGU 74 | INDL 75 | ITLT 76 | JDST 77 | JGBD 78 | JGBT 79 | JNUG 80 | JPNL 81 | JPX 82 | KOLD 83 | KORU 84 | KRU 85 | LBJ 86 | LBND 87 | LLDM 88 | LLSC 89 | LLSP 90 | LMLP 91 | LTL 92 | MATL 93 | MDLL 94 | MFLA 95 | MIDU 96 | MIDZ 97 | MLPL 98 | MORL 99 | MVV 100 | MZZ 101 | NUGT 102 | PST 103 | QID 104 | QLD 105 | RETL 106 | REW 107 | RGRA 108 | RGRC 109 | RGRE 110 | RGRI 111 | RGRP 112 | ROLA 113 | ROM 114 | RTLA 115 | RUSL 116 | RUSS 117 | RWXL 118 | RXD 119 | RXL 120 | SAA 121 | SBND 122 | SCC 123 | SCO 124 | SDD 125 | SDOW 126 | SDP 127 | SDS 128 | SDYL 129 | SFLA 130 | SIJ 131 | SKF 132 | SMDD 133 | SMK 134 | SMLL 135 | SMN 136 | SOXL 137 | SOXS 138 | SPLX 139 | SPUU 140 | SPXL 141 | SPXS 142 | SPXU 143 | SQQQ 144 | SRS 145 | SRTY 146 | SSG 147 | SSO 148 | SYTL 149 | SZK 150 | TBAR 151 | TBT 152 | TBZ 153 | TECL 154 | TECS 155 | TLL 156 | TMF 157 | TMV 158 | TNA 159 | TPS 160 | TQQQ 161 | TTT 162 | TVIX 163 | TVIZ 164 | TWM 165 | TYD 166 | TYO 167 | TZA 168 | UBR 169 | UBT 170 | UCC 171 | UCD 172 | UCI 173 | UCO 174 | UDNT 175 | UDOW 176 | UGAZ 177 | UGE 178 | UGL 179 | UGLD 180 | UJB 181 | ULE 182 | UMDD 183 | UMX 184 | UPRO 185 | UPV 186 | UPW 187 | URE 188 | URR 189 | URTY 190 | USD 191 | USLV 192 | UST 193 | USV 194 | UUPT 195 | UVXY 196 | UWM 197 | UWTI 198 | UXI 199 | UXJ 200 | UYG 201 | UYM 202 | XPP 203 | YANG 204 | YCL 205 | YCS 206 | YINN 207 | ZSL 208 | -------------------------------------------------------------------------------- /zipline/resources/security_lists/leveraged_etf_list/20020103/20150123/delete: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhanghan1990/zipline-chinese/86904cac4b6e928271f640910aa83675ce945b8b/zipline/resources/security_lists/leveraged_etf_list/20020103/20150123/delete -------------------------------------------------------------------------------- /zipline/sources/__init__.py: -------------------------------------------------------------------------------- 1 | from zipline.sources.data_frame_source import DataFrameSource, DataPanelSource 2 | from zipline.sources.test_source import SpecificEquityTrades 3 | from .simulated import RandomWalkSource 4 | __all__ = [ 5 | 'DataFrameSource', 6 | 'DataPanelSource', 7 | 'SpecificEquityTrades', 8 | 'RandomWalkSource' 9 | ] 10 | -------------------------------------------------------------------------------- /zipline/sources/data_source.py: -------------------------------------------------------------------------------- 1 | from abc import ( 2 | ABCMeta, 3 | abstractproperty 4 | ) 5 | 6 | from six import with_metaclass 7 | 8 | from zipline.protocol import DATASOURCE_TYPE 9 | from zipline.protocol import Event 10 | 11 | 12 | class DataSource(with_metaclass(ABCMeta)): 13 | 14 | @property 15 | def event_type(self): 16 | return DATASOURCE_TYPE.TRADE 17 | 18 | @property 19 | def mapping(self): 20 | """ 21 | Mappings of the form: 22 | target_key: (mapping_function, source_key) 23 | """ 24 | return {} 25 | 26 | @abstractproperty 27 | def raw_data(self): 28 | """ 29 | An iterator that yields the raw datasource, 30 | in chronological order of data, one event at a time. 31 | """ 32 | NotImplemented 33 | 34 | @abstractproperty 35 | def instance_hash(self): 36 | """ 37 | A hash that represents the unique args to the source. 38 | """ 39 | pass 40 | 41 | def get_hash(self): 42 | return self.__class__.__name__ + "-" + self.instance_hash 43 | 44 | def apply_mapping(self, raw_row): 45 | """ 46 | Override this to hand craft conversion of row. 47 | """ 48 | row = {} 49 | row.update({'type': self.event_type}) 50 | row.update({target: mapping_func(raw_row[source_key]) 51 | for target, (mapping_func, source_key) 52 | in self.mapping.items()}) 53 | row.update({'source_id': self.get_hash()}) 54 | return row 55 | 56 | @property 57 | def mapped_data(self): 58 | for row in self.raw_data: 59 | yield Event(self.apply_mapping(row)) 60 | 61 | def __iter__(self): 62 | return self 63 | 64 | def next(self): 65 | return self.mapped_data.next() 66 | 67 | def __next__(self): 68 | return next(self.mapped_data) 69 | -------------------------------------------------------------------------------- /zipline/testing/__init__.py: -------------------------------------------------------------------------------- 1 | from .core import ( # noqa 2 | EPOCH, 3 | ExceptionSource, 4 | ExplodingObject, 5 | add_security_data, 6 | all_pairs_matching_predicate, 7 | all_subindices, 8 | assert_single_position, 9 | assert_timestamp_equal, 10 | check_allclose, 11 | check_arrays, 12 | chrange, 13 | drain_zipline, 14 | gen_calendars, 15 | make_commodity_future_info, 16 | make_future_info, 17 | make_jagged_equity_info, 18 | make_rotating_equity_info, 19 | make_simple_equity_info, 20 | make_test_handler, 21 | make_trade_panel_for_asset_info, 22 | parameter_space, 23 | permute_rows, 24 | powerset, 25 | product_upper_triangle, 26 | seconds_to_timestamp, 27 | security_list_copy, 28 | setup_logger, 29 | str_to_seconds, 30 | subtest, 31 | teardown_logger, 32 | temp_pipeline_engine, 33 | tmp_asset_finder, 34 | tmp_assets_db, 35 | to_series, 36 | to_utc, 37 | ) 38 | -------------------------------------------------------------------------------- /zipline/transforms/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2012 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from . batch_transform import BatchTransform, batch_transform 17 | 18 | __all__ = [ 19 | 'BatchTransform', 20 | 'batch_transform' 21 | ] 22 | -------------------------------------------------------------------------------- /zipline/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from .cli import run_pipeline, parse_args, parse_cell_magic 17 | 18 | __all__ = ['run_pipeline', 'parse_args', 'parse_cell_magic'] 19 | -------------------------------------------------------------------------------- /zipline/utils/algo_instance.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | import threading 16 | context = threading.local() 17 | 18 | 19 | def get_algo_instance(): 20 | return getattr(context, 'algorithm', None) 21 | 22 | 23 | def set_algo_instance(algo): 24 | context.algorithm = algo 25 | -------------------------------------------------------------------------------- /zipline/utils/api_support.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2014 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from functools import wraps 17 | 18 | import zipline.api 19 | from zipline.utils.algo_instance import get_algo_instance, set_algo_instance 20 | 21 | 22 | class ZiplineAPI(object): 23 | """ 24 | Context manager for making an algorithm instance available to zipline API 25 | functions within a scoped block. 26 | """ 27 | 28 | def __init__(self, algo_instance): 29 | self.algo_instance = algo_instance 30 | 31 | def __enter__(self): 32 | """ 33 | Set the given algo instance, storing any previously-existing instance. 34 | """ 35 | self.old_algo_instance = get_algo_instance() 36 | set_algo_instance(self.algo_instance) 37 | 38 | def __exit__(self, _type, _value, _tb): 39 | """ 40 | Restore the algo instance stored in __enter__. 41 | """ 42 | set_algo_instance(self.old_algo_instance) 43 | 44 | 45 | def api_method(f): 46 | # Decorator that adds the decorated class method as a callable 47 | # function (wrapped) to zipline.api 48 | @wraps(f) 49 | def wrapped(*args, **kwargs): 50 | # Get the instance and call the method 51 | return getattr(get_algo_instance(), f.__name__)(*args, **kwargs) 52 | # Add functor to zipline.api 53 | setattr(zipline.api, f.__name__, wrapped) 54 | zipline.api.__all__.append(f.__name__) 55 | f.is_api_method = True 56 | return f 57 | 58 | 59 | def require_not_initialized(exception): 60 | """ 61 | Decorator for API methods that should only be called during or before 62 | TradingAlgorithm.initialize. `exception` will be raised if the method is 63 | called after initialize. 64 | 65 | Usage 66 | ----- 67 | @require_not_initialized(SomeException("Don't do that!")) 68 | def method(self): 69 | # Do stuff that should only be allowed during initialize. 70 | """ 71 | def decorator(method): 72 | @wraps(method) 73 | def wrapped_method(self, *args, **kwargs): 74 | if self.initialized: 75 | raise exception 76 | return method(self, *args, **kwargs) 77 | return wrapped_method 78 | return decorator 79 | 80 | 81 | def require_initialized(exception): 82 | """ 83 | Decorator for API methods that should only be called after 84 | TradingAlgorithm.initialize. `exception` will be raised if the method is 85 | called before initialize has completed. 86 | 87 | Usage 88 | ----- 89 | @require_initialized(SomeException("Don't do that!")) 90 | def method(self): 91 | # Do stuff that should only be allowed after initialize. 92 | """ 93 | def decorator(method): 94 | @wraps(method) 95 | def wrapped_method(self, *args, **kwargs): 96 | if not self.initialized: 97 | raise exception 98 | return method(self, *args, **kwargs) 99 | return wrapped_method 100 | return decorator 101 | -------------------------------------------------------------------------------- /zipline/utils/cache.py: -------------------------------------------------------------------------------- 1 | """ 2 | Cached object with an expiration date. 3 | """ 4 | from collections import namedtuple 5 | 6 | 7 | class Expired(Exception): 8 | pass 9 | 10 | 11 | class CachedObject(namedtuple("_CachedObject", "value expires")): 12 | """ 13 | A simple struct for maintaining a cached object with an expiration date. 14 | 15 | Parameters 16 | ---------- 17 | value : object 18 | The object to cache. 19 | expires : datetime-like 20 | Expiration date of `value`. The cache is considered invalid for dates 21 | **strictly greater** than `expires`. 22 | 23 | Methods 24 | ------- 25 | get(self, dt) 26 | Get the cached object. 27 | 28 | Usage 29 | ----- 30 | >>> from pandas import Timestamp, Timedelta 31 | >>> expires = Timestamp('2014', tz='UTC') 32 | >>> obj = CachedObject(1, expires) 33 | >>> obj.unwrap(expires - Timedelta('1 minute')) 34 | 1 35 | >>> obj.unwrap(expires) 36 | 1 37 | >>> obj.unwrap(expires + Timedelta('1 minute')) 38 | Traceback (most recent call last): 39 | ... 40 | Expired: 2014-01-01 00:00:00+00:00 41 | """ 42 | 43 | def unwrap(self, dt): 44 | """ 45 | Get the cached value. 46 | 47 | Returns 48 | ------- 49 | value : object 50 | The cached value. 51 | 52 | Raises 53 | ------ 54 | Expired 55 | Raised when `dt` is greater than self.expires. 56 | """ 57 | if dt > self.expires: 58 | raise Expired(self.expires) 59 | return self.value 60 | -------------------------------------------------------------------------------- /zipline/utils/context_tricks.py: -------------------------------------------------------------------------------- 1 | @object.__new__ 2 | class nop_context(object): 3 | """A nop context manager. 4 | """ 5 | def __enter__(self): 6 | pass 7 | 8 | def __exit__(self, *excinfo): 9 | pass 10 | 11 | 12 | def _nop(*args, **kwargs): 13 | pass 14 | 15 | 16 | class CallbackManager(object): 17 | """Create a context manager from a pre-execution callback and a 18 | post-execution callback. 19 | 20 | Parameters 21 | ---------- 22 | pre : (...) -> any, optional 23 | A pre-execution callback. This will be passed ``*args`` and 24 | ``**kwargs``. 25 | post : (...) -> any, optional 26 | A post-execution callback. This will be passed ``*args`` and 27 | ``**kwargs``. 28 | 29 | Notes 30 | ----- 31 | The enter value of this context manager will be the result of calling 32 | ``pre(*args, **kwargs)`` 33 | 34 | Examples 35 | -------- 36 | >>> def pre(where): 37 | ... print('entering %s block' % where) 38 | >>> def post(where): 39 | ... print('exiting %s block' % where) 40 | >>> manager = CallbackManager(pre, post) 41 | >>> with manager('example'): 42 | ... print('inside example block') 43 | entering example block 44 | inside example 45 | exiting example block 46 | 47 | These are reusable with different args: 48 | >>> with manager('another'): 49 | ... print('inside another block') 50 | entering another block 51 | inside another block 52 | exiting another block 53 | """ 54 | def __init__(self, pre=None, post=None): 55 | self.pre = pre if pre is not None else _nop 56 | self.post = post if post is not None else _nop 57 | 58 | def __call__(self, *args, **kwargs): 59 | return _ManagedCallbackContext(self.pre, self.post, args, kwargs) 60 | 61 | 62 | class _ManagedCallbackContext(object): 63 | def __init__(self, pre, post, args, kwargs): 64 | self._pre = pre 65 | self._post = post 66 | self._args = args 67 | self._kwargs = kwargs 68 | 69 | def __enter__(self): 70 | return self._pre(*self._args, **self._kwargs) 71 | 72 | def __exit__(self, *excinfo): 73 | self._post(*self._args, **self._kwargs) 74 | -------------------------------------------------------------------------------- /zipline/utils/control_flow.py: -------------------------------------------------------------------------------- 1 | """ 2 | Control flow utilities. 3 | """ 4 | from six import iteritems 5 | 6 | 7 | class nullctx(object): 8 | """ 9 | Null context manager. Useful for conditionally adding a contextmanager in 10 | a single line, e.g.: 11 | 12 | with SomeContextManager() if some_expr else nullctx(): 13 | do_stuff() 14 | """ 15 | def __enter__(self): 16 | return self 17 | 18 | def __exit__(*args): 19 | return False 20 | 21 | 22 | def invert(d): 23 | """ 24 | Invert a dictionary into a dictionary of sets. 25 | 26 | >>> invert({'a': 1, 'b': 2, 'c': 1}) 27 | {1: {'a', 'c'}, 2: {'b'}} 28 | """ 29 | out = {} 30 | for k, v in iteritems(d): 31 | try: 32 | out[v].add(k) 33 | except KeyError: 34 | out[v] = {k} 35 | return out 36 | -------------------------------------------------------------------------------- /zipline/utils/deprecate.py: -------------------------------------------------------------------------------- 1 | """Utilities for marking deprecated functions.""" 2 | # Copyright 2016 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import warnings 17 | from functools import wraps 18 | 19 | 20 | def deprecated(msg=None, stacklevel=2): 21 | """ 22 | Used to mark a function as deprecated. 23 | 24 | Parameters 25 | ---------- 26 | msg : str 27 | The message to display in the deprecation warning. 28 | stacklevel : int 29 | How far up the stack the warning needs to go, before 30 | showing the relevant calling lines. 31 | Usage 32 | ----- 33 | @deprecated(msg='function_a is deprecated! Use function_b instead.') 34 | def function_a(*args, **kwargs): 35 | """ 36 | def deprecated_dec(fn): 37 | @wraps(fn) 38 | def wrapper(*args, **kwargs): 39 | warnings.warn( 40 | msg or "Function %s is deprecated." % fn.__name__, 41 | category=DeprecationWarning, 42 | stacklevel=stacklevel 43 | ) 44 | return fn(*args, **kwargs) 45 | return wrapper 46 | return deprecated_dec 47 | -------------------------------------------------------------------------------- /zipline/utils/enum.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from ctypes import ( 17 | Structure, 18 | c_ubyte, 19 | c_uint, 20 | c_ulong, 21 | c_ulonglong, 22 | c_ushort, 23 | sizeof, 24 | ) 25 | 26 | import numpy as np 27 | import pandas as pd 28 | from six.moves import range 29 | 30 | 31 | _inttypes_map = { 32 | sizeof(t) - 1: t for t in { 33 | c_ubyte, 34 | c_uint, 35 | c_ulong, 36 | c_ulonglong, 37 | c_ushort 38 | } 39 | } 40 | _inttypes = list( 41 | pd.Series(_inttypes_map).reindex( 42 | range(max(_inttypes_map.keys())), 43 | method='bfill', 44 | ), 45 | ) 46 | 47 | 48 | def enum(option, *options): 49 | """ 50 | Construct a new enum object. 51 | 52 | Parameters 53 | ---------- 54 | *options : iterable of str 55 | The names of the fields for the enum. 56 | 57 | Returns 58 | ------- 59 | enum 60 | A new enum collection. 61 | 62 | Examples 63 | -------- 64 | >>> e = enum('a', 'b', 'c') 65 | >>> e 66 | 67 | >>> e.a 68 | 0 69 | >>> e.b 70 | 1 71 | >>> e.a in e 72 | True 73 | >>> tuple(e) 74 | (0, 1, 2) 75 | 76 | Notes 77 | ----- 78 | Identity checking is not guaranteed to work with enum members, instead 79 | equality checks should be used. From CPython's documentation: 80 | 81 | "The current implementation keeps an array of integer objects for all 82 | integers between -5 and 256, when you create an int in that range you 83 | actually just get back a reference to the existing object. So it should be 84 | possible to change the value of 1. I suspect the behaviour of Python in 85 | this case is undefined. :-)" 86 | """ 87 | options = (option,) + options 88 | rangeob = range(len(options)) 89 | 90 | try: 91 | inttype = _inttypes[int(np.log2(len(options) - 1)) // 8] 92 | except IndexError: 93 | raise OverflowError( 94 | 'Cannot store enums with more than sys.maxsize elements, got %d' % 95 | len(options), 96 | ) 97 | 98 | class _enum(Structure): 99 | _fields_ = [(o, inttype) for o in options] 100 | 101 | def __iter__(self): 102 | return iter(rangeob) 103 | 104 | def __contains__(self, value): 105 | return 0 <= value < len(options) 106 | 107 | def __repr__(self): 108 | return '' % ( 109 | ('%d fields' % len(options)) 110 | if len(options) > 10 else 111 | repr(options) 112 | ) 113 | 114 | return _enum(*rangeob) 115 | -------------------------------------------------------------------------------- /zipline/utils/functional.py: -------------------------------------------------------------------------------- 1 | from pprint import pformat 2 | 3 | from six import viewkeys 4 | from six.moves import map 5 | 6 | 7 | def mapall(funcs, seq): 8 | """ 9 | Parameters 10 | ---------- 11 | funcs : iterable[function] 12 | Sequence of functions to map over `seq`. 13 | seq : iterable 14 | Sequence over which to map funcs. 15 | 16 | Yields 17 | ------ 18 | elem : object 19 | Concatenated result of mapping each ``func`` over ``seq``. 20 | 21 | Example 22 | ------- 23 | >>> list(mapall([lambda x: x + 1, lambda x: x - 1], [1, 2, 3])) 24 | [2, 3, 4, 0, 1, 2] 25 | """ 26 | for func in funcs: 27 | for elem in seq: 28 | yield func(elem) 29 | 30 | 31 | def same(*values): 32 | """ 33 | Check if all values in a sequence are equal. 34 | 35 | Returns True on empty sequences. 36 | 37 | Example 38 | ------- 39 | >>> same(1, 1, 1, 1) 40 | True 41 | >>> same(1, 2, 1) 42 | False 43 | >>> same() 44 | True 45 | """ 46 | if not values: 47 | return True 48 | first, rest = values[0], values[1:] 49 | return all(value == first for value in rest) 50 | 51 | 52 | def _format_unequal_keys(dicts): 53 | return pformat([sorted(d.keys()) for d in dicts]) 54 | 55 | 56 | def dzip_exact(*dicts): 57 | """ 58 | Parameters 59 | ---------- 60 | *dicts : iterable[dict] 61 | A sequence of dicts all sharing the same keys. 62 | 63 | Returns 64 | ------- 65 | zipped : dict 66 | A dict whose keys are the union of all keys in *dicts, and whose values 67 | are tuples of length len(dicts) containing the result of looking up 68 | each key in each dict. 69 | 70 | Raises 71 | ------ 72 | ValueError 73 | If dicts don't all have the same keys. 74 | 75 | Example 76 | ------- 77 | >>> result = dzip_exact({'a': 1, 'b': 2}, {'a': 3, 'b': 4}) 78 | >>> result == {'a': (1, 3), 'b': (2, 4)} 79 | True 80 | """ 81 | if not same(*map(viewkeys, dicts)): 82 | raise ValueError( 83 | "dict keys not all equal:\n\n%s" % _format_unequal_keys(dicts) 84 | ) 85 | return {k: tuple(d[k] for d in dicts) for k in dicts[0]} 86 | -------------------------------------------------------------------------------- /zipline/utils/math_utils.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2013 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | import math 17 | 18 | 19 | def tolerant_equals(a, b, atol=10e-7, rtol=10e-7): 20 | return math.fabs(a - b) <= (atol + rtol * math.fabs(b)) 21 | 22 | 23 | try: 24 | # fast versions 25 | import bottleneck as bn 26 | nanmean = bn.nanmean 27 | nanstd = bn.nanstd 28 | nansum = bn.nansum 29 | nanmax = bn.nanmax 30 | nanmin = bn.nanmin 31 | nanargmax = bn.nanargmax 32 | nanargmin = bn.nanargmin 33 | except ImportError: 34 | # slower numpy 35 | import numpy as np 36 | nanmean = np.nanmean 37 | nanstd = np.nanstd 38 | nansum = np.nansum 39 | nanmax = np.nanmax 40 | nanmin = np.nanmin 41 | nanargmax = np.nanargmax 42 | nanargmin = np.nanargmin 43 | -------------------------------------------------------------------------------- /zipline/utils/munge.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | from pandas.core.common import mask_missing 16 | try: 17 | from pandas.core.common import backfill_2d, pad_2d 18 | except ImportError: 19 | # In 0.17, pad_2d and backfill_2d werw moved from pandas.core.common to 20 | # pandas.core.missing 21 | from pandas.core.missing import backfill_2d, pad_2d 22 | 23 | 24 | def _interpolate(values, method, axis=None): 25 | if values.ndim == 1: 26 | axis = 0 27 | elif values.ndim == 2: 28 | axis = 1 29 | else: 30 | raise Exception("Cannot interpolate array with more than 2 dims") 31 | 32 | values = values.copy() 33 | values = interpolate_2d(values, method, axis=axis) 34 | return values 35 | 36 | 37 | def interpolate_2d(values, method='pad', axis=0, limit=None, fill_value=None): 38 | """ 39 | Copied from the 0.15.2. This did not exist in 0.12.0. 40 | 41 | Differences: 42 | - Don't depend on pad_2d and backfill_2d to return values 43 | - Removed dtype kwarg. 0.12.0 did not have this option. 44 | """ 45 | transf = (lambda x: x) if axis == 0 else (lambda x: x.T) 46 | 47 | # reshape a 1 dim if needed 48 | ndim = values.ndim 49 | if values.ndim == 1: 50 | if axis != 0: # pragma: no cover 51 | raise AssertionError("cannot interpolate on a ndim == 1 with " 52 | "axis != 0") 53 | values = values.reshape(tuple((1,) + values.shape)) 54 | 55 | if fill_value is None: 56 | mask = None 57 | else: # todo create faster fill func without masking 58 | mask = mask_missing(transf(values), fill_value) 59 | 60 | # Note: pad_2d and backfill_2d work inplace in 0.12.0 and 0.15.2 61 | # in 0.15.2 they also return a reference to values 62 | if method == 'pad': 63 | pad_2d(transf(values), limit=limit, mask=mask) 64 | else: 65 | backfill_2d(transf(values), limit=limit, mask=mask) 66 | 67 | # reshape back 68 | if ndim == 1: 69 | values = values[0] 70 | 71 | return values 72 | 73 | 74 | def ffill(values, axis=None): 75 | return _interpolate(values, 'pad', axis=axis) 76 | 77 | 78 | def bfill(values, axis=None): 79 | return _interpolate(values, 'bfill', axis=axis) 80 | -------------------------------------------------------------------------------- /zipline/utils/pandas_utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | Utilities for working with pandas objects. 3 | """ 4 | import pandas as pd 5 | 6 | 7 | def explode(df): 8 | """ 9 | Take a DataFrame and return a triple of 10 | 11 | (df.index, df.columns, df.values) 12 | """ 13 | return df.index, df.columns, df.values 14 | 15 | 16 | try: 17 | # pandas 0.16 compat 18 | sort_values = pd.DataFrame.sort_values 19 | except AttributeError: 20 | sort_values = pd.DataFrame.sort 21 | -------------------------------------------------------------------------------- /zipline/utils/sentinel.py: -------------------------------------------------------------------------------- 1 | """ 2 | Construction of sentinel objects. 3 | 4 | Sentinel objects are used when you only care to check for object identity. 5 | """ 6 | import sys 7 | from textwrap import dedent 8 | 9 | 10 | def sentinel(name, doc=None): 11 | try: 12 | value = sentinel._cache[name] # memoized 13 | except KeyError: 14 | pass 15 | else: 16 | if doc == value.__doc__: 17 | return value 18 | 19 | raise ValueError(dedent( 20 | """\ 21 | New sentinel value %r conflicts with an existing sentinel of the 22 | same name. 23 | Old sentinel docstring: %r 24 | New sentinel docstring: %r 25 | Resolve this conflict by changing the name of one of the sentinels. 26 | """, 27 | ) % (name, value.__doc__, doc)) 28 | 29 | @object.__new__ # bind a single instance to the name 'Sentinel' 30 | class Sentinel(object): 31 | __doc__ = doc 32 | __slots__ = ('__weakref__',) 33 | __name__ = name 34 | 35 | def __new__(cls): 36 | raise TypeError('cannot create %r instances' % name) 37 | 38 | def __repr__(self): 39 | return 'sentinel(%r)' % name 40 | 41 | def __reduce__(self): 42 | return sentinel, (name, doc) 43 | 44 | def __deepcopy__(self, _memo): 45 | return self 46 | 47 | def __copy__(self): 48 | return self 49 | 50 | cls = type(Sentinel) 51 | try: 52 | # traverse up one frame to find the module where this is defined 53 | cls.__module__ = sys._getframe(1).f_globals['__name__'] 54 | except (ValueError, KeyError): 55 | # Couldn't get the name from the calling scope, just use None. 56 | cls.__module__ = None 57 | 58 | sentinel._cache[name] = Sentinel # cache result 59 | return Sentinel 60 | sentinel._cache = {} 61 | -------------------------------------------------------------------------------- /zipline/utils/serialization_utils.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015 Quantopian, Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | from six import BytesIO 17 | import pickle 18 | from functools import partial 19 | 20 | from zipline.assets import AssetFinder 21 | from zipline.finance.trading import TradingEnvironment 22 | 23 | # Label for the serialization version field in the state returned by 24 | # __getstate__. 25 | VERSION_LABEL = '_stateversion_' 26 | 27 | 28 | def _persistent_id(obj): 29 | if isinstance(obj, AssetFinder): 30 | return AssetFinder.PERSISTENT_TOKEN 31 | if isinstance(obj, TradingEnvironment): 32 | return TradingEnvironment.PERSISTENT_TOKEN 33 | return None 34 | 35 | 36 | def _persistent_load(persid, env): 37 | if persid == AssetFinder.PERSISTENT_TOKEN: 38 | return env.asset_finder 39 | if persid == TradingEnvironment.PERSISTENT_TOKEN: 40 | return env 41 | 42 | 43 | def dumps_with_persistent_ids(obj, protocol=None): 44 | """ 45 | Performs a pickle dumps on the given object, substituting all references to 46 | a TradingEnvironment or AssetFinder with tokenized representations. 47 | 48 | All arguments are passed to pickle.Pickler and are described therein. 49 | """ 50 | file = BytesIO() 51 | pickler = pickle.Pickler(file, protocol) 52 | pickler.persistent_id = _persistent_id 53 | pickler.dump(obj) 54 | return file.getvalue() 55 | 56 | 57 | def loads_with_persistent_ids(str, env): 58 | """ 59 | Performs a pickle loads on the given string, substituting the given 60 | TradingEnvironment in to any tokenized representations of a 61 | TradingEnvironment or AssetFinder. 62 | 63 | Parameters 64 | ---------- 65 | str : String 66 | The string representation of the object to be unpickled. 67 | env : TradingEnvironment 68 | The TradingEnvironment to be inserted to the unpickled object. 69 | 70 | Returns 71 | ------- 72 | obj 73 | An unpickled object formed from the parameter 'str'. 74 | """ 75 | file = BytesIO(str) 76 | unpickler = pickle.Unpickler(file) 77 | unpickler.persistent_load = partial(_persistent_load, env=env) 78 | return unpickler.load() 79 | -------------------------------------------------------------------------------- /zipline/utils/simfactory.py: -------------------------------------------------------------------------------- 1 | import zipline.utils.factory as factory 2 | 3 | from zipline.test_algorithms import TestAlgorithm 4 | 5 | 6 | def create_test_zipline(**config): 7 | """ 8 | :param config: A configuration object that is a dict with: 9 | 10 | - sid - an integer, which will be used as the asset ID. 11 | - order_count - the number of orders the test algo will place, 12 | defaults to 100 13 | - order_amount - the number of shares per order, defaults to 100 14 | - trade_count - the number of trades to simulate, defaults to 101 15 | to ensure all orders are processed. 16 | - algorithm - optional parameter providing an algorithm. defaults 17 | to :py:class:`zipline.test.algorithms.TestAlgorithm` 18 | - trade_source - optional parameter to specify trades, if present. 19 | If not present :py:class:`zipline.sources.SpecificEquityTrades` 20 | is the source, with daily frequency in trades. 21 | - slippage: optional parameter that configures the 22 | :py:class:`zipline.gens.tradingsimulation.TransactionSimulator`. 23 | Expects an object with a simulate mehod, such as 24 | :py:class:`zipline.gens.tradingsimulation.FixedSlippage`. 25 | :py:mod:`zipline.finance.trading` 26 | """ 27 | assert isinstance(config, dict) 28 | 29 | try: 30 | sid_list = config['sid_list'] 31 | except KeyError: 32 | try: 33 | sid_list = [config['sid']] 34 | except KeyError: 35 | raise Exception("simfactory create_test_zipline() requires " 36 | "argument 'sid_list' or 'sid'") 37 | 38 | concurrent_trades = config.get('concurrent_trades', False) 39 | 40 | if 'order_count' in config: 41 | order_count = config['order_count'] 42 | else: 43 | order_count = 100 44 | 45 | if 'order_amount' in config: 46 | order_amount = config['order_amount'] 47 | else: 48 | order_amount = 100 49 | 50 | # ------------------- 51 | # Create the Algo 52 | # ------------------- 53 | if 'algorithm' in config: 54 | test_algo = config['algorithm'] 55 | else: 56 | test_algo = TestAlgorithm( 57 | sid_list[0], 58 | order_amount, 59 | order_count, 60 | sim_params=config.get('sim_params', 61 | factory.create_simulation_parameters()), 62 | slippage=config.get('slippage'), 63 | identifiers=sid_list 64 | ) 65 | 66 | # ------------------- 67 | # Trade Source 68 | # ------------------- 69 | if 'trade_source' in config: 70 | trade_source = config['trade_source'] 71 | else: 72 | trade_source = factory.create_daily_trade_source( 73 | sid_list, 74 | test_algo.sim_params, 75 | test_algo.trading_environment, 76 | concurrent=concurrent_trades, 77 | ) 78 | if trade_source: 79 | test_algo.set_sources([trade_source]) 80 | 81 | # ------------------- 82 | # Benchmark source 83 | # ------------------- 84 | 85 | test_algo.benchmark_return_source = config.get('benchmark_source', None) 86 | 87 | # ------------------ 88 | # generator/simulator 89 | sim = test_algo.get_generator() 90 | 91 | return sim 92 | --------------------------------------------------------------------------------