Exemple #1
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)
Exemple #2
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