关于filter的处理
发布于 1 个月前 作者 Trammell42 50 次浏览 来自 报bug

回测 https://github.com/QUANTAXIS/QUANTAXIS/issues/1299 中的策略发现股票池中如果有期间停盘的股票, 对指标做filter会报异常,下面是代码:

import QUANTAXIS as QA
if QA.__version__>= '1.5.22':
    pass
else:
    print('请升级QUANTAXIS, 1.5.22增加了FILTER函数, 此函数为本次教程必须')
    print('使用 pip install QUANTAXIS==1.5.22 来使用')
	
#600987期间有停牌
data = QA.QA_fetch_stock_day_adv(['600987','002327'],'2018-01-01','2018-06-01')
data = data.to_qfq()

from QUANTAXIS.QAIndicator.base import *
def indx(data):
    N=20 
    N1=7 
    N2=69 
    #T1=IF((DATE<=1341231),1,0) 
    #print(data.date)
    
    #T1 = data.date.apply(lambda x: 1 if x<=1341231 else 0)
    T1  = 1
    M3=EMA(data.close,3) 
    M8=EMA(data.close,8)
    
#     M8:EMA(data.close,8),COLOR008000 
    M13 = EMA(M8, 13)
#     M13:EMA(M8,13),COLOR0000FF
    M62 = EMA(M8, 55)
#     M62:EMA(M8,55),COLORF0F000 
    VAR1=EMA(M13,62) 
    M81 = EMA(M62,81)
    M144 = EMA(VAR1, 144)
    M250 = EMA(VAR1,250)
#     M81:EMA(M62,81),COLOR00FFFF 
#     M144:EMA(VAR1,144),LINETHICK2,COLOR0000FF 
#     M250:EMA(VAR1,250),POINTDOT,LINETHICK2,COLORF00FF0 
    VAR2=(data.close+data.high+data.low)/(3) 
    VAR3=(EMA(VAR2,10))*(T1) 
    VAR4=(REF(VAR3,1))*(T1) 
    Line1 = IF(VAR3>VAR4, VAR3, VAR4)
    Line2 = IF(VAR3<VAR4, VAR3, VAR4)
#     STICKLINE((VAR3>VAR4),VAR3,VAR4,4,0),COLOR00FFFF 
#     STICKLINE((VAR3<VAR4),VAR3,VAR4,4,0),COLORFF0000 
    VAR5=((REF(data.high,1)+REF(data.low,1))/(2))*(T1) 
    VAR6=MA(data.close,N)+((2)*(STD(data.close,N)))*(T1) 
    
    Text1 =CROSS(VAR3,VAR4)
    Text2 = CROSS(VAR5,VAR6)
#     DRAWTEXT(CROSS(VAR3,VAR4),(VAR3)*(0.98),'¡ï'),COLOR00FFFF 
#     DRAWTEXT(CROSS(VAR5,VAR6),(data.high)*(1.04),'¡ýÂô'),COLOR008000 
    VAR7=REF(data.close,1) 
    VAR8=((SMA(MAX(data.close-VAR7,0),N1,1))/(SMA(ABS(data.close-VAR7),N1,1)))*(100) 
    VAR9=CROSS(N2,VAR8)
    VARA=(FILTER(VAR9,4))*(T1) 
    Text3= VARA
    
#     DRAWTEXT(VARA,(data.high)*(1.04),'¡ýÂô'),COLORFFFFFF 
    VARB=((2)*(data.close)+data.high+data.low)/(4) 
    VARC=LLV(data.low,34) 
    VARD=HHV(data.high,34) 
    VARE=(EMA(((VARB-VAR1)/(VAR5-VAR1))*(100),7))*(T1) 
    VARF=(EMA((0.667)*(REF(VARE,1))+(0.333)*(VARE),5))*(T1) 
    Text4 = IF(((COUNT((data.close<REF(data.close,1)),8))/(8)>0.6) & (data.volume>=(1.5)*(MA(data.volume,5))) & COUNT((VARE>=VARF),3) & (REF(data.low,1)==LLV(data.low,120)),1,0)
    Text5 = IF(((COUNT((data.close<REF(data.close,1)),13))/(13)>0.6)& 
               COUNT((VARE>VARF),6) & 
               (REF(data.low,5)==LLV(data.low,120)) & 
               REF((data.close>=data.open),4) & 
               REF((data.close>data.open),3) & 
               REF((data.close>data.open),2) & 
               REF((data.open>data.close),1) & 
               (data.open>REF(data.close,1)),1,0)
#     DRAWTEXT(IF(((COUNT((data.close<REF(data.close,1)),8))/(8)>0.6) & (data.volume>=(1.5)*(MA(data.volume,5))) & COUNT((VARE>=VARF),3) & (REF(data.low,1)=LLV(data.low,120)),1,0),data.low,'¡ñÂò½ø'),COLORFF00FF 
#     DRAWTEXT(IF(((COUNT((data.close<REF(data.close,1)),13))/(13)>0.6) & COUNT((VARE>VARF),6) & (REF(data.low,5)=LLV(data.low,120)) & REF((data.close>=data.open),4) & REF((data.close>data.open),3) & REF((data.close>data.open),2) & REF((data.open>data.close),1) & (data.open>REF(data.close,1)),1,0),data.low,'¡ñÂò½ø'),COLOR00FFFF 
    VAR10=(MA(data.close,80)-(MA(data.close,10))/(3))*(T1) 
    
    return pd.DataFrame({
        'M3': M3,
        'VAR1': VAR1,
        'VAR2': VAR2,
        'VAR3': VAR3,
        'VAR4': VAR4,
        'VAR5': VAR5,
        'VAR6': VAR6,
        'VAR7': VAR7,
        'VAR8': VAR8,
        'VAR9': VAR9,
        'VARA': VARA,
        'VARB': VARB,
        'VARC': VARC,
        'VARD': VARD,
        'VARE': VARE,
        'VARF': VARF,
        'VAR10': VAR10,
        'Line1': Line1,
        'Line2': Line2,
        'Text1': Text1,
        'Text2': Text2,
        'Text3': Text3,
        'Text4': Text4,
        'Text5': Text5,
    })

