mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 05:21:48 +00:00
Co-authored-by: jmaslek <jmaslek11@gmail.com> Co-authored-by: Chavithra <chavithra@gmail.com> Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com> Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com> Co-authored-by: Didier Rodrigues Lopes <dro.lopes@campus.fct.unl.pt> * improve user experience phase 1 * add difflib replacement * implement auto-complete on discovery menu * auoto-complete, difflib,jump through menus with cd * Updating tests : terminal * improve navigation on gst * add q command to terminal.py * usage instructions * Updating tests : terminal * fix cd command * vars updated * show menu when user is going through menus * allow user to forget the intiail / on scripting * terminal to accept text file with configuration * refactor -n to -l and clean code * add all commands to the tests_stocks_disc * remove fds from disc since we have search on stocks/ and stocks/sia * hopefully fix tests * skip test * fix completer type and calling menu after command for stocks/disc * show help menu when moving forward/backwards in menus * skip test * skip test - bug fix * add reset, help and quit to choices * improve stocks/disc controller * refactor sia menu * UX improvements for sia menu * improve ux design * refactor ca controller * fix infinite loop * improve ux on dps controller * Add capability of going back from a menu going into a menu * Ux economy (#1056) * First round of UX improvements on economy menu * Do fred menu + sample tests file * revert to > on menu * rename jobs file to .gst * remove cd option * improve return * final refactoring of disc_controller * fix ca,dps,sia,disc,stocks and terminal controllers * work on ba_controller menu * fix reset and try_except * UX on screener controller * fix for full reset name * Ux etfs (#1059) * Refactor ETF menu for new UX * Add couple more examples to test * r->reset * Clean up switch * gov menu refactored * fix reset issue * 80% of refactoring options and pricing/payoff menu is done * improve payoff controller * minor fix * fix: refactor pred and ta stocks controller * fix: refactor fa(and fmp) controllers * fix: refactored dd controller (still missing .gst tests) * fix infinite loop bug * refactor options controller * add tests for payoff and pricing * fix reset for options * UX Upgrade for Crypto controller(s) (#1049) * refactor crypto controller * refactor crypto/defi controller * added simple test and fixes not returning queue in commands * Clean up unused functions / iex load (#1047) * Clean up unused functions and adjust iex load function to not depend on dates * Had an incorrect tab on s_start definition * wrong default arg * regenerate pytest yml * Comment out parametrization * Tests : Stocks > Discovery (#1050) * Updating tests : stocks/discovery/ark * Updating tests : stocks/discovery/fidelity * Updating tests : stocks/discovery/fidelity * Updating tests : stocks/discovery/fidelity * Updating tests : stocks/discovery/seeking_alpha * Updating tests : stocks/discovery/seeking_alpha * Updating tests : stocks/discovery/shortinterest * Updating tests : stocks/discovery/shortinterest * Updating tests : stocks/discovery/seeking_alpha * Updating tests : stocks/discovery/shortinterest * Updating tests : stocks/discovery/disc_controller * Updating tests : stocks/dd * Updating tests : stocks/fa * Updating tests : stocks/discovery/financedatabase * Updating tests : stocks/discovery/finnhub * Updating tests : stocks/discovery/geekofwallstreet * Updating tests : stocks/discovery/marketbeat * Updating tests : stocks/discovery/nasdaq * Updating tests : stocks / discovery / yahoofinance * Updating tests : black * Updating tests : unused import * Updating tests : stocks/discovery/ark * Tests : Stocks > Doc + Helpers + Government (#1051) * Updating tests : stocks/government/controller * Updating tests : stocks/government * Updating tests : conftest * Updating tests : stocks > government * Updating tests : stocks/government * Updating tests : docs * Tests : Stocks > Insider (#1054) * Updating tests : stocks > helper * Updating tests : doc * Updating tests : clean stocks > fa > av * Updating tests : stocks/insider/controller * Updating tests : stocks/insider/business insider * Updating tests : stocks/insider/finviz * Updating tests : stocks/insider/openinsider * Updating tests : stocks/insider/controller * Updating tests : black * Updating tests : stocks/insider/controller * Tests : Stocks > Backtesting + Behavioural Analysis (#1057) * Updating tests : add __init__ files * Updating tests : stocsk/behavioural_analysis * Updating tests : black * Updating tests : stocks/backtesting * Updating tests : stocks/backtesting * fix: converted top to limit arguments * fix: refactor disc controller, updated docs * fix: refactor dd_controller and docs * fix: refactor nft controller and docs * fix: pass test temporarily, onchain controller refactor, docs * fix: overview controller refactor, fix quit command * fix: linting issues * fix: refacot ta controller * fix: refactor pred controller * fix: linting * fix: crypto controller * fix: added tests, updated all controlers * fix: menu print renamed from stocks to crypto controller, added tests * fix: fix faster function execution * Fix annoying pylint C0201 error * fix: renamed commands to long format * DISC Cleanup (#1058) * Get rid/cleanup of marketbeat functions * Get rid of last unused disc function + fix related tests * fix test * Dividend History Chart (#1053) * Dividend History Chart * changed to default number 10 * changed divs flag default to false * updating the expected result for test_fa_controller. * else statement * fix: added load on dd * fix: added autocompletion in all commands with argument choices * fix: linting * Workflows + Stocks > Insider (#1061) * Updating code : stocks/insider remove unused file * Updating workflows : tests - set mypy==0.910 * fix: linting * Fixing Linting (#1062) * Updating code : fixing linting mypy message = Too many arguments for <lambda> * Updating workflows : allow newest mypy version * Updating code : apply black * Updating code : black * fix: updated predict controller help to match stocks * Ux: portfolio (#1063) * First step of portfolio UX * Add all broker controllers to UX stuff * UX Improvement for PO Co-authored-by: Didier Rodrigues Lopes <dro.lopes@campus.fct.unl.pt> * UX Improvement for resources (#1064) Co-authored-by: Didier Rodrigues Lopes <dro.lopes@campus.fct.unl.pt> * UX: stock/bt (#1065) * UX: stocks>qa (#1068) * fix mypy lambda * skip tests for now * remove unused paths.py * rename tests/simulator to tests/e2e * refactor options/screener menu * UX : move `gst` files location * fix stocks_controller * UX upgrade of the Forex menu (#1048) * Add basic forex menu test * Add forex routes to terminal paths * Remove commands from paths because they're not menus * WIP Refactor forex controller * refactor crypto controller * Remove commented code * Improve user output with printed text * Add "help" back to the list of choices * Enable the command queue for the forex controller * refactor crypto/defi controller * Fix candle chart blocking the terminal * Black * Return help and reset to the list of known args * added simple test and fixes not returning queue in commands * Add currency codes loading to the av_model * Return the queue in edge cases (when commands don't process) * Split select into to and from to enable autocompletion * Style text output * Stop printing help after the execution of every command * Update test scenarios * Black * fix: converted top to limit arguments * fix: refactor disc controller, updated docs * Delete oanda the old view file that was moved into the oanda menu * fix: refactor dd_controller and docs * fix: refactor nft controller and docs * fix: pass test temporarily, onchain controller refactor, docs * fix: overview controller refactor, fix quit command * fix: linting issues * fix: refacot ta controller * fix: refactor pred controller * fix: linting * fix: crypto controller * fix: added tests, updated all controlers * fix: menu print renamed from stocks to crypto controller, added tests * fix: fix faster function execution * Implement basic UX improvement of the oanda controller * Update the forex controller * Fix annoying pylint C0201 error * Add latest convention changes to the forex controller * fix: renamed commands to long format * fix: added load on dd * Merge the latest updates of the controller design * fix: added autocompletion in all commands with argument choices * fix: linting * fix: linting * fix: updated predict controller help to match stocks * Update the forex menu e2e test scripts * Add check_non_negative_float helper * Update the controller with the latest changes to the convention * Update the currency pair loading mechanism * Move data models logic to the models file * Update update oanda views to be interact with the models * Upgrade oanda controller * Remove raw stocks ba and eda code from the forex menu * Update "help wanted" banner for ba and eda in forex menu * Remove remainders of stocks ba and eda from forex context * Improve typing, docstrings and error messages * Remove stocks ba and eda code that was merged from other ux branches * Add fixes that prevented incorrect argument handling * Remove -argument from the candles call If there is an expected agr `-g` and you put an incorrect argument `-argument` the difflib will interpret `argument` as `g` Co-authored-by: jose-donato <zmcdonato@gmail.com> Co-authored-by: Didier Rodrigues Lopes <dro.lopes@campus.fct.unl.pt> * fix reset for menus that require ticker * UX improvement on research menu * skip insider controller test * initial refactoring of insider_controller * improve insider controller ux part 2 * finish refactoring ins controller ux menu * fix bug of when commands in queue have args * skip test * comment test stuff * UX: stocks>qa (#1071) * refactor return self.queue * fix return None * minor fix * minor tabulate fix * Get rid of leading '/' in terminal and add some newlines on quits. (#1074) * Get rid of leading '/' in terminal and add some newlines on quits. * print crypto help on menu entry * looks like a format issue here... * newlines on quit * remove unnecessary line break * fix pylint issue * refactor * refactor * allow portfolio optimization to quit context * add exit at end of scripts * improve dd_controller * test ux in stocks/dd menu * Ux upgrade for reports context (#1075) * Refactor reports and upgrade them with the latest controller convention * Add test scenarios for the reports context * test ux stocks/ta * ux branch scripts/ta * improvement on main param * Upgrades for the UX upgrade for the fa and fa/fmp menus (#1076) * Change --num to --limit * Remove inconsistent/unreliable functions from the choices list * Improve docstring compliance with numpy doc format * Fix hardcoded class id in dcf scrapper * Fix annoying pylint error in discordbot that was blocking the hook * Add test scenarios for the fa and fa/fmp menus * Fix forgotten replacement of --num to --limit * Fix tests * Return some excluded commands to the choices list * Fix dd report to reflect the updates * Fix set function not returning the queue * Update gst test files * fix exit on ca from another submenu * fix changes of options/screener * fix dps controller * fix fa when running out of AV calls * Fix TVL plot locking interpreter during defi test * Add exception handling for broken coingecko scrapper * skip fa/av test that fails * fix dps tests Co-authored-by: Chavithra PARANA <chavithra@gmail.com> Co-authored-by: jmaslek <jmaslek11@gmail.com> Co-authored-by: jose-donato <zmcdonato@gmail.com> Co-authored-by: jose-donato <43375532+jose-donato@users.noreply.github.com> Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com> Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
628 lines
18 KiB
Python
628 lines
18 KiB
Python
""" Prediction helper functions """
|
|
__docformat__ = "numpy"
|
|
|
|
import argparse
|
|
from typing import List, Union
|
|
import os
|
|
from warnings import simplefilter
|
|
from datetime import timedelta
|
|
import numpy as np
|
|
import pandas as pd
|
|
from pandas.plotting import register_matplotlib_converters
|
|
import matplotlib.pyplot as plt
|
|
from colorama import Fore, Style
|
|
from sklearn.metrics import (
|
|
mean_absolute_error,
|
|
r2_score,
|
|
mean_squared_error,
|
|
)
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer
|
|
from tabulate import tabulate
|
|
from tensorflow.keras.models import Sequential
|
|
from gamestonk_terminal.helper_funcs import (
|
|
check_positive,
|
|
check_positive_float,
|
|
parse_known_args_and_warn,
|
|
valid_date,
|
|
plot_autoscale,
|
|
)
|
|
from gamestonk_terminal import config_neural_network_models as cfg
|
|
from gamestonk_terminal import feature_flags as gtff
|
|
from gamestonk_terminal.config_plot import PLOT_DPI
|
|
|
|
|
|
register_matplotlib_converters()
|
|
|
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
|
|
|
simplefilter(action="ignore", category=FutureWarning)
|
|
|
|
# store the user's TensorFlow environment variables
|
|
ORIGINAL_TF_XLA_FLAGS = os.environ.get("TF_XLA_FLAGS")
|
|
ORIGINAL_TF_FORCE_GPU_ALLOW_GROWTH = os.environ.get("TF_FORCE_GPU_ALLOW_GROWTH")
|
|
|
|
PREPROCESSER = cfg.Preprocess
|
|
|
|
|
|
def check_valid_frac(num) -> float:
|
|
"""Argparse type checker for valid float between 0 and 1"""
|
|
if (num < 0) or (num > 1):
|
|
raise argparse.ArgumentTypeError(f"{num} is an invalid percentage")
|
|
return num
|
|
|
|
|
|
def restore_env():
|
|
"""Restore environment variables to original values"""
|
|
|
|
def restore(key, value):
|
|
if value is None:
|
|
if key in os.environ:
|
|
del os.environ[key]
|
|
else:
|
|
os.environ[key] = value
|
|
|
|
restore("TF_XLA_FLAGS", ORIGINAL_TF_XLA_FLAGS)
|
|
restore("TF_FORCE_GPU_ALLOW_GROWTH", ORIGINAL_TF_FORCE_GPU_ALLOW_GROWTH)
|
|
|
|
|
|
def parse_args(prog: str, description: str, other_args: List[str]):
|
|
"""
|
|
Create an argparser and parse other_args. Will print help if user requests it.
|
|
Parameters
|
|
----------
|
|
prog: str
|
|
Program for argparser
|
|
description: str
|
|
Description for argparser
|
|
other_args
|
|
Argparse arguments to pass
|
|
Returns
|
|
-------
|
|
ns_parser: argparse.Namespace
|
|
Parsed argument parser
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(
|
|
prog=prog,
|
|
description=description,
|
|
add_help=False,
|
|
formatter_class=argparse.RawTextHelpFormatter, # enable multiline help messages
|
|
)
|
|
parser.add_argument(
|
|
"-d",
|
|
"--days",
|
|
action="store",
|
|
dest="n_days",
|
|
type=check_positive,
|
|
default=5,
|
|
help="prediction days.",
|
|
)
|
|
parser.add_argument(
|
|
"-i",
|
|
"--input",
|
|
action="store",
|
|
dest="n_inputs",
|
|
type=check_positive,
|
|
default=40,
|
|
help="number of days to use for prediction.",
|
|
)
|
|
parser.add_argument(
|
|
"--epochs",
|
|
action="store",
|
|
dest="n_epochs",
|
|
type=check_positive,
|
|
default=50,
|
|
help="number of training epochs.",
|
|
)
|
|
parser.add_argument(
|
|
"-e",
|
|
"--end",
|
|
action="store",
|
|
type=valid_date,
|
|
dest="s_end_date",
|
|
default=None,
|
|
help="The end date (format YYYY-MM-DD) to select - Backtesting",
|
|
)
|
|
parser.add_argument(
|
|
"--batch_size",
|
|
action="store",
|
|
dest="n_batch_size",
|
|
type=check_positive,
|
|
default=None,
|
|
help="batch size for model fitting (use a power of 2)",
|
|
)
|
|
parser.add_argument(
|
|
"--xla_cpu",
|
|
action="store_true",
|
|
dest="b_xla_cpu",
|
|
default=False,
|
|
help="enable XLA for CPU (see https://www.tensorflow.org/xla)",
|
|
)
|
|
parser.add_argument(
|
|
"--xla_gpu",
|
|
action="store_true",
|
|
dest="b_xla_gpu",
|
|
default=False,
|
|
help="enable XLA for GPU (see https://www.tensorflow.org/xla)",
|
|
)
|
|
parser.add_argument(
|
|
"--force_gpu_allow_growth",
|
|
action="store",
|
|
dest="s_force_gpu_allow_growth",
|
|
default="true",
|
|
choices=["true", "false", "default"],
|
|
help="true: GPU memory will grow as needed. \n"
|
|
"false: TensorFlow will allocate 100%% of GPU memory. \n"
|
|
"default: usually the same as false, uses env/TensorFlow default",
|
|
)
|
|
parser.add_argument(
|
|
"-l",
|
|
"--loops",
|
|
action="store",
|
|
dest="n_loops",
|
|
type=check_positive,
|
|
default=1,
|
|
help="number of loops to iterate and train models",
|
|
)
|
|
parser.add_argument(
|
|
"-v",
|
|
"--valid",
|
|
type=check_valid_frac,
|
|
dest="valid_split",
|
|
default=0.1,
|
|
help="Validation data split fraction",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--lr",
|
|
type=check_positive_float,
|
|
dest="lr",
|
|
default=0.01,
|
|
help="Specify learning rate for optimizer.",
|
|
)
|
|
|
|
parser.add_argument(
|
|
"--no_shuffle",
|
|
action="store_false",
|
|
dest="no_shuffle",
|
|
default=True,
|
|
help="Specify if shuffling validation inputs.",
|
|
)
|
|
|
|
try:
|
|
ns_parser = parse_known_args_and_warn(parser, other_args)
|
|
if not ns_parser:
|
|
return None
|
|
|
|
# set xla flags if requested
|
|
xla_flags = (
|
|
set(ORIGINAL_TF_XLA_FLAGS.split(" ")) if ORIGINAL_TF_XLA_FLAGS else set()
|
|
)
|
|
if ns_parser.b_xla_cpu or ns_parser.b_xla_gpu:
|
|
xla_flags.add("--tf_xla_enable_xla_devices")
|
|
if ns_parser.b_xla_cpu:
|
|
xla_flags.add("--tf_xla_cpu_global_jit")
|
|
if ns_parser.b_xla_gpu:
|
|
xla_flags.add("--tf_xla_auto_jit=2")
|
|
os.environ["TF_XLA_FLAGS"] = " ".join(xla_flags)
|
|
|
|
# set GPU memory growth flag
|
|
if ns_parser.s_force_gpu_allow_growth == "true":
|
|
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
|
|
elif ns_parser.s_force_gpu_allow_growth == "false":
|
|
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "false"
|
|
|
|
return ns_parser
|
|
|
|
except SystemExit:
|
|
print("")
|
|
return None
|
|
|
|
|
|
def prepare_scale_train_valid_test(
|
|
data: Union[pd.DataFrame, pd.Series],
|
|
n_input_days: int,
|
|
n_predict_days: int,
|
|
test_size: float,
|
|
s_end_date: str,
|
|
no_shuffle: bool,
|
|
):
|
|
"""
|
|
Prepare and scale train, validate and test data.
|
|
Parameters
|
|
----------
|
|
data: pd.DataFrame
|
|
Dataframe of stock prices
|
|
ns_parser: argparse.Namespace
|
|
Parsed arguments
|
|
Returns
|
|
-------
|
|
X_train: np.ndarray
|
|
Array of training data. Shape (# samples, n_inputs, 1)
|
|
X_test: np.ndarray
|
|
Array of validation data. Shape (total sequences - #samples, n_inputs, 1)
|
|
y_train: np.ndarray
|
|
Array of training outputs. Shape (#samples, n_days)
|
|
y_test: np.ndarray
|
|
Array of validation outputs. Shape (total sequences -#samples, n_days)
|
|
X_dates_train: np.ndarray
|
|
Array of dates for X_train
|
|
X_dates_test: np.ndarray
|
|
Array of dates for X_test
|
|
y_dates_train: np.ndarray
|
|
Array of dates for y_train
|
|
y_dates_test: np.ndarray
|
|
Array of dates for y_test
|
|
test_data: np.ndarray
|
|
Array of prices after the specified end date
|
|
dates_test: np.ndarray
|
|
Array of dates after specified end date
|
|
scaler:
|
|
Fitted preprocesser
|
|
"""
|
|
|
|
# Pre-process data
|
|
if PREPROCESSER == "standardization":
|
|
scaler = StandardScaler()
|
|
|
|
elif PREPROCESSER == "minmax":
|
|
scaler = MinMaxScaler()
|
|
|
|
elif PREPROCESSER == "normalization":
|
|
scaler = Normalizer()
|
|
|
|
elif (PREPROCESSER == "none") or (PREPROCESSER is None):
|
|
scaler = None
|
|
# Test data is used for forecasting. Takes the last n_input_days data points.
|
|
# These points are not fed into training
|
|
|
|
if s_end_date:
|
|
data = data[data.index <= s_end_date]
|
|
if n_input_days + n_predict_days > data.shape[0]:
|
|
print("Cannot train enough input days to predict with loaded dataframe\n")
|
|
return (
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
None,
|
|
True,
|
|
)
|
|
|
|
test_data = data.iloc[-n_input_days:]
|
|
train_data = data.iloc[:-n_input_days]
|
|
|
|
dates = data.index
|
|
dates_test = test_data.index
|
|
if scaler:
|
|
train_data = scaler.fit_transform(data.values.reshape(-1, 1))
|
|
test_data = scaler.transform(test_data.values.reshape(-1, 1))
|
|
else:
|
|
train_data = data.values.reshape(-1, 1)
|
|
test_data = test_data.values.reshape(-1, 1)
|
|
|
|
prices = train_data
|
|
|
|
input_dates = []
|
|
input_prices = []
|
|
next_n_day_prices = []
|
|
next_n_day_dates = []
|
|
|
|
for idx in range(len(prices) - n_input_days - n_predict_days):
|
|
input_prices.append(prices[idx : idx + n_input_days])
|
|
input_dates.append(dates[idx : idx + n_input_days])
|
|
next_n_day_prices.append(
|
|
prices[idx + n_input_days : idx + n_input_days + n_predict_days]
|
|
)
|
|
next_n_day_dates.append(
|
|
dates[idx + n_input_days : idx + n_input_days + n_predict_days]
|
|
)
|
|
|
|
input_dates = np.asarray(input_dates) # type: ignore
|
|
input_prices = np.array(input_prices) # type: ignore
|
|
next_n_day_prices = np.array(next_n_day_prices) # type: ignore
|
|
next_n_day_dates = np.asarray(next_n_day_dates) # type: ignore
|
|
|
|
(
|
|
X_train,
|
|
X_valid,
|
|
y_train,
|
|
y_valid,
|
|
X_dates_train,
|
|
X_dates_valid,
|
|
y_dates_train,
|
|
y_dates_valid,
|
|
) = train_test_split(
|
|
input_prices,
|
|
next_n_day_prices,
|
|
input_dates,
|
|
next_n_day_dates,
|
|
test_size=test_size,
|
|
shuffle=no_shuffle,
|
|
)
|
|
return (
|
|
X_train,
|
|
X_valid,
|
|
y_train,
|
|
y_valid,
|
|
X_dates_train,
|
|
X_dates_valid,
|
|
y_dates_train,
|
|
y_dates_valid,
|
|
test_data,
|
|
dates_test,
|
|
scaler,
|
|
False,
|
|
)
|
|
|
|
|
|
def forecast(
|
|
input_values: np.ndarray, future_dates: List, model: Sequential, scaler
|
|
) -> pd.DataFrame:
|
|
"""
|
|
Forecast the stock movement over future days and rescale
|
|
Parameters
|
|
----------
|
|
input_values: np.ndarray
|
|
Array of values to be fed into the model
|
|
future_dates: List
|
|
List of future dates
|
|
model: Sequential
|
|
Pretrained model
|
|
scaler:
|
|
Fit scaler to be used to 'unscale' the data
|
|
|
|
Returns
|
|
-------
|
|
df_future: pd.DataFrame
|
|
Dataframe of predicted values
|
|
"""
|
|
if scaler:
|
|
future_values = scaler.inverse_transform(
|
|
model.predict(input_values.reshape(1, -1, 1)).reshape(-1, 1)
|
|
)
|
|
else:
|
|
future_values = model.predict(input_values.reshape(1, -1, 1)).reshape(-1, 1)
|
|
|
|
df_future = pd.DataFrame(
|
|
future_values, index=future_dates, columns=["Predicted Price"]
|
|
)
|
|
return df_future
|
|
|
|
|
|
# pylint:disable=too-many-arguments
|
|
|
|
|
|
def plot_data_predictions(
|
|
data,
|
|
preds,
|
|
y_valid,
|
|
y_dates_valid,
|
|
scaler,
|
|
title,
|
|
forecast_data,
|
|
n_loops,
|
|
time_str: str = "",
|
|
):
|
|
"""Plots data predictions for the different ML techniques"""
|
|
|
|
plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI)
|
|
plt.plot(
|
|
data.index,
|
|
data.values,
|
|
"-ob",
|
|
lw=1,
|
|
ms=2,
|
|
label="Prices",
|
|
)
|
|
for i in range(len(y_valid) - 1):
|
|
|
|
if scaler:
|
|
y_pred = scaler.inverse_transform(preds[i].reshape(-1, 1)).ravel()
|
|
y_act = scaler.inverse_transform(y_valid[i].reshape(-1, 1)).ravel()
|
|
else:
|
|
y_pred = preds[i].ravel()
|
|
y_act = y_valid[i].ravel()
|
|
plt.plot(
|
|
y_dates_valid[i],
|
|
y_pred,
|
|
"r",
|
|
lw=1,
|
|
)
|
|
plt.fill_between(
|
|
y_dates_valid[i],
|
|
y_pred,
|
|
y_act,
|
|
where=(y_pred < y_act),
|
|
color="r",
|
|
alpha=0.2,
|
|
)
|
|
plt.fill_between(
|
|
y_dates_valid[i],
|
|
y_pred,
|
|
y_act,
|
|
where=(y_pred > y_act),
|
|
color="g",
|
|
alpha=0.2,
|
|
)
|
|
|
|
# Leave this one out of the loop so that the legend doesn't get overpopulated with "Predictions"
|
|
if scaler:
|
|
final_pred = scaler.inverse_transform(preds[-1].reshape(-1, 1)).ravel()
|
|
final_valid = scaler.inverse_transform(y_valid[-1].reshape(-1, 1)).ravel()
|
|
else:
|
|
final_pred = preds[-1].reshape(-1, 1).ravel()
|
|
final_valid = y_valid[-1].reshape(-1, 1).ravel()
|
|
plt.plot(
|
|
y_dates_valid[-1],
|
|
final_pred,
|
|
"r",
|
|
lw=2,
|
|
label="Predictions",
|
|
)
|
|
plt.fill_between(
|
|
y_dates_valid[-1],
|
|
final_pred,
|
|
final_valid,
|
|
color="k",
|
|
alpha=0.2,
|
|
)
|
|
|
|
_, _, ymin, ymax = plt.axis()
|
|
plt.vlines(
|
|
forecast_data.index[0],
|
|
ymin,
|
|
ymax,
|
|
colors="k",
|
|
linewidth=3,
|
|
linestyle="--",
|
|
color="k",
|
|
)
|
|
if n_loops == 1:
|
|
plt.plot(
|
|
forecast_data.index,
|
|
forecast_data.values,
|
|
"-og",
|
|
ms=3,
|
|
label="Forecast",
|
|
)
|
|
else:
|
|
plt.plot(
|
|
forecast_data.index,
|
|
forecast_data.median(axis=1).values,
|
|
"-og",
|
|
ms=3,
|
|
label="Forecast",
|
|
)
|
|
plt.fill_between(
|
|
forecast_data.index,
|
|
forecast_data.quantile(0.25, axis=1).values,
|
|
forecast_data.quantile(0.75, axis=1).values,
|
|
color="c",
|
|
alpha=0.3,
|
|
)
|
|
# Subtracting 1 day only works nicely for daily data. For now if not daily, then start line on last point
|
|
if not time_str or time_str == "1D":
|
|
plt.axvspan(
|
|
forecast_data.index[0] - timedelta(days=1),
|
|
forecast_data.index[-1],
|
|
facecolor="tab:orange",
|
|
alpha=0.2,
|
|
)
|
|
plt.xlim(data.index[0], forecast_data.index[-1] + timedelta(days=1))
|
|
|
|
else:
|
|
plt.axvspan(
|
|
forecast_data.index[0],
|
|
forecast_data.index[-1],
|
|
facecolor="tab:orange",
|
|
alpha=0.2,
|
|
)
|
|
plt.xlim(data.index[0], forecast_data.index[-1])
|
|
|
|
plt.legend(loc=0)
|
|
plt.xlabel("Time")
|
|
plt.ylabel("Value")
|
|
plt.grid(b=True, which="major", color="#666666", linestyle="-")
|
|
plt.title(title)
|
|
if gtff.USE_ION:
|
|
plt.ion()
|
|
plt.show()
|
|
|
|
|
|
def price_prediction_color(val: float, last_val: float) -> str:
|
|
"""Set prediction to be a colored string"""
|
|
if float(val) > last_val:
|
|
color = Fore.GREEN
|
|
else:
|
|
color = Fore.RED
|
|
return f"{color}{val:.2f} ${Style.RESET_ALL}"
|
|
|
|
|
|
def print_pretty_prediction(df_pred: pd.DataFrame, last_price: float):
|
|
"""Print predictions"""
|
|
print("")
|
|
if gtff.USE_COLOR:
|
|
print(f"Actual price: {Fore.YELLOW}{last_price:.2f} ${Style.RESET_ALL}\n")
|
|
if gtff.USE_TABULATE_DF:
|
|
df_pred = pd.DataFrame(df_pred)
|
|
df_pred.columns = ["pred"]
|
|
df_pred["pred"] = df_pred["pred"].apply(
|
|
lambda x: price_prediction_color(x, last_val=last_price)
|
|
)
|
|
print("Prediction:")
|
|
print(tabulate(df_pred, headers=["Prediction"], tablefmt="fancy_grid"))
|
|
|
|
else:
|
|
|
|
print("Prediction:")
|
|
print(
|
|
df_pred.apply(price_prediction_color, last_val=last_price).to_string()
|
|
)
|
|
else:
|
|
if gtff.USE_TABULATE_DF:
|
|
df_pred = pd.DataFrame(df_pred)
|
|
df_pred.columns = ["pred"]
|
|
print("Prediction:")
|
|
print(tabulate(df_pred, headers=["Prediction"], tablefmt="fancy_grid"))
|
|
else:
|
|
print(f"Actual price: {last_price:.2f} $\n")
|
|
print("Prediction:")
|
|
print(df_pred.to_string())
|
|
|
|
|
|
def print_pretty_prediction_nn(df_pred: pd.DataFrame, last_price: float):
|
|
if gtff.USE_COLOR:
|
|
print(f"Actual price: {Fore.YELLOW}{last_price:.2f} ${Style.RESET_ALL}\n")
|
|
print("Prediction:")
|
|
print(
|
|
df_pred.applymap(
|
|
lambda x: price_prediction_color(x, last_val=last_price)
|
|
).to_string()
|
|
)
|
|
else:
|
|
print(f"Actual price: {last_price:.2f} $\n")
|
|
print("Prediction:")
|
|
print(df_pred.to_string())
|
|
|
|
|
|
def mean_absolute_percentage_error(y_true: np.ndarray, y_pred: np.ndarray) -> np.number:
|
|
"""Calculate mean absolute percent error"""
|
|
y_true, y_pred = np.array(y_true), np.array(y_pred)
|
|
return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
|
|
|
|
|
|
def print_prediction_kpis(real: np.ndarray, pred: np.ndarray):
|
|
"""Print prediction statistics"""
|
|
kpis = {
|
|
"MAPE": f"{mean_absolute_percentage_error(real, pred) :.3f} %",
|
|
"R2": f"{r2_score(real, pred) :.3f}",
|
|
"MAE": f"{mean_absolute_error(real, pred):.3f}",
|
|
"MSE": f"{mean_squared_error(real, pred):.3f}",
|
|
"RMSE": f"{mean_squared_error(real, pred, squared=False):.3f}",
|
|
}
|
|
|
|
print("KPIs")
|
|
df = pd.DataFrame.from_dict(kpis, orient="index")
|
|
if gtff.USE_TABULATE_DF:
|
|
print(tabulate(df, tablefmt="fancy_grid", showindex=True))
|
|
else:
|
|
print(df.to_string())
|
|
|
|
|
|
def price_prediction_backtesting_color(val: list) -> str:
|
|
"""Add color to backtest data"""
|
|
err_pct = 100 * (val[0] - val[1]) / val[1]
|
|
if val[0] > val[1]:
|
|
s_err_pct = f" {Fore.GREEN} +{err_pct:.2f} %"
|
|
else:
|
|
s_err_pct = f" {Fore.RED} {err_pct:.2f} %"
|
|
return f"{val[1]:.2f} x {Fore.YELLOW}{val[0]:.2f}{s_err_pct}{Style.RESET_ALL}"
|