Last active
December 8, 2018 08:22
-
-
Save kozyszoo/a627ca99263420aae87f663f9ec1dfb1 to your computer and use it in GitHub Desktop.
日本株システムトレードプラットフォーム QuantX 上で RSI による売買アルゴリズムを開発する ref: https://qiita.com/kozzy/items/06692c4908659f3ca26c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import talib as ta | |
import numpy as np | |
def initialize(ctx): | |
# 設定 | |
ctx.logger.debug("initialize() called") | |
ctx.configure( | |
channels={ # 利用チャンネル | |
"jp.stock": { | |
"symbols": [ | |
"jp.stock.1305", | |
"jp.stock.9984", | |
"jp.stock.9983", | |
"jp.stock.7201", | |
"jp.stock.9201", | |
"jp.stock.9202", | |
"jp.stock.7203" | |
], | |
"columns": [ | |
#"open_price_adj", # 始値(株式分割調整後) | |
#"high_price_adj", # 高値(株式分割調整後) | |
#"low_price_adj", # 安値(株式分割調整後) | |
#"volume_adj", # 出来高 | |
#"txn_volume", # 売買代金 | |
"close_price", # 終値 | |
"close_price_adj", # 終値(株式分割調整後) | |
] | |
} | |
} | |
) | |
def _my_signal(data): | |
cp = data["close_price_adj"].fillna(method="ffill") | |
rsi9 = pd.DataFrame(data=0,columns=[], index=cp.index) | |
for (sym,val) in cp.items(): | |
rsi9[sym] = ta.RSI(cp[sym].values.astype(np.double), timeperiod=9) | |
return { | |
"rsi9": rsi9, | |
"buy:sig": rsi9 < 20, | |
"sell:sig": rsi9 > 80, | |
} | |
# シグナル登録 | |
ctx.regist_signal("my_signal", _my_signal) | |
def handle_signals(ctx, date, current): | |
''' | |
current: pd.DataFrame | |
''' | |
done_syms = set([]) | |
buy = current["buy:sig"].dropna() | |
sell = current["sell:sig"].dropna() | |
df_buy = buy[buy > 0] | |
for (sym,val) in df_buy.items(): | |
sec = ctx.getSecurity(sym) | |
sec.order(sec.unit() * 1, comment="SIGNAL BUY") | |
#ctx.logger.debug("BUY: %s, %f" % (sec.code(), val)) | |
df_sell = sell[sell > 0] | |
for (sym,val) in df_sell.items(): | |
sec = ctx.getSecurity(sym) | |
sec.order_target_percent(0, comment="SIGNAL SELL") | |
#ctx.logger.debug("SELL: %s, %f" % (sec.code(), val)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cp = data["close_price_adj"].fillna(method="ffill") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (sym,val) in cp.items(): | |
rsi9[sym] = ta.RSI(cp[sym].values.astype(np.double), timeperiod=9) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return { | |
"rsi9": rsi9, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rsi9 = current["rsi9"].dropna() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_buy = rsi9[rsi9 < 20] | |
df_sell = rsi9[rsi9 > 80] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (sym,val) in df_buy.items(): | |
sec = ctx.getSecurity(sym) | |
sec.order(sec.unit() * 1, comment="SIGNAL BUY") | |
# ctx.logger.debug("BUY: %s, %f" % (sec.code(), val)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment