对于学校项目,我正在尝试计算在给定历史日期由三只股票组成的表现最高的投资组合(锐利率)。
我已经知道如何收集信息并计算python的平均回报和差异。但是,我不知道如何迭代数据并比较 所有可能的投资组合的结果。
例如:
result 1 = stockA + stock B + stock C
result 2 = stockB + Stock C + Stock D
result 3 = stockC + Stock D + Stock E
等等。
加上并发症,股票将被加权。我知道,对于每种组合中的3只股票,所有可能的权重都需要几天的时间来计算,所以我会做一些更精简的事情:
# checks to see which stock will perform better with higher weight:
trialstockA = (stockA * .75) + (stockB * .125) + (stockC * .125)
trialstockB = (stockA * .125) + (stockB * .75) + (stockC * .125)
trialstockC = (stockA * .125) + (stockB * .125) + (stockC * .75)
# if trialstockA should be weighed higher:
if trialstockA > trialstockB and trialstockC:
trialstockA2 = (stockA * .95) + (stockB * .025) + (stockC * .025)
trialstockA3 = (stockA * .85) + (stockB * .075) + (stockC * .075)
trialstockA4 = (stockA * .80) + (stockB * .10) + (stockC * .10)
trialstockA5 = (stockA * .70) + (stockB * .15) + (stockC * .15)
trialstockA6 = (stockA * .65) + (stockB * .175) + (stockC * .175)
trialstockA7 = (stockA * .60) + (stockB * .20) + (stockC * .20)
trialstockA8 = (stockA * .55) + (stockB * .225) + (stockC * .225)
trialstockA9 = (stockA * .50) + (stockB * .25) + (stockC * .25)
然而,这一部分并不重要。我只是不确定如何遍历每个可能的投资组合。我可以将数据导出到CSV文件,在该文件中它将显示为[NAME (string) ,MEAN RETURN (INT) ,VARIANCE (INT), RISK (INT)]
,例如[AAPL, .427, .1957, .442]
,或者我可以将它们作为变量留在scrapy项目中。我觉得CSV方法可能会更容易。
我还想知道将股票分成三组会有所帮助吗?
希望这是有道理的。不知道还有什么要提......
提前感谢您的帮助!
对于每种股票组合和每种权重组合,目标是以下计算,因为股票组合被命名为stockA,stockB和StockC。为了节省时间,我将把每个股票数据导出到csv,其中股票的均值,方差,标准差(风险)和回报列表(约100个数字)存储在不同的列中一排股票。
首先,需要在两种股票的每种组合之间找到相关性:
stockAB_corr = numpy.corrcoef(stockA_returns, stockB_returns)[0, 1]
stockAC_corr = numpy.corrcoef(stockA_returns, stockC_returns)[0, 1]
stockBC_corr = numpy.corrcoef(stockB_returns, stockC_returns)[0, 1]
然后,我们可以运行以下功能:
portfolio_return = (stockA[avgret] * stockA[weight]) + (stockB)
portfolio_variance = ((stockA[weight]^2) * stockA[variance]) + ((stockB[weight]^2) * stockB[variance]) + ((stockC[weight]^2) * stockC[variance]) + (2*stockA[weight]*stockB[weight]*stockAB_corr*stockA[risk]*stockB[risk]) + (2*stockA[weight]*stockC[weight]*stockAC_corr*stockA[risk]*stockC[risk]) + (2*stockB[weight]*stockC[weight]*stockBC_corr*stockB[risk]*stockC[risk])
portfolio_risk = portfolio_variance ** 0.5
Sharpe = (porfolio_return - 0.03)/portfolio_variance
夏普比率是最终结果。对于每只股票,已经计算了它们各自的方差和平均值。
答案 0 :(得分:1)
根据@ Aaron的评论,您可以使用itertools
模块,特别是combinations
和permutations
。
您可以使用combinations
创建所有可能股票列表中的股票集合,使用permutations
和set
进行权重的唯一排序,使用zip
合并它们,和来自生成器函数的yield
。
import itertools
stocks = ['stock{}'.format(x) for x in xrange(10)]
weights_list = [(0.95, 0.025, 0.025),
(0.90, 0.05, 0.05),
(0.85, 0.075, 0.075),
(0.80, 0.1, 0.1),
(0.75, 0.125, 0.125),
(0.70, 0.15, 0.15),
(0.65, 0.175, 0.175),
(0.60, 0.20, 0.20),
(0.55, 0.225, 0.225),
(0.50, 0.25, 0.25)]
def portfolios(stocks, weights_list):
for stock_triplet in itertools.combinations(stocks, 3):
for weights in weights_list:
unique_weight_orders = set(itertools.permutations(weights))
for weight_order in unique_weight_orders:
yield zip(stock_triplet, weight_order)
for port in portfolios(stocks, weights_list):
print port
输出看起来像这样:
>>> ...
[('stock0', 0.95), ('stock1', 0.025), ('stock2', 0.025)]
[('stock0', 0.025), ('stock1', 0.025), ('stock2', 0.95)]
[('stock0', 0.025), ('stock1', 0.95), ('stock2', 0.025)]
[('stock0', 0.075), ('stock1', 0.075), ('stock2', 0.85)]
[('stock0', 0.075), ('stock1', 0.85), ('stock2', 0.075)]
[('stock0', 0.85), ('stock1', 0.075), ('stock2', 0.075)]
[('stock0', 0.1), ('stock1', 0.1), ('stock2', 0.8)]
...