量子化取引実戦の市場価値中性化選株
3097 ワード
pandas、numpyなど、当社のプラットフォームでサポートされているサードパーティ製pythonモジュールを自分でimportできます.
1.市場価値と市場純率因子データの取得
因子:極値、標準化、中性化処理
2.選択した株式プール(方向の重みによる)
市場純率の小さい株
from sklearn.linear_model import LinearRegression
この方法では任意の初期化ロジックを記述する.contextオブジェクトは、アルゴリズムポリシーの任意の方法の間で伝達されます.
def init(context):
def get_data(context, bar_dict):
def treat_data(context):
before_tradingこの関数は、毎日のポリシー取引が開始される前に呼び出され、当日に1回のみ呼び出されます.
def before_trading(context):
選択した証券のデータ更新は、日または分の履歴データスライスやリアルタイムデータスライス更新などの論理をトリガーします.
def handle_bar(context, bar_dict):
after_trading関数は毎日取引が終了した後に呼び出され、当日に1回しか呼び出されません.
def after_trading(context):
def trade(context, bar_dict):
ぜったいへんさ
import numpy as npdef mad(factor):
標準化
def stand(factor):
1.市場価値と市場純率因子データの取得
因子:極値、標準化、中性化処理
2.選択した株式プール(方向の重みによる)
市場純率の小さい株
from sklearn.linear_model import LinearRegression
この方法では任意の初期化ロジックを記述する.contextオブジェクトは、アルゴリズムポリシーの任意の方法の間で伝達されます.
def init(context):
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この関数は、毎日のポリシー取引が開始される前に呼び出され、当日に1回のみ呼び出されます.
def before_trading(context):
pass
選択した証券のデータ更新は、日または分の履歴データスライスやリアルタイムデータスライス更新などの論理をトリガーします.
def handle_bar(context, bar_dict):
# TODO: !
pass
after_trading関数は毎日取引が終了した後に呼び出され、当日に1回しか呼び出されません.
def after_trading(context):
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 npdef mad(factor):
"""
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):
"""
"""
mean = factor.mean()
std = factor.std()
return (factor - mean) / std