Last active
August 15, 2018 09:57
-
-
Save kozyszoo/4bc73188109daa9cc5a64ccd727628dd to your computer and use it in GitHub Desktop.
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
############################################################################ | |
# Sample Algorithm | |
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 | |
} | |
# シグナル登録 | |
ctx.regist_signal("my_signal", _my_signal) | |
def handle_signals(ctx, date, current): | |
''' | |
current: pd.DataFrame | |
''' | |
done_syms = set([]) | |
rsi9 = current["rsi9"].dropna() | |
df_buy = rsi9[rsi9 < 20] | |
df_sell = rsi9[rsi9 > 80] | |
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)) | |
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)) | |
# for (sym,val) in ctx.portfolio.positions.items(): | |
# returns = val["returns"] | |
# if returns < -0.03: | |
# sec = ctx.getSecurity(sym) | |
# #sec.order(-val["amount"], comment="損切り(%f)" % returns) | |
# done_syms.add(sym) | |
# elif returns > 0.05: | |
# sec = ctx.getSecurity(sym) | |
# #sec.order(-val["amount"], comment="利益確定売(%f)" % returns) | |
# done_syms.add(sym) | |
#buy = current["buy:sig"].dropna() | |
#for (sym,val) in buy.items(): | |
# if sym in done_syms: | |
# continue | |
# | |
# sec = ctx.getSecurity(sym) | |
# #sec.order(sec.unit() * 1, comment="SIGNAL BUY") | |
# #ctx.logger.debug("BUY: %s, %f" % (sec.code(), val)) | |
# pass | |
#sell = current["sell:sig"].dropna() | |
#for (sym,val) in sell.items(): | |
# if sym in done_syms: | |
# continue | |
# | |
# sec = ctx.getSecurity(sym) | |
# #sec.order(sec.unit() * -1, comment="SIGNAL SELL") | |
# #ctx.logger.debug("SELL: %s, %f" % (sec.code(), val)) | |
# pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment