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 handle_data(self, data): if self.incr == 0: assert 0 not in self.portfolio.positions else: assert self.portfolio.positions[0].amount == \ self.incr, "Orders not filled immediately." assert self.portfolio.positions[0].last_sale_price == \ self.last_price, "Orders was not filled at last price." self.incr += 1 self.order_value(self.sid(0), data.current(sid(0), "price")) self.last_price = data.current(sid(0), "price")
def handle_data_api(context, data): if context.incr == 0: assert 0 not in context.portfolio.positions else: assert context.portfolio.positions[0].amount == \ context.incr, "Orders not filled immediately." assert context.portfolio.positions[0].last_sale_price == \ data.current(sid(0), "price"), \ "Orders not filled at current price." context.incr += 1 order(sid(0), 1) record(incr=context.incr)
def handle_data(self, data): if self.incr == 0: assert 0 not in self.portfolio.positions else: assert self.portfolio.positions[0].amount == \ self.incr, "Orders not filled immediately." assert self.portfolio.positions[0].last_sale_price == \ data.current(sid(0), "price"), \ "Orders not filled at current price." self.incr += 2 multiplier = 2. if isinstance(self.sid(0), Future): multiplier *= self.sid(0).multiplier self.order_value( self.sid(0), data.current(sid(0), "price") * multiplier )
def handle_data(self, data): if self.target_shares == 0: assert 0 not in self.portfolio.positions 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.target_shares = 10 self.order_target(self.sid(0), self.target_shares)
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_target_value(self.sid(0), 20) self.target_shares = np.round(20 / data.current(sid(0), "price")) if isinstance(self.sid(0), Equity): self.target_shares = np.round(20 / data.current(sid(0), "price")) if isinstance(self.sid(0), Future): self.target_shares = np.round( 20 / (data.current(sid(0), "price") * self.sid(0).multiplier))
def handle_data(self, data): if not self.ordered: assert not self.portfolio.positions else: # Since you can't own fractional shares (at least in this # example), we want to make sure that our target amount is # no more than a share's value away from our current # holdings. target_value = self.portfolio.portfolio_value * 0.002 position_value = self.portfolio.positions[0].amount * \ self.sale_price assert abs(target_value - position_value) <= self.sale_price, \ "Orders not filled correctly" assert self.portfolio.positions[0].last_sale_price == \ data.current(sid(0), "price"), \ "Orders not filled at current price." self.sale_price = data.current(sid(0), "price") self._order(sid(0), .002) self.ordered = True
def handle_data(self, data): if self.incr == 0: assert len(self.portfolio.positions.keys()) == 0 method_to_check = getattr(self, self.method_name) method_to_check(self.sid(133), data.current(sid(0), "price"), style=StopLimitOrder(10, 10)) assert len(self.blotter.open_orders[self.sid(133)]) == 1 result = self.blotter.open_orders[self.sid(133)][0] assert result.limit == 10 assert result.stop == 10 self.incr += 1