Last active
April 17, 2023 00:01
-
-
Save sirmo/dc16bf4d4db1b955747078050fd51e5a to your computer and use it in GitHub Desktop.
openbb overview for multiple stocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openbb_terminal.sdk import openbb | |
from dataclasses import dataclass | |
from typing import Union, List, Dict, Optional | |
from cache_to_disk import cache_to_disk, delete_disk_caches_for_function | |
from rich.console import Console | |
from rich.table import Table | |
CACHE_DAYS = 1 | |
TICKERS = ['AMD', 'INTC', 'NVDA'] | |
# hides the columns you don't want to see | |
BLACKLIST = ['Description', 'Name', 'Sector', 'Asset type', 'C i k', 'Exhange', 'Currency', 'Country', 'Industry', 'Address'] | |
@dataclass | |
class Metric(): | |
name: str | |
ticker: str | |
value: Union[str, int, float] | |
@cache_to_disk(CACHE_DAYS) | |
def get_overview(ticker): | |
d = openbb.stocks.fa.overview(ticker) | |
return d.to_dict()[0] | |
class ProcessOverview(): | |
def __init__(self): | |
self.comp = {} | |
def process(self): | |
for ticker in TICKERS: | |
o = get_overview(ticker) | |
for k, v in o.items(): | |
m = Metric(k, ticker, v) | |
if k not in self.comp: | |
self.comp[k] = [] | |
self.comp[k].append(m) | |
else: | |
self.comp[k].append(m) | |
return self.comp | |
class Richify(): | |
def __init__(self): | |
self.tbl = Table(title=f"Fundamentals Overview for {TICKERS}") | |
self.tbl.add_column("Metric", justify="right", style="magenta", no_wrap=True) | |
for ticker in TICKERS: | |
self.tbl.add_column(ticker, justify="right", style="green") | |
def process(self, data): | |
for k, v in data.items(): | |
ms = [] | |
for m in v: | |
ms.append(m.value) | |
metric_name = m.name | |
if metric_name not in BLACKLIST: | |
self.tbl.add_row(metric_name, *ms) | |
def print(self): | |
console = Console() | |
console.print(self.tbl) | |
def main(): | |
po = ProcessOverview() | |
data = po.process() | |
r = Richify() | |
r.process(data) | |
r.print() | |
if __name__ == '__main__': | |
#delete_disk_caches_for_function("get_overview") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: