def handle_data(self, data): if self.target_shares == 0: assert 0 not in self.portfolio.positions self.order(self.sid(0), 10) self.target_shares = 10 return else: assert self.portfolio.positions[0].amount == \ self.target_shares, "Orders not filled immediately." assert self.portfolio.positions[0].last_sale_price == \ data.current(sid(0), "price"), \ "Orders not filled at current price." self.order_percent(self.sid(0), .001) if isinstance(self.sid(0), Equity): price = data.current(sid(0), "price") new_shares = (.001 * self.portfolio.portfolio_value) / price elif isinstance(self.sid(0), Future): new_shares = (.001 * self.portfolio.portfolio_value) / \ (data.current(sid(0), "price") * self.sid(0).contract_multiplier) new_shares = int(round_if_near_integer(new_shares)) self.target_shares += new_shares
def round_order(self, amount: float) -> float: return int(round_if_near_integer(amount))
def round_order(self, amount: float) -> float: return int(round_if_near_integer( amount / self._precision)) * self._precision
def round_order(amount): if pd.isna(amount): return 0 return int(round_if_near_integer(amount))