Files
OpenBB/openbb_terminal/cryptocurrency/overview/glassnode_view.py
Jeroen Bouma 0ee2ebcf1e Refractor stocks/fa by incorporating stocks/dd and making the menu less reliant on Yahoo Finance (#4055)
* I missed some sheet_name ones :)

* Move quote to main menu and combine yahoo finance commands

* Update stocks for quote

* First update to move yfinance away from main functionality

* Small updates

* Update tests to reflect changes

* Fix some little things

* Update docs a little too

* Remove the Due Diligence menu

* Update tests for new change

* Update fa and stocks with new text and move some stuff around

* Bye bye dd

* Pls work

* work..

* work..

* work..

* Work..|

* Add tests back in

* Remove tests

* Fix fix fix

* Remove tests to fix conflicts

* Improve the `stocks/search` command (#4084)

* Improve search command

* Make it more intuitive

* Fix tests

* Update the docs

* Ok done

* Ok done 2

* Ok done 3

* Git update tests that failed

* Update commands with --sheet-name that didn't work (#4088)

* Update commands with --sheet-name that didn't work

* Fix test

* oops

* Adds auto completion to `news` (#4089)

* Fix plot look when using eval and add query to choices (#3881)

* convert index to datetime and update choices

* uncomment economy integration test

* fix treasury concat bug

* fix datasets concat on duplciates

* Lock ruff version so that new lints dont break our CI (#3905)

* Lock ruff version so that new lints dont break our CI

* Bumped pre-commit ruff version

* returning always a dataframe

* Revert "returning always a dataframe"

This reverts commit d5dbf3132f.

* quick fix to auto complete args

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Hotfix/Windows-CLI (#4087)

* init

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* Update windows10_build.yml

* oops

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Adds end date to `forex/load` (#4077)

* Fix plot look when using eval and add query to choices (#3881)

* convert index to datetime and update choices

* uncomment economy integration test

* fix treasury concat bug

* fix datasets concat on duplciates

* Lock ruff version so that new lints dont break our CI (#3905)

* Lock ruff version so that new lints dont break our CI

* Bumped pre-commit ruff version

* returning always a dataframe

* Revert "returning always a dataframe"

This reverts commit d5dbf3132f.

* adding end date to the forex load

* added some tests

* added example

* always show msg

* rewriting test

* updating tests

* updating tests

* Redo load tests

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>

* Update tests to reflect changes

* work..

* Remove tests

* Fix fix fix

* Remove tests

* Update the whole test folder

* Small updates

* Revert change

* Update eodhd to reflect it requires a paid plan and update tests

* Fix all the tests

* Update tests

* Update integration tests

* Removing tests so no whining happens

* ADD ALL THE TESTS

* Fix linting

* gitignore.  thanks jer

* Add back dd and redirect to fa

* Update to just display a message

* Update OpenBB SDK commands

* Fixing issues with exporting

* Fix ruff messing up

* Fix tests

* Fix the tests by disabling isort to run on tests folder

---------

Co-authored-by: Jeroen Bouma <jeroenbouma@Jeroens-MacBook-Pro.local>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Henrique Joaquim <h.joaquim@campus.fct.unl.pt>
Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
Co-authored-by: teh_coderer <me@tehcoderer.com>
2023-02-06 23:24:15 +01:00

152 lines
4.8 KiB
Python

import logging
import os
from datetime import datetime
from typing import List, Optional
import matplotlib
import numpy as np
import pandas as pd
from matplotlib import (
dates as mdates,
pyplot as plt,
)
from openbb_terminal import config_plot as cfgPlot
from openbb_terminal.config_terminal import theme
from openbb_terminal.cryptocurrency.overview.glassnode_model import get_btc_rainbow
from openbb_terminal.decorators import check_api_key, log_start_end
from openbb_terminal.helper_funcs import (
export_data,
is_valid_axes_count,
plot_autoscale,
)
logger = logging.getLogger(__name__)
@log_start_end(log=logger)
@check_api_key(["API_GLASSNODE_KEY"])
def display_btc_rainbow(
start_date: str = "2010-01-01",
end_date: Optional[str] = None,
export: str = "",
sheet_name: str = None,
external_axes: Optional[List[plt.Axes]] = None,
):
"""Displays bitcoin rainbow chart
[Price data from source: https://glassnode.com]
[Inspired by: https://blockchaincenter.net]
Parameters
----------
start_date : str
Initial date, format YYYY-MM-DD
end_date : Optional[str]
Final date, format YYYY-MM-DD
export : str
Export dataframe data to csv,json,xlsx file
external_axes : Optional[List[plt.Axes]], optional
External axes (1 axis is expected in the list), by default None
"""
if end_date is None:
end_date = datetime.now().strftime("%Y-%m-%d")
df_data = get_btc_rainbow(start_date, end_date)
if df_data.empty:
return
# This plot has 1 axis
if not external_axes:
_, ax = plt.subplots(figsize=plot_autoscale(), dpi=cfgPlot.PLOT_DPI)
elif is_valid_axes_count(external_axes, 1):
(ax,) = external_axes
else:
return
d0 = datetime.strptime("2012-01-01", "%Y-%m-%d")
dend = datetime.strptime(end_date, "%Y-%m-%d")
x = range((df_data.index[0] - d0).days, (dend - d0).days + 1)
y0 = [10 ** ((2.90 * ln_x) - 19.463) for ln_x in [np.log(val + 1400) for val in x]]
y1 = [10 ** ((2.886 * ln_x) - 19.463) for ln_x in [np.log(val + 1375) for val in x]]
y2 = [10 ** ((2.872 * ln_x) - 19.463) for ln_x in [np.log(val + 1350) for val in x]]
y3 = [10 ** ((2.859 * ln_x) - 19.463) for ln_x in [np.log(val + 1320) for val in x]]
y4 = [
10 ** ((2.8445 * ln_x) - 19.463) for ln_x in [np.log(val + 1293) for val in x]
]
y5 = [
10 ** ((2.8295 * ln_x) - 19.463) for ln_x in [np.log(val + 1275) for val in x]
]
y6 = [10 ** ((2.815 * ln_x) - 19.463) for ln_x in [np.log(val + 1250) for val in x]]
y7 = [10 ** ((2.801 * ln_x) - 19.463) for ln_x in [np.log(val + 1225) for val in x]]
y8 = [10 ** ((2.788 * ln_x) - 19.463) for ln_x in [np.log(val + 1200) for val in x]]
x_dates = pd.date_range(df_data.index[0], dend, freq="d")
ax.fill_between(x_dates, y0, y1, color="red", alpha=0.7)
ax.fill_between(x_dates, y1, y2, color="orange", alpha=0.7)
ax.fill_between(x_dates, y2, y3, color="yellow", alpha=0.7)
ax.fill_between(x_dates, y3, y4, color="green", alpha=0.7)
ax.fill_between(x_dates, y4, y5, color="blue", alpha=0.7)
ax.fill_between(x_dates, y5, y6, color="violet", alpha=0.7)
ax.fill_between(x_dates, y6, y7, color="indigo", alpha=0.7)
ax.fill_between(x_dates, y7, y8, color="purple", alpha=0.7)
ax.semilogy(df_data.index, df_data["v"].values)
ax.set_xlim(df_data.index[0], dend)
ax.set_title("Bitcoin Rainbow Chart")
ax.set_ylabel("Price [USD]")
ax.legend(
[
"Bubble bursting imminent!!",
"SELL!",
"Everyone FOMO'ing....",
"Is this a bubble??",
"Still cheap",
"Accumulate",
"BUY!",
"Basically a Fire Sale",
"Bitcoin Price",
],
prop={"size": 8},
)
sample_dates = np.array(
[
datetime(2012, 11, 28),
datetime(2016, 7, 9),
datetime(2020, 5, 11),
datetime(2024, 4, 4),
]
)
sample_dates = mdates.date2num(sample_dates)
ax.vlines(x=sample_dates, ymin=0, ymax=max(y0), color="grey")
for i, x in enumerate(sample_dates):
if mdates.date2num(d0) < x < mdates.date2num(dend):
ax.text(x, 1, f"Halving {i+1}", rotation=-90, verticalalignment="center")
ax.minorticks_off()
ax.yaxis.set_major_formatter(
matplotlib.ticker.FuncFormatter(lambda x, _: int(x) if x >= 1 else x)
)
ax.yaxis.set_major_locator(
matplotlib.ticker.LogLocator(base=100, subs=[1.0, 2.0, 5.0, 10.0])
)
theme.style_primary_axis(ax)
if not external_axes:
theme.visualize_output()
export_data(
export,
os.path.dirname(os.path.abspath(__file__)),
"btcrb",
df_data,
sheet_name,
)