python - 获取列表中项目的错误值?设计问题还是逻辑问题?

标签 python algorithm

我正在尝试创建一个程序来跟踪我拥有的股票并随着值(value)的变化调整每只股票的百分比。

我遇到的问题是随着投资组合的变化跟踪百分比本身的设计/算法(我有两个限制是我只使用百分比并且有几天我不买卖任何东西但那些日子必须是占。所以那些日子里的股票保持不变,但百分比发生变化,因为股票的基本值(value)正在发生变化。如果我拥有 50% 的 IBM 和 50% 的谷歌,谷歌的股票上涨而 IBM 的下跌,那么谷歌的权重有增加了,而 IBM 的已经下降了)。我觉得我是从这个简单的目标开始的,但随着我进入不同的阶段,我不断地把它复杂化,所以这是我的方法,但我完全愿意放弃任何或所有效率低下的部分。

我首先创建两个词典。 holding_stockholding_stock_weight,每个的键是日期,值是股票或其权重的列表。例如:

    holding_stock[2012-07-17] = ["RY", "IBM"]
    holding_stock_weight[2012-07-17] = [50.0, 50.0]
    holding_stock[2012-07-18] = ["GS", "A", "IBM"]
    holding_stock_weight[2012-07-18] = [20.0,30.0,50.0]
    holding_stock[2012-07-23] = ["GS", "A", "IBM", "BSE"]
    holding_stock_weight[2012-07-23] = [10.0,35,40,3]

所有股票信息都是返回的对象列表(按日期排序)。我的想法是从最早的日期(在本例中为 2012-07-17)开始,然后在调整百分比的同时一直到当前日期(我认为这是最简单的方法)。我可以隔离每个不同的日期,除非日期作为我的 holding_stock 字典中的键存在,否则我将继续使用相同的值。最后,在每天结束时,我想获取每只股票的返回值,并将其除以每日总计以更新百分比。

但是我的设计有问题,很多天我都None作为每日返回,我的数字看起来很奇怪。

这是我到目前为止所做的事情(我尽我所能对其进行评论以解释我的逻辑,对于它看起来多么丑陋,我提前表示歉意。我认为我的简单方法已经变得过于复杂并且可能远非如此现在最好的解决方案)

current_holding_date = fund_date[0] #this is used to track which version of fund is current
current_date = fund_date[0] #this variable used to track date changes
current_holdings = holding_stock[current_holding_date] #list of current stocks being held
current_weight = holding_stock_weight[current_holding_date] #list of current weights of stock
current_return = [None]*len(current_holdings) #create a blank lists of the number of items we need so we can put in returns here
total = 0 #init variable to keep overall total
today_total = 0 #init variable to keep track of daily total
for x in stock_returns: #stock_returns is a list of objects that contains 3 items per object, x.ticker=stockname, x.date=date, x.close=price
    if x.date != current_date: #this checks if the day has changed.
        #this is where i planned to revalue the new percentages 
        print 'new date', x.date
        #print 'todays total is ', today_total
        #get new percentages
        for item in range(len(current_holdings)):
            #TODO: this skips the first and last item
            pass
            print current_holdings[item], ' - ', current_weight[item], current_return[item]
        today_total = 0
        current_date = x.date

    if x.date in fund_date:  #fund_date is just a list of all the days we can pick from
        current_holding_date = x.date #change current date to the new day if it exists in the database
        current_holdings = holding_stock[current_holding_date] #replace new holdings
        current_weight = holding_stock_weight[current_holding_date] #replace new weights
        current_return = [None]*len(current_holdings) #recreate area to hold results
        #print 'we just hit a date' 

    if x.ticker in current_holdings: #go through each object(its already sorted by date) and if the stock exists in the current list then calculate the return
        location = current_holdings.index(x.ticker)
        today_total = today_total + ((current_weight[location]  * 0.01) * x.close)
        current_return[location] = ((current_weight[location]  * 0.01) * x.close)
        #print x.date, ': ', x.ticker, current_weight[location] * 0.01 ,' * ', x.close, ' = ', current_return[location], ' total = ', total
    #print current_holding_date
    #print x.ticker, x.date, x.close

下面是我运行上面代码的结果:

2012-07-17 [u'RY', u'IBM'] [50.0, 50.0]
2012-07-18 [u'GS', u'A', u'IBM'] [20.0, 30.0, 50.0]
2012-07-23 [u'GS', u'A', u'IBM', u'BSE'] [10.0, 35.0, 40.0, 3.0]
new date 2012-07-18
RY  -  50.0 25.865
IBM  -  50.0 None
new date 2012-07-19
GS  -  20.0 None
A  -  30.0 None
IBM  -  50.0 None
new date 2012-07-20
GS  -  20.0 19.0
A  -  30.0 11.538
IBM  -  50.0 97.67
new date 2012-07-23
GS  -  20.0 18.832
A  -  30.0 11.268
IBM  -  50.0 96.225
new date 2012-07-24
GS  -  10.0 None
A  -  35.0 None
IBM  -  40.0 None
BSE  -  3.0 None

我怀疑这与我分配和更改 current_holdings、current_weight 和 current_return 列表的方式有关,但我看不到我的错误。出于某种原因,我只获得 2012-07-20 的数据,而不是其他日子的数据。

最佳答案

  1. 检查您的“stock_returns”是否包含您需要的所有代码。
  2. 在您的内部循环中,您首先要执行 current_return = [None]*len(current_holdings),然后才执行:

print current_holdings[item], ' - ', current_weight[item], current_return[item]

因此,即使您的代码正确计算了 current_return,您也需要在打印之前重置此变量。

编辑:

您正在使用 list.index() 来查找特定代码在您的 current_holdings 列表中的索引。如果 current_holdings 包含重复项,这样的调用将总是返回第一次出现的 x.ticker(参见关于 Sequence Types 的 Python 文档)

location = current_holdings.index(x.ticker)
today_total = today_total + ((current_weight[location]  * 0.01) * x.close)
current_return[location] = ((current_weight[location]  * 0.01) * x.close)

关于python - 获取列表中项目的错误值?设计问题还是逻辑问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663452/

相关文章:

Python 二维数组 bool 值缩减

c - Linux中crypt函数的时间复杂度是多少?

javascript - 使用本轮和傅立叶变换绘制/渲染 3D 对象 [动画]

c++ - 提出链表的简单公式

python - 将属性列表插入python中的对象列表

python - 将数据从一个 python 文件返回到另一个文件的最佳方法

使用动态编程进行字符串操作

algorithm - 修改堆的生成排列的算法

python - 删除所有内容,但保留数字和点

python - geopandas 的最快方法(读取和空间连接)