Exemplo n.º 1
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'])
Exemplo n.º 2
0
class Calmar(KPI):
    kpi_name = Ct.calmar_key()

    def __init__(self, params=None):
        super().__init__(params)
        if not params:
            self.params = {}

    def calculate(self, df, params=None):
        super().calculate(df, params)
        self.result = Calmar.get_calmar(df, self.params)
        return self.result

    @staticmethod
    def get_calmar(input_df, params=None):
        """function to calculate Calmar"""
        if params is None:
            params = {}

        cagr = CAGR.get_cagr(input_df, params)
        max_dd = MaxDrawdown.get_max_drawdown(input_df)

        result_df = pd.DataFrame()
        result_df[Calmar.kpi_name] = (cagr[Ct.cagr_key()] /
                                      max_dd[Ct.max_drawdown_key()])

        return result_df