Files
OpenBB/openbb_platform/obbject_extensions/charting/examples.md
Danglewood 3a8a71dd15 [Docs] Refactor Docs Pages For Diataxis (#6315)
* This change creates a PR

* start moving stuff around

* data_models/_category_.json

* reference/_category_.json

* moving some stuff around

* Introduction

* Documentation updates

* few broken links, clean up sidebar titles for how-to

* codespell

* quickstart - new provider extension

* add a comment

* map provider interface

* quickstart for new router extension

* [Docs] diataxis refactor refactor (#6425)

* Start refactor

* Edits

* [DOCS] Reorganize and edit. Fix sidebar navigation (#6445)

* Reorganize and edit. Fix sidebar navigation

* Fix broken linkages

* Edit

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>

* Fix links

---------

Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>

* Fix broken links

* Reword CLA part in the Licensing FAQ

* change titles and order suggestion - cc @IgorWounds @piiq

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
Co-authored-by: hjoaquim <henriquecjoaquim@gmail.com>
2024-05-22 13:50:43 +00:00

4.4 KiB
Vendored

title, sidebar_position, description, keywords
title sidebar_position description keywords
Examples 1 This page provides examples of creating charts with the `openbb-charting` extension.
tutorial
OpenBB Platform
Python client
Fast API
getting started
extensions
charting
view
Plotly
toolkits
how-to
generic
figure

import HeadTitle from '@site/src/components/General/HeadTitle.tsx';

Overview

This page will walk through creating different charts using the openbb-charting extension. The perspective for this content is from the the Python Interface, and the examples will assume that the OpenBB Platform is installed with all optional packages.

from datetime import datetime, timedelta
from openbb import obb

Cumulative Returns

The historical (equity) prices can be requested for multiple symbols. The extension will attempt to handle variations accordingly. By default, more than three symbols will draw the chart as cumulative returns from the beginning of the series.

Default View

The tickers below are a collection of State Street Global Advisors SPDR funds, representing S&P 500 components. The data is looking back five years.

SPDRS = [
    "SPY",
    "XLE",
    "XLB",
    "XLI",
    "XHB",
    "XLP",
    "XLY",
    "XRT",
    "XLF",
    "XLV",
    "XLK",
    "XLC",
    "XLU",
    "XLRE",
]
start_date = (datetime.now() - timedelta(weeks=52*5)).date()
spdrs = obb.equity.price.historical(SPDRS, start_date=start_date, provider="yfinance", chart=True)

spdrs.show()

SPDRs Cumulative Returns - 5 years

Redraw as YTD

The charting attribute of the command output has methods for creating the chart again. The data parameter allows modifications to the data before creating the figure. In this example, the length of the data is trimmed to the beginning of the year.

new_data = spdrs.to_df().loc[datetime(2023,12,29).date():]
spdrs.charting.to_chart(data=new_data, title="YTD")

:::note This replaces the chart that was already created. :::

SPDRs Cumulative Returns - YTD

Price Performance Bar Chart

The obb.equity.price.performance endpoint will create a bar chart over intervals.

price_performance = obb.equity.price.performance(SPDRS, chart=True)
price_performance.show()

Price Performance

Create Bar Chart

This example uses the create_bar_chart() method, which does not replace the existing chart, in price_performance.chart. It isolates the one-month performance and orients the layout as horizontal.

new_data = price_performance.to_df().set_index("symbol").multiply(100).reset_index()
price_performance.charting.create_bar_chart(
    data=new_data,
    x="symbol",
    y="one_month",
    orientation="h",
    title="One Month Price Performance",
    xtitle="Percent (%)"
)

Horizonontal Price Performance

Create Your Own

This example analyzes the share volume turnover of the S&P 500 Energy Sector constituents, year-to-date.

symbols = [
    'XOM',
    'CVX',
    'COP',
    'EOG',
    'MPC',
    'SLB',
    'PSX',
    'PXD',
    'VLO',
    'WMB',
    'OKE',
    'OXY',
    'HES',
    'FANG',
    'HAL',
    'KMI',
    'DVN',
    'BKR',
    'TRGP',
    'CTRA',
    'MRO',
    'EQT',
    'APA'
]
data = obb.equity.price.historical(symbols, start_date="2024-01-01", provider="yfinance")
create_bar_chart = data.charting.create_bar_chart
volume = data.to_df().groupby("symbol").sum()["volume"]
shares = obb.equity.profile(
    symbols, provider="yfinance"
).to_df().set_index("symbol")["shares_float"]
df = volume.to_frame().join(shares)
df["Turnover"] = (df.shares_float/df.volume).round(4)
df = df.sort_values(by="Turnover", ascending=False).reset_index()
create_bar_chart(
    data=df,
    x="symbol",
    y="Turnover",
    title="S&P Energy Sector YTD Turnover Rate",
)

S&P 500 Energy Sector Turnover Rate