Exemplo n.º 1
0
    def test_get_first_commonly_available_year(self):
        yb = YearBegin()
        n = np.nan

        timeseries_coll = [
            pd.Series([1, 2, 3, 4], index=pd.date_range('2000-01-01', periods=4, freq=yb)),
            pd.Series(   [2, 3, 4], index=pd.date_range('2001-01-01', periods=3, freq=yb)),
            pd.Series(      [n, 4], index=pd.date_range('2002-01-01', periods=2, freq=yb)),
            pd.Series(         [4], index=pd.date_range('2003-01-01', periods=1, freq=yb)),
        ]

        self.assertEqual(measurement.get_first_commonly_available_year(timeseries_coll, 0.25), 2000)
        self.assertEqual(measurement.get_first_commonly_available_year(timeseries_coll, 0.26), 2001)
        self.assertEqual(measurement.get_first_commonly_available_year(timeseries_coll, 0.5), 2001)
        self.assertEqual(measurement.get_first_commonly_available_year(timeseries_coll, 0.51), 2003)
Exemplo n.º 2
0
    def test_get_first_commonly_available_year__smaller_freq(self):
        qb = QuarterBegin()
        n = np.nan

        timeseries_coll = [
            pd.Series([n, n, n, 4], index=pd.date_range('2000-01-01', periods=4, freq=qb)),
            pd.Series(   [n, n, 4], index=pd.date_range('2000-04-01', periods=3, freq=qb)),
            pd.Series(      [n, 4], index=pd.date_range('2000-07-01', periods=2, freq=qb)),
            pd.Series(         [4], index=pd.date_range('2000-10-01', periods=1, freq=qb)),
        ]

        self.assertEqual(measurement.get_first_commonly_available_year(timeseries_coll), 2000)
Exemplo n.º 3
0
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
buy_year = get_first_commonly_available_year(pe_ratios_list)
pe_ratios_at_buy_year_list = [ts.loc[str(buy_year)] for ts in pe_ratios_list]
buy_dates = [ser.index[0] for ser in pe_ratios_at_buy_year_list]
pe_ratios = [ser.iat[0] for ser in pe_ratios_at_buy_year_list]

dated_metrics = [m.at(buy_date) for m, buy_date in zip(metrics, buy_dates)]

sell_date = pd.Timestamp(end_date) - np.timedelta64(1, "D")
percentage_annual_returns = [dm.calc_annual_return(sell_date) * 100 for dm in dated_metrics]

profit_margin_labels = [k.title() + " Profit Margin (%)" for k in PROFIT_MARGIN_KINDS]
profit_margins = {
    label: [dm.calc_profit_margin(kind) * 100 for dm in dated_metrics]
    for label, kind in zip(profit_margin_labels, PROFIT_MARGIN_KINDS)
}