Roy Lee
Roy Lee

Reputation: 29

Algorithm for efficient portfolio optimization

I'm trying to find the best allocation for a portfolio based on backtesting data. As a general rule, I've divided stocks into large caps and small/mid caps and growth/value and want no more than 80% of my portfolio in large caps or 70% of my portfolio in value. I need an algorithm that will be flexible enough to use for more than two stocks. So far, what I have is (including a random class called 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

The "function" I have so far just iterates through -5 to 6 in a sort of

for i in range (-5,6):
    ticker1AllocationPercent + i
    ticker2AllocationPercent - i
    #update the bestBalance if the new allocation is better

How would I modify this algorithm to work for 3, 4, 5, etc. stocks, and how would I go about changing the allocations for the large/small-mid cap stocks and such?

Upvotes: 0

Views: 355

Answers (1)

BhaskarT
BhaskarT

Reputation: 47

As mentioned in the above answer, typically Quadratic solver is used in such problems. You can use Quadratic solver available in Pyportfolio. See this link for more details.

Upvotes: 1

Related Questions