def collect(self,strategic = None ,statisticsProjectPath = None): """ 对于整体数据,缓存之前计算。对于指定策略,暂时只收集15日内数据,反映短期内变动情况。 收集统计数据 >> statistics.stock == testCase['stock'] >> statistics.collect() 收集指定策略统计数据 >> statistics.stock == testCase['stock'] >> statistics.collect(strategic = testCase['collectStrategic']) @param {list} strategic 符合策略的code list """ conf={} conf.update(self.conf) isCache = False data={} if strategic: #计算指定策略统计 allCode = strategic #符合指定策略的 code 可通过strategic[date][strategicName]读出 allDate = [] for i in range(0,conf.get("MIN_DATA_NUM",MIN_DATA_NUM)): date=Date.getDate(0-i,conf.get('date',Date.getDate())) allDate.append(date) else: #计算整体统计 isCache = True allDate = self.stock.allDate allCode = self.stock.allCode if isCache: #读取之前统计缓存 data = Reader.read(path = self.getDataPath(dataType = "cache")) for date in allDate: if date in data and date in allDate: allDate.remove(date) conf.update({ "date":self.date, "stock":self.stock, "allCode":allCode }) expression=Expression(conf) statisticsProjectPath = statisticsProjectPath or os.path.join(conf.get("SYS_HOME"),conf.get("STATISTICS_PROJECT_PATH")) expressions = expression.load(path = statisticsProjectPath) for date in allDate: result={} #初始化result变量,让result能在公式里直接使用,并且result[name]也能使用 for code in allCode: result[code]={} expression.execute(expression= conf.get("expressions",expressions), context = {"result":result,"date":date}) if not result: break; data[date]=result if isCache: Dumper.dump(path = self.getDataPath(dataType = "cache"),data =data) return data
def compute(self,strategic,statistics): """ 传入统计数据和模型数据,计算得出报告 >>> report.compute(strategic = case['strategic'] , statistics = case['statistics']) == testResult['compute'] True """ projectPath=os.path.join(self.conf.get("SYS_HOME"),self.conf.get("REPORTER_PROJECT_PATH",REPORTER_PROJECT_PATH)) expression=Expression(self.conf) expressions = expression.load(path=projectPath) result={} for func in expressions: result[func.get("name")]=[] expression.execute(expression= self.conf.get("expressions",expressions), context = {"result":result},conf = {"dataName":"statistics","data":statistics,"itemName" : "strategic"}) #format data self.data=self.getFullReporter(strategic = strategic,statistics = statistics ,report = result) return self.data