上値抵抗性突破とは
過去N日間の株価終値の最高値を、今日の終値が突破した時、さらに上昇することを期待して買いポジションを持つストラテジーです。
とても簡単なアルゴリズムなので、コードも簡単に書けます。
シグナル
過去90日間の終値の最高値を今日の終値が突破した場合、ロングポジションを持つ。
手仕舞い
益出し:+5%
損切り:ー3%
コード
(昨日の勉強会で知ったのですが、QuantXのコードは公開+Cloneできるんですね。ログイン後、このリンクを辿って、上にあるクローンボタンをおしてクローンしてください。)
clone : https://factory.quantx.io/developer/2729d13d204b4f2db1c08385ad5d13b0
############################################################################
# 上値抵抗線突破
############################################################################
def initialize(ctx):
ctx.target = 0.10
ctx.period = 90
ctx.loss_cut = -0.03
ctx.plofit = 0.05
ctx.codes = [1605, 1925, 2503, 4519, 4911, 6301, 6752, 7741, 8001 ]
ctx.symbol_list = ["jp.stock.{}".format(code) for code in ctx.codes]
ctx.configure(
channels={
"jp.stock": {
"symbols": ctx.symbol_list,
"columns": ["close_price_adj", # 終値(株式分割調整後)
]}})
def _RESISTANCE_LINE(data):
df_close = data["close_price_adj"].fillna(method='ffill')
df_Max_close = df_close.rolling(window=ctx.period).max()
buy_sig = df_Max_close == df_close
# memo 参照
ctx.logger.info(df_Max_close.tail())
return {
"buy:sig": buy_sig,
"max": df_Max_close,
}
# シグナル登録
ctx.regist_signal("RESISTANCE_LINE", _RESISTANCE_LINE)
def handle_signals(ctx, date, current):
# ポジションクローズ
for (sym,val) in ctx.portfolio.positions.items():
rtn = val["returns"]
if rtn < ctx.loss_cut:
sec = ctx.getSecurity(sym)
# memo 参照
sec.order_target_percent(0, comment="損切り {:.2%}".format(rtn))
elif rtn > ctx.plofit:
sec = ctx.getSecurity(sym)
sec.order_target_percent(0, comment="益出し {:.2%}".format(rtn))
df = current.copy()
df = df[df["buy:sig"]]
if not df.empty:
for (sym, val) in df.iterrows():
sec = ctx.getSecurity(sym)
sec.order_target_percent(ctx.target, comment= "シグナル買")
結果
memo
log と print
QuantX では print
は使用不可です。
出力したい場合は、ctx.logger.info
(レベルは debut/info/warn/error/critical ) が使えます。
ログは、IDE画面の下に出るログ画面に表示されます。
order_target_percent の comment
上のコードの sec.order_target_percent(0, comment="損切り {:.2%}".format(rtn))
で使っているcomment はどこに出力されるの?という質問を受けました。
バックテストの結果画面の、個別銘柄詳細タブを開くと、各個別銘柄の各取引を確認できます。そこにコメントという欄がありそこで確認できます。
感想
QuantX ユーザーのための pandas
勉強会をしたほうがいいなーと思っています。
免責注意事項
- このコードに基づき投資した結果、損害が発生しても,一切責任を持ちません.
- このコードが正しく機能する保証は一切致しません.
- このアルゴリズムを勧めているわけではありません.あくまで QuantX / Python のサンプルコードとして掲載しているだけです.