How to optimise my data frame processing? S&P Highs and Lows

I want to get daily if a stock price is in it’s 52-weeks low or high and for that I wrote this piece of code:

prices = yf.download(tickers, start=start, end=end)["Adj Close"]  max_min = pd.DataFrame(0, index=np.arange(len(prices)), columns=list(prices.columns))  for i in range(251, len(prices)):     for j in range(prices.shape[1]):         if prices.iloc[i, j] >= prices.iloc[:i].max()[j]:             max_min.loc[i, prices.columns[j]] = 1         elif prices.iloc[i, j] <= prices.iloc[:i].min()[j]:             max_min.loc[i, prices.columns[j]] = -1 

Where "tickers" is a list with all the tickers of the S&P 500 and the start date is oct from 2006 and end date is today.

prices.shape = (3451, 505)

It’s taking forever and I need to get this data daily. How can I optimise my code?

P.D: I ran an example with this code.

a = pd.DataFrame(np.random.randint(0,200, size=(10,10)), index=np.arange(10), columns=list("ABCDEFGHIJ"))  max_min = pd.DataFrame(0, index=np.arange(len(prices)), columns=list(prices.columns))  for i in range(4, len(a)): for j in range(a.shape[1]):     if a.iloc[i, j] >= a.iloc[:i].max()[j]:         max_min.loc[i, a.columns[j]] = 1     elif a.iloc[i, j] <= a.iloc[:i].min()[j]:         max_min.loc[i, a.columns[j]] = -1 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.