python-3.x - 智能测试版投资组合 : How to generate the weights based on dollar volume traded for each date

标签 python-3.x

def generate_dollar_volume_weights(close, volume):
     """
     Generate dollar volume weights.

     Parameters
     ----------
     close : DataFrame
           Close price for each ticker and date
     volume : str
            Volume for each ticker and date

     Returns
     -------
     dollar_volume_weights : DataFrame
                           The dollar volume weights for each ticker and date
      """
     assert close.index.equals(volume.index)
     assert close.columns.equals(volume.columns)

     #TODO: Implement function
     dollar_volume_weights=np.cumsum(close * volume) / np.cumsum(volume)

     return dollar_volume_weights
 project_tests.test_generate_dollar_volume_weights(generate_dollar_volume_weights)#testing the function

所以我的结果如下

INPUT close:
                  XHNP        FCUB        ZYRP
2005-09-09 35.44110000 34.17990000 34.02230000
2005-09-10 92.11310000 91.05430000 90.95720000
2005-09-11 57.97080000 57.78140000 58.19820000
2005-09-12 34.17050000 92.45300000 58.51070000

INPUT volume:
                        XHNP              FCUB              ZYRP
2005-09-09  9836830.00000000 17807200.00000000  8829820.00000000
2005-09-10 82242700.00000000 68531500.00000000 48160100.00000000
2005-09-11 16234800.00000000 13052700.00000000  9512010.00000000
2005-09-12 10674200.00000000 56831300.00000000  9316010.00000000

OUTPUT dollar_volume_weights:
                  XHNP        FCUB        ZYRP
2005-09-09 35.44110000 34.17990000 34.02230000
2005-09-10 86.05884636 79.32405834 82.13590461
2005-09-11 81.84884187 76.49494177 78.71200871
2005-09-12 77.57172242 82.30022612 76.22980471

EXPECTED OUTPUT FOR dollar_volume_weights:
                 XHNP       FCUB       ZYRP
2005-09-09 0.27719777 0.48394253 0.23885970
2005-09-10 0.41632975 0.34293308 0.24073717
2005-09-11 0.41848548 0.33536102 0.24615350
2005-09-12 0.05917255 0.85239760 0.08842984

我是这里的初学者,我真的不明白我在这里缺少什么,我认为生成美元体积权重的表达式应该类似于 dollar_volume_weights=np.cumsum(close *volume)/np.cumsum(体积) 有人可以告诉我为什么我的结果不同吗?

最佳答案

我认为你应该这样做close * volume并除以 np.sum(x, axis=1) 的总和 ( volume * close )并指定axis=1 。此外,您可以使用div()此除法的函数并指定 axis=0 .

关于python-3.x - 智能测试版投资组合 : How to generate the weights based on dollar volume traded for each date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869809/

相关文章:

python - 导入错误: cannot import name 'tqdm' from 'tqdm'

python - h5py文件和pickle文件保存模型的区别

python - 熄灯

python - 在虚拟环境中使用 Apt-Get Python 包

python - 如何添加两个 python 字典,使不等于认为变为零?

python - 变量赋值错误

python - 类型错误 : expected string or bytes-like object – with Python/NLTK word_tokenize

python-3.x - 连接拒绝在 docker 上使用 Postgresql

python - 命名空间与常规包

python - 如何在列表列表中找到最常见的元素?