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