def _genSpecificRisk(self):
     Args = {"RiskDB":self.RiskDB,
             "RiskESTDTs":self.RiskESTDTs,
             "TargetTable":self.TargetTable,
             "ModelArgs":self.Config.ModelArgs,
             "FactorCovESTArgs":self.Config.FactorCovESTArgs,
             "SpecificRiskESTArgs":self.Config.SpecificRiskESTArgs}
     if Args["ModelArgs"]['运行模式']=='串行':
         _SpecificRiskGeneration(Args)
     else:
         nTask = len(self.RiskESTDTs)
         nPrcs = min((nTask,self.Config.ModelArgs["子进程数"]))
         ProgBar = ProgressBar(max_value=nTask)
         Procs, Main2SubQueue, Sub2MainQueue = startMultiProcess(pid="0", n_prc=nPrcs, target_fun=_SpecificRiskGeneration,
                                                                 arg=Args, partition_arg=["RiskESTDTs"],
                                                                 main2sub_queue="None", sub2main_queue="Single")
         iProg = 0
         ProgBar.start()
         while (iProg<nTask):
             iPID,iErrorCode,iMsg = Sub2MainQueue.get()
             if iErrorCode==-1:
                 for iProc in Procs:
                     if iProc.is_alive(): iProc.terminate()
                 print('进程 '+iPID+' :运行失败:'+str(iMsg))
                 break
             else:
                 iProg += 1
                 ProgBar.update(iProg)
         ProgBar.finish()
         for iPID,iPrcs in Procs.items(): iPrcs.join()
     return 0
 def _genFactorAndSpecificReturn(self):
     Args = {"FT":self.FT,
             "RiskDB":self.RiskDB,
             "RegressDTs":self.RegressDTs,
             "TargetTable":self.TargetTable,
             "ModelArgs":self.Config.ModelArgs}
     if Args["ModelArgs"]['运行模式']=='串行':
         _FactorAndSpecificReturnGeneration(Args)
     else:
         nTask = len(self.RegressDTs)
         nPrcs = min(nTask, self.Config.ModelArgs["子进程数"])
         Procs,Main2SubQueue,Sub2MainQueue = startMultiProcess(pid="0", n_prc=nPrcs, target_fun=_FactorAndSpecificReturnGeneration,
                                                               arg=Args, partition_arg=["RegressDTs"], n_partition_tail=1,
                                                               main2sub_queue="None", sub2main_queue="Single")
         iProg = 0
         with ProgressBar(max_value=nTask) as ProgBar:
             while (iProg<nTask):
                 iPID, iErrorCode, iMsg = Sub2MainQueue.get()
                 if iErrorCode==-1:
                     for iProc in Procs:
                         if iProc.is_alive(): iProc.terminate()
                     self._QS_Logger.error("进程: "+iPID+" 运行失败: "+str(iMsg))
                     break
                 else:
                     iProg += 1
                     ProgBar.update(iProg)
         for iPID,iPrcs in Procs.items(): iPrcs.join()
     return 0
