```python
import pandas as pd
import numpy as np
import yfinance as yf
def get_stock_data(tickers, start_date, end_date):
"""
Function to fetch historical stock data from Yahoo Finance API.
Parameters:
tickers (list): List of stock tickers (e.g., ['AAPL', 'GOOG', 'MSFT']).
start_date (str): Start date for historical data in 'YYYYMMDD' format.
end_date (str): End date for historical data in 'YYYYMMDD' format.
Returns:
DataFrame: DataFrame containing historical stock data for the specified tickers.
"""
Initialize an empty DataFrame
stock_data = pd.DataFrame()
Fetch historical data for each ticker
for ticker in tickers:
Use yfinance to fetch data
data = yf.download(ticker, start=start_date, end=end_date)
Add a 'Ticker' column to identify the stock
data['Ticker'] = ticker
Append data to the main DataFrame
stock_data = stock_data.append(data)
return stock_data
def calculate_returns(stock_data):
"""
Function to calculate daily returns for each stock in the DataFrame.
Parameters:
stock_data (DataFrame): DataFrame containing historical stock data.
Returns:
DataFrame: DataFrame containing daily returns for each stock.
"""
Calculate daily returns
stock_data['Daily_Return'] = stock_data['Adj Close'].pct_change()
return stock_data
def select_top_performers(stock_data, num_top):
"""
Function to select top performing stocks based on cumulative returns.
Parameters:
stock_data (DataFrame): DataFrame containing historical stock data.
num_top (int): Number of top performers to select.
Returns:
DataFrame: DataFrame containing data for the top performing stocks.
"""
Calculate cumulative returns for each stock
cumulative_returns = stock_data.groupby('Ticker')['Daily_Return'].apply(lambda x: (1 x).cumprod() 1)
Select top performers based on cumulative returns
top_performers = cumulative_returns.iloc[1].nlargest(num_top)
Filter stock_data for top performers
top_data = stock_data[stock_data['Ticker'].isin(top_performers.index)]
return top_data
Example usage:
if __name__ == "__main__":
Define parameters
tickers = ['AAPL', 'GOOG', 'MSFT', 'AMZN', 'TSLA'] Example tickers
start_date = '20200101'
end_date = '20240525'
num_top = 3 Number of top performers to select
Fetch historical stock data
stock_data = get_stock_data(tickers, start_date, end_date)
Calculate daily returns
stock_data = calculate_returns(stock_data)
Select top performers
top_performers_data = select_top_performers(stock_data, num_top)
print(top_performers_data.head())
```
这段代码实现了一个简单的股票池筛选功能。它首先通过Yahoo Finance API获取指定股票的历史数据,然后计算每只股票的每日收益率。接着,根据累积收益率选取表现最好的几只股票,并返回这些股票的历史数据。你可以根据需要修改代码中的股票列表、起止日期和选取的股票数量。