Ejemplo n.º 1
0
    def test_calc_annual_return(self):
        historical_data = {"Adj Close": [3.0, 3.5, 4.2], "Close": [3.0, 5.0, 4.2]}
        historical_index = pd.date_range("2012-01-01", periods=3, freq='2D')
        metric = Metric(historical=pd.DataFrame(historical_data, index=historical_index))

        annual_return1 = metric.calc_annual_return(pd.Timestamp("2012-01-02"), pd.Timestamp("2012-01-04"))
        annual_return2 = metric.calc_annual_return(pd.Timestamp("2012-01-02"), pd.Timestamp("2012-01-04"), "Close")

        self.assertAlmostEqual(annual_return1, 36.5)
        self.assertAlmostEqual(annual_return2, -29.2)
Ejemplo n.º 2
0
    def test_calc_debt_to_asset_ratio(self):
        balance_sheet_data = {
            "Total Liabilities / Total Assets": [0.69, 0.68, 0.67]
        }
        balance_sheet_index = pd.to_datetime(["2012-01-01", "2012-01-02", "2012-01-03"])

        metric = Metric(
            balance_sheet=pd.DataFrame(balance_sheet_data, index=balance_sheet_index),
            financial_report_preparation_lag=np.timedelta64(1, 'D')
        )

        debt_ratio = metric.calc_debt_to_asset_ratio()

        # because of 1 day financial_report_preparation_lag
        expected_index = pd.to_datetime(["2012-01-02", "2012-01-03", "2012-01-04"])
        self.assertFrameEqual(debt_ratio, pd.Series([0.69, 0.68, 0.67], index=expected_index))
Ejemplo n.º 3
0
    def test_calc_profit_margin(self):
        income_statement_data = {
            "Gross Income": [62, 50, 36],
            "Sales/Revenue": [3.1, 2.0, 1.2]
        }
        income_statement_index = pd.to_datetime(["2012-01-01", "2012-01-02", "2012-01-03"])

        metric = Metric(
            income_statement=pd.DataFrame(income_statement_data, index=income_statement_index),
            financial_report_preparation_lag=np.timedelta64(1, 'D')
        )

        gross_profit_margin = metric.calc_profit_margin("gross")

        # because of 1 day financial_report_preparation_lag
        expected_index = pd.to_datetime(["2012-01-02", "2012-01-03", "2012-01-04"])
        self.assertFrameEqual(gross_profit_margin, pd.Series([20, 25, 30], index=expected_index), check_dtype=False)
Ejemplo n.º 4
0
    def test_at(self):
        # make up some fake data for testing attribute transfer
        data = lambda: {'d': np.random.randn(3)}
        index = pd.date_range("2012-01-01", periods=3)
        metric = Metric(
            historical=pd.DataFrame(data(), index=index),
            balance_sheet=pd.DataFrame(data(), index=index),
            income_statement=pd.DataFrame(data(), index=index),
            cash_flow=pd.DataFrame(data(), index=index)
        )

        date = pd.Timestamp("2012-12-21")
        obj = metric.at(date)

        self.assertEqual(obj.date, date)
        other_attributes = {k: v for k, v in vars(obj).items() if k != "date"}
        self.assertEqual(other_attributes, vars(metric))
        self.assertIsInstance(obj, DatedMetric)
Ejemplo n.º 5
0
    def test_calc_pe_ratio(self):
        historical_data = {"Adj Close": [3.0, 3.5, 4.2], "Close": [3.0, 5.0, 4.2]}
        historical_index = pd.to_datetime(["2012-01-01", "2012-01-03", "2012-01-05"])

        income_statement_data = {"EPS (Basic)": [1.2, 2.5, 3.5], "EPS (Diluted)": [6.0, 4.0, 8.0]}
        income_statement_index = pd.to_datetime(["2012-01-01", "2012-01-02", "2012-01-03"])

        metric = Metric(
            historical=pd.DataFrame(historical_data, index=historical_index),
            income_statement=pd.DataFrame(income_statement_data, index=income_statement_index),
            financial_report_preparation_lag=np.timedelta64(1, 'D')
        )

        pe_ratios1 = metric.calc_pe_ratio()
        pe_ratios2 = metric.calc_pe_ratio("Close", "EPS (Diluted)")

        # because of 1 day financial_report_preparation_lag
        expected_index = pd.to_datetime(["2012-01-02", "2012-01-03", "2012-01-04"])
        self.assertFrameEqual(pe_ratios1, pd.Series([2.5, 1.4, 1], index=expected_index))
        self.assertFrameEqual(pe_ratios2, pd.Series([0.5, 1.25, 0.625], index=expected_index))
Ejemplo n.º 6
0
    def test_from_archive(self):
        fake_load = lambda data_type, symbol: data_type[0]

        with patch("fa.analysis.finance.load_fundamental_data", MagicMock(side_effect=fake_load)) as mock_load_fundamental_data, \
             patch("fa.analysis.finance.Metric.__init__", MagicMock(return_value=None)) as mock_constructor:

            obj = Metric.from_archive("C6L.SI", 1, b=2)
            mock_load_fundamental_data.assert_has_calls([
                call("price", "C6L.SI"),
                call("balance_sheet", "C6L.SI"),
                call("income_statement", "C6L.SI"),
                call("cash_flow", "C6L.SI"),
            ])
            mock_constructor.assert_called_once_with(1, b=2, historical='p', balance_sheet='b', cash_flow='c', income_statement='i')

        self.assertIsInstance(obj, Metric)
Ejemplo n.º 7
0
initialize.init()

symbols = OrderedDict(
    [
        ("C6L.SI", "Singapore Airlines"),
        ("J7X.SI", "Tiger Airways"),
        ("S53.SI", "SMRT"),
        ("C52.SI", "ComfortDelGro"),
        ("N03.SI", "Neptune Orient Lines"),
        ("Z74.SI", "Singtel"),
        ("CC3.SI", "Starhub"),
        ("B2F.SI", "M1"),
    ]
)

metrics = [Metric.from_archive(s) for s in symbols]
pe_ratios_list = [m.calc_pe_ratio() for m in metrics]

# figure 1
fig = plt.figure(figsize=(12, 10))

title = "P/E Ratio of stocks over Date\n"
xticks = reduce(lambda a, b: a | b, [ts.index for ts in pe_ratios_list])

with pd.plot_params.use("x_compat", True):
    for series, label in zip(pe_ratios_list, symbols.values()):
        graph = series.plot(label=label, legend=True, title=title, xticks=xticks)
        graph.set_xlabel("Date")
        graph.set_ylabel("P/E Ratio")

# prepare graph data