Exemple #3
0
 def _runMultiProcs(self, subprocess_num):
     nPrcs = min(subprocess_num, len(self.Modules))
     Args = {
         "mdl": self,
         "module_inds": np.arange(len(self.Modules)).tolist(),
         "OutputPipe": QSPipe()
     }
     TotalStartT = time.perf_counter()
     print("==========历史回测==========", "1. 初始化", sep="\n", end="\n")
     Procs, Main2SubQueue, Sub2MainQueue = startMultiProcess(
         pid="0",
         n_prc=nPrcs,
         target_fun=_runModel,
         arg=Args,
         partition_arg=["module_inds"],
         main2sub_queue=None,
         sub2main_queue="Single")
     nTask = nPrcs * len(self._QS_TestDateTimes)
     InitStage, CalcStage, EndStage = 0, 0, 0
     self._Output = {}
     while True:
         if CalcStage >= nTask:
             ProgBar.finish()
             print(
                 ("耗时 : %.2f" % (time.perf_counter() - CalcStageStartT, )),
                 "3. 结果生成",
                 sep="\n",
                 end="\n")
             CalcStage = -1
             EndStageStartT = time.perf_counter()
         if EndStage >= nPrcs:
             print(("耗时 : %.2f" % (time.perf_counter() - EndStageStartT, )),
                   ("总耗时 : %.2f" % (time.perf_counter() - TotalStartT, )),
                   "=" * 28,
                   sep="\n",
                   end="\n")
             break
         iStage = Sub2MainQueue.get()
         if iStage == 0:  # 初始化阶段
             if InitStage == 0:
                 print(
                     ("耗时 : %.2f" % (time.perf_counter() - TotalStartT, )),
                     "2. 循环计算",
                     sep="\n",
                     end="\n")
                 CalcStageStartT = time.perf_counter()
                 ProgBar = ProgressBar(max_value=nTask)
                 ProgBar.start()
             InitStage += 1
         elif iStage == 1:  # 计算阶段
             CalcStage += 1
             ProgBar.update(CalcStage)
         elif iStage == 2:  # 结果生成阶段
             EndStage += 1
             self._Output.update(Args["OutputPipe"].get())
     for iPID, iPrcs in Procs.items():
         iPrcs.join()
     return 0
 def _genFactorCovariance(self):
     Args = {"RiskDB":self.RiskDB,
             "RiskESTDTs":self.RiskESTDTs,
             "TargetTable":self.TargetTable,
             "ModelArgs":self.Config.ModelArgs,
             "FactorCovESTArgs":self.Config.FactorCovESTArgs,
             "EigenfactorRiskAdjustmentArgs":self.Config.EigenfactorRiskAdjustmentArgs,
             "FactorVolatilityRegimeAdjustmentArgs":self.Config.FactorVolatilityRegimeAdjustmentArgs}
     if Args["ModelArgs"]['运行模式']=='串行':
         _FactorCovarianceGeneration(Args)
     else:
         nTask = len(self.RiskESTDTs)
         nPrcs = min((nTask,self.Config.ModelArgs["子进程数"]))
         ProgBar = ProgressBar(max_value=nTask)
         Procs,Main2SubQueue,Sub2MainQueue = startMultiProcess(pid="0", n_prc=nPrcs, target_fun=_FactorCovarianceGeneration,
                                                               arg=Args, partition_arg=["RiskESTDTs"],
                                                               main2sub_queue="None", sub2main_queue="Single")
         iProg = 0
         ProgBar.start()
         while (iProg<nTask):
             iPID,iErrorCode,iMsg = Sub2MainQueue.get()
             if iErrorCode==-1:
                 for iProc in Procs:
                     if iProc.is_alive(): iProc.terminate()
                 print('进程 '+iPID+' :运行失败:'+str(iMsg))
                 break
             else:
                 iProg += 1
                 ProgBar.update(iProg)
         ProgBar.finish()
         for iPID,iPrcs in Procs.items():
             iPrcs.join()
     #if self.Config.ModelArgs["FactorVolatilityRegimeAdjustment"]:# TODO
         #print("Factor Volatility Regime Adjustment进行中...")
         #FactorReturnDates = pd.Series(self.RiskDB.getFactorReturnDateTime(self.TargetTable))
         #iFactorReturn = None
         #iFactorReturnDates = []
         #FactorVolatility = []# 用于FactorVolatilityRegimeAdjustment
         #iSampleDates = []# 用于FactorVolatilityRegimeAdjustment
         #for iDate in self.RiskESTDTs:
             #iLastDates = iSampleDates
             #iSampleDates = self.getTableDate(self.TargetTable,end_date=iDate)
             #if self.Config.FactorVolatilityRegimeAdjustmentArgs["样本长度"]>0:
                 #iSampleDates = iSampleDates[max((0,len(iSampleDates)-1-self.Config.FactorVolatilityRegimeAdjustmentArgs["样本长度"])):]
             #iNewSampleDates = list(set(iSampleDates).difference(set(iLastDates)))
             #iNewSampleDates.sort()
             #FactorVolatility = self.RiskDB.readFactorReturn(self.TargetTable,dates=None)
             #iFactorVolatility = pd.Series(np.diag(iFactorCov)**0.5)
         #print("Factor Volatility Regime Adjustment完成")
     return 0