Python,Pands,股票投资组合滚动值时间序列问题

我已经在Python中创建了一个时间序列,用于计算每个给定日期的滚动投资组合市场价值。现在的时间序列看起来像这样(总数是NHY.OL和YAR.OL的总和)

tickers     NHY.OL  YAR.OL   total
date                              
2020-07-27    0.00   763.60  763.60
2020-07-28   26.49   764.80  791.29
2020-07-29   26.39   755.80  782.19
2020-07-30   25.05   744.00  769.05
2020-07-31   25.42   380.70  406.12

此时间序列由头寸表和包含股票收盘价的数据集建立,头寸表是动态的,这意味着它将从Django模型收集数据,用户可以在其中输入其持股量,因此提供的这个数据集是解决我遇到的这个问题的简单示例。

位置表:

         Date Direction  Ticker  Price  Volume  FX-rate  Comission  Short  Cost-price
0  2020-07-27       Buy  YAR.OL  381.0   2      1.0        0.0  False       381.0
1  2020-07-31      Sell  YAR.OL  380.0  -1      1.0        0.0  False      -380.0

[2 rows x 9 columns]
         Date Direction  Ticker  Price  Volume  FX-rate  Comission  Short  Cost-price
2  2020-07-28       Buy  NHY.OL   26.5   1      1.0        0.0  False        26.5

当日股票价格数据集:

            NHY.OL  YAR.OL
date                      
2020-07-27   26.35   381.8
2020-07-28   26.49   382.4
2020-07-29   26.39   377.9
2020-07-30   25.05   372.0
2020-07-31   25.42   380.7

但是,滚动投资组合价值中的初始价格是错误的。例如,在2020年7月27日,用户以381的价格购买了所购买的股票,因此市场价值应为762。投资组合价值不是由购买价格计算的,而是由市场收盘价计算的。

如何解决此问题,所以最终产品看起来更像这样?

我希望它的外观:

tickers     NHY.OL  YAR.OL   total
date                              
2020-07-27   0.00    762.0   762.0
2020-07-28   26.50   764.80  791.30
2020-07-29   26.39   755.80  782.19
2020-07-30   25.05   744.00  769.05
2020-07-31   25.42   380.70  406.12

StackOverflow的一位出色的程序员帮助我完成了这段代码。该线程的链接Python pandas,stock portfolio value timeseries

代码

from io import StringIO
import pandas as pd

# create data frame with closing prices
data = '''date YAR.OL NHY.OL
2020-07-27  381.799988  26.350000
2020-07-28  382.399994  26.490000
2020-07-29  377.899994  26.389999
2020-07-30  372.000000  25.049999
2020-07-31  380.700012  25.420000
'''
closing_prices = (pd.read_csv(StringIO(data),sep='\s+',engine='python',parse_dates=['date']
                            )
                  .set_index('date')
                  @R_404_6407@rt_index()
                  @R_404_6407@rt_index(axis=1)
                 )
print(closing_prices.round(2))

输出

           NHY.OL  YAR.OL
date                      
2020-07-27   26.35   381.8
2020-07-28   26.49   382.4
2020-07-29   26.39   377.9
2020-07-30   25.05   372.0
2020-07-31   25.42   380.7

定义位置表:

positions = [
    ('YAR.OL','2020-07-27',2),('YAR.OL','2020-07-31',-1),('NHY.OL','2020-07-28',1),]
# changed cost_price to volume
positions = pd.DataFrame(positions,columns=['tickers','date','volume'])
positions['date'] = pd.to_datetime(positions['date'])

positions = (positions.pivot(index='date',columns='tickers',values='volume')
             @R_404_6407@rt_index()
             @R_404_6407@rt_index(axis=1)
            )
positions = positions.reindex( closing_prices.index ).fillna(0).cumsum()
print(positions)

输出

tickers     NHY.OL  YAR.OL
    date                      
    2020-07-27     0.0     2.0  # <-- these are transaction volumes
    2020-07-28     1.0     2.0
    2020-07-29     1.0     2.0
    2020-07-30     1.0     2.0
    2020-07-31     1.0     1.0

计算投资组合价值:

port_value = positions * closing_prices
port_value['total'] = port_value.sum(axis=1)
print(port_value.round(2))

现在的最终投资组合价值:

tickers     NHY.OL  YAR.OL   total
date                              
2020-07-27    0.00   762.00  763.6
2020-07-28   26.49   764.80  791.29
2020-07-29   26.39   755.80  782.19
2020-07-30   25.05   744.00  769.05
2020-07-31   25.42   380.70  406.12 

总结:目标是,当添加新头寸时,用于计算当时市价的价格将从头寸表价格栏中获取。 例如:

Date         Calculation comment     Calculation
2020-07-27 - Volume * Price paid     2 * 381
2020-07-28 - Volume * Market price   2* 282.4
....
If a user buys more shares of the same stock then for the day they bouth it,it would be: (New volume * prie paid)+(PrevIoUs volume * Market price)
....
Date          Calculation comment                                       Calculation
2020-07-29 - (New volume * prie paid)+(PrevIoUs volume * Market price) (1* 380) + (2 * 377.9)
2020-07-30 - Volume * Market price                                      3 * 372.0

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...