我正在尝试创建一个程序来跟踪我拥有的股票并随着价值的变化调整每只股票的百分比。
我遇到的问题是随着投资组合的变化跟踪百分比本身的设计/算法(我有两个限制是我只使用百分比并且有些日子我不买卖任何东西,但必须考虑那些日子。所以股票在那些日子里保持不变,但是百分比变化,因为股票的基础价值在变化。如果我拥有 50% 的 IBM 和 50% 的谷歌,谷歌的股票上涨而 IBM 的股票下跌,那么谷歌的权重增加了,而 IBM 的已经下降)。我觉得我是从这个简单的目标开始的,但随着我进入不同的阶段,我不断地把它复杂化,所以这是我的方法,但我完全愿意消化任何或所有效率低下的部分。
我首先创建两个字典。holding_stock
和holding_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 的数据,而不是其他日子的数据。