Skip to content

Commit

Permalink
降低以周期策略的编写冗余
Browse files Browse the repository at this point in the history
  • Loading branch information
bbfamily committed Sep 1, 2017
1 parent f8abb36 commit 870bc58
Show file tree
Hide file tree
Showing 9 changed files with 521 additions and 76 deletions.
59 changes: 59 additions & 0 deletions abupy/FactorBuyBu/ABuFactorBuyBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,62 @@ def fit_day(self, today):

# def fit_month(self, *args, **kwargs):
# pass


class AbuFactorBuyXD(AbuFactorBuyBase):
"""以周期为重要参数的策略,xd代表参数'多少天'如已周期为参数可直接继承使用"""

def read_fit_day(self, today):
"""
覆盖base函数完成过滤统计周期内前xd天以及为fit_day中切片周期金融时间序列数据
:param today: 当前驱动的交易日金融时间序列数据
:return: 生成的交易订单AbuOrder对象
"""
if self.skip_days > 0:
self.skip_days -= 1
return None

# 今天这个交易日在整个金融时间序列的序号
self.today_ind = int(today.key)
# 回测中默认忽略最后一个交易日
if self.today_ind >= self.kl_pd.shape[0] - 1:
return None

# 忽略不符合买入的天(统计周期内前xd天)
if self.today_ind < self.xd - 1:
return None

# 完成为fit_day中切片周期金融时间序列数据
self.xd_kl = self.kl_pd[self.today_ind - self.xd + 1:self.today_ind + 1]

return self.fit_day(today)

def buy_tomorrow(self):
"""
覆盖base函数,明天进行买入操作,比如突破策略使用了今天收盘的价格做为参数,发出了买入信号,
需要进行明天买入操作,不能执行今天买入操作,使用周期参数xd赋予skip_days
:return 生成的交易订单AbuOrder对象
"""

self.skip_days = self.xd
return self.make_buy_order(self.today_ind)

def buy_today(self):
"""
覆盖base函数,今天即进行买入操作,需要不能使用今天的收盘数据等做为fit_day中信号判断,
适合如比特币非明确一天交易日时间或者特殊情况的买入信号,,使用周期参数xd赋予skip_days
:return 生成的交易订单AbuOrder对象
"""
self.skip_days = self.xd
return self.make_buy_order(self.today_ind - 1)

def _init_self(self, **kwargs):
"""子类因子针对可扩展参数的初始化"""
# 突破周期参数 xd, 比如20,30,40天...突破, 不要使用kwargs.pop('xd', 20), 明确需要参数xq
self.xd = kwargs['xd']
# 在输出生成的orders_pd中显示的名字
self.factor_name = '{}:{}'.format(self.__class__.__name__, self.xd)

def fit_day(self, today):
"""raise NotImplementedError"""
raise NotImplementedError('NotImplementedError fit_day')
32 changes: 31 additions & 1 deletion abupy/FactorBuyBu/ABuFactorBuyBreak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import print_function
from __future__ import division

from .ABuFactorBuyBase import AbuFactorBuyBase, BuyCallMixin, BuyPutMixin
from .ABuFactorBuyBase import AbuFactorBuyBase, AbuFactorBuyXD, BuyCallMixin, BuyPutMixin

__author__ = '阿布'
__weixin__ = 'abu_quant'
Expand Down Expand Up @@ -43,6 +43,21 @@ def fit_day(self, today):
return None


# noinspection PyAttributeOutsideInit
class AbuFactorBuyXDBK(AbuFactorBuyXD, BuyCallMixin):
"""示例继承AbuFactorBuyXD完成正向突破买入择时类"""
def fit_day(self, today):
"""
针对每一个交易日拟合买入交易策略,寻找向上突破买入机会
:param today: 当前驱动的交易日金融时间序列数据
:return:
"""
# 今天的收盘价格达到xd天内最高价格则符合买入条件
if today.close == self.xd_kl.close.max():
return self.buy_tomorrow()
return None


