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
def factor_definition(self): """ :return: """ s = time.time() needData = self.needData # 计算所需数据 adjLow = needData[t.LOW] * needData[t.ADJFCT] adjHigh = needData[t.HIGH] * needData[t.ADJFCT] adjClose = needData[t.CLOSE] * needData[t.ADJFCT] preClose = Calculator.Delay(x=adjClose, num=1) distrib = (adjClose >= preClose)*(adjClose - Calculator.cmpMin(preClose, adjLow)) + (adjClose < preClose)*(adjClose - Calculator.cmpMax(preClose, adjHigh)) factor = -Calculator.Sum(x=distrib, num=5)/adjClose factor = factor.to_frame() factor.columns = [self.factorName] # 因子计算结果应该只有一列, 如果此处报错 请检查因子定义 print('factor {0} done with {1} seconds'.format(self.factorName, time.time() - s)) return factor
def test_return_result(self): """ 测试返回值的 计算结果 :return: """ calculator = Calculator() simDataX = simDataGen(colnames=('colX', ), stkNum=3, dayNum=4) simDataY = simDataGen(colnames=('colY', ), stkNum=3, dayNum=4)
def factor_definition(self): s = time.time() needData = self.needData # 计算所需数据 needData.loc[needData[t.TRDSTAT].isin([5,6]), t.PCTCHG] = np.nan # 将停牌对应 股票收益率 设为 NaN factor = -Calculator.Sum(x=needData[t.PCTCHG], num=5) # 计算5日动量 factor = factor.to_frame() factor.columns = [self.factorName] # 因子计算结果应该只有一列, 如果此处报错 请检查因子定义 print('factor {0} done with {1} seconds'.format(self.factorName, time.time() - s)) return factor
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')
def test_init(self): calculator = Calculator()