0

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.

1 Answer 1

0

EMA calculation is the same irrespective of the time period. The most generic one is your last case i.e. 200 EMA over the day. So basically, in that case, you need the closing price for 200 days and after that, your EMA would start populating.

Similarly, in the case of hourly EMA calculation, you need a closing price (like hourly, every 4 hours...) and you need 200 such data points. So based on your problem you need the pricing every hour and you could use it for hourly, 4-hour, and 12-hour calculations. EOD closing prices for 1-day calculation.

You have to think in terms of the data points that you need for EMA calculation and get those data points based on the time period.

2
  • Thanks, yes I know that, but the thing is that yahoo finance allow only 1h or 1 day interval, how to calculate the 200ema for the 4 and 12hour interval while using the 1h interval? shouldn't I fetch the data based on a 4hours interval's closing price? I was thinking of calling the polygon.io api twice for the 4 and 12H intervals
    – Ezio
    Jun 4 at 11:12
  • If you have 1 hour interval data then you already have 4/12 hour data. Just create a time series for 1 hour data and based on the time period pick up the datapoints. So for 4 hours you would pick up the datapoints 4,8,12 ... and similarly for 12 hour.
    – PraveenB
    Jun 4 at 23:44

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.