# Demo entry 6415436

Submitted by 赵剑辉 on May 25, 2017 at 15:40
Language: Python 3. Code size: 4.1 kB.

```import numpy as np
import datetime

def initialize(account):

set_benchmark('000300.SH')
set_margin_rate(40,40) #设置股指期货 佣金比例 做多/做空
account.days = 0
# 参数设置
account.holxMax = 50 #取前n只股票
# 动量因子计算
def get_momentum(stock,tdate):
df_one = get_price(stock,None,tdate,'20d',['close'],skip_paused=None,bar_count=3)
momentum = 0
momentum = (df_one.close[-1]/df_one.close[-2]-1.0)+(df_one.close[-2]/df_one.close[-3]-1.0)

return momentum
# 非流动性因子计算
def get_ILLIQ(stock,tdate):
df_one = get_price(stock,None,tdate,'1d',['close','volume'],skip_paused=True,bar_count=5+1)
df_one['roc'] = abs(df_one.close/df_one.close.shift(1)-1.0)
df_one['ILLIQ'] = df_one.roc/df_one.volume
df_one = df_one.dropna()
if len(df_one)==0:
return 0
ILLIQ = df_one.ILLIQ.values.mean()

return ILLIQ
def handle_data(account,data):

if account.days%5==0:
tdate = get_datetime().strftime("%Y%m%d") #当天日期
today_datetime = datetime.datetime.strptime(tdate,"%Y%m%d")
# 获取所有股票
stocks = get_all_securities('stock',tdate)
stocks = stocks[(today_datetime- stocks.start_date) > datetime.timedelta(60)].index.values # 排除新股
value = data.current(stocks)
# 排序停牌股票
stocks = [stock for stock in stocks if value[stock].is_paused == 0]
log.info(len(stocks))
# 排除开盘涨跌停
stocks = [stock for stock in stocks if value[stock].open!= value[stock].high_limit\
and value[stock].open!= value[stock].low_limit]
# # 排除st
# stocks = [stock for stock in stocks if value[stock].is_st == 0]

# 投资活动产生的现金流量净额
q = query(
cashflow.symbol,
cashflow.net_cashflows_from_investing_act
).filter(
cashflow.symbol.in_(stocks)
)
dfgb_one_periods = get_fundamentals(q,tdate)
dfgb_one_periods = dfgb_one_periods.dropna()
stocks = dfgb_one_periods.cashflow_symbol.values        #更新股票池

# 因子列
momentum_array = np.zeros(len(dfgb_one_periods))
ILLIQ_array = np.zeros(len(dfgb_one_periods))
# 遍历所有股票 算因子
ii = 0
for stock in stocks:
momentum = get_momentum(stock,tdate)
ILLIQ = get_ILLIQ(stock,tdate)

momentum_array[ii] = momentum
ILLIQ_array[ii] = ILLIQ
ii+=1
dfgb_one_periods['momentum'] = momentum_array # 动量因子
dfgb_one_periods['ILLIQ'] = ILLIQ_array # 流动性因子

# 删除无用数据
dfgb_one_periods = dfgb_one_periods[dfgb_one_periods.ILLIQ!=0] #ILLIQ 无数据的
dfgb_one_periods = dfgb_one_periods[dfgb_one_periods.momentum!=0] #momentum 无数据的
dfgb_one_periods = dfgb_one_periods.dropna()

scores = range(len(dfgb_one_periods))
#排序 打分
dfgb_one_periods = dfgb_one_periods.sort_values(by='momentum',ascending=0) #降序
dfgb_one_periods['s1'] = scores
dfgb_one_periods = dfgb_one_periods.sort_values(by='cashflow_net_cashflows_from_investing_act') #升序
dfgb_one_periods['s2'] = scores
dfgb_one_periods = dfgb_one_periods.sort_values(by='ILLIQ') #升序
dfgb_one_periods['s3'] = scores
dfgb_one_periods['scores'] = dfgb_one_periods.s1.values*0.00608+dfgb_one_periods.s2.values*0.00294+\
dfgb_one_periods.s2.values*0.00533
dfgb_one_periods = dfgb_one_periods.sort_values(by='scores') #升序

stocks = dfgb_one_periods.cashflow_symbol.values[-account.holxMax:]
#log.info(dfgb_one_periods)

#卖掉不在股票池的股票
sell_stocks = []
for bar in account.positions.keys():
if bar not in stocks:
sell_stocks.append(bar)
for bar in sell_stocks:
order_target(bar,0)
#买入选出且并未持仓的股票
cash = account.portfolio_value/account.holxMax
for bar in stocks:
if bar not in account.positions.keys():
order_target_value(bar,cash)
account.days+=1
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.