Lesson 1 · Finance and Stock Market Analytics
Introduction to Technical Analysis for Finance and Stock Market Analytics
This lesson explores how to use Python for real-world finance and stock market analytics. Finance and stock analytics are essential for understanding…
- CourseFinance and Stock Market Analytics
- Lesson1 of 16
- Video23 min
- FormatJupyter notebook · 20 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbIntroduction to Finance and Stock Market Analytics#
- This lesson explores how to use Python for real-world finance and stock market analytics.
- Finance and stock analytics are essential for understanding company valuation, risk, and trends.
- We will use real datasets and solve practical analytics problems, from basic to advanced.
- By the end, you will be able to load, analyze, and visualize financial data and extract actionable insights.
- All examples use real data from yfinance and GitHub.
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np
import yfinance as yf
Understanding the Data in Finance and Stock Market Analytics#
- Financial data covers prices, fundamentals, and trading activity for companies and funds.
- Data can come in 'wide' format (one column per ticker) or 'long' format (each row is date/ticker pair).
- The structure of data impacts how you filter, reshape, and analyze it.
- Common mistakes: flattening index incorrectly, mislabeling tickers, or misunderstanding real vs. synthetic data.
url = 'https://raw.githubusercontent.com/datasets/s-and-p-500-companies/main/data/constituents.csv'
sp500 = pd.read_csv(url)
print(sp500.shape)
print(sp500.head(3))
tickers = ['AAPL','MSFT','GOOGL','AMZN','TSLA']
prices = yf.download(tickers, period='1y', auto_adjust=True, progress=False)
prices = prices['Close'].reset_index()
prices.columns.name = None
print(prices.shape)
print(prices.head(3))
tickers = ['AAPL','MSFT','GOOGL','AMZN','TSLA']
ohlcv = yf.download(tickers, period='1y', auto_adjust=True, progress=False)
ohlcv = ohlcv.stack(future_stack=True).rename_axis(['Date','Ticker']).reset_index()
ohlcv.columns.name = None
print(ohlcv.shape)
print(ohlcv.head(3))
tsla_df = ohlcv[ohlcv['Ticker'] == 'TSLA'].copy()
tsla_df['daily_return'] = tsla_df['Close'].pct_change() * 100
print(tsla_df[['Date','Close','daily_return']].head(7))
aapl_returns = ohlcv[ohlcv['Ticker'] == 'AAPL'].copy()
msft_returns = ohlcv[ohlcv['Ticker'] == 'MSFT'].copy()
aapl_returns['cumulative_return'] = (1 + aapl_returns['Close'].pct_change()).cumprod() - 1
msft_returns['cumulative_return'] = (1 + msft_returns['Close'].pct_change()).cumprod() - 1
print(aapl_returns[['Date','cumulative_return']].tail(3))
print(msft_returns[['Date','cumulative_return']].tail(3))
close_pivot = ohlcv.pivot(index='Date', columns='Ticker', values='Close')
print(close_pivot.shape)
print(close_pivot.head(3))
fundamentals_tickers = ['AAPL','MSFT','GOOGL','AMZN','TSLA','NVDA','META','JPM','JNJ','XOM','WMT','V']
rows = []
for tk in fundamentals_tickers:
info = yf.Ticker(tk).info
rows.append({
'ticker': tk,
'sector': info.get('sector', 'Unknown'),
'market_cap': info.get('marketCap'),
'pe_ratio': info.get('trailingPE'),
'eps': info.get('trailingEps'),
'profit_margin':info.get('profitMargins'),
'debt_equity': info.get('debtToEquity')
})
fund_df = pd.DataFrame(rows)
print(fund_df.shape)
print(fund_df.head(3))
sector_stats = fund_df.groupby('sector').agg({'market_cap':'mean','pe_ratio':'mean','profit_margin':'mean'}).round(2)
print(sector_stats)
np.random.seed(42)
print('Random seed set to 42 for reproducibility')
portfolio_tickers = ['AAPL','MSFT','GOOGL','AMZN','TSLA','NVDA','META','NFLX','JPM','JNJ']
sector_map = {'AAPL':'Tech','MSFT':'Tech','GOOGL':'Tech','AMZN':'Consumer',
'TSLA':'Auto','NVDA':'Tech','META':'Tech','NFLX':'Media',
'JPM':'Finance','JNJ':'Health'}
prices = yf.download(portfolio_tickers, period='1y', auto_adjust=True, progress=False)['Close']
rows = []
for tk in portfolio_tickers:
buy = round(float(prices[tk].iloc[0]), 2)
cur = round(float(prices[tk].iloc[-1]), 2)
shares = int(np.random.randint(5, 100))
rows.append({'ticker': tk, 'shares': shares, 'buy_price': buy, 'cur_price': cur, 'sector': sector_map[tk]})
portfolio_df = pd.DataFrame(rows)
portfolio_df['gain_pct'] = np.round((portfolio_df['cur_price'] - portfolio_df['buy_price']) / portfolio_df['buy_price'] * 100, 2)
print(portfolio_df.shape)
print(portfolio_df.head(3))
sector_summary = portfolio_df.groupby('sector').agg({'gain_pct':'mean','shares':'sum'})
print(sector_summary)
close_pivot['AAPL_return'] = close_pivot['AAPL'].pct_change()
close_pivot['AAPL_volatility'] = close_pivot['AAPL_return'].rolling(window=21).std() * np.sqrt(252)
close_pivot['AAPL_cum'] = (1 + close_pivot['AAPL_return']).cumprod()
cum_max = close_pivot['AAPL_cum'].cummax()
drawdown = close_pivot['AAPL_cum'] / cum_max - 1
max_drawdown = drawdown.min()
print(f"Annualized volatility: {close_pivot['AAPL_volatility'].iloc[-1]:.4f}")
print(f"Maximum drawdown: {max_drawdown:.2%}")
aapl_data = yf.download('AAPL', period='1y', auto_adjust=True, progress=False)[['Close','Volume']].reset_index()
aapl_data.columns = ['date','close','volume']
aapl_data['sma20'] = aapl_data['close'].rolling(20).mean().round(2)
aapl_data['sma50'] = aapl_data['close'].rolling(50).mean().round(2)
aapl_data['signal'] = np.where(aapl_data['sma20'] > aapl_data['sma50'], 1, -1)
print(aapl_data.tail(3))
nan_count = ohlcv.isna().sum().sum()
print(f'Total missing values in OHLCV data: {nan_count}')
ohlcv_clean = ohlcv.dropna()
print(f'Shape after dropping NaNs: {ohlcv_clean.shape}')
try:
data = yf.download('ZZZZ_NOT_A_TICKER', period='1y', auto_adjust=True, progress=False)
print('Data shape:', data.shape)
except Exception as e:
print('Download failed:', e)
aapl_filtered = ohlcv[ohlcv['Ticker']=='AAPL'][['Date','Close']].reset_index(drop=True)
print(aapl_filtered.head(3))
# Calculate 7-day rolling average closing price for TSLA
tsla_close = ohlcv[ohlcv['Ticker']=='TSLA'].copy()
tsla_close['rolling_avg'] = tsla_close['Close'].rolling(7).mean().round(2)
print(tsla_close[['Date', 'Close', 'rolling_avg']].tail(7))
tech_syms = sp500[sp500['GICS Sector']=='Information Technology']['Symbol'].tolist()[:5]
tech_prices = yf.download(tech_syms, period='1y', auto_adjust=True, progress=False)['Close'].reset_index()
returns = tech_prices[tech_syms].pct_change().mean() * 100
summary = pd.DataFrame({'ticker': tech_syms, 'avg_daily_return_pct': returns.round(3).values})
print(summary)
summary.to_csv('tech_returns_summary.csv', index=False)
print('Results saved to tech_returns_summary.csv')
Next steps: Keep learning and practicing!#
- Practice these analytics examples with your favourite stocks.
- Explore the yfinance and pandas documentation for deeper skills.
- Watch our channel on YouTube for advanced finance Python projects!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



