Files
OpenBB/gamestonk_terminal/common/prediction_techniques/mc_view.py
jmaslek 7ba964d61e (Finally) add prediction abilities to crypto (#1004)
* Basic integration of crypto pred

* Add loading into crypto pred

* Change all pred _views to allow for other datetime than daily

* small mypy change

* Add pred menu description and catch if no coin is loaded.

Co-authored-by: James Maslek <james@jamess-mbp.myfiosgateway.com>
2021-12-01 23:20:24 +00:00

80 lines
2.4 KiB
Python

"""Monte Carlo View"""
__docformat__ = "numpy"
from typing import Union
import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from gamestonk_terminal.common.prediction_techniques import mc_model
from gamestonk_terminal.helper_funcs import (
plot_autoscale,
get_next_stock_market_days,
export_data,
)
from gamestonk_terminal.config_plot import PLOT_DPI
from gamestonk_terminal import feature_flags as gtff
def display_mc_forecast(
data: Union[pd.Series, np.ndarray],
n_future: int,
n_sims: int,
use_log=True,
export: str = "",
time_res: str = "",
):
"""Display monte carlo forecasting
Parameters
----------
data : Union[pd.Series, np.array]
Data to forecast
n_future : int
Number of days to forecast
n_sims : int
Number of simulations to run
use_log : bool, optional
Flag to use lognormal, by default True
export: str
Format to export data
time_res : str
Resolution for data, allowing for predicting outside of standard market days
"""
predicted_values = mc_model.get_mc_brownian(data, n_future, n_sims, use_log)
if not time_res or time_res == "1D":
future_index = get_next_stock_market_days(data.index[-1], n_next_days=n_future) # type: ignore
else:
future_index = pd.date_range(data.index[-1], periods=n_future + 1, freq=time_res)[1:] # type: ignore
dateFmt = mdates.DateFormatter("%m/%d/%Y")
fig, ax = plt.subplots(1, 2, figsize=plot_autoscale(), dpi=PLOT_DPI)
ax[0].plot(data)
ax[0].plot(future_index, predicted_values, alpha=0.3)
ax[0].set_title("Data Predictions")
ax[0].xaxis.set_major_formatter(dateFmt)
ax[0].tick_params(axis="x", labelrotation=45)
ax[0].grid("on")
sns.histplot(predicted_values[-1, :], ax=ax[1], kde=True)
ax[1].set_xlabel("Price")
ax[1].axvline(x=data.values[-1], c="k", label="Last Value", lw=3, ls="-") # type: ignore
ax[1].set_title(f"Distribution of final values after {n_future} steps.")
ax[1].set_xlim(np.min(predicted_values[-1, :]), np.max(predicted_values[-1, :]))
ax[1].grid("on")
ax[1].legend()
fig.tight_layout(pad=2)
if gtff.USE_ION:
plt.ion()
plt.show()
export_data(export, os.path.dirname(os.path.abspath(__file__)), "mc")
print("")