Example #1
0
    def test_KPI_max_drawdown(self):
        tickers = ["TSLA", "SPY"]
        interval = Ct.INTERVAL.DAY

        conf = {
            Ct.tickers_key(): tickers,
            Ct.historical_type_key(): DATASOURCETYPE.YFINANCE,
            Ct.fundamentals_type_key(): None,
            Ct.fundamentals_options_key(): [],
            Ct.force_fundamentals_key(): False,
            Ct.indicators_key(): [],
            Ct.start_date_key(): dt.datetime(2021, 3, 7) - dt.timedelta(1825),
            Ct.end_date_key(): dt.datetime(2021, 3, 7),
            Ct.interval_key(): interval,
            Ct.period_key(): None,
            Ct.bulk_key(): True
        }

        stocks = \
            StocksFactory.create_stocks(
                conf=conf
            )

        prices_df = stocks[0].get_prices_data(keys={Ct.adj_close_key(): True})

        df = Financials.pct_change(prices_df)

        md = MaxDrawdown()
        result = md.calculate(df)

        self.assertEqual(0.6062653645917145,
                         result[Ct.max_drawdown_key()]['TSLA'])
        self.assertEqual(0.3371725544013077,
                         result[Ct.max_drawdown_key()]['SPY'])
Example #2
0
    def test_KPI_calmar(self):
        tickers = ["TSLA", "SPY"]
        interval = Ct.INTERVAL.DAY

        conf = {
            Ct.tickers_key(): tickers,
            Ct.historical_type_key(): DATASOURCETYPE.YFINANCE,
            Ct.fundamentals_type_key(): None,
            Ct.fundamentals_options_key(): [],
            Ct.force_fundamentals_key(): False,
            Ct.indicators_key(): [],
            Ct.start_date_key(): dt.datetime(2021, 3, 7) - dt.timedelta(1825),
            Ct.end_date_key(): dt.datetime(2021, 3, 7),
            Ct.interval_key(): interval,
            Ct.period_key(): None,
            Ct.bulk_key(): True
        }

        stocks = \
            StocksFactory.create_stocks(
                conf=conf
            )

        prices_df = stocks[0].get_prices_data(keys={Ct.adj_close_key(): True})

        df = Financials.pct_change(prices_df)

        params = {Ct.interval_key(): interval}
        calmar = Calmar()
        result = calmar.calculate(df, params)

        self.assertEqual(1.1700790532917946, result[Ct.calmar_key()]['TSLA'])
    def test_portfolio_rebalance(self):

        #  DJI constituent stocks
        tickers = ["MMM", "AXP", "T", "BA", "CAT", "CSCO", "KO", "XOM", "GE",
                   "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "MCD", "MRK", "MSFT",
                   "NKE", "PFE", "PG", "TRV", "UNH", "VZ", "V", "WMT", "DIS"]

        ref_tickers = ["^DJI"]

        end_date = dt.datetime.now()  # dt.datetime(2021, 3, 7)
        interval = Ct.INTERVAL.MONTH

        conf = {
            Ct.tickers_key(): tickers,
            Ct.historical_type_key(): DATASOURCETYPE.YFINANCE,
            Ct.fundamentals_type_key(): None,
            Ct.fundamentals_options_key(): [],
            Ct.force_fundamentals_key(): False,
            Ct.indicators_key(): [],
            Ct.start_date_key(): end_date-dt.timedelta(3650),
            Ct.end_date_key(): end_date,
            Ct.interval_key(): interval,
            Ct.period_key(): None,
            Ct.bulk_key(): True
        }

        stocks = \
            StocksFactory.create_stocks(
                conf=conf
            )

        conf[Ct.tickers_key()] = ref_tickers

        stocks_ref = \
            StocksFactory.create_stocks(
                conf=conf
            )

        prices_df = stocks[0].get_prices_data(keys={Ct.adj_close_key(): True})
        ref_prices_df = stocks_ref[0].get_prices_data(keys={Ct.adj_close_key(): True})

        pr = PortfolioRebalance()
        params = {"m": 6, "x": 3, Ct.interval_key(): conf[Ct.interval_key()]}
        pr.backtest(prices_df, ref_prices_df, params)
Example #4
0
    def get_prices_data(self, keys=None):

        if keys is None or len(keys) == 0:
            print("No keys has been specified. All keys were selected. ")

            keys = {
                Ct.high_key(): True,
                Ct.low_key(): True,
                Ct.open_key(): True,
                Ct.close_key(): True,
                Ct.adj_close_key(): True,
                Ct.volume_key(): True
            }

        method_tag = "get_prices_data"

        if self.data_source_historical is not None:
            if self.data_source_historical.prices is None or self.data_source_historical.prices.empty is True:
                raise ValueError(
                    "No historical data available, call method self.get_historical_data() first"
                )

            key_titles = self.get_key_titles(keys)

            if len(key_titles) > 0:

                prices = self.data_source_historical.get_prices(
                    self.tickers, key_titles)

            else:
                print("{} - There are no prices information, for ticker:{}".
                      format(method_tag, self.tickers))
                raise ValueError

        else:
            print("There has been an error in {}".format(method_tag))
            raise ValueError

        # Validate Price dataframe
        if prices.empty == True:
            print("There has been an error in {}".format(method_tag))
            raise ValueError

        self.price_info = prices
        return prices