Exemple #1
0
    def sell(self, direction, price, quantity, price_type, contract):
        """ 平仓。
        
        Args:
           direction (str/int): 下单方向。多头 - 'long' / 1 ;空头 - 'short'  / 2

           price (float): 价格。

           quantity (int): 数量。

           price_type (str/int): 下单价格类型。限价单 - 'lmt' / 1;市价单 - 'mkt' / 2

           contract (Contract): 合约
        """
        self._orders.append(
            Order(
                self._datetime,
                contract,
                PriceType.arg_to_type(price_type),
                TradeSide.PING,
                Direction.arg_to_type(direction),
                float(price),
                quantity,
            )
        )
Exemple #2
0
 def sell(self, direction, price, quantity, price_type, contract):
     self._orders.append(Order(
             self._datetime,
             contract,
             PriceType.arg_to_type(price_type),
             TradeSide.PING,
             Direction.arg_to_type(direction),
             float(price),
             quantity
     ))
Exemple #3
0
 def buy(self, direction, price, quantity, price_type, contract):
     self._orders.append(Order(
             ## @todo 时间放到blotter中设置
             self._datetime,
             contract,
             PriceType.arg_to_type(price_type),
             TradeSide.KAI,
             Direction.arg_to_type(direction),
             float(price),
             quantity
     ))
Exemple #4
0
 def buy(self, direction, price, quantity, price_type='LMT', contract=None):
     """ 开仓。
     
        :param str/int direction: 下单方向。多头 - 'long' / 1 ;空头 - 'short'  / 2
        :param float price: 价格。
        :param int quantity: 数量。
        :param str/int price_type: 下单价格类型。限价单 - 'lmt' / 1;市价单 - 'mkt' / 2
     """
     contract = None
     con = Contract(contract) if contract else self._main_contract
     self._orders.append(
         Order(self.datetime, con,
               PriceType.arg_to_type(price_type), TradeSide.KAI,
               Direction.arg_to_type(direction), float(price), quantity))
Exemple #5
0
    def pos(self, direction='long', symbol=None):
        """  合约的当前可平仓位。
       
        Args:
            direction (str/int): 持仓方向。多头 - 'long' / 1 ;空头 - 'short'  / 2
            , 默认为多头。

            symbol (str): 字符串合约,默认为None表示主合约。
        
        Returns:
            int. 该合约的持仓数目。
        """
        if not self._trading:
            raise Exception('只有on_bar函数内能查询当前持仓!')
        direction = Direction.arg_to_type(direction)
        contract = Contract(symbol) if symbol else self._cur_data_context.contract 
        ## @TODO assert direction
        return self._cur_strategy_context.pos(contract, direction)
Exemple #6
0
 def sell(self, direction, price, quantity, price_type='MKT', contract=None):
     """ 平仓。
     
        :param str/int direction: 下单方向。多头 - 'long' / 1 ;空头 - 'short'  / 2
        :param float price: 价格。
        :param int quantity: 数量。
        :param str/int price_type: 下单价格类型。限价单 - 'lmt' / 1;市价单 - 'mkt' / 2
     """
     con = Contract(contract) if contract else self._main_contract
     self._orders.append(Order(
             self.datetime,
             con,
             PriceType.arg_to_type(price_type),
             TradeSide.PING,
             Direction.arg_to_type(direction),
             float(price),
             quantity
     ))
Exemple #7
0
    def position(self, direction='long', symbol=None):
        """ 当前仓位。

        Args:
            direction (str/int): 持仓方向。多头 - 'long' / 1 ;空头 - 'short'  / 2
            , 默认为多头。

            symbol (str): 字符串合约,默认为None表示主合约。

        Returns:
            Position. 该合约的持仓
        """
        if not self._trading:
            raise Exception('只有on_bar函数内能查询当前持仓!')
        direction = Direction.arg_to_type(direction)
        contract = Contract(symbol) if symbol else \
            self._cur_data_context.contract
        # @TODO assert direction
        return self._cur_strategy_context.position(contract, direction)
Exemple #8
0
    def position(self, direction='long', symbol=None):
        """ 合约当前持仓仓位。

        Args:
            direction (str/int): 持仓方向。多头 - 'long' / 1 ;空头 - 'short'  / 2
            , 默认为多头。

            symbol (str): 字符串合约,默认为None表示主合约。

        Returns:
            Position. 该合约的持仓
        """
        if not self.on_bar:
            raise Exception('只有on_bar函数内能查询当前持仓!')
        direction = Direction.arg_to_type(direction)
        contract = Contract(symbol) if symbol else \
            self.contract
        # @TODO assert direction
        try:
            poskey = PositionKey(contract, direction)
            return self.blotter.positions[poskey]
        except KeyError:
            return None