Пример #1
0
    def factor_definition(self):
        s = time.time()
        needData = self.needData                                # 计算所需数据
        needData.loc[needData[t.TRDSTAT].isin([5,6]), t.ADJFCT] = np.nan        # 将停牌对应 股票收益率 设为 NaN

        num = 5
        rHigh = Calculator.Max(x=needData[t.HIGH]*needData[t.ADJFCT], num=num)
        rLow = Calculator.Max(x=needData[t.LOW]*needData[t.ADJFCT], num=num)
        rChg = Calculator.Sum(x = Calculator.Diff(x=needData[t.CLOSE], num=1), num=num)
        factor = -rChg/(rHigh - rLow)
        factor = factor.to_frame()
        factor.columns=[self.factorName]                         # 因子计算结果应该只有一列, 如果此处报错 请检查因子定义
        print('factor {0} done with {1} seconds'.format(self.factorName, time.time() - s))
        return factor
Пример #2
0
    def test_return_shape(self):
        """
        测试 返回值形状
        :return:
        """
        calculator = Calculator()
        simDataX = simDataGen(colnames=('colX', ))
        simDataY = simDataGen(colnames=('colY', ))
        dataShape = simDataX.shape
        # test functions
        self.assertTrue(
            np.all(dataShape == calculator.Delay(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Sumif(x=simDataX,
                                                 num=3,
                                                 by=ALIAS_FIELDS.STKCD,
                                                 condition=simDataX > 0,
                                                 retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Diff(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Max(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Min(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Mean(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Sum(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Var(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Std(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Wma(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Sma(
                x=simDataX, n=3, m=2, by=ALIAS_FIELDS.STKCD,
                retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Decaylinear(
                x=simDataX, d=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Rank(
                x=simDataX, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.TsToMax(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.TsToMin(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.FindRank(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD, retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.Corr(x=simDataX,
                                                y=simDataY,
                                                num=3,
                                                by=ALIAS_FIELDS.STKCD,
                                                retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.RegAlpha(x=simDataX,
                                                    y=simDataY,
                                                    num=3,
                                                    by=ALIAS_FIELDS.STKCD,
                                                    retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.RegBeta(x=simDataX,
                                                   y=simDataY,
                                                   num=3,
                                                   by=ALIAS_FIELDS.STKCD,
                                                   retDf=True).shape))
        self.assertTrue(
            np.all(dataShape == calculator.RegResi(x=simDataX,
                                                   y=simDataY,
                                                   num=3,
                                                   by=ALIAS_FIELDS.STKCD,
                                                   retDf=True).shape))
        print('DataFrame return shape test passed')

        self.assertTrue(
            np.all(dataShape[0] == calculator.Delay(
                x=simDataX, num=3, by=ALIAS_FIELDS.STKCD).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Sumif(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
                condition=simDataX > 0,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Diff(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Max(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Min(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Mean(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Sum(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Var(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Std(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Wma(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Sma(
                x=simDataX,
                n=3,
                m=2,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Decaylinear(
                x=simDataX,
                d=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Rank(
                x=simDataX,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.TsToMax(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.TsToMin(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.FindRank(
                x=simDataX,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.Corr(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.RegAlpha(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.RegBeta(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        self.assertTrue(
            np.all(dataShape[0] == calculator.RegResi(
                x=simDataX,
                y=simDataY,
                num=3,
                by=ALIAS_FIELDS.STKCD,
            ).shape[0]))
        print('Series return shape test passed')