同样的代码复制一遍就可以用了,但是原代码就用不了,求助!

策略分享
新手专区
标签: #<Tag:0x00007f8c663a73d0> #<Tag:0x00007f8c663a7290>

(siyishenqing) #1
克隆策略

参数

In [6]:
start_date = '2016-01-01'
end_date = '2017-09-14'
instrument = D.instruments(start_date=start_date, end_date=end_date)

1.基本信息层面

获取指数成分股列表

In [7]:
# 沪深300指数成分
df = D.history_data(instruments, start_date, end_date, ['in_csi300'])
instruments = list(set(df[df['in_csi300']==1]['instrument']))
print('沪深300指数成分股预览10只股票:', instruments[:10])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-b7fd4b5d5879> in <module>()
      1 # 沪深300指数成分
----> 2 df = D.history_data(instruments, start_date, end_date, ['in_csi300'])
      3 instruments = list(set(df[df['in_csi300']==1]['instrument']))
      4 print('沪深300指数成分股预览10只股票:', instruments[:10])

NameError: name 'instruments' is not defined

获取某个行业股票列表

In [ ]:
# (举例)获取国防军工行业股票列表
df = D.history_data(instrument, start_date, end_date, ['industry_sw_level1'])
instruments = list(set(df[df['industry_sw_level1'] == 650000]['instrument']))
D.history_data(instruments, '2017-07-27', '2017-07-27', ['company_name']).head() 

获取某个概念、板块的股票列表

In [ ]:
df = D.history_data(instrument, '2017-08-23', '2017-08-23', ['concept']).dropna()   # 'concept' 是股票的概念字段
df['is_ai'] = df['concept'].map(lambda x: '人工智能' in x)  # 以人工智能为例,找到相关概念股票
st = list(df[df['is_ai'] == True]['instrument'])
D.history_data(st,'2017-08-23','2017-08-23',['name', 'concept']).head()

获取每日创业板次新股股票列表

次新股的定义为上市90天以内

In [ ]:
df = D.history_data(instrument, start_date, end_date, ['list_date', 'list_board'])
from datetime import timedelta
df[(df['list_board']=='创业板')&(df['date']<=df['list_date']+timedelta(days=20))].head(10)

2.财务信息层面

选出满足以下条件的股票

  • 市盈率小于15倍
  • 市净率小于1.5倍
In [ ]:
 # 获取市盈率、市净率、成交额数据
history_data = D.history_data(instrument, start_date=start_date, end_date= end_date, fields=[ 'pb_lf', 'pe_ttm','amount'])
result = history_data[(history_data['pb_lf'] < 1.5)
                                       & (history_data['pe_ttm'] < 15) 
                                       & (history_data['amount'] > 0) 
                                       & (history_data['pb_lf'] > 0)
                                       & (history_data['pe_ttm'] > 0)]
daily_buy_stock = result.groupby('date').apply(lambda df:list(df.sort_values(['pe_ttm','pb_lf']).instrument)[:30])
daily_buy_stock.head()

选出满足以下条件的股票

  • 市净率小于8
  • 市销率大于0.4
  • 资产周转率大于0.4
  • 市值最小的10只股票
In [ ]:
stock_num = 10
# 通过history_data接口获取历史数据
history_data = D.history_data(instrument, start_date, end_date, ['pb_lf', 'ps_ttm', 'market_cap', 'amount'])
# 通过feature接口获取财务数据
financial = D.features(instrument, start_date=start_date, end_date=end_date,
                        fields=['fs_operating_revenue_ttm_0','fs_current_assets_0','fs_non_current_assets_0']) 
# 总资产=流动资产+非流动资产
financial['total_assets'] = financial['fs_current_assets_0'] + financial['fs_non_current_assets_0']
# 资产周转率=营业收入/总资产
financial['asset_turnover'] = financial['fs_operating_revenue_ttm_0'] / financial['total_assets']
financial_data = financial[['date','instrument','asset_turnover']] 
# 两个DataFrame:历史数据、财务数据 合并
result = history_data.merge(financial_data, on=['date','instrument'], how='outer')  
# 按照选股法则选出股票
daily_buy_stock = result.groupby('date').apply(lambda df:list(df[(df['ps_ttm']>0.4) & (df['pb_lf']<8) & (df['asset_turnover']>0.4)
                                         & (df['amount']>100)].sort_values('market_cap')['instrument'])[:stock_num])
