Python – Simple Stock Analysis with yfinance

Sometimes, the graphs of stocks are useful. Sometimes these are not. In general, do your own research, none of this is financial advice.

And while doing that, if you want to analyze stocks with just a few lines of python, this article might help? This simple yet powerful script helps you spot potential buy and sell opportunities for Apple (AAPL) using two classic technical indicators: moving averages and RSI.

Understanding the Strategy

1. SMA Crossover: The Trend Following Signal

The script first calculates two Simple Moving Averages (SMA):

  • 20-day SMA: The average closing price over the last 20 trading days (short-term trend)

  • 50-day SMA: The average over 50 days (longer-term trend)

The crossover strategy is simple:

  • Buy Signal: When the 20-day SMA crosses above the 50-day SMA (suggests upward momentum)

  • Sell Signal: When the 20-day SMA crosses below the 50-day SMA (suggests downward momentum)

This works because moving averages smooth out price noise, helping identify the overall trend direction.

2. RSI: The Overbought/Oversold Indicator

The Relative Strength Index (RSI) measures whether a stock is overbought or oversold:

  • RSI < 30: Oversold (potential buying opportunity)

  • RSI > 70: Overbought (potential selling opportunity)

By combining SMA crossovers (trend confirmation) and RSI extremes (timing), we get stronger signals.

This plot is generated with less than 40 lines of python code

The code looks like that:

The code above, but in way more details is explained in the YT video below:

And it is available in GitHub as well.

Tagged with: , , , , , , , ,