rqalpha探究 1 setup.py
rqalpha是难得几个好的做量化交易的开源项目,不过由于自己python用的实在不多,看起来还是觉得很复杂。
因此准备抽取出框架,从最简单的搭建。
思路
从setup着手,看一下如何建立一个发布工程,在此基础上完成一个最简单回测模型。
首先建立demo工程bwtougu,确保可以生成脚本文件bwtougu-script.py和bwtougu.exe(如在linux建立,则生成可执行程序bwtougu)
操作步骤
1 首先新建文件夹bwtougu, 在里面建立文件setup.py
2 建立依赖文件requirements.txt
3 建立子文件夹bwtougu,并在其中新建版本文件VERSION.txt
4 建立初始化文件__init__.py
5 建立程序入口文件__main__.py
操作完后即可进行测试了,建议用PyCharm进行调试,调试截图如下:
运行完成后即可在python的安装目录下找到bwtougu.exe文件和bwtougu-script.py
附上对应文件的源码
bwtougu\setup.py
#!usr/bin/evn python3
# -*- coding: utf-8 -*-
from pip.req import parse_requirements
from os.path import (
dirname,
join
)
from setuptools import (
setup,
find_packages,
)
with open(join(dirname(__file__), 'bwtougu/VERSION.txt'), 'rb') as f:
version = f.read().decode('ascii').strip()
requirements = [str(ir.req) for ir in parse_requirements("requirements.txt", session=False)]
setup(
name='bwtougu',
version=version,
description='bwtougu for python3',
author='luhx',
author_email='luhx@bangth.com',
license='Apache License v2',
package_data={'': ['*.*']},
url='http://www.bangth.com',
packages=find_packages(exclude=[]),
install_requires=requirements,
zip_safe=False,
entry_points={
"console_scripts":[
"bwtougu = bwtougu.__main__:entry_point"
]
},
classifiers=[
'Programming Language :: Python',
'Operating System :: Microsoft :: Windows',
'Operating System :: Unix',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
bwtougu\requirements.txt
requests
numpy>=1.11.1
six>=1.10.0
logbook==1.0.0
click>6.6
bwtougu\bwtougu\__init__.py
#!usr/bin/env python3
# -*- coding: utf-8 -*-
import pkgutil
__all__ = [
'__version__',
'version_info'
]
__version__ = pkgutil.get_data(__package__, 'VERSION.txt').decode('ascii').strip()
version_info = tuple(int(v) if v.isdigit() else v
for v in __version__.split('.'))
bwtougu\bwtougu\__main__.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import six
import click
CONTEXT_SETTINGS={
'default_map': {
'run': {
}
}
}
@click.group(context_settings=CONTEXT_SETTINGS)
@click.option('-v', '--verbose', count=True)
@click.help_option('-h', '--help')
@click.pass_context
def cli(ctx, verbose):
ctx.obj["VERBOSE"] = verbose
def entry_point():
print("entry_point...")
cli(obj={})
@cli.command()
@click.option('-v', '--verbose', is_flag=True)
def version(**kwargs):
"""
Output Version Info
"""
from bwtougu import version_info
six.print_("Current Version: ", version_info)
if __name__ == '__main__':
entry_point()
完毕