Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuantConnect
GitHub Repository: QuantConnect/Research
Path: blob/master/Documentation/Python/Plotting-Price-Data-Using-Seaborn.ipynb
984 views
Kernel: Python 3

QuantConnect Logo


Visualizing Data With Seaborn

An example of visualizing data with Seaborn's plots and heatmap.

Import Library

Let's start by importing Seaborn

import seaborn as sns sns.set(rc={'figure.figsize':(16,8)})

Gather & Prepare Data

Make a History request to fetch daily prices of several securities.

qb = QuantBook() # Make symbols for each plot line_tickers = ['SPY'] line_symbols = [qb.Symbol(ticker) for ticker in line_tickers] scatter_tickers = ["BAR", "GLD"] scatter_symbols = [qb.Symbol(ticker) for ticker in scatter_tickers] heatmap_tickers = [ "BAC", # Bank of America Corporation "COF", # Capital One Financial Corporation "C", # Citigroup Inc. "JPM", # J P Morgan Chase & Co "WFC", # Wells Fargo & Company ] heatmap_symbols = [qb.Symbol(ticker) for ticker in heatmap_tickers] # Make history request history = qb.History(line_symbols + scatter_symbols + heatmap_symbols, datetime(2019, 1, 1), datetime(2020, 7, 1), Resolution.Daily).close.unstack(level=0) # Seperate the history for each plot line_history = history[[str(symbol.ID) for symbol in line_symbols]] scatter_history = history[[str(symbol.ID) for symbol in scatter_symbols]] heatmap_history = history[[str(symbol.ID) for symbol in heatmap_symbols]]

Line Plot

Plot the price data using line plots for a quick visualization.

plot = sns.lineplot(x='time', y=str(line_symbols[0].ID), data=line_history.reset_index()) plot.set(ylabel="price", title="SPY Price Over Time");

Scatter Plot

Use scatterplot to study the price cointegration.

returns = scatter_history.pct_change()[1:] plot = sns.regplot(x=returns.columns[0], y=returns.columns[1], data=returns) plot.set(xlabel='GLD % Returns', ylabel='BAR % Returns', title='GLD vs BAR Daily % Returns');

Heatmap

Use heatmap to visualize the correlation between security returns.

sns.heatmap(heatmap_history.corr());