Example #1
0
 def __init__(self,
              span: int = 60,
              a_col: str = 'Adj Close',
              a_df: DataFrame = DataFrame(),
              t_s: TimeSpan = TimeSpan()):
     self.__model = SVR(kernel='rbf', C=1e3,
                        gamma=0.2)  #percent 80/20 = 0.2
     self._forward_span = span
     print(self._label)
     self._column = self._label + self._name + str(span)
     print(self._column)
     self._src_col = a_col
     self._src_data = a_df
     self._setData()
     self._time_span = t_s
     self._forward_array = np.array(self._data.drop(
         [self._column], 1))[-self._forward_span:]
     self._setIndependent()
     self._setDependent()
     # split into 80% train / 20% test => 0.2
     X_train, X_test, Y_train, Y_test = train_test_split(self._x_array,
                                                         self._y_array,
                                                         test_size=0.2)
     # fit model TRAINED
     self.__model.fit(X_train, Y_train)
     # score TESTED
     self._score = round(self.__model.score(X_test, Y_test), 7)
     self._prediction = self.__model.predict(self._forward_array)
     self._setForecast()
 def __init__(self,
              span: int = 60,
              a_col: str = 'Adj Close',
              a_df: DataFrame = DataFrame(),
              t_s: TimeSpan = TimeSpan()):
     self._forward_span = span
     print(self._label)
     self._column = self._label + self._name + str(span)
     print(self._column)
     self._src_col = a_col
     self._src_data = a_df
     self._setData()
     self._time_span = t_s
     dataSet: ndarray = self._data[self._src_col].values
     print(dataSet.shape)
     dataLength = np.math.ceil(len(dataSet) * 0.8)
     scale = MinMaxScaler(feature_range=(0, 1))
     dataScaled = scale.fit_transform(dataSet.reshape(-1, 1))
     dataTrained = dataScaled[0:dataLength, :]
     x_train = []
     y_train = []
     for i in range(self._forward_span, len(dataTrained)):
         x_train.append(
             dataTrained[i - self._forward_span:self._forward_span, 0])
         y_train.append(dataTrained[self._forward_span, 0])
     x_train, y_train = np.array(x_train), np.array(y_train)
     print('0', x_train.shape[0])
     print('1', x_train.shape)
     x_train = np.reshape(x_train, (x_train.shape[0], 1, 1))
     print('X_T shape', x_train.shape)
     self.__model = Sequential()
     self.__model.add(
         LSTM(50, return_sequences=True, input_shape=(x_train.shape[1], 1)))
     self.__model.add(LSTM(50, return_sequences=False))
     self.__model.add(Dense(25))
     self.__model.add(Dense(1))
     self.__model.compile(optimizer='adam', loss='mean_squared_error')
Example #3
0
 def _updateTimeSpan(
     self, t_s: TimeSpan, a_df: pd.DataFrame = pd.DataFrame()) -> TimeSpan:
     t_s.setStartDateStr(a_df.index.to_pydatetime()[0].strftime('%Y-%m-%d'))
     return t_s
