Пример #1
0
    def all_orders(self, asset=None, before=None, status='all'):
        a1 = 'ASSET1'
        a2 = 'ASSET2'

        return {
            'o01':
            ZPOrder(
                dt=pd.Timestamp('2018-10-31 09:40:00-0400'),
                asset=a1,
                amount=2,
                id='o01',
            ),
            'o02':
            ZPOrder(
                dt=pd.Timestamp('2018-10-31 09:45:00-0400'),
                asset=a1,
                amount=5,
                id='o02',
            ),
            'o03':
            ZPOrder(
                dt=pd.Timestamp('2018-10-31 09:45:00-0400'),
                asset=a2,
                amount=3,
                id='o03',
            ),
        }
Пример #2
0
def test_open_orders():
    algo = get_algo('')

    a1 = algo.symbol('ASSET1')
    a2 = algo.symbol('ASSET2')
    algo._backend.orders = {
        'o01':
        ZPOrder(
            dt=pd.Timestamp('2018-10-31 09:40:00-0400'),
            asset=a1,
            amount=2,
            id='o01',
        ),
        'o02':
        ZPOrder(
            dt=pd.Timestamp('2018-10-31 09:45:00-0400'),
            asset=a1,
            amount=5,
            id='o02',
        ),
        'o03':
        ZPOrder(
            dt=pd.Timestamp('2018-10-31 09:45:00-0400'),
            asset=a2,
            amount=3,
            id='o03',
        ),
        'o04':
        ZPOrder(
            dt=pd.Timestamp('2018-10-31 09:45:00-0400'),
            asset=a2,
            amount=0,
            id='o04',
        ),
    }

    orders = algo.get_open_orders()
    assert len(orders[a1]) == 2
    assert orders[a1][0].dt < orders[a1][1].dt
    assert orders[a1][0].id == 'o01'
    assert len(orders[a2]) == 1

    orders = algo.get_open_orders(a1)
    assert len(orders) == 2
    assert orders[0].id == 'o01'

    a0 = algo.symbol('ASSET0')
    assert len(algo.get_open_orders(a0)) == 0
Пример #3
0
 def order(self, asset, amount, style, quantopian_compatible=True):
     if amount == 0:
         return
     limit_price = style.get_limit_price(amount > 0) or None
     stop_price = style.get_stop_price(amount > 0) or None
     self._order_seq += 1
     zpOrder = ZPOrder(dt=self.get_last_traded_dt(asset),
                       asset=asset,
                       amount=amount,
                       limit=limit_price,
                       stop=stop_price,
                       id=self._order_seq)
     self._orders[zpOrder.id] = zpOrder
     return zpOrder
Пример #4
0
    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,
        )

        try:
            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
        except APIError as e:
            log.warning('order is rejected {}'.format(e))
            return None
Пример #5
0
 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