CH9选股条件

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

(njchenxin) #1

克隆策略
In [ ]:
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

df=D.history_data(D.instruments(),start_date='2016-09-01', end_date='2017-05-12', 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)
#将9个月的收盘价放到一起
month_close_df=month_df.groupby('instrument').apply(lambda df:list(df.close))
print(month_close_df)#类型是Series,下方红方块区域
#想选出9个月的最高收盘价除以17年5月12日收盘价>1.45的股票
for instrument in month_close_df:#ok
    if max(instrument)/(instrument[-1])> 1.45:#ok
        print(month_close_df.index)#问题无法关联到股票代码,因为这个时候‘instrument’已经变成index了。如何才能关联到股票代码

这个选股方法是将连续9个月的收盘价重的最高的收盘价除以最近的收盘价大于一定阈值的股票

(iQuant) #2

@njchenxin 下面的代码应该解决了你的这个问题。

补充一下:由于最近行情不好,满足这个条件的股票很多。

克隆策略
In [31]:
import numpy as np
import pandas as pd
from pandas import Series, DataFrame

df=D.history_data(D.instruments(),start_date='2016-09-01', end_date='2017-05-12', 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
In [32]:
# 使用这个函数来寻找你所需要条件的股票
def stock_seeked(df):
    monthly_close = list(df['close'])
    return np.max(monthly_close)/monthly_close[-1] >= 1.15
In [33]:
# 计算月K数据
month_df = df.groupby('instrument').apply(resample)
month_df.reset_index(drop=True, inplace=True)
# 满足一定条件的股票 
result = month_df.groupby('instrument').apply(stock_seeked)
In [38]:
# 输出股票列表
stock_list = result[result == True].index
stock_list
Out[38]:
Index(['000002.SZA', '000004.SZA', '000005.SZA', '000006.SZA', '000008.SZA',
       '000009.SZA', '000010.SZA', '000012.SZA', '000014.SZA', '000017.SZA',
       ...
       '603955.SHA', '603959.SHA', '603966.SHA', '603987.SHA', '603990.SHA',
       '603991.SHA', '603993.SHA', '603996.SHA', '603998.SHA', '603999.SHA'],
      dtype='object', name='instrument', length=2332)