# noinspection PyAttributeOutsideInit
class AbuFactorBuyPutBreak(AbuFactorBuyBase, BuyPutMixin):
"""示例反向突破买入择时类,混入BuyPutMixin,即向下突破触发买入event,详情请查阅期货回测示例demo"""
Expand Down Expand Up @@ -70,3 +85,18 @@ def fit_day(self, today):
self.skip_days = self.xd
return self.buy_tomorrow()
return None


# noinspection PyAttributeOutsideInit
class AbuFactorBuyPutXDBK(AbuFactorBuyXD, BuyPutMixin):
"""示例继承AbuFactorBuyXD完成反向突破买入择时类"""
def fit_day(self, today):
"""
针对每一个交易日拟合买入交易策略,寻找向上突破买入机会
:param today: 当前驱动的交易日金融时间序列数据
:return:
"""
# 与AbuFactorBuyBreak区别就是买向下突破的,即min()
if today.close == self.xd_kl.close.min():
return self.buy_tomorrow()
return None
3 changes: 2 additions & 1 deletion abupy/FactorBuyBu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import absolute_import

from .ABuFactorBuyBase import AbuFactorBuyBase, BuyCallMixin, BuyPutMixin
from .ABuFactorBuyBase import AbuFactorBuyBase, AbuFactorBuyXD, BuyCallMixin, BuyPutMixin
from .ABuFactorBuyBreak import AbuFactorBuyBreak, AbuFactorBuyPutBreak
from .ABuFactorBuyDemo import AbuSDBreak, AbuTwoDayBuy, AbuFactorBuyBreakUmpDemo
from .ABuFactorBuyDemo import AbuFactorBuyBreakReocrdHitDemo, AbuFactorBuyBreakHitPredictDemo

