调试策略代码运行出bug,大家帮忙看看

策略分享
标签: #<Tag:0x00007f61f3e4c008>

(njchenxin) #1

克隆策略
In [2]:
df=D.history_data(D.instruments(),start_date='2017-01-01', end_date='2017-05-19', fields=['open', 'high', 'low', 'close','list_date'])
df.set_index('date', inplace=True)

def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = '91D'

    year_df = df.resample(period, how='last')
    year_df['open'] = df['open'].resample(period, how='first')
    year_df['high'] = df['high'].resample(period, how='max')
    year_df['low'] = df['low'].resample(period, how='min')
    year_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据(没有交易的周)
    year_df = year_df[year_df.instrument.notnull()]
    year_df.reset_index(inplace=True)
    return year_df

year_df = df.groupby('instrument').apply(resample)
year_df.reset_index(drop=True, inplace=True)




    
In [3]:
year_df = year_df['close'] > (year_df['high']+ year_df['low'])/2
year_df = year_df[year_df.list_date < '2016-01-01']
year_df
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-53aa3d0c117a> in <module>()
      1 year_df = year_df['close'] > (year_df['high']+ year_df['low'])/2
----> 2 year_df = year_df[year_df.list_date < '2016-01-01']
      3 year_df

AttributeError: 'Series' object has no attribute 'list_date'
In [ ]:
df=D.history_data(D.instruments(),start_date='2017-04-01', end_date='2017-06-30', fields=['open', 'high', 'low', 'close','list_date'])
df.set_index('date', inplace=True)

def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = 'Q'

    quarter_df = df.resample(period, how='last')
    quarter_df['open'] = df['open'].resample(period, how='first')
    quarter_df['high'] = df['high'].resample(period, how='max')
    quarter_df['low'] = df['low'].resample(period, how='min')
    quarter_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据(没有交易的周)
    quarter_df = quarter_df[quarter_df.instrument.notnull()]
    quarter_df.reset_index(inplace=True)
    return quarter_df

quarter_df = df.groupby('instrument').apply(resample)
quarter_df.reset_index(drop=True, inplace=True)

quarter_df['condi_0'] = quarter_df['high']/quarter_df['low']>1.7
quarter_df = quarter_df[(quarter_df['condi_0'] == True)]
quarter_df = quarter_df[quarter_df.list_date < '2016-01-01']
In [ ]:
df=D.history_data(D.instruments(),start_date='2016-09-01', end_date='2017-05-11', fields=['open', 'high', 'low', 'close','list_date'])
df.set_index('date', inplace=True)

def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = 'M'

    month_df = df.resample(period, how='last')
    month_df['open'] = df['open'].resample(period, how='first')
    month_df['high'] = df['high'].resample(period, how='max')
    month_df['low'] = df['low'].resample(period, how='min')
    month_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据(没有交易的周)
    month_df = month_df[month_df.instrument.notnull()]
    month_df.reset_index(inplace=True)
    return month_df

month_df = df.groupby('instrument').apply(resample)
month_df.reset_index(drop=True, inplace=True)

month_df['condi_0'] = month_df['high']/month_df['low'].shift(0)>1.25
month_df['condi_1'] = month_df['condi_0'].shift(1)
month_df = month_df[(month_df['condi_0'] == True) & month_df['condi_1']]
month_df = month_df[month_df.list_date < '2016-01-01']
month_df = month_df[month_df.date == '2017-05-31']
In [ ]:
for i in list(year_df['instrument']):
    if i in list(quarter_df['instrument']):
        print(i)
In [ ]:
 

有bug帮忙看一下

(小米) #2

主要原因在:year_df = year_df['close'] > (year_df['high']+ year_df['low'])/2 返回的是Series
如果想要返回DataFrame,应该是:year_df = year_df[year_df['close'] > ((year_df['high']+ year_df['low'])/2)]

克隆策略
In [2]:
df=D.history_data(D.instruments(),start_date='2017-01-01', end_date='2017-05-19', fields=['open', 'high', 'low', 'close','list_date'])
df.set_index('date', inplace=True)

def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = '91D'

    year_df = df.resample(period, how='last')
    year_df['open'] = df['open'].resample(period, how='first')
    year_df['high'] = df['high'].resample(period, how='max')
    year_df['low'] = df['low'].resample(period, how='min')
    year_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据(没有交易的周)
    year_df = year_df[year_df.instrument.notnull()]
    year_df.reset_index(inplace=True)
    return year_df

year_df = df.groupby('instrument').apply(resample)
year_df.reset_index(drop=True, inplace=True)

    
In [1]:
# 数据框的条件索引应该采用以下形式
year_df = year_df[year_df['close'] > ((year_df['high']+ year_df['low'])/2)]
year_df = year_df[year_df.list_date < '2016-01-01']
year_df
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-4a1c46b0f7d4> in <module>()
      1 # 数据框的条件索引应该采用以下形式
----> 2 year_df = year_df[year_df['close'] > ((year_df['high']+ year_df['low'])/2)]
      3 year_df = year_df[year_df.list_date < '2016-01-01']
      4 year_df

NameError: name 'year_df' is not defined
In [ ]:
df=D.history_data(D.instruments(),start_date='2017-04-01', end_date='2017-06-30', fields=['open', 'high', 'low', 'close','list_date'])
df.set_index('date', inplace=True)

def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = 'Q'

    quarter_df = df.resample(period, how='last')
    quarter_df['open'] = df['open'].resample(period, how='first')
    quarter_df['high'] = df['high'].resample(period, how='max')
    quarter_df['low'] = df['low'].resample(period, how='min')
    quarter_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据(没有交易的周)
    quarter_df = quarter_df[quarter_df.instrument.notnull()]
    quarter_df.reset_index(inplace=True)
    return quarter_df

quarter_df = df.groupby('instrument').apply(resample)
quarter_df.reset_index(drop=True, inplace=True)

quarter_df['condi_0'] = quarter_df['high']/quarter_df['low']>1.7
quarter_df = quarter_df[(quarter_df['condi_0'] == True)]
quarter_df = quarter_df[quarter_df.list_date < '2016-01-01']
In [ ]:
df=D.history_data(D.instruments(),start_date='2016-09-01', end_date='2017-05-11', fields=['open', 'high', 'low', 'close','list_date'])
df.set_index('date', inplace=True)

def resample(df):
    # https://pandas-docs.github.io/pandas-docs-travis/timeseries.html#offset-aliases
    # 周 W、月 M、季度 Q、10天 10D、2周 2W
    period = 'M'

    month_df = df.resample(period, how='last')
    month_df['open'] = df['open'].resample(period, how='first')
    month_df['high'] = df['high'].resample(period, how='max')
    month_df['low'] = df['low'].resample(period, how='min')
    month_df['close'] = df['close'].resample(period, how='last')
  
    # 去除空的数据(没有交易的周)
    month_df = month_df[month_df.instrument.notnull()]
    month_df.reset_index(inplace=True)
    return month_df

month_df = df.groupby('instrument').apply(resample)
month_df.reset_index(drop=True, inplace=True)

month_df['condi_0'] = month_df['high']/month_df['low'].shift(0)>1.25
month_df['condi_1'] = month_df['condi_0'].shift(1)
month_df = month_df[(month_df['condi_0'] == True) & month_df['condi_1']]
month_df = month_df[month_df.list_date < '2016-01-01']
month_df = month_df[month_df.date == '2017-05-31']
In [ ]:
for i in list(year_df['instrument']):
    if i in list(quarter_df['instrument']):
        print(i)
In [ ]:
 

(njchenxin) #3

谢谢了!!