Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

python - How to replace 'for' loop with more efficient code for stock market analysis example

I am working on a stock market analysis project. I am attempting to find the highest price in the past 5 days, the volume on the day of the highest price, and how many days before that highest price occurred.

I have constructed a solution utilizing a couple of 'for' loops, but would like to find a more efficient way to code this without utilizing 'for' loops. Any suggestions would be appreciated.

A1 = pd.merge(A, B, left_index = True, right_index = True)
A1["Date"] = A1.index
A1.reset_index(inplace = True)

### 5 Day High and Volume
Indexes = []
for index in range(len(A1.index) - 5):
    M = 0
    H = 0
    for i in range(1,6):
        if H < A1.iloc[i+index,2]:
            H = A1.iloc[i+index,2]     
            M = i+index
    Indexes.append(M)

Vol = pd.DataFrame(columns = ['B','C'])
Vol5 = []
DH5 = []
Z = []
count = 0
for i in Indexes:
   Vol5.append(A1.iloc[i,1]) 
   DH5.append(A1.iloc[i,2]) 
   Z.append(count - i)
   count += 1
for i in range(5): 
    Vol5.append(np.nan)
    DH5.append(np.nan)
    Z.append(np.nan)
Vol['B'] = Vol5
Vol.index = A1['Date']
Vol['C'] = DH5
Vol['D'] = Z
question from:https://stackoverflow.com/questions/66056888/how-to-replace-for-loop-with-more-efficient-code-for-stock-market-analysis-exa

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest using the rolling method to find the index of the maximum value computed over the previous 5 rows:

import pandas as pd
import numpy as np

d={'date':np.random.random(10), 'open':np.random.random(10), 'high':np.random.random(10), 'low':np.random.random(10), 'close':np.random.random(10), 'volume':np.random.random(10)}    
A1=pd.DataFrame(data=d)

df=A1.rolling(window=5).apply(np.argmax).shift(1).fillna(0)

Then to find the volume associated with this maximum value (in this example for the highest column):

A1['volume associated with maximum price']=A1.iloc[df.high]['volume']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...