__all__ = [
'AbuFactorBuyBase',
'AbuFactorBuyXD',
'BuyCallMixin',
'BuyPutMixin',
'AbuFactorBuyBreak',
Expand Down
38 changes: 38 additions & 0 deletions abupy/FactorSellBu/ABuFactorSellBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,41 @@ def make_sell_order_ml_feature(self, day_ind):

# def fit_month(self, *args, **kwargs):
# pass


class AbuFactorSellXD(AbuFactorSellBase):
"""以周期为重要参数的策略,xd代表参数'多少天'如已周期为参数可直接继承使用 """

def _init_self(self, **kwargs):
"""kwargs中必须包含: 突破参数xd 比如20,30,40天...突破"""
# 向下突破参数 xd, 比如20,30,40天...突破
self.xd = kwargs['xd']
# 在输出生成的orders_pd中显示的名字
self.sell_type_extra = '{}:{}'.format(self.__class__.__name__, self.xd)

def read_fit_day(self, today, orders):
"""
覆盖base函数, 为fit_day中切片周期金融时间序列数据
:param today: 当前驱动的交易日金融时间序列数据
:param orders: 买入择时策略中生成的订单序列
:return: 生成的交易订单AbuOrder对象
"""
# 今天这个交易日在整个金融时间序列的序号
self.today_ind = int(today.key)
# 回测中默认忽略最后一个交易日
if self.today_ind >= self.kl_pd.shape[0] - 1:
return
orders = list(filter(lambda order: order.expect_direction in self.support_direction(), orders))

# 完成为fit_day中切片周期金融时间序列数据
self.xd_kl = self.kl_pd[self.today_ind - self.xd + 1:self.today_ind + 1]

return self.fit_day(today, orders)

def support_direction(self):
"""raise NotImplementedError"""
raise NotImplementedError('NotImplementedError support_direction')

def fit_day(self, today, orders):
"""raise NotImplementedError"""
raise NotImplementedError('NotImplementedError support_direction')
22 changes: 20 additions & 2 deletions abupy/FactorSellBu/ABuFactorSellBreak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from __future__ import print_function
from __future__ import division

from .ABuFactorSellBase import AbuFactorSellBase, ESupportDirection
from .ABuFactorSellBase import AbuFactorSellBase, AbuFactorSellXD, ESupportDirection

__author__ = '阿布'
__weixin__ = 'abu_quant'
Expand All @@ -33,9 +33,27 @@ def fit_day(self, today, orders):
寻找向下突破作为策略卖出驱动event
:param today: 当前驱动的交易日金融时间序列数据
:param orders: 买入择时策略中生成的订单序列
:return:
"""
# 今天的收盘价格达到xd天内最低价格则符合条件
if today.close == self.kl_pd.close[self.today_ind - self.xd + 1:self.today_ind + 1].min():
for order in orders:
self.sell_tomorrow(order)


class AbuFactorSellXDBK(AbuFactorSellXD):
"""示例继承AbuFactorBuyXD, 向下突破卖出择时因子"""

def support_direction(self):
"""支持的方向,只支持正向"""
return [ESupportDirection.DIRECTION_CAll.value]

def fit_day(self, today, orders):
"""
寻找向下突破作为策略卖出驱动event
:param today: 当前驱动的交易日金融时间序列数据
:param orders: 买入择时策略中生成的订单序列
"""
# 今天的收盘价格达到xd天内最低价格则符合条件
if today.close == self.xd_kl.close.min():
for order in orders:
self.sell_tomorrow(order)
3 changes: 2 additions & 1 deletion abupy/FactorSellBu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import

from .ABuFactorSellBase import AbuFactorSellBase, ESupportDirection
from .ABuFactorSellBase import AbuFactorSellBase, AbuFactorSellXD, ESupportDirection
from .ABuFactorPreAtrNStop import AbuFactorPreAtrNStop
from .ABuFactorAtrNStop import AbuFactorAtrNStop
from .ABuFactorCloseAtrNStop import AbuFactorCloseAtrNStop
Expand All @@ -11,6 +11,7 @@

__all__ = [
'AbuFactorSellBase',
'AbuFactorSellXD',
'ESupportDirection',
'AbuFactorPreAtrNStop',
'AbuFactorAtrNStop',
Expand Down
85 changes: 35 additions & 50 deletions abupy_lecture/1-择时策略的开发(ABU量化使用文档).ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -720,32 +720,17 @@
},
"outputs": [],
"source": [
"from abupy import AbuFactorBuyBase, BuyPutMixin\n",
"\n",
"class AbuFactorBuyPutBreak(AbuFactorBuyBase, BuyPutMixin):\n",
" \"\"\"示例反向突破买入择时类,混入BuyPutMixin,即向下突破触发买入event,详情请查阅期货回测示例demo\"\"\"\n",
"\n",
" def _init_self(self, **kwargs):\n",
" \"\"\"kwargs中必须包含: 突破参数xd 比如20,30,40天...突破\"\"\"\n",
"\n",
" # 突破参数 xd, 比如20,30,40天...突破, 不要使用kwargs.pop('xd', 20), 明确需要参数xq\n",
" self.xd = kwargs['xd']\n",
" self.factor_name = '{}:{}'.format(self.__class__.__name__, self.xd)\n",
"from abupy import AbuFactorBuyXD, BuyPutMixin\n",
"\n",
"class AbuFactorBuyPutXDBK(AbuFactorBuyXD, BuyPutMixin):\n",
" \"\"\"示例继承AbuFactorBuyXD完成反向突破买入择时类\"\"\"\n",
" def fit_day(self, today):\n",
" \"\"\"\n",
" 针对每一个交易日拟合买入交易策略,寻找向下突破买入机会\n",
" 针对每一个交易日拟合买入交易策略,寻找向上突破买入机会\n",
" :param today: 当前驱动的交易日金融时间序列数据\n",
" :return:\n",
" \"\"\"\n",
" # 忽略不符合买入的天(统计周期内前xd天)\n",
" if self.today_ind < self.xd - 1:\n",
" return None\n",
" \"\"\"\n",
" 与AbuFactorBuyBreak区别就是买向下突破的,即min()\n",
" \"\"\"\n",
" if today.close == self.kl_pd.close[self.today_ind - self.xd + 1:self.today_ind + 1].min():\n",
" self.skip_days = self.xd\n",
" # 与AbuFactorBuyBreak区别就是买向下突破的,即min()\n",
" if today.close == self.xd_kl.close.min():\n",
" return self.buy_tomorrow()\n",
" return None\n",
" \n",
Expand Down
Loading

0 comments on commit 870bc58

Please sign in to comment.