Sign inGet started
← Back to all guides

Introduction to Python for banking

By Filip Žitný

Updated on March 6, 2024

Python is an incredibly versatile programming language widely adopted in the financial industry. It is used for tasks ranging from data analysis and financial modeling to building automated trading systems and risk management tools. Deepnote is a collaborative, cloud-based platform that allows data scientists and analysts to write, run, and share Python code in notebooks, making it an excellent choice for financial projects. Its integration with powerful data analysis libraries and real-time collaboration features enhances productivity and fosters teamwork.

Loading and exploring financial data

Importing libraries

Before working with financial data, you need to import the necessary libraries:

  • pandas: for data manipulation and analysis.
  • numpy: for numerical operations.
  • matplotlib: for data visualization.
  • yfinance: for fetching financial data from Yahoo Finance.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf

Downloading financial data

Using yfinance: This library allows you to easily download historical market data. It provides data on stocks, ETFs, indices, and more.

# Example: Fetching data for Apple Inc. (AAPL)
data = yf.download('AAPL', start='2020-01-01', end='2023-01-01')

This code downloads historical price data for Apple Inc. (AAPL) from January 1, 2020, to January 1, 2023. The data includes fields like 'Open', 'High', 'Low', 'Close', 'Volume', and 'Adj Close'.

Exploring the data

Inspecting the data: It's crucial to understand the structure and contents of the data.

# Display the first few rows
print(data.head())

The head() method displays the first five rows of the dataset, providing a quick overview of the data.

Basic statistics:

# Basic statistics
print(data.describe())

The describe() method provides summary statistics for each numeric column in the dataset, such as mean, standard deviation, min, and max values.

Plotting:

# Plotting the closing price
data['Close'].plot(title='AAPL Closing Price')
plt.show()

This code plots the closing prices of AAPL over time, providing a visual understanding of the stock's price movements.

Data analysis and visualization

Time series analysis

Time series analysis involves analyzing time-ordered data points. For financial data, this often means analyzing stock prices, trading volumes, etc.

Moving averages: These are commonly used to smooth out short-term fluctuations and highlight longer-term trends in data.

# Calculating moving averages
data['MA50'] = data['Close'].rolling(window=50).mean()
data['MA200'] = data['Close'].rolling(window=200).mean()

Here, MA50 and MA200 represent the 50-day and 200-day moving averages, respectively. The rolling(window=...).mean() method calculates the moving average over a specified window of days.

Plotting moving averages:

# Plotting moving averages
data[['Close', 'MA50', 'MA200']].plot(title='AAPL Price with Moving Averages')
plt.show()

This visualization helps in identifying trends and potential buy/sell signals based on the crossovers of moving averages.

Correlation analysis

Correlation measures the statistical relationship between two variables. In finance, understanding the correlation between different assets can inform diversification strategies.

# Fetching another stock's data
msft = yf.download('MSFT', start='2020-01-01', end='2023-01-01')

# Calculating correlation
correlation = data['Close'].corr(msft['Close'])
print(f"Correlation between AAPL and MSFT: {correlation:.2f}")

This example calculates the correlation between the closing prices of AAPL and Microsoft (MSFT). A correlation value close to 1 indicates a strong positive relationship, while a value close to -1 indicates a strong negative relationship.

Financial modeling

Simple moving average (SMA) strategy

A simple moving average strategy is a basic trading strategy that uses moving averages to generate buy/sell signals.

# SMA Strategy
data['Signal'] = np.where(data['MA50'] > data['MA200'], 1, 0)

In this strategy:

A "buy" signal (represented by 1) is generated when the 50-day MA is above the 200-day MA.

A "sell" signal (represented by 0) is generated otherwise.

# Plotting the strategy
data[['Close', 'MA50', 'MA200', 'Signal']].plot(subplots=True, title='SMA Trading Strategy')
plt.show()

This plot shows the closing prices, the moving averages, and the buy/sell signals over time, helping to visualize the effectiveness of the strategy.

Portfolio optimization

Portfolio optimization involves selecting the best mix of assets to maximize returns for a given level of risk.

# Sample code to optimize a simple two-asset portfolio
returns = data['Close'].pct_change().dropna()
msft_returns = msft['Close'].pct_change().dropna()

This calculates the daily returns for AAPL and MSFT.

# Portfolio returns and risk
portfolio_return = 0.5 * returns + 0.5 * msft_returns
portfolio_risk = portfolio_return.std()

print(f"Portfolio Return: {portfolio_return.mean():.2f}")
print(f"Portfolio Risk: {portfolio_risk:.2f}")

In this example:

The portfolio consists of 50% AAPL and 50% MSFT.

portfolio_return calculates the expected return, and portfolio_risk calculates the portfolio's risk (standard deviation of returns).

Automating financial tasks

Automating data retrieval

Automating repetitive tasks like data retrieval can save time and ensure data is always up to date.

import schedule
import time

def fetch_data():
    # Fetch and save data daily
    new_data = yf.download('AAPL', start='2020-01-01', end='2023-01-01')
    new_data.to_csv('aapl_data.csv')

This function fetches and saves AAPL data to a CSV file.

# Schedule the function to run every day at 6 PM
schedule.every().day.at("18:00").do(fetch_data)

while True:
    schedule.run_pending()
    time.sleep(1)

schedule library: A Python library for scheduling jobs.

The schedule.every().day.at("18:00").do(fetch_data) line schedules the fetch_data function to run daily at 6 PM.

The infinite while loop continuously checks for scheduled tasks and executes them when due.

Advanced analytics with machine learning

Predictive modeling with machine learning

Machine learning can be used to predict future asset prices, detect anomalies, or even automate trading strategies.

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split

# Feature Engineering
data['Returns'] = data['Close'].pct_change()
data = data.dropna()

Feature engineering: This involves creating new features (inputs) from the existing data that can improve the model's predictions. Here, we're calculating daily returns.

X = data[['MA50', 'MA200']]
y = data['Returns']

Defining features and target:

X represents the input features (in this case, the 50-day and 200-day moving averages).

y is the target variable (daily returns).

# Splitting the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Train-test split: The dataset is split into training and testing sets. The model is trained on the training set and evaluated on the testing set.

# Training the model
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

Model training: A Random Forest Regressor is used here. This ensemble method combines multiple decision trees to improve predictive accuracy.

# Predicting and evaluating
predictions = model.predict(X_test)

Predictions: The model makes predictions on the test set, which can then be compared against the actual values to evaluate performance.

Conclusion

This guide provides a comprehensive introduction to using Python for banking and finance within the Deepnote platform. It covers basic data handling, analysis, visualization, financial modeling, automation, and machine learning. As you gain experience, you can explore more advanced topics such as deep learning, quantitative trading strategies, and risk management.

Deepnote's collaborative environment and support for Python make it an ideal platform for exploring and sharing financial analyses and models. The skills and techniques covered in this guide are foundational and can be expanded upon with further study and practice.

Filip Žitný

Data Scientist

Follow Filip on Twitter, LinkedIn and GitHub

That’s it, time to try Deepnote

Get started – it’s free
Book a demo

Footer

Solutions

  • Notebook
  • Data apps
  • Machine learning
  • Data teams

Product

Company

Comparisons

Resources

  • Privacy
  • Terms

© Deepnote