daily_buy_stock.head()

3.技术指标层面

选出满足以下条件的股票

  • 7日均线上穿63日均线
  • 收盘价突破ATR上轨
In [1]:
df= D.history_data(instrument, start_date, end_date, ['open', 'high', 'low', 'close'])

import talib as ta
from numpy import float as f

def seek_stocks(df):
    df['ma_7'] = ta.SMA(df.close.map(f).values, 7)
    df['ma_63'] = ta.SMA(df.close.map(f).values, 63)
    try:
        df['atr'] = ta.ATR(df.high.map(f).values, df.low.map(f).values, df.close.map(f).values, 14)
    except Exception as e:
        df['atr'] = np.nan
    df['upperline'] = df.close.rolling(14).mean() + df['atr'] 
    return df[(df['ma_7']>df['ma_63'])&(df['close']>=df['upperline'])].drop('instrument', axis=1)

result = df.groupby('instrument').apply(seek_stocks).reset_index()
daily_buy_stock = result.groupby('date').apply(lambda df:list(df['instrument']))
daily_buy_stock.head()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-91c78ab8af83> in <module>()
----> 1 df= D.history_data(instrument, start_date, end_date, ['open', 'high', 'low', 'close'])
      2 
      3 import talib as ta
      4 from numpy import float as f
      5 

NameError: name 'instrument' is not defined

选出满足以下条件的股票

  • 股价创60日最高点
  • 3日线上穿5日均线,5日均线上穿10日均线
  • 当日成交额是昨日成交额的1.4倍
  • macd柱状图处于红色区域
In [ ]:
df =D.history_data(instrument, start_date, end_date, ['close', 'amount','high'])

def seek_stocks(df):
    df['highest_60'] = df['high'].rolling(60).max()  # 计算60天最高点
    df['ma3_cross_ma5'] = df['close'].rolling(3).mean() - df['close'].rolling(5).mean()  > 0  # 3日均线上穿5日均线
    df['ma5_cross_ma10'] = df['close'].rolling(5).mean() - df['close'].rolling(10).mean()  > 0  # 5日均线上穿10日均线
    df['amount_cond'] = df['amount'] / df['amount'].shift(1) - 1 >= 0.4   # 当日成交量比前一日成交量大40%
    prices = df['close'].map(np.float)  # 转化成float格式
    # macd:diff线 信号线:dea 柱状图:diff-dea
    macd, signal, hist = ta.MACD(np.array(prices), 12, 26, 9)  # 计算macd各个指标
    df['is_highest'] = df['close'] == df['highest_60']  # 该列是布尔型变量,表明是否是60日最高点
    df['hist_is_red'] =  hist > 0   # macd柱状图是否在红色区域
    return df
  
managed_df = df.groupby('instrument').apply(seek_stocks).reset_index()
result= managed_df[
    (managed_df['hist_is_red'])&   # macd在红色区域
    (managed_df['is_highest'])&   # 是60日最高点
    (managed_df['ma3_cross_ma5'])&  # 3日均线上穿5日均线
    (managed_df['ma5_cross_ma10'])&   # 5日均线上穿10日均线
    (managed_df['amount_cond'])]   # 满足成交量条件
    
# 整理出每日符合买入条件的列表               
daily_buy_stock = result.groupby('date').apply(lambda df:list(df.instrument)).reset_index().rename(columns={0:'stocks'})
daily_buy_stock.head()

(ZjFy) #2

你看这个错误提示信息吧

NameError: name ‘instruments’ is not defined

代表 ‘instruments’ 这个变量没有被定义,你定义的是instrument 少一个s

start_date = '2016-01-01’
end_date = '2017-09-14’
instrument = D.instruments(start_date=start_date, end_date=end_date)


(大胡子) #3


从这个代码和报错,问题很明显啊,D.history_data(instruments…)中需要instruments这个变量,但是这个变量并没有定义,如果你没发现,那是因为你可能粗心了,将

instrument = D.instruments(start_date=start_date............)

这段代码的instrument看做了instruments,这分明是两个不同的变量啊~~~

你觉得呢?