原先的计算是把系统开发的差不多,再写专栏。
随着星球小伙伴越来越多,对于系统构建思路,代码讲解,系统的可运行性,包括向后兼容提出更高的要求。
因此今天开始,把专栏这个事情提上日程,尽量更新吧。
说实话,这个写完差不多是一本书了。
不一定会按照顺序写,一开始肯定是围绕一个可以跑起来,但扩展性要好的回测系统。然后数据模块,因子模块,机器学习模型,最后是策略实战。
昨天我们提及的bt。http://pmorissette.github.io/bt/
特点:
1. 矩阵模式回测
2. 结构简单,只三个文件,再引用了ffn库,也是三个文件
3. 可以绘图,生成权重、持仓、交易清单等。
另外:
1. 支持策略树,可以将多个策略进行叠加、嵌套,组合成复杂策略
2. 支持大类资产配置,内置了等权、波动率倒数,均值方法、风险平价四种方法
3. 模块化,可以对模块进行组合定制。如定时运行、指定权重等等
咱们之类的框架,实现了它的大类资产配置和模块化,策略树这个有点意思。
因为它只有三个文件,而且backtest.py和algos我们都比较熟悉了,只需要熟悉一下bt这个树结构,在core.py中。
上述就是我们的传统策略,SPY和AGG做股债平衡,但我们可能需要这样的策略,债券是被动操作,但权益类的需要主动管理,比如动量策略,那么策略树可以改成这样:
在我们自己用的“绝对型收益”组合策略构建上,这个很有多,通常策略有择时、轮动,大类资产配置,还是就是策略组合,多策略组合在一起,可以有效降低回撤,提升夏普比。
node是这个树结构里的重要节点,它即是策略的基类,也是证券的基类,我们可以把多策略组合,策略与证券也做组合等等。
class Node(object): _capital = cy.declare(cy.double) _price = cy.declare(cy.double) _value = cy.declare(cy.double) _notl_value = cy.declare(cy.double) _weight = cy.declare(cy.double) _issec = cy.declare(cy.bint) _has_strat_children = cy.declare(cy.bint) _fixed_income = cy.declare(cy.bint) _bidoffer_set = cy.declare(cy.bint) _bidoffer_paid = cy.declare(cy.double) def __init__(self, name, parent=None, children=None): self.name = name # children helpers self.children = {} self._lazy_children = {} self._universe_tickers = [] self._childrenv = [] # Shortcut to self.children.values() self._original_children_are_present = (children is not None) and ( len(children) >= 1 ) # strategy children helpers self._has_strat_children = False self._strat_children = [] if parent is None: self.parent = self self.root = self # by default all positions are integer self.integer_positions = True else: self.parent = parent parent._add_children([self], dc=False) self._add_children(children, dc=True) # set default value for now self.now = 0 # make sure root has stale flag # used to avoid unnecessary update # sometimes we change values in the tree and we know that we will need # to update if another node tries to access a given value (say weight). # This avoid calling the update until it is actually needed. self.root.stale = False
由于bt有一个bug,我看了它的issue,似乎没有复现与解决:
因此我把代码拉出来自己改进:
核心指数数据:
数据加载及表达式引擎示例,
最新的“大小盘月度再平衡”的例子:
重构后的引擎及策略代码均已在星球发布,请自行更新。
本次属于从零重构,改动很大,请大家知晓。
发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/103965
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!