简体   繁体   中英

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

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM