在几个熊猫数据帧优化上计算列

时间:2020-05-07 07:33:56

标签: python optimization finance

我有几个数据框(实际上是100个),其中包含纳斯达克股票最近20年的数据(每只股票1个数据框)。

我必须计算第一个月的夏普比率。

  def calculate(self, dataframes, calculationDates):
        for asset in dataframes:
            asset['Calculate'] = np.where(
                asset.index.isin(calculationDates), 1, 0)
            asset['PerfAverage'] = np.where(asset['Calculate'] == 1,  self.annualPeriod *
                                            asset['PerfPerc'].rolling(self.lookbackPeriodPerf).mean(), np.NaN)
            asset['Volatility'] = np.where(asset['Calculate'] == 1, math.sqrt(
                self.annualPeriod) * asset['PerfPerc'].rolling(self.lookbackPeriodVol).apply(np.std), np.NaN)
            asset['SharpeRatio'] = np.where(asset['Calculate'] == 1, asset['PerfAverage'] / np.power(
                asset['Volatility'], self.volatilityAttenuator), np.NaN)
        return dataframes

它可以工作,但是大约需要3分钟才能计算所有数据。

有什么方法可以优化它?

0 个答案:

没有答案