Example #1
0
    def place_order(self, order):
        params = PlaceModifyOrderParams()
        params.account = order.account
        params.contract = order.contract
        params.action = order.action
        params.order_type = order.order_type
        params.order_id = order.order_id
        params.quantity = order.quantity
        params.limit_price = order.limit_price
        params.aux_price = order.aux_price
        params.trail_stop_price = order.trail_stop_price
        params.trailing_percent = order.trailing_percent
        params.percent_offset = order.percent_offset
        params.time_in_force = order.time_in_force
        params.outside_rth = order.outside_rth
        request = OpenApiRequest(PLACE_ORDER, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                return response.order_id == order.order_id
            else:
                raise ApiException(response.code, response.message)

        return False
Example #2
0
    def modify_order(self, order, quantity=None, limit_price=None, aux_price=None,
                     trail_stop_price=None, trailing_percent=None, percent_offset=None,
                     time_in_force=None, outside_rth=None):
        params = PlaceModifyOrderParams()
        params.account = order.account
        params.order_id = order.order_id
        params.contract = order.contract
        params.action = order.action
        params.order_type = order.order_type
        params.quantity = quantity if quantity is not None else order.quantity
        params.limit_price = limit_price if limit_price is not None else order.limit_price
        params.aux_price = aux_price if aux_price is not None else order.aux_price
        params.trail_stop_price = trail_stop_price if trail_stop_price is not None else order.trail_stop_price
        params.trailing_percent = trailing_percent if trailing_percent is not None else order.trailing_percent
        params.percent_offset = percent_offset if percent_offset is not None else order.percent_offset
        params.time_in_force = time_in_force if time_in_force is not None else order.time_in_force
        params.outside_rth = outside_rth if outside_rth is not None else order.outside_rth
        request = OpenApiRequest(MODIFY_ORDER, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                return response.order_id == order.order_id
            else:
                raise ApiException(response.code, response.message)

        return False
Example #3
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
Example #4
0
    def modify_order(self,
                     order,
                     quantity=None,
                     limit_price=None,
                     aux_price=None,
                     trail_stop_price=None,
                     trailing_percent=None,
                     percent_offset=None,
                     time_in_force=None,
                     outside_rth=None):
        """
        修改订单
        :param order:
        :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 = PlaceModifyOrderParams()
        params.account = order.account
        params.order_id = order.order_id
        params.id = order.id
        params.contract = order.contract
        params.action = order.action
        params.order_type = order.order_type
        params.quantity = quantity if quantity is not None else order.quantity
        params.limit_price = limit_price if limit_price is not None else order.limit_price
        params.aux_price = aux_price if aux_price is not None else order.aux_price
        params.trail_stop_price = trail_stop_price if trail_stop_price is not None else order.trail_stop_price
        params.trailing_percent = trailing_percent if trailing_percent is not None else order.trailing_percent
        params.percent_offset = percent_offset if percent_offset is not None else order.percent_offset
        params.time_in_force = time_in_force if time_in_force is not None else order.time_in_force
        params.outside_rth = outside_rth if outside_rth is not None else order.outside_rth
        params.secret_key = order.secret_key if order.secret_key else self._secret_key

        request = OpenApiRequest(MODIFY_ORDER, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                return response.order_id == order.order_id if order.order_id else response.id == order.id
            else:
                raise ApiException(response.code, response.message)

        return False
Example #5
0
    def cancel_order(self, order_id):
        params = CancelOrderParams()
        params.account = self._account
        params.order_id = order_id
        request = OpenApiRequest(CANCEL_ORDER, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                return response.order_id == order_id
            else:
                raise ApiException(response.code, response.message)

        return False
Example #6
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
Example #7
0
    def place_order(self, order):
        """
        下单
        :param order:  Order 对象
        :return:
        """
        params = PlaceModifyOrderParams()
        params.account = order.account
        params.contract = order.contract
        params.action = order.action
        params.order_type = order.order_type
        params.order_id = order.order_id
        params.quantity = order.quantity
        params.limit_price = order.limit_price
        params.aux_price = order.aux_price
        params.trail_stop_price = order.trail_stop_price
        params.trailing_percent = order.trailing_percent
        params.percent_offset = order.percent_offset
        params.time_in_force = order.time_in_force
        params.outside_rth = order.outside_rth
        params.order_legs = order.order_legs
        params.algo_params = order.algo_params
        params.secret_key = order.secret_key if order.secret_key else self._secret_key

        request = OpenApiRequest(PLACE_ORDER, 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.id
                if order.order_id is None and response.order_id:
                    order.order_id = response.order_id
                return True
            else:
                raise ApiException(response.code, response.message)

        return False
Example #8
0
    def cancel_order(self, account=None, id=None, order_id=None):
        """
        取消订单
        :param account:
        :param id: 全局订单 id
        :param order_id: 账户自增订单 id
        :return:
        """
        params = CancelOrderParams()
        params.account = account if account else self._account
        params.order_id = order_id
        params.id = id
        request = OpenApiRequest(CANCEL_ORDER, biz_model=params)
        response_content = self.__fetch_data(request)
        if response_content:
            response = OrderIdResponse()
            response.parse_response_content(response_content)
            if response.is_success():
                return response.order_id == order_id if order_id else response.id == id
            else:
                raise ApiException(response.code, response.message)

        return False