量化交易 實戰之市值中性化選股

能夠本身import咱們平臺支持的第三方python模塊,好比pandas、numpy等。

1. 獲取市值和市淨率因子數據

因子: 極值, 標準化, 中性化處理

2. 選定股票池 (根據方向權重)

市淨率小的某些股票

from sklearn.linear_model import LinearRegressionhtml

在這個方法中編寫任何的初始化邏輯。context對象將會在你的算法策略的任何方法之間作傳遞。

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

before_trading此函數會在天天策略交易開始前被調用,當天只會被調用一次

def before_trading(context):調試

pass

你選擇的證券的數據更新將會觸發此段邏輯,例如日或分鐘歷史數據切片或者是實時數據切片更新

def handle_bar(context, bar_dict):code

# TODO: 開始編寫你的算法吧!
pass

after_trading函數會在天天交易結束後被調用,當天只會被調用一次

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
相關文章
相關標籤/搜索