def get_qty(self, new_ord_req, market_data, config): if config.partial_fill: if is_buy(new_ord_req): return min(market_data.ask_size, new_ord_req.qty) elif is_sell(new_ord_req): return min(market_data.bid_size, new_ord_req.qty) return new_ord_req.qty
def process_w_bar(self, new_ord_req, bar, qty, new_order=False): self._init_order_trailing_stop(new_ord_req) trailing_stop_exec_price = self.__trailing_stop_exec_price[ new_ord_req.cl_id][new_ord_req.cl_ord_id] if is_buy(new_ord_req): trailing_stop_exec_price = min(trailing_stop_exec_price, bar.low + new_ord_req.stop_price) self.__trailing_stop_exec_price[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = trailing_stop_exec_price if bar.high >= trailing_stop_exec_price: fill_price = trailing_stop_exec_price if self.__slippage: fill_price = self.__slippage.calc_price_w_bar( new_ord_req, fill_price, qty, bar) return FillInfo(qty, fill_price) elif is_sell(new_ord_req): trailing_stop_exec_price = max(trailing_stop_exec_price, bar.high - new_ord_req.stop_price) self.__trailing_stop_exec_price[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = trailing_stop_exec_price if bar.low <= trailing_stop_exec_price: fill_price = trailing_stop_exec_price if self.__slippage: fill_price = self.__slippage.calc_price_w_bar( new_ord_req, fill_price, qty, bar) return FillInfo(qty, fill_price) return None
def calc_price(self, new_ord_req, price, qty, avail_qty): vol_share = float(qty) / float(avail_qty) impacted_price = vol_share**2 * self.price_impact if is_buy(new_ord_req): return price * (1 + impacted_price) else: return price * (1 - impacted_price)
def _init_order_trailing_stop(self, new_ord_req): trailing_stop_exec_price = self.__trailing_stop_exec_price[ new_ord_req.cl_id].get(new_ord_req.cl_ord_id, 0) if trailing_stop_exec_price == 0: if is_buy(new_ord_req): self.__trailing_stop_exec_price[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = sys.float_info.max elif is_sell(new_ord_req): self.__trailing_stop_exec_price[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = sys.float_info.min
def process_w_price_qty(self, new_ord_req, price, qty): stop_limit_ready = self.__stop_limit_ready[new_ord_req.cl_id].get( new_ord_req.cl_ord_id, False) if is_buy(new_ord_req): if price >= new_ord_req.stop_price: self.__stop_limit_ready[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = True return FillInfo(qty, price) elif is_sell(new_ord_req): if price <= new_ord_req.stop_price: self.__stop_limit_ready[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = True return FillInfo(qty, price) return None
def process_w_bar(self, new_ord_req, bar, qty, new_order=False): stop_limit_ready = self.__stop_limit_ready[new_ord_req.cl_id].get( new_ord_req.cl_ord_id, False) if is_buy(new_ord_req): if not stop_limit_ready and bar.high >= new_ord_req.stop_price: self.__stop_limit_ready[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = True elif stop_limit_ready and bar.low <= new_ord_req.limit_price: return FillInfo(qty, new_ord_req.limit_price) elif is_sell(new_ord_req): if not stop_limit_ready and bar.low <= new_ord_req.stop_price: self.__stop_limit_ready[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = True elif stop_limit_ready and bar.high >= new_ord_req.limit_price: return FillInfo(qty, new_ord_req.limit_price) return None
def process_w_price_qty(self, new_ord_req, price, qty): self._init_order_trailing_stop(new_ord_req) trailing_stop_exec_price = self.__trailing_stop_exec_price[ new_ord_req.cl_id][new_ord_req.cl_ord_id] if is_buy(new_ord_req): trailing_stop_exec_price = min(trailing_stop_exec_price, price + new_ord_req.stop_price) self.__trailing_stop_exec_price[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = trailing_stop_exec_price if price >= trailing_stop_exec_price: return FillInfo(qty, trailing_stop_exec_price) elif is_sell(new_ord_req): trailing_stop_exec_price = max(trailing_stop_exec_price, price - new_ord_req.stop_price) self.__trailing_stop_exec_price[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = trailing_stop_exec_price if price <= trailing_stop_exec_price: return FillInfo(qty, trailing_stop_exec_price) return None
def process_w_bar(self, new_ord_req, bar, qty, new_order=False): stop_limit_ready = self.__stop_limit_ready[new_ord_req.cl_id].get( new_ord_req.cl_ord_id, False) if is_buy(new_ord_req): if bar.high >= new_ord_req.stop_price: self.__stop_limit_ready[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = True fill_price = new_ord_req.stop_price if self.__slippage: fill_price = self.__slippage.calc_price_w_bar( new_ord_req, fill_price, qty, bar) return FillInfo(qty, fill_price) elif is_sell(new_ord_req): if bar.low <= new_ord_req.stop_price: self.__stop_limit_ready[new_ord_req.cl_id][ new_ord_req.cl_ord_id] = True fill_price = new_ord_req.stop_price if self.__slippage: fill_price = self.__slippage.calc_price_w_bar( new_ord_req, fill_price, qty, bar) return FillInfo(qty, fill_price) return None
def process_w_price_qty(self, new_ord_req, price, qty): if is_buy(new_ord_req) and price <= new_ord_req.limit_price: return FillInfo(qty, new_ord_req.limit_price) elif is_sell(new_ord_req) and price >= new_ord_req.limit_price: return FillInfo(qty, new_ord_req.limit_price) return None
def process_w_bar(self, new_ord_req, bar, qty, new_order=False): if is_buy(new_ord_req) and bar.low <= new_ord_req.limit_price: return FillInfo(qty, new_ord_req.limit_price) elif is_sell(new_ord_req) and bar.high >= new_ord_req.limit_price: return FillInfo(qty, new_ord_req.limit_price) return None
def calc_price_w_quote(self, new_ord_req, price, qty, quote): if is_buy(new_ord_req): return self.calc_price(new_ord_req, price, qty, quote.bid_size) else: return self.calc_price(new_ord_req, price, qty, quote.ask_size)
def get_price(self, new_ord_req, market_data, config, new_order=False): if is_buy(new_ord_req) and market_data.ask > 0: return market_data.ask elif is_sell(new_ord_req) and market_data.bid > 0: return market_data.bid return 0.0