factor_new = factor_new.replace(-np.inf, np.nan)
factor_new = factor_new.replace(np.inf, np.nan)
factor_new = factor_new.dropna()
data_minute = data_minute.reset_index()
data_minute = data_minute.set_index(['TradingDay','TradingTime'])
data_minute['Rank'] = data_minute['TurnoverMoney'].groupby(['TradingDay','TradingTime']).rank()
data_minute = data_minute.reset_index()
data_minute = data_minute.set_index(['SecuCode','TradingDay'])
回测框架
for Period in range(len(date)-20):
print(date[T])
T = 1 + Period
# curpool:T-1天所选股票池,T日买入,已经去掉T日停牌/一字板无法买进的股票;now: T日期,num:股票池中的实时买入股票的数量
# emptypool:股票池中可以填入的空位
try: a = stock_tp.loc[date[T]].index
except KeyError: a = {}
try: b = YZB.loc[date[T]].index
except KeyError: b = {}
curpool = set(stock_choose.loc[date[T-1]]['code'].values) - set(a) - set(b)
emptypool = daily_stock.loc[date[T]][daily_stock.loc[date[T]]==0][0:len(curpool)]
curpool = list(curpool)[0:len(emptypool)]
for i in range(len(curpool)):
stock_sql = daily.loc[curpool[i]].loc[date[T]:].iloc[0:3*date_hold]
stock_date = stock_sql.index
daily_ratio.loc[date[T]][emptypool.index[i]] = stock_sql.iloc[0]['OpenProfit']
d = 1
while d<date_hold-1 or stock_sql.iloc[d].IfCloseDt==1 or stock_sql.iloc[d].IfCloseZt==1:
daily_ratio.loc[stock_date[d]][emptypool.index[i]] = stock_sql.iloc[d]['CloseProfit']
d+=1
daily_ratio.loc[stock_date[d]][emptypool.index[i]] = stock_sql.iloc[d]['CloseProfit'] - fee
daily_stock.loc[date[T]:stock_date[d]][emptypool.index[i]] = curpool[i]
网友评论