# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Machine Learning Nanodegree
#
# ## Capstone Project
#
# ### Project: Stock Price Prediction
#
#

# --- 
#
#
# ## User Interface for random stock and date range prediction

# First, importing all required modules and libraries

# +
# %matplotlib inline

import numpy as np
np.random.seed(0)

import time
import datetime
from calendar import monthrange
import pandas as pd

from IPython.display import display 
from IPython.display import clear_output

import warnings
warnings.filterwarnings('ignore')

from StockRegressor import StockRegressor
from StockRegressor import StockGridSearch

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (15,8)

from IPython.display import Markdown, display

# initializing numpy seed so that we get reproduciable results, especially with Keras
np.random.seed(0)
# -

# ---
#
# The below is just to enable the Jupyter Notebook to show long output, without having to scroll.

# + language="html"
# <style>
# .output_wrapper, .output {
# height:auto !important;
# max-height:15000px; /* your desired max-height here */
# }
# .output_scroll {
# box-shadow:none !important;
# webkit-box-shadow:none !important;
# }
# </style>
# -

# ### Entering the Tickers and the Date Range

# Please enter below the list of ticker symbols for the stocks you wish to predict, separated by commas e.g. GOOG, AAPL, IBM, GLD, XOM

stock_names_str = input()

# Please enter below the data range for the stocks you wish to predict, separated by a comma, in YYYY-MM-DD format
# e.g. 2011-01-01, 2013-08-18

date_range_str = input()

# ### Instantiating the StockRegressor Objects
#
# The below will instantiate the StockRegressor objects.
#
#
# > **Note:**  Please note that there might be a bug in the Pandas library, that is causing an intermitten error with the Yahoo Finance web call. The bug could be traced to the file in /anaconda/envs/**your_environment**/lib/python3.5/site-packages/pandas/core/indexes/datetimes.py, at line 1050:
# This line is causing the error: "if this.freq is None:". Another if condition should be inserted before that, to test for the "freq" attribute, such as: "if hasattr(this, 'freq'):"
#
# > **Note:**  The fixed datetimes.py file is included with the submission
#
#

# +
date_range = date_range_str.replace(" ", "").strip('\n').split(',')
stock_tickers = stock_names_str.replace(" ", "").strip('\n').split(',')

stocks = {}

for ticker in stock_tickers:
    stocks[ticker] = StockRegressor(ticker, dates=date_range, n_days_to_read_ahead = 150)
    
# -

# ### Training, Scoring, Predicting and Plotting
#
# Default values of the hyper-parameters are: 
#
# * Regressions: regressions of polynomial order 1, 2, and 3 are implemented
# * Number of harmonics: default is set to 4
# * Underlying Trend Poly of order 3 is used for the FFT de-trending
# * Days for Regression: the default is set to 15
# * Momentum Split: the default is set to 0.25

for ticker, stock in stocks.items():
    print("\n--------------------------------------------------------------------")
    print("--------------------------------------------------------------------")
    print("--------------------------------------------------------------------")
    print("--------------------------------------------------------------------\n")
    current_ticker = "**Now training {} ... \n**".format(ticker)
    display(Markdown(current_ticker))
    stock.train()
    stock.score_verbose()
    stock.predict()
    stock.plot_learning_data_frame()

StarcoderdataJupyterScriptsDedupFiltered