I'm trying to analyze the Exponential Moving Average (EMA) of some stocks using different timeframes (1 hour, 4 hours, 12 hours, and 1 day). Specifically, I need to:
Calculate the 200 EMA for each stock on these timeframes. Determine if each stock is above the 200 EMA for the respective timeframes. Display the results for each stock. I'm fetching stock prices using the yfinance library. However, I'm uncertain about the data-fetching aspect. To calculate a 200 EMA for the 12-hour timeframe, for example, would I need to fetch the data at a 12-hour interval, or can I use 1-hour interval data and somehow aggregate it?
Here's what I've done so far:
import yfinance as yf
tickers = "AAPL MSFT GOOGL AMZN TSLA"
period = "2mo"
interval = '1h'
data = yf.download(tickers=tickers, period=period, interval=interval, auto_adjust=True, prepost=False, threads=True, proxy=None)
df = data['Close'][tickers.split()]
def fetch_stock_info(tickers):
stock_info = {}
for ticker in tickers.split():
stock = yf.Ticker(ticker)
stock_info[ticker] = {
'Name': stock.info.get('longName'),
'MarketCap': stock.info.get('marketCap'),
}
return stock_info
stock_info = fetch_stock_info(tickers)
How can I modify this code to perform EMA analysis across the different specified timeframes? Any help would be greatly appreciated.