예제 #1
0
    def parse_order(item):
        contract_fields = {}
        order_fields = {}
        for key, value in item.items():
            if value is None:
                continue
            if isinstance(value, six.string_types):
                value = get_string(value)
            tag = ORDER_FIELD_MAPPINGS[key] if key in ORDER_FIELD_MAPPINGS else key
            if tag in CONTRACT_FIELDS:
                contract_fields[tag] = value
            else:
                order_fields[tag] = value

        contract_id = contract_fields.get('contract_id')
        symbol = contract_fields.get('symbol')
        currency = contract_fields.get('currency')
        sec_type = contract_fields.get('sec_type')
        exchange = contract_fields.get('exchange')
        origin_symbol = contract_fields.get('origin_symbol')
        local_symbol = contract_fields.get('local_symbol')
        expiry = contract_fields.get('expiry')
        strike = contract_fields.get('strike')
        put_call = contract_fields.get('right')
        multiplier = contract_fields.get('multiplier')
        contract = Contract(symbol, currency, contract_id=contract_id, sec_type=sec_type, exchange=exchange,
                            origin_symbol=origin_symbol, local_symbol=local_symbol, expiry=expiry,
                            strike=strike, put_call=put_call, multiplier=multiplier)
        account = order_fields.get('account')
        action = order_fields.get('action')
        order_type = order_fields.get('order_type')
        quantity = order_fields.get('quantity')
        limit_price = order_fields.get('limit_price')
        aux_price = order_fields.get('aux_price')
        trail_stop_price = order_fields.get('trail_stop_price')
        trailing_percent = order_fields.get('trailing_percent')
        percent_offset = order_fields.get('percent_offset')
        time_in_force = order_fields.get('time_in_force')
        outside_rth = order_fields.get('outside_rth')
        filled = order_fields.get('filled')
        avg_fill_price = order_fields.get('avg_fill_price')
        commission = order_fields.get('commission')
        realized_pnl = order_fields.get('realized_pnl')
        id = order_fields.get('id')
        order_id = order_fields.get('order_id')
        parent_id = order_fields.get('parent_id')
        status = OrdersResponse.get_status(order_fields.get('status'))

        order = Order(account, contract, action, order_type, quantity, limit_price=limit_price, aux_price=aux_price,
                      trail_stop_price=trail_stop_price, trailing_percent=trailing_percent,
                      percent_offset=percent_offset, time_in_force=time_in_force, outside_rth=outside_rth,
                      filled=filled, avg_fill_price=avg_fill_price, commission=commission,
                      realized_pnl=realized_pnl, id=id, order_id=order_id, parent_id=parent_id)
        if 'order_time' in order_fields:
            order.order_time = order_fields.get('order_time')
        if 'trade_time' in order_fields:
            order.trade_time = order_fields.get('trade_time')
        order.status = status

        return order
예제 #2
0
def limit_order_with_legs(account,
                          contract,
                          action,
                          quantity,
                          limit_price,
                          order_legs=None):
    """
    限价单 + 附加订单(仅环球账户支持)
    :param account:
    :param contract:
    :param action: BUY/SELL
    :param quantity:
    :param limit_price: 限价单价格
    :param order_legs: 附加订单列表
    :return:
    """
    if order_legs and len(order_legs) > 2:
        raise Exception('2 order legs at most')
    return Order(account,
                 contract,
                 action,
                 'LMT',
                 quantity,
                 limit_price=limit_price,
                 order_legs=order_legs)
def algo_order(account,
               contract,
               action,
               quantity,
               strategy,
               algo_params=None,
               limit_price=None):
    """
    算法订单
    :param account:
    :param contract:
    :param action:
    :param quantity:
    :param strategy: 交易量加权平均价格(VWAP)/时间加权平均价格(TWAP)
    :param algo_params: tigeropen.trade.domain.order.AlgoParams
    :param limit_price:
    :return:
    """
    return Order(account,
                 contract,
                 action,
                 order_type=strategy,
                 quantity=quantity,
                 algo_params=algo_params,
                 limit_price=limit_price,
                 outside_rth=False)