Example #4
0
class BuffettIndicator(AbstractTechIndicator):
    _data: DataFrame = DataFrame()
    _us_gdp: DataFrame = DataFrame()
    _wilshire_index: Wilshire5kIndex
    _time_span: TimeSpan = TimeSpan()

    def __init__(self, y_stock_option: YahooStockOption):
        super().__init__(y_stock_option)
        self._time_span = y_stock_option.TimeSpan
        self._name = 'Buffett'
        self._label = self._name + self._label
        self._main_label += ' ' + self._label
        self._setData(y_stock_option.DataFrame)

    def GetData(self) -> DataFrame:
        return self._data

    def PlotAx(self, ax: object) -> object:
        an_alpha: float = 1.0
        self._data[self._label].plot(alpha=an_alpha, ax=ax)
        return ax

    def PlotData(self) -> plt:
        plt.figure(figsize=self._fig_size)
        plt.style.use(self._plot_style)
        colors = cm.coolwarm
        an_alpha: float = 1.0
        self._data[self._label].plot(alpha=an_alpha)
        #for a_ind, col in enumerate(self._data.columns[-2:self._data.columns.size]):
        #    an_alpha: float = 0.5 if a_ind != 0 else 1.0
        #    self._data[col].plot(alpha=an_alpha)
        plt.title(self._main_label)
        plt.xlabel(self._x_label)
        plt.xticks(rotation=self._x_ticks_angle)
        plt.ylabel(self._y_label)
        plt.legend(loc=self._legend_place)
        plt.grid(True)
        plt.tight_layout()
        return plt

    def _setData(self, a_df: DataFrame):
        self.__setIndex()
        wilshire_quarterly: DataFrame = self.__setIndexQuarterly()
        self.__setGdp()
        self._data = pd.merge(wilshire_quarterly,
                              self._us_gdp,
                              left_index=True,
                              right_index=True)
        # Multiply the Wilshire 5000 Full Cap index by $1.19 billion to set it to 1980's USD per Wilshire's notes.
        self._data['WIL5000'] = self._data['WIL5000'] * 1190000000
        # Calculate the indicator as market cap / GDP
        self._data[self._label] = self._data['WIL5000'] / self._data["GDP"]
        print(self._data.columns)

    def __setIndex(self):
        self._wilshire_index = Wilshire5kIndex('yahoo', "^W5000",
                                               self._time_span)
        #self._wilshire_index.Data.dropna(inplace=True)
        self._wilshire_index.Data.fillna(method='ffill', inplace=True)
        self._wilshire_index.Data.fillna(method='bfill', inplace=True)
        self._wilshire_index.Data.columns = ['WIL5000']
        print('WIL', self._wilshire_index.Data.columns)

    def __setIndexQuarterly(self) -> DataFrame:
        df: DataFrame = self._wilshire_index.Data.copy()
        #df.resample('3M').last()
        print('3M', df.columns)
        return df

    def __setGdp(self):
        self._us_gdp = pdr.DataReader('GDP', 'fred', self._time_span.StartDate,
                                      self._time_span.EndDate)
        #self._us_gdp.dropna(inplace=True)
        self._us_gdp.fillna(method='ffill', inplace=True)
        self._us_gdp.fillna(method='bfill', inplace=True)
        print('GDP', self._us_gdp.columns)
class AbstractStockOption(ABC):
    IsDaily: bool = False
    IsWeekly: bool = False
    IsMonthly: bool = False
    IsQuarterly: bool = False
    IsAnnually: bool = False
    _source: str = 'yahoo'
    _column: str = 'Adj Close'
    _ticker: str = 'TD'
    _price: float = -1.1
    _high52: float = -1.1
    _low52: float = -1.1
    _range52: List[float]
    _sigma: float = -1.1
    _mu: float = -1.1
    _median: float = -1.1
    _norm_pdf: ndarray
    _data_range: ndarray
    _t_s: TimeSpan = TimeSpan()
    _data: DataFrame
    _historical: DataFrame
    _fin_viz_engine: FinVizEngine
    _y_finance_engine: YahooFinanceEngine
    _yahooSummaryScrapper: YahooSummaryScrapper

    @property
    def Column(self):
        return self._column

    @property
    def Data(self):
        return self._data

    @property
    def DataFrame(self):
        return self._historical

    @property
    def Price(self):
        return self._price

    @property
    def Source(self):
        return self._source

    @property
    def Ticker(self):
        return self._ticker

    @property
    def TimeSpan(self):
        return self._t_s

    @property
    def High52(self):
        return self._high52

    @property
    def Low52(self):
        return self._low52

    @property
    def Range52(self):
        return self._range52

    @property
    def DataRange(self):
        return self._data_range

    @property
    def Mu(self):
        return self._mu

    @property
    def Median(self):
        return self._median

    @property
    def Sigma(self):
        return self._sigma

    @property
    def NormProbDensityFn(self):
        return self._norm_pdf