mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-02 15:32:04 +00:00
* bump * Update ALL the exports with sheet name option * Update contributing too * Add in sheet name to each docstring * Update tests * Fix the small things for tests * Converting CONTRIBUTING.md back since I put minimal changes in and it complains * Fix terminal exit after `--sort p/e` (#4013) * fix scr sort exit * rewrite unit test output * rewind slash replace * rewind slash replace * fix regex * rewrite expected scr tests * black Co-authored-by: James Maslek <jmaslek11@gmail.com> * fix: small typo (#4036) * fix income comparison error (#4029) * Update tests due to yfinance error * I broke tests and fixed them * Fix tests? * Missed a few sheet name additions * missed one more * Fix some missing sheet_name parameters * Fix a test I broke * Update tests due to changes with sheet_name * Update stocks and common * Mutual funds just because * Portfolio update * Update Forecast * Update Futures * Update ETF * Update Economy * Update Crypto * Update others * Update documentation to explain --sheet-name * It should be None test * Add None for sheet_name since empty string can result in errors * Formatting * Fix tests * Update documentation * Also update the image * Add back to options commands Co-authored-by: james <jmaslek11@gmail.com> Co-authored-by: Jeroen Bouma <jeroenbouma@Jeroens-MacBook-Pro.local> Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Co-authored-by: jose-donato <43375532+jose-donato@users.noreply.github.com> Co-authored-by: teh_coderer <me@tehcoderer.com>
73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
"""Beta view"""
|
|
__docformat__ = "numpy"
|
|
|
|
import logging
|
|
import os
|
|
|
|
import matplotlib.pyplot as plt
|
|
import pandas as pd
|
|
from openbb_terminal.decorators import log_start_end
|
|
from openbb_terminal.rich_config import console
|
|
from openbb_terminal.stocks.quantitative_analysis.beta_model import beta_model
|
|
from openbb_terminal.helper_funcs import export_data
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def beta_view(
|
|
symbol: str,
|
|
ref_symbol: str,
|
|
data: pd.DataFrame = None,
|
|
ref_data: pd.DataFrame = None,
|
|
interval: int = 1440,
|
|
export: str = "",
|
|
sheet_name: str = None,
|
|
) -> None:
|
|
"""Display the beta scatterplot + linear regression.
|
|
|
|
Parameters
|
|
----------
|
|
symbol : str
|
|
A ticker to calculate beta for
|
|
ref_symbol : str
|
|
A reference ticker symbol for the beta calculation (default in terminal is SPY)
|
|
data : pd.DataFrame
|
|
The selected ticker symbols price data
|
|
ref_data : pd.DataFrame
|
|
The reference ticker symbols price data
|
|
interval: int
|
|
The interval of the ref_data. This will ONLY be used if ref_data is None
|
|
"""
|
|
try:
|
|
sr, rr, beta, alpha = beta_model(
|
|
symbol, ref_symbol, data, ref_data, interval=interval
|
|
)
|
|
except Exception as e:
|
|
if str(e) == "Invalid ref ticker":
|
|
console.print(str(e) + "\n")
|
|
return
|
|
raise e
|
|
|
|
_, ax = plt.subplots()
|
|
ax.scatter(rr, sr) # plot returns
|
|
ax.plot(ax.get_xlim(), [x * beta + alpha for x in ax.get_xlim()]) # plot lin reg
|
|
ax.set(
|
|
xlabel=f"{ref_symbol} Returns (%)",
|
|
ylabel=f"{symbol} Returns (%)",
|
|
title=f"Beta of {symbol} with respect to {ref_symbol}",
|
|
)
|
|
beta_text = f"Raw Beta={round(beta, 2)}\nAlpha={round(alpha, 2)}"
|
|
ax.text(0.9, 0.1, beta_text, horizontalalignment="right", transform=ax.transAxes)
|
|
console.print()
|
|
|
|
df = pd.DataFrame({"sr": sr, "rr": rr})
|
|
|
|
export_data(
|
|
export,
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
f"beta_alpha={alpha}_beta={beta}",
|
|
df,
|
|
sheet_name,
|
|
)
|