コード例 #1
0
ファイル: util.py プロジェクト: zengyu1990/quantdigger
def stock(code, period='1.Day'):
    """ 构建周期合约结构的便捷方式。
    
       :param str code: 股票代码。
       :param str period: 回测周期。
       :return: 周期合约。
       :rtype: PContract
    """
    from quantdigger.datastruct import PContract, Contract, Period
    return PContract(Contract('%s.stock' % code), Period(period))
コード例 #2
0
def pcontract(contract, period):
    """ 构建周期合约结构的便捷方式。
    
       :param str contract: 合约如:'IF000.SHEF'
       :param str Period: 周期如:'10.Minute' 
       :return: 周期合约
       :rtype: PContract
    """
    from quantdigger.datastruct import PContract, Contract, Period
    return PContract(Contract(contract), Period(period))
コード例 #3
0
def stock(code, period='1.Day'):
    """ 构建周期合约结构的便捷方式。
    
    Args:
        code (str) : 股票代码
        period(str): 回测周期

    Returns:
        PContract. 周期合约
    """
    from quantdigger.datastruct import PContract, Contract, Period
    return PContract(Contract('%s.stock' % code), Period(period))
コード例 #4
0
ファイル: strategy.py プロジェクト: zengyu1990/quantdigger
 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))
コード例 #5
0
 def cover(self, price, quantity, symbol=None):
     """ 平空仓。
     
     Args:
        price (float): 价格, 0表市价。
        quantity (int): 数量。
        symbol (str): 合约
     """
     if not self._trading:
         raise Exception('只有on_bar函数内能下单!')
     contract = Contract(symbol) if symbol else copy.deepcopy(
         self._cur_data_context.contract)
     price_type = PriceType.MKT if price == 0 else PriceType.LMT
     self._cur_strategy_context.sell(Direction.SHORT, price, quantity,
                                     price_type, contract)
コード例 #6
0
 def buy(self, price, quantity, symbol=None):
     """ 开多仓    
     
     Args:
         price (float): 价格, 0表市价。
         quantity (int): 数量。
         symbol (str): 合约
     """
     if not self._trading:
         raise Exception('只有on_bar函数内能下单!')
     contract = Contract(
         symbol) if symbol else self._cur_data_context.contract
     price_type = PriceType.MKT if price == 0 else PriceType.LMT
     self._cur_strategy_context.buy(Direction.LONG, price, quantity,
                                    price_type, contract)
コード例 #7
0
    def cover(self, price, quantity, symbol=None):
        """ 平空仓。

        Args:
           price (float): 价格, 0表市价。
           quantity (int): 数量。
           symbol (str): 合约
        """
        if not self._trading:
            raise Exception('只有on_bar函数内能下单!')
        if symbol:
            contract = Contract(symbol) if isinstance(symbol, str) else symbol
        else:
            contract = self._cur_data_context.contract
        price_type = PriceType.MKT if price == 0 else PriceType.LMT
        self._cur_strategy_context.cover(price, quantity, price_type, contract)
コード例 #8
0
    def sell(self, price, quantity, symbol=None):
        """ 平多仓。

        Args:
           price (float): 价格, 0表市价。
           quantity (int): 数量。
           symbol (str): 合约
        """
        if not self.on_bar:
            raise Exception('只有on_bar函数内能下单!')
        if symbol:
            contract = Contract(symbol) if isinstance(symbol, str) else symbol
        else:
            contract = self.contract
        price_type = PriceType.MKT if price == 0 else PriceType.LMT
        self._orders.append(
            Order(None, contract, price_type, TradeSide.CLOSE, Direction.LONG,
                  float(price), quantity))
コード例 #9
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)
コード例 #10
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
コード例 #11
0
 def _mk_contract(code, exchage):
     s = '%s.%s' % (code, exchage)
     return Contract(s)