对于不太清晰的帖子标题表示歉意。 我有一个文件,我在其中调用 yfinance 来获取股票代码列表的股票数据。
import yfinance as yf
import pandas as pd
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
def fetch_stock_data(ticker):
info = yf.Tickers(ticker).tickers[ticker].info
data = [ticker, f"{info['currentPrice']}", f"{info['volume']}"]
return data
tickers = ['AAPL', 'AMZN', 'GOOG']
table_title = ['Symbol', 'Price', 'TotalVol']
df = pd.DataFrame(columns = table_title)
if __name__ == "__main__":
with ThreadPoolExecutor() as executor:
while True:
results = list(executor.map(fetch_stock_data, tickers))
print(results)
# Adds items from results
for result in results:
df.loc[len(df)] = result
print(df)
time.sleep(10)
这会生成以下输出:
Symbol Price TotalVol
0 AAPL 222.91 63519990
1 AMZN 197.93 96644650
2 GOOG 172.65 21385165
我有一个 while True: 循环,我在其中休眠 10 秒并再次执行循环。
我想添加一个“CurrVol”列,其输出如下所示。
Symbol Price CurrVol TotalVol
0 AAPL 222.91 6351 63519990
1 AMZN 197.93 9665 96644650
2 GOOG 172.65 23765 21385165
CurrVol 是前一次执行的 TotalVol 与本次执行的 TotalVol 之间的差异。
我需要将 TotalVol 传递给 fetch_stock_data,以便我可以计算差异并将其输出到 CurrVol 列中。
def fetch_stock_data(ticker):
info = yf.Tickers(ticker).tickers[ticker].info
data = [ticker, f"{info['currentPrice']}", f"{info['volume']-prevVolume}", f"{info['volume']}"]
return data
如何跟踪多个股票代码的 PrevVolume 并将它们传递到并发函数进行处理?
我没有提到的一个复杂因素是,当当前分钟翻到下一分钟时,PrevVolume 应重置为 0。
我花了一天的时间来解决这个问题,我想出了一个解决方案,在再次调用 yf 函数之前,我能够在新的数据框中保留最后的卷信息。
tickers = ['AAPL', 'AMZN', 'GOOG']
table_title = ['Symbol', 'Price', 'TotVolume']
prevVol_df = pd.DataFrame(columns = ['Symbol', 'PrevVolume'])
if __name__ == "__main__":
with ThreadPoolExecutor() as executor:
while True:
df = pd.DataFrame(columns = table_title)
results = list(executor.map(fetch_stock_data, tickers))
# Adds items from results
for result in results:
df.loc[len(df)] = result
# Convert TotVolume from string to number
df['TotVolume'] = pd.to_numeric(df['TotVolume'], errors='coerce')
# Copy volume data for each symbol to a new df.
prevVol_df = df[['Symbol', 'TotVolume']].copy()
prevVol_df.rename(columns={'TotVolume': 'PrevVolume'}, inplace=True)
# Create a new df by merging df and prevVol_df
# adding PrevVolume to the end of df.
tmp_df = pd.merge(df, prevVol_df, on='Symbol', how='left')
curr_volume = tmp_df['TotVolume'].astype(int) - tmp_df['PrevVolume'].astype(int)
tmp_df.insert(2, 'CurrVol', curr_volume)
# print the dataframe for columns Symbol, Price, CurrVolume and TotVolume
print(tmp_df.iloc[:, 0:4]) # or use df.iloc[:, :-1]
# reset dataframes
df.drop(df.index, inplace=True)
tmp_df.drop(tmp_df.index, inplace=True)
time.sleep(10)