예제 #4
0
    def create_order(self,
                     account,
                     contract,
                     action,
                     order_type,
                     quantity,
                     limit_price=None,
                     aux_price=None,
                     trail_stop_price=None,
                     trailing_percent=None,
                     percent_offset=None,
                     time_in_force=None,
                     outside_rth=None):
        """
        创建订单对象.
        :param account:
        :param contract:
        :param action:
        :param order_type:
        :param quantity:
        :param limit_price: 限价
        :param aux_price: 在止损单表示止损价格; 在跟踪止损单表示价差
        :param trail_stop_price: 跟踪止损单--触发止损单的价格
        :param trailing_percent: 跟踪止损单--百分比
        :param percent_offset:
        :param time_in_force: 订单有效期, 'DAY'(当日有效)和'GTC'(取消前有效)
        :param outside_rth: 是否允许盘前盘后交易(美股专属)
        :return:
        """
        params = AccountsParams()
        params.account = account if account else self._account
        request = OpenApiRequest(ORDER_NO, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                order_id = response.order_id
                order = Order(account,
                              contract,
                              action,
                              order_type,
                              quantity,
                              limit_price=limit_price,
                              aux_price=aux_price,
                              trail_stop_price=trail_stop_price,
                              trailing_percent=trailing_percent,
                              percent_offset=percent_offset,
                              time_in_force=time_in_force,
                              outside_rth=outside_rth,
                              order_id=order_id)
                return order
            else:
                raise ApiException(response.code, response.message)

        return None
예제 #5
0
def market_order(account, contract, action, quantity):
    """
    市价单
    :param account:
    :param contract:
    :param action: BUY/SELL
    :param quantity:
    :return:
    """
    return Order(account, contract, action, 'MKT', quantity)
예제 #6
0
def stop_order(account, contract, action, quantity, aux_price):
    """
    止损单
    :param account:
    :param contract:
    :param action: BUY/SELL
    :param quantity:
    :param aux_price: 触发止损单的价格
    :return:
    """
    return Order(account, contract, action, 'STP', quantity, aux_price=aux_price)
예제 #7
0
def limit_order(account, contract, action, quantity, limit_price):
    """
    限价单
    :param account:
    :param contract:
    :param action: BUY/SELL
    :param quantity:
    :param limit_price: 限价的价格
    :return:
    """
    return Order(account, contract, action, 'LMT', quantity, limit_price=limit_price)
예제 #8
0
def trail_order(account, contract, action, quantity, trailing_percent=None, aux_price=None):
    """
    移动止损单
    :param account:
    :param contract:
    :param action: BUY/SELL
    :param quantity:
    :param trailing_percent: 百分比 (0~100)
    :param aux_price: 价差  aux_price 和 trailing_percent 两者互斥
    :return:
    """
    return Order(account, contract, action, 'TRAIL', quantity, trailing_percent=trailing_percent, aux_price=aux_price)
예제 #9
0
def stop_limit_order(account, contract, action, quantity, limit_price, aux_price):
    """
    限价止损单
    :param account:
    :param contract:
    :param action: BUY/SELL
    :param quantity:
    :param limit_price: 发出限价单的价格
    :param aux_price: 触发止损单的价格
    :return:
    """
    return Order(account, contract, action, 'STP_LMT', quantity, limit_price=limit_price, aux_price=aux_price)
예제 #10
0
    def create_order(self, account, contract, action, order_type, quantity, limit_price=None, aux_price=None,
                     trail_stop_price=None, trailing_percent=None, percent_offset=None, time_in_force=None,
                     outside_rth=None):
        params = AccountsParams()
        params.account = self._account
        request = OpenApiRequest(ORDER_NO, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                order_id = response.order_id
                order = Order(account, contract, action, order_type, quantity, limit_price=limit_price,
                              aux_price=aux_price, trail_stop_price=trail_stop_price,
                              trailing_percent=trailing_percent, percent_offset=percent_offset,
                              time_in_force=time_in_force, outside_rth=outside_rth, order_id=order_id)
                return order
            else:
                raise ApiException(response.code, response.message)

        return None
    def parse_order(item, secret_key=None):
        contract_fields = {}
        order_fields = {}
        for key, value in item.items():
            if value is None:
                continue
            tag = ORDER_FIELD_MAPPINGS[
                key] if key in ORDER_FIELD_MAPPINGS else key
            if tag in CONTRACT_FIELDS:
                contract_fields[tag] = value
            else:
                order_fields[tag] = value

        contract_id = contract_fields.get('contract_id')
        symbol = contract_fields.get('symbol')
        currency = contract_fields.get('currency')
        sec_type = contract_fields.get('sec_type')
        exchange = contract_fields.get('exchange')
        origin_symbol = contract_fields.get('origin_symbol')
        local_symbol = contract_fields.get('local_symbol')
        expiry = contract_fields.get('expiry')
        strike = contract_fields.get('strike')
        put_call = contract_fields.get('right')
        multiplier = contract_fields.get('multiplier')
        identifier = contract_fields.get('identifier')
        contract = Contract(symbol,
                            currency,
                            contract_id=contract_id,
                            sec_type=sec_type,
                            exchange=exchange,
                            origin_symbol=origin_symbol,
                            local_symbol=local_symbol,
                            expiry=expiry,
                            strike=strike,
                            put_call=put_call,
                            multiplier=multiplier,
                            identifier=identifier)
        account = order_fields.get('account')
        action = order_fields.get('action')
        order_type = order_fields.get('order_type')
        quantity = order_fields.get('quantity')
        limit_price = order_fields.get('limit_price')
        aux_price = order_fields.get('aux_price')
        trail_stop_price = order_fields.get('trail_stop_price')
        trailing_percent = order_fields.get('trailing_percent')
        percent_offset = order_fields.get('percent_offset')
        time_in_force = order_fields.get('time_in_force')
        outside_rth = order_fields.get('outside_rth')
        filled = order_fields.get('filled')
        avg_fill_price = order_fields.get('avg_fill_price')
        commission = order_fields.get('commission')
        realized_pnl = order_fields.get('realized_pnl')
        id_ = order_fields.get('id')
        order_id = order_fields.get('order_id')
        parent_id = order_fields.get('parent_id')
        status = get_order_status(order_fields.get('status'))
        algo_params = AlgoParams.from_tags(order_fields.get('algo_params'))
        liquidation = order_fields.get('liquidation')
        algo_strategy = order_fields.get('algo_strategy')
        discount = order_fields.get('discount')

        order = Order(account,
                      contract,
                      action,
                      order_type,
                      quantity,
                      limit_price=limit_price,
                      aux_price=aux_price,
                      trail_stop_price=trail_stop_price,
                      trailing_percent=trailing_percent,
                      percent_offset=percent_offset,
                      time_in_force=time_in_force,
                      outside_rth=outside_rth,
                      filled=filled,
                      avg_fill_price=avg_fill_price,
                      commission=commission,
                      realized_pnl=realized_pnl,
                      id=id_,
                      order_id=order_id,
                      parent_id=parent_id,
                      algo_params=algo_params,
                      liquidation=liquidation,
                      algo_strategy=algo_strategy,
                      discount=discount)
        if 'order_time' in order_fields:
            order.order_time = order_fields.get('order_time')
        if 'trade_time' in order_fields:
            order.trade_time = order_fields.get('trade_time')
        if 'reason' in order_fields:
            order.reason = order_fields.get('reason')
        if secret_key is not None:
            order.secret_key = secret_key
        order.status = status

        return order