【发布时间】:2022-03-05 21:12:03
【问题描述】:
我正在尝试根据回测数据为投资组合找到最佳配置。作为一般规则,我将股票分为大盘股和中小盘股以及成长股/价值股,并且不希望超过 80% 的大盘股投资组合或 70% 的价值投资组合。我需要一种足够灵活的算法,可以用于两只以上的股票。到目前为止,我所拥有的是(包括一个名为 Ticker 的随机类):
randomBoolean=True
listOfTickers=[]
listOfLargeCaps=[]
listOfSmallMidCaps=[]
largeCapAllocation=0
listOfValue=[]
listOfGrowthBlend=[]
valueAllocation=0
while randomBoolean:
tickerName=input("What is the name of the ticker?")
tickerCap=input("What is the cap of the ticker?")
tickerAllocation=int(input("Around how much do you want to allocate in this ticker?"))
tickerValue=input("Is this ticker a Value, Growth, or Blend stock?")
tickerName=Ticker(tickerCap,tickerValue,tickerAllocation,tickerName)
listOfTickers.append(tickerName)
closer=input("Type DONE if you are finished. Type ENTER to continue entering tickers")
if closer=="DONE":
randomBoolean=False
for ticker in listOfTickers:
if ticker.cap==("Large" or "large"):
listOfLargeCaps.append(ticker)
else:
listOfSmallMidCaps.append(ticker)
if ticker.value==("Value" or "value"):
listOfValue.append(ticker)
else:
listOfGrowthBlend.append(ticker)
for largeCap in listOfLargeCaps:
largeCapAllocation +=largeCap.allocation
if largeCapAllocation>80:
#run a function that will readjust ticker stuff and decrease allocation to large cap stocks
for value in listOfValue:
valueAllocation+=value.allocation
if valueAllocation>70:
#run a function that will readjust ticker stuff and decrease allocation to value stocks
到目前为止,我的“函数”只是以某种方式迭代 -5 到 6
for i in range (-5,6):
ticker1AllocationPercent + i
ticker2AllocationPercent - i
#update the bestBalance if the new allocation is better
我将如何修改此算法以适用于 3、4、5 等股票,以及我将如何更改大/中小盘股等的分配?
【问题讨论】:
-
通常使用二次规划求解器。你不想实现它。只需使用现有的。并阅读一本关于投资组合优化的书。
-
如果您需要编程方面的帮助(这就是我们在这里所做的),如果您不认为每个人都熟悉您的问题的术语和概念,这通常会有所帮助领域(投资和投资组合?)。这样你可以提出的是一个编程问题,而不是一个问题域问题。如果它确实是一个问题域问题,那么最好在站点上询问特定问题域的问题。如果它是both,那么看看您是否可以将您的问题分成不同的部分可能会有所帮助。 (此外,许多特定领域的网站都有专门从事该领域的程序员)。
标签: python algorithm optimization finance portfolio