Created
November 19, 2018 00:48
-
-
Save nrm176/28997ed31355a73e854adab1e8d58033 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.fastperiod = 12 | |
ctx.slowperiod = 26 | |
ctx.signalperiod = 9 | |
ctx.configure( | |
channels={ # 利用チャンネル | |
"jp.stock": { | |
"symbols": [ | |
"jp.stock.1333", | |
], | |
"columns": [ | |
"close_price_adj", # 終値(株式分割調整後) | |
] | |
} | |
} | |
) | |
def _my_signal(data): | |
df = data["close_price_adj"].fillna(method="ffill") | |
d_macd = dict() | |
d_macdsignal = dict() | |
d_macdhist = dict() | |
d_macd_upper_bb, d_macd_mid_bb, d_macd_lower_bb = dict(), dict(), dict() | |
buy_sig = pd.DataFrame(data=0,columns=[], index=df.index) | |
sell_sig = pd.DataFrame(data=0,columns=[], index=df.index) | |
for symbol in data.minor_axis: | |
macd, macdsignal, macdhist = ta.MACD(df[symbol].values.astype(np.double), | |
fastperiod=ctx.fastperiod , slowperiod=ctx.slowperiod, signalperiod=ctx.signalperiod) | |
d_macd[symbol] = macd | |
d_macdsignal[symbol] = macdsignal | |
d_macdhist[symbol] = macdhist | |
for symbol in data.minor_axis: | |
macd_upper_bb, macd_mid_bb, macd_lower_bb = ta.BBANDS(d_macd[symbol], | |
timeperiod=10, | |
nbdevup=2, | |
nbdevdn=2, | |
matype=0) | |
ctx.logger.info(macd_upper_bb) | |
d_macd_upper_bb[symbol] = macd_upper_bb | |
d_macd_mid_bb[symbol] = macd_mid_bb | |
d_macd_lower_bb[symbol] = macd_lower_bb | |
# ctx.logger.info(d_macd[symbol]) | |
# ctx.logger.info(d_macd_upper_bb[symbol]) | |
buy_sig[symbol] = d_macd[symbol] > d_macd_upper_bb[symbol] | |
sell_sig[symbol] = d_macd[symbol] < d_macd_lower_bb[symbol] | |
return { | |
"buy:sig": buy_sig, | |
"sell:sig": sell_sig, | |
} | |
# シグナル登録 | |
ctx.regist_signal("my_signal", _my_signal) | |
def handle_signals(ctx, date, current): | |
''' | |
current: pd.DataFrame | |
''' | |
done_syms = set([]) | |
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() * 10, 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() * -10, 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