def _get_orders(asset1, asset2): return { sentinel.order_id1: ZPOrder(dt=sentinel.dt, asset=asset1, amount=12, stop=sentinel.stop1, limit=sentinel.limit1, id=sentinel.order_id1), sentinel.order_id2: ZPOrder(dt=sentinel.dt, asset=asset1, amount=-12, limit=sentinel.limit2, id=sentinel.order_id2), sentinel.order_id3: ZPOrder(dt=sentinel.dt, asset=asset2, amount=3, stop=sentinel.stop2, limit=sentinel.limit2, id=sentinel.order_id3), sentinel.order_id4: ZPOrder(dt=sentinel.dt, asset=asset2, amount=-122, id=sentinel.order_id4), }
def order(self, asset, amount, style): symbol = asset.symbol qty = amount if amount > 0 else -amount side = 'buy' if amount > 0 else 'sell' order_type = 'market' zp_order_id = self._new_order_id() dt = pd.to_datetime('now', utc=True) stop_price = self.get_stop_price(symbol) limit_price = self.get_limit_price(symbol) zp_order = ZPOrder( dt=dt, asset=asset, amount=amount, stop=stop_price, limit=limit_price, id=zp_order_id, ) order = self.api_client.Order.Order_new(symbol=symbol, orderQty=qty, price=limit_price).result()[0] zp_order = self._order2zp(order) return zp_order
def _create_zp_order(self, order_id, asset, price, amount, order_type): zp_order_id = self._tdx_to_zp_order_id(order_id) if zp_order_id in self._orders: return self._orders[zp_order_id] dt = pd.to_datetime("now", utc=True) self._orders[zp_order_id] = ZPOrder( dt=dt, asset=asset, amount=amount, stop=None, limit=price if order_type is LIMIT_CHARGE else None, id=zp_order_id, broker_order_id=order_id) return self._orders[zp_order_id]
def order(self, asset, amount, style): if style.exit_take_profit_price is not None or style.exit_stop_loss_price is not None: raise NotImplementedError( 'Order exit T/P or S/L prices support not implemented (Alpaca)!' ) symbol = asset.symbol qty = amount if amount > 0 else -amount side = 'buy' if amount > 0 else 'sell' order_type = 'market' if isinstance(style, MarketOrder): order_type = 'market' elif isinstance(style, LimitOrder): order_type = 'limit' elif isinstance(style, StopOrder): order_type = 'stop' elif isinstance(style, StopLimitOrder): order_type = 'stop_limit' limit_price = style.get_limit_price(side == 'buy') or None stop_price = style.get_stop_price(side == 'buy') or None zp_order_id = self._new_order_id() dt = pd.to_datetime('now', utc=True) zp_order = ZPOrder( dt=dt, asset=asset, amount=amount, stop=stop_price, limit=limit_price, id=zp_order_id, ) order = self._api.submit_order( symbol=symbol, qty=qty, side=side, type=order_type, time_in_force='day', limit_price=limit_price, stop_price=stop_price, client_order_id=zp_order.id, ) zp_order = self._order2zp(order) return zp_order
def order(self, asset, amount, limit_price, stop_price, style): is_buy = (amount > 0) zp_order = ZPOrder( dt=pd.to_datetime('now', utc=True), asset=asset, amount=amount, stop=style.get_stop_price(is_buy), limit=style.get_limit_price(is_buy)) contract = Contract() contract.m_symbol = str(asset.symbol) contract.m_currency = self.currency contract.m_exchange = 'SMART' contract.m_secType = 'STK' order = Order() order.m_totalQuantity = int(fabs(amount)) order.m_action = "BUY" if amount > 0 else "SELL" order.m_lmtPrice = 0 order.m_auxPrice = 0 if isinstance(style, MarketOrder): order.m_orderType = "MKT" elif isinstance(style, LimitOrder): order.m_orderType = "LMT" order.m_lmtPrice = limit_price elif isinstance(style, StopOrder): order.m_orderType = "STP" order.m_auxPrice = stop_price elif isinstance(style, StopLimitOrder): order.m_orderType = "STP LMT" order.m_auxPrice = stop_price order.m_lmtPrice = limit_price order.m_tif = "DAY" ib_order_id = self._tws.next_order_id zp_order.broker_order_id = ib_order_id self.orders[zp_order.id] = zp_order self._tws.placeOrder(ib_order_id, contract, order) return zp_order.id
def _order2zp(self, order): zp_order = ZPOrder( id=order.client_order_id, asset=symbol_lookup(order.symbol), amount=int(order.qty) if order.side == 'buy' else -int(order.qty), stop=float(order.stop_price) if order.stop_price else None, limit=float(order.limit_price) if order.limit_price else None, dt=order.submitted_at, commission=0, ) zp_order.status = ZP_ORDER_STATUS.OPEN if order.canceled_at: zp_order.status = ZP_ORDER_STATUS.CANCELLED if order.failed_at: zp_order.status = ZP_ORDER_STATUS.REJECTED if order.filled_at: zp_order.status = ZP_ORDER_STATUS.FILLED zp_order.filled = int(order.filled_qty) return zp_order
def tdx_order_to_zipline_order(self, order): """ status 0 已报 1 部分成交 2 全部成交 3 部分撤单 4 全部撤单 5 交易所拒单 6 柜台未接受 :param order: :return: """ if order.status == "3" or '4' == order.status: zp_status = ZP_ORDER_STATUS.CANCELLED elif order.status == "0": zp_status = ZP_ORDER_STATUS.OPEN elif order.status == "1": zp_status = ZP_ORDER_STATUS.FILLED elif order.status == "2": zp_status = ZP_ORDER_STATUS.HELD elif order.status == "5" or order.status == "6": zp_status = ZP_ORDER_STATUS.REJECTED zp_order_id = self._tdx_to_zp_order_id(order.order_id) od = ZPOrder( dt=order.dt, asset=symbol(order.symbol), amount=order.amount, filled=order.filled, stop=None, limit=order.price, # TODO 市价单和限价单 id=zp_order_id, ) od.broker_order_id = order.order_id od.status = zp_status return od
def order(self, asset, amount, style): symbol = asset.symbol qty = amount if amount > 0 else -amount side = 'buy' if amount > 0 else 'sell' order_type = 'market' if isinstance(style, MarketOrder): order_type = 'market' elif isinstance(style, LimitOrder): order_type = 'limit' elif isinstance(style, StopOrder): order_type = 'stop' elif isinstance(style, StopLimitOrder): order_type = 'stop_limit' limit_price = style.get_limit_price(side == 'buy') or None stop_price = style.get_stop_price(side == 'buy') or None zp_order_id = self._new_order_id() dt = pd.to_datetime('now', utc=True) zp_order = ZPOrder( dt=dt, asset=asset, amount=amount, stop=stop_price, limit=limit_price, id=zp_order_id, ) order = self._api.submit_order( symbol=symbol, qty=qty, side=side, type=order_type, time_in_force='day', limit_price=limit_price, stop_price=stop_price, client_order_id=zp_order.id, ) zp_order = self._order2zp(order) return zp_order
def tdx_order_to_zipline_order(self, order): if order.status is not None and 'CANCEL' == order.status: zp_status = ZP_ORDER_STATUS.CANCELLED elif order.filled == 0: zp_status = ZP_ORDER_STATUS.OPEN else: zp_status = ZP_ORDER_STATUS.FILLED zp_order_id = self._tdx_to_zp_order_id(order.order_id) od = ZPOrder( dt=pd.to_datetime(order.dt), asset=symbol(order.symbol), amount=order.amount, filled=order.filled, stop=None, limit=order.price, # TODO 市价单和限价单 id=zp_order_id, ) od.broker_order_id = order.order_id od.status = zp_status return od
def _get_or_create_zp_order(self, ib_order_id, ib_order=None, ib_contract=None): zp_order_id = self._ib_to_zp_order_id(ib_order_id) if zp_order_id in self._orders: return self._orders[zp_order_id] # Try to reconstruct the order from the given information: # open order state and execution state ib_symbol, order_details = None, None if ib_order and ib_contract: ib_symbol = ib_contract.symbol order_details = self._parse_order_ref(ib_order.orderRef) if not order_details and ib_order_id in self._tws.open_orders: open_order = self._tws.open_orders[ib_order_id] ib_symbol = open_order['contract'].symbol order_details = self._parse_order_ref(open_order['order'].orderRef) if not order_details and ib_order_id in self._tws.executions: executions = self._tws.executions[ib_order_id] last_exec_detail = list(executions.values())[-1]['exec_detail'] last_exec_contract = list(executions.values())[-1]['contract'] ib_symbol = last_exec_contract.symbol order_details = self._parse_order_ref(last_exec_detail.orderRef) asset = self._safe_symbol_lookup(ib_symbol) if not asset: log.warning("Ignoring symbol {symbol} which has associated " "order but it is not registered in bundle".format( symbol=ib_symbol)) return None if order_details: amount = self._action_qty_to_amount(order_details['action'], order_details['qty']) stop_price = order_details['stop_price'] limit_price = order_details['limit_price'] dt = order_details['dt'] else: dt = pd.to_datetime('now', utc=True) amount, stop_price, limit_price = 0, None, None if ib_order_id in self._tws.open_orders: open_order = self._tws.open_orders[ib_order_id]['order'] amount = self._action_qty_to_amount(open_order.action, open_order.totalQuantity) stop_price = open_order.auxPrice limit_price = open_order.lmtPrice stop_price = None if stop_price == 0 else stop_price limit_price = None if limit_price == 0 else limit_price self._orders[zp_order_id] = ZPOrder(dt=dt, asset=asset, amount=amount, stop=stop_price, limit=limit_price, id=zp_order_id) self._orders[zp_order_id].broker_order_id = ib_order_id return self._orders[zp_order_id]