fromzipline.apiimportorder_target, record, symboldefinitialize(context):
context.i=0context.asset=symbol('AAPL')
defhandle_data(context, data):
# Skip first 300 days to get full windowscontext.i+=1ifcontext.i<300:
return# Compute averages# data.history() has to be called with the same params# from above and returns a pandas dataframe.short_mavg=data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
long_mavg=data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()
# Trading logicifshort_mavg>long_mavg:
# order_target orders as many shares as needed to# achieve the desired number of shares.order_target(context.asset, 100)
elifshort_mavg<long_mavg:
order_target(context.asset, 0)
# Save values for later inspectionrecord(AAPL=data.current(context.asset, 'price'),
short_mavg=short_mavg,
long_mavg=long_mavg)
install_req = ['ipython']
if sys.version_info[0] < 3 and 'bdist_wheel' not in sys.argv:
install_req.remove('ipython')
install_req.append('ipython<6')
setup(
...
install_requires=install_req
)
# Create project from the cookiecutter-pypackage.git repo template# You'll be prompted to enter values.# Then it'll create your Python package in the current working directory,# based on those values.
$ cookiecutter https://github.com/audreyfeldroy/cookiecutter-pypackage
# For the sake of brevity, repos on GitHub can just use the 'gh' prefix
$ cookiecutter gh:audreyfeldroy/cookiecutter-pypackage
在带有本地模板的命令行中使用它:
# Create project in the current working directory, from the local# cookiecutter-pypackage/ template
$ cookiecutter cookiecutter-pypackage/
或者从Python中使用它:
fromcookiecutter.mainimportcookiecutter# Create project from the cookiecutter-pypackage/ templatecookiecutter('cookiecutter-pypackage/')
# Create project from the cookiecutter-pypackage.git repo templatecookiecutter('https://github.com/audreyfeldroy/cookiecutter-pypackage.git')
# Clone cookiecutter-pypackage
$ cookiecutter gh:audreyfeldroy/cookiecutter-pypackage
# Now you can use the already cloned cookiecutter by name
$ cookiecutter cookiecutter-pypackage