ind = data.add_func(indx)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/usr/local/lib/python3.6/site-packages/pandas/core/groupby/groupby.py in apply(self, func, *args, **kwargs)
    688             try:
--> 689                 result = self._python_apply_general(f)
    690             except Exception:

/usr/local/lib/python3.6/site-packages/pandas/core/groupby/groupby.py in _python_apply_general(self, f)
    706         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 707                                                    self.axis)
    708 

/usr/local/lib/python3.6/site-packages/pandas/core/groupby/ops.py in apply(self, f, data, axis)
    189             group_axes = _get_axes(group)
--> 190             res = f(group)
    191             if not _is_indexed_like(res, group_axes):

<ipython-input-7-8fdb07b68146> in indx(data)
     42     VAR9=CROSS(N2,VAR8)
---> 43     VARA=(FILTER(VAR9,4))*(T1)
     44     Text3= VARA

/usr/local/lib/python3.6/site-packages/QUANTAXIS/QAIndicator/base.py in FILTER(COND, N)
    138     afterfilter = needfilter.diff().apply(lambda x: False if x < N else True)
--> 139     k1.iloc[afterfilter[afterfilter].index] = 2
    140     return k1.apply(lambda x: 1 if x == 2 else 0)

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    188             key = com.apply_if_callable(key, self.obj)
--> 189         indexer = self._get_setitem_indexer(key)
    190         self._setitem_with_indexer(indexer, value)

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _get_setitem_indexer(self, key)
    174         try:
--> 175             return self._convert_to_indexer(key, is_setter=True)
    176         except TypeError as e:

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   2246         try:
-> 2247             self._validate_key(obj, axis)
   2248             return obj

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   2080             if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
-> 2081                 raise IndexError("positional indexers are out-of-bounds")
   2082         else:

IndexError: positional indexers are out-of-bounds

During handling of the above exception, another exception occurred:

IndexError                                Traceback (most recent call last)
<ipython-input-8-7a87aa5e4023> in <module>
----> 1 ind = data.add_func(indx)

/usr/local/lib/python3.6/site-packages/QUANTAXIS/QAData/base_datastruct.py in add_func(self, func, *arg, **kwargs)
   1043         """
   1044 
-> 1045         return self.groupby(level=1, sort=False).apply(func, *arg, **kwargs)
   1046 
   1047     def add_funcx(self, func, *arg, **kwargs):

/usr/local/lib/python3.6/site-packages/pandas/core/groupby/groupby.py in apply(self, func, *args, **kwargs)
    699 
    700                 with _group_selection_context(self):
--> 701                     return self._python_apply_general(f)
    702 
    703         return result

/usr/local/lib/python3.6/site-packages/pandas/core/groupby/groupby.py in _python_apply_general(self, f)
    705     def _python_apply_general(self, f):
    706         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 707                                                    self.axis)
    708 
    709         return self._wrap_applied_output(

/usr/local/lib/python3.6/site-packages/pandas/core/groupby/ops.py in apply(self, f, data, axis)
    188             # group might be modified
    189             group_axes = _get_axes(group)
--> 190             res = f(group)
    191             if not _is_indexed_like(res, group_axes):
    192                 mutated = True

<ipython-input-7-8fdb07b68146> in indx(data)
     41     VAR8=((SMA(MAX(data.close-VAR7,0),N1,1))/(SMA(ABS(data.close-VAR7),N1,1)))*(100)
     42     VAR9=CROSS(N2,VAR8)
---> 43     VARA=(FILTER(VAR9,4))*(T1)
     44     Text3= VARA
     45 

/usr/local/lib/python3.6/site-packages/QUANTAXIS/QAIndicator/base.py in FILTER(COND, N)
    137     needfilter = pd.Series(idx, index=idx)
    138     afterfilter = needfilter.diff().apply(lambda x: False if x < N else True)
--> 139     k1.iloc[afterfilter[afterfilter].index] = 2
    140     return k1.apply(lambda x: 1 if x == 2 else 0)
    141 

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    187         else:
    188             key = com.apply_if_callable(key, self.obj)
--> 189         indexer = self._get_setitem_indexer(key)
    190         self._setitem_with_indexer(indexer, value)
    191 

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _get_setitem_indexer(self, key)
    173 
    174         try:
--> 175             return self._convert_to_indexer(key, is_setter=True)
    176         except TypeError as e:
    177 

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   2245 
   2246         try:
-> 2247             self._validate_key(obj, axis)
   2248             return obj
   2249         except ValueError:

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   2079 
   2080             if len(arr) and (arr.max() >= len_axis or arr.min() < -len_axis):
-> 2081                 raise IndexError("positional indexers are out-of-bounds")
   2082         else:
   2083             raise ValueError("Can only index by location with "

IndexError: positional indexers are out-of-bounds

ind.tail(25)
1 回复

有可能是没数据 这个得做个try的处理

回到顶部