from sklearn.linear_model import LinearRegressionhtml
def init(context):python
scheduler.run_weekly(get_data, tradingday=1) scheduler.run_weekly(trade, tradingday=1)
def get_data(context, bar_dict):算法
# 查询两个因子的整数结果 q = query( fundamentals.eod_derivative_indicator.pb_ratio, fundamentals.eod_derivative_indicator.market_cap ).order_by( fundamentals.eod_derivative_indicator.pb_ratio ) fund = get_fundamentals(q) # 转置 context.fund = fund.T # 查看fund格式 # logger.info(fund.T) # 进行因子数据的处理, 去极值, 标准化 treat_data(context) # 利用市净率进行选股 [PayPal](https://www.gendan5.com/wallet/PayPal.html)(市净率小的表现好) context.stock_list = context.fund["pb_ratio"][ context.fund["pb_ratio"] <= context.fund["pb_ratio"].quantile(0.05) # 取前5% ].index # 调试输出 logger.info(context.stock_list) logger.info(context.stock_list.shape)
def treat_data(context):函数
""" 因子数据的处理逻辑 """ # 去除NaN context.fund = context.fund.dropna() # 对市净率去极极值标准化 context.fund["pb_ratio"] = mad(context.fund["pb_ratio"]) context.fund["pb_ratio"] = stand(context.fund["pb_ratio"]) # 调试输出 logger.info(context.fund.shape) # 选股的处理, 对市净率进行市值中性化 # 特征值: 市值 # 目标值: 市净率因子 x = context.fund["market_cap"].values.reshape(-1, 1) y = context.fund["pb_ratio"] # 创建线性回归, 中性化处理 lr = LinearRegression() lr.fit(x, y) y_predict = lr.predict(x) # 去除残差 context.fund["pb_ratio"] = y - y_predict
def before_trading(context):调试
pass
def handle_bar(context, bar_dict):code
# TODO: 开始编写你的算法吧! pass
def after_trading(context):htm
pass
def trade(context, bar_dict):对象
# ----------------卖出---------------- for stock in context.portfolio.positions.keys(): # 判断是否还在股票池 if stock not in context.stock_list: order_target_percent(stock, 0) # ----------------买入----------------- weight = 1.0 / len(context.stock_list) for stock in context.stock_list: order_target_percent(stock, weight)
import numpy as np
def mad(factor):get
""" 3倍中位数去极值 """ # 求出因子值的中位数 med = np.median(factor) # 求出因子值与中位数的差值, 进行绝对值 mad = np.median(abs(factor - med)) # 定义几倍的中位数上下限 high = med + (3 * 1.4826 * mad) low = med - (3 * 1.4826 * mad) # 替换上下限之外的值 factor = np.where(factor > high, high, factor) factor = np.where(factor < low, low, factor) return factor
def stand(factor):pandas
""" 自实现标准化 """ mean = factor.mean() std = factor.std() return (factor - mean) / std