Tradeクラスの実装例 ①移動平均線の交差
バックテストで使用するTradeクラスの実装例として、移動平均線の交差を使用した例を挙げておきます。
from output.trade.trade_base import TradeBase, Reason
from signals.indicator import Indicator as Ind
from signals.price_util import PriceUtil as PUtil
class Trade(TradeBase):
# 移動平均計算日数
SHORT = 5
LONG = 10
def __init__(self, price_row, price_values, trade_values, investment):
super().__init__(price_row, price_values, trade_values, investment)
# 買い可能判定(売買)
def is_buy_signal(self):
can_buy = False
if not self.trade.is_hold:
# 短期移動平均が長期移動平均を下回る。当日終値が短期移動平均を下回っている。
close_list = self.price.close_list
price_days = self.price.days
short_period = self.SHORT
long_period = self.LONG
back_days = 1
if price_days <= (long_period + back_days):
return False
# 前日
ytd_short_ma = Ind.ma(close_list, price_days, short_period, back_days)
ytd_long_ma = Ind.ma(close_list, price_days, long_period, back_days)
# 本日
back_days = 0
today_short_ma = Ind.ma(close_list, price_days, short_period, back_days)
today_long_ma = Ind.ma(close_list, price_days, long_period, back_days)
today_close = self.price.close
if ytd_short_ma > ytd_long_ma:
if today_short_ma < today_long_ma:
if today_close < today_short_ma:
can_buy = True
return can_buy
この記事が気に入ったらサポートをしてみませんか?