昨日の記事を受けて、動くものを作り直した。
how-to-make-stock-trading-system.dogwood008.com
必要な部分のみ抜粋し、その内容を下記の通り掲載する。
(前略) def _size(self, unit_price: float, is_buy: bool) -> Optional[int]: ''' Params ------------------ unit_price : flat ある銘柄の価格 is_buy : bool 買い注文なら True, 売り注文なら False ''' if not is_buy: # 売り注文なら、全量を指定する return None min_size = 100 max_size = 2000 for i in range(int(min_size / 100), int(max_size / 100 + 1)): unit = i * 100 order_value = unit_price * unit if order_value < self.p.min_order_price: # 安すぎる場合、購入量を増やす continue elif self.p.min_order_price <= order_value <= self.p.max_order_price: return unit else: # 高すぎて買えない場合、買わない return 0 (中略) def next_open(self): # 当日の始値を見るためにチートする open_today = self._dataopen[0] high_today = self._datahigh[0] low_today = self._datalow[0] close_yesterday = self._dataclose[-1] if self._is_to_buy(open_today, close_yesterday, high_today): size = self._size(unit_price=open_today, is_buy=True) self._info(lambda: 'BUY CREATE @{price:.2f}, #{unit:4d} (open_today, close_yesterday)=({open_today:f}, {close_yesterday:f})'.format( # FIXME: fix unit size price=open_today, unit=-100, open_today=open_today, close_yesterday=close_yesterday) ) self._debug(lambda: '(o, h, l, c) = ({o:}, {h:}, {l:}, {c:})'.format( o=self._dataopen[0], h=self._datahigh[0], l=self._datalow[0], c=self._dataclose[0])) self.buy(size=size, price=open_today, exectype=bt.Order.Limit, valid=bt.Order.DAY) elif self._is_to_close(open_today, close_yesterday, low_today): size = self._size(unit_price=open_today, is_buy=False) self._info(lambda: 'CLOSE (SELL) CREATE @{price:.2f}, #all (open_today, close_yesterday)=({open_today:f}, {close_yesterday:f})'.format( price=open_today, open_today=open_today, close_yesterday=close_yesterday) ) self.close(size=size, price=open_today, exectype=bt.Order.Limit, valid=bt.Order.DAY) (後略)