- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个程序来跟踪我拥有的股票并随着值(value)的变化调整每只股票的百分比。
我遇到的问题是随着投资组合的变化跟踪百分比本身的设计/算法(我有两个限制是我只使用百分比并且有几天我不买卖任何东西但那些日子必须是占。所以那些日子里的股票保持不变,但百分比发生变化,因为股票的基本值(value)正在发生变化。如果我拥有 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 的数据,而不是其他日子的数据。
最佳答案
stock_returns
”是否包含您需要的所有代码。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/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!