示例#1
0
    def test_default_formats(self):
        B = float("-1,250,500,880.76".replace(",", ""))
        M = B / 1000.0
        k = M / 1000.0
        p = k / 1000000.0
        tests = [
            (B, "$(1.3B)", fmt.BillionDollarsFormatter),
            (B, "(1.3B)", fmt.BillionsFormatter),
            (M, "$(1.3M)", fmt.MillionDollarsFormatter),
            (M, "(1.3M)", fmt.MillionsFormatter),
            (k, "$(1.3k)", fmt.ThousandDollarsFormatter),
            (k, "(1.3k)", fmt.ThousandsFormatter),
            (k, "(1,250.50)", fmt.FloatFormatter),
            (k, "(1,251)", fmt.IntFormatter),
            # Floats
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            (str(k), "-1,251",
             fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            # Percents
            (0.12433, "12.4%", fmt.new_percent_formatter(commas=1,
                                                         precision=1)),
            (0.12433, "12.433%",
             fmt.new_percent_formatter(commas=1, precision=3)),
            (
                -0.12433,
                "-12.4%",
                fmt.new_percent_formatter(commas=1, parens=0, precision=1),
            ),
            (
                -0.12433,
                "(12.4%)",
                fmt.new_percent_formatter(commas=1, parens=1, precision=1),
            ),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEqual(expected, actual)
            # Test if it were a list
            actual = fct([val] * 5)
            self.assertEqual([expected] * 5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val] * 5))
            pt.assert_series_equal(pd.Series([expected] * 5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({"a": [val] * 5, "b": [val] * 5}))
            pt.assert_frame_equal(
                pd.DataFrame({
                    "a": [expected] * 5,
                    "b": [expected] * 5
                }), actual)
示例#2
0
    def test_default_formats(self):
        B = float('-1,250,500,880.76'.replace(',', ''))
        M = B / 1000.
        k = M / 1000.
        p = k / 1000000.
        tests = [
            (B, '$(1.3B)', fmt.BillionDollarsFormatter),
            (B, '(1.3B)', fmt.BillionsFormatter),
            (M, '$(1.3M)', fmt.MillionDollarsFormatter),
            (M, '(1.3M)', fmt.MillionsFormatter),
            (k, '$(1.3k)', fmt.ThousandDollarsFormatter),
            (k, '(1.3k)', fmt.ThousandsFormatter),
            (k, '(1,250.50)', fmt.FloatFormatter),
            (k, '(1,251)', fmt.IntFormatter),
            # Floats
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            (str(k), '-1,251',
             fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            # Percents
            (.12433, '12.4%', fmt.new_percent_formatter(commas=1,
                                                        precision=1)),
            (.12433, '12.433%', fmt.new_percent_formatter(commas=1,
                                                          precision=3)),
            (-.12433, '-12.4%',
             fmt.new_percent_formatter(commas=1, parens=0, precision=1)),
            (-.12433, '(12.4%)',
             fmt.new_percent_formatter(commas=1, parens=1, precision=1)),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEquals(expected, actual)
            # Test if it were a list
            actual = fct([val] * 5)
            self.assertEquals([expected] * 5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val] * 5))
            pt.assert_series_equal(pd.Series([expected] * 5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({'a': [val] * 5, 'b': [val] * 5}))
            pt.assert_frame_equal(
                pd.DataFrame({
                    'a': [expected] * 5,
                    'b': [expected] * 5
                }), actual)
示例#3
0
文件: ret.py 项目: xie3ge/tia
    def plot_ltd(self, ax=None, style='k', label='ltd', show_dd=1, title=True, legend=1):
        ltd = self.ltd_rets
        ax = ltd.plot(ax=ax, style=style, label=label)
        if show_dd:
            dd = self.drawdowns
            dd.plot(style='r', label='drawdowns', alpha=.5, ax=ax)
            ax.fill_between(dd.index, 0, dd.values, facecolor='red', alpha=.25)
            fmt = PercentFormatter

            AxesFormat().Y.percent().X.label("").apply(ax)
            legend and ax.legend(loc='upper left', prop={'size': 12})

            # show the actualy date and value
            mdt, mdd = self.maxdd_dt, self.maxdd
            bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.25)
            try:
                dtstr = '{0}'.format(mdt.to_period())
            except:
                # assume daily
                dtstr = '{0}'.format(hasattr(mdt, 'date') and mdt.date() or mdt)
            ax.text(mdt, dd[mdt], "{1} \n {0}".format(fmt(mdd), dtstr).strip(), ha="center", va="top", size=8,
                    bbox=bbox_props)

        if title is True:
            pf = new_percent_formatter(1, parens=False, trunc_dot_zeros=True)
            ff = new_float_formatter(precision=1, parens=False, trunc_dot_zeros=True)
            total = pf(self.ltd_ann)
            vol = pf(self.std_ann)
            sh = ff(self.sharpe_ann)
            mdd = pf(self.maxdd)
            title = 'ret$\mathregular{_{ann}}$ %s     vol$\mathregular{_{ann}}$ %s     sharpe %s     maxdd %s' % (
            total, vol, sh, mdd)

        title and ax.set_title(title, fontdict=dict(fontsize=10, fontweight='bold'))
        return ax
示例#4
0
    def plot_hist(self, ax=None, **histplot_kwargs):
        pf = new_percent_formatter(precision=1,
                                   parens=False,
                                   trunc_dot_zeros=1)
        ff = new_float_formatter(precision=1, parens=False, trunc_dot_zeros=1)

        ax = self.rets.hist(ax=ax, **histplot_kwargs)
        AxesFormat().X.percent(1).apply(ax)
        m, s, sk, ku = pf(self.mean), pf(self.std), ff(self.skew), ff(
            self.kurtosis)
        txt = (
            "$\mathregular{\mu}$=%s   $\mathregular{\sigma}$=%s   skew=%s   kurt=%s"
            % (m, s, sk, ku))
        bbox = dict(facecolor="white", alpha=0.5)
        ax.text(
            0,
            1,
            txt,
            fontdict={"fontweight": "bold"},
            bbox=bbox,
            ha="left",
            va="top",
            transform=ax.transAxes,
        )
        return ax
示例#5
0
文件: test_fmt.py 项目: sradnor/tia
    def test_default_formats(self):
        B = float("-1,250,500,880.76".replace(",", ""))
        M = B / 1000.0
        k = M / 1000.0
        p = k / 1000000.0
        tests = [
            (B, "$(1.3B)", fmt.BillionDollarsFormatter),
            (B, "(1.3B)", fmt.BillionsFormatter),
            (M, "$(1.3M)", fmt.MillionDollarsFormatter),
            (M, "(1.3M)", fmt.MillionsFormatter),
            (k, "$(1.3k)", fmt.ThousandDollarsFormatter),
            (k, "(1.3k)", fmt.ThousandsFormatter),
            (k, "(1,250.50)", fmt.FloatFormatter),
            (k, "(1,251)", fmt.IntFormatter),
            # Floats
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            (str(k), "-1,251", fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, "-1,251", fmt.new_int_formatter(commas=1, parens=False)),
            (k, "-1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1251", fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), "1,251", fmt.new_int_formatter(commas=1)),
            # Percents
            (0.12433, "12.4%", fmt.new_percent_formatter(commas=1, precision=1)),
            (0.12433, "12.433%", fmt.new_percent_formatter(commas=1, precision=3)),
            (-0.12433, "-12.4%", fmt.new_percent_formatter(commas=1, parens=0, precision=1)),
            (-0.12433, "(12.4%)", fmt.new_percent_formatter(commas=1, parens=1, precision=1)),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEquals(expected, actual)
            # Test if it were a list
            actual = fct([val] * 5)
            self.assertEquals([expected] * 5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val] * 5))
            pt.assert_series_equal(pd.Series([expected] * 5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({"a": [val] * 5, "b": [val] * 5}))
            pt.assert_frame_equal(pd.DataFrame({"a": [expected] * 5, "b": [expected] * 5}), actual)
示例#6
0
文件: test_fmt.py 项目: vanife/tia
    def test_default_formats(self):
        B = float('-1,250,500,880.76'.replace(',', ''))
        M = B / 1000.
        k = M / 1000.
        p = k / 1000000.
        tests = [
            (B, '$(1.3B)', fmt.BillionDollarsFormatter),
            (B, '(1.3B)', fmt.BillionsFormatter),
            (M, '$(1.3M)', fmt.MillionDollarsFormatter),
            (M, '(1.3M)', fmt.MillionsFormatter),
            (k, '$(1.3k)', fmt.ThousandDollarsFormatter),
            (k, '(1.3k)', fmt.ThousandsFormatter),
            (k, '(1,250.50)', fmt.FloatFormatter),
            (k, '(1,251)', fmt.IntFormatter),
            # Floats
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            (str(k), '-1,251', fmt.new_int_formatter(commas=1, coerce=True, parens=0)),
            # Ints
            (k, '-1,251', fmt.new_int_formatter(commas=1, parens=False)),
            (k, '-1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1251', fmt.new_int_formatter(commas=0, parens=False)),
            (abs(k), '1,251', fmt.new_int_formatter(commas=1)),
            # Percents
            (.12433, '12.4%', fmt.new_percent_formatter(commas=1, precision=1)),
            (.12433, '12.433%', fmt.new_percent_formatter(commas=1, precision=3)),
            (-.12433, '-12.4%', fmt.new_percent_formatter(commas=1, parens=0, precision=1)),
            (-.12433, '(12.4%)', fmt.new_percent_formatter(commas=1, parens=1, precision=1)),
        ]

        for val, expected, fct in tests:
            actual = fct(val)
            self.assertEqual(expected, actual)
            # Test if it were a list
            actual = fct([val]*5)
            self.assertEqual([expected]*5, actual)
            # Test if it were a series
            actual = fct(pd.Series([val]*5))
            pt.assert_series_equal(pd.Series([expected]*5), actual)
            # Test if it were a DataFrame
            actual = fct(pd.DataFrame({'a': [val]*5, 'b': [val]*5}))
            pt.assert_frame_equal(pd.DataFrame({'a': [expected]*5, 'b': [expected]*5}), actual)
示例#7
0
    def plot_hist(self, ax=None, **histplot_kwargs):
        pf = new_percent_formatter(precision=1, parens=False, trunc_dot_zeros=1)
        ff = new_float_formatter(precision=1, parens=False, trunc_dot_zeros=1)

        ax = self.rets.hist(ax=ax, **histplot_kwargs)
        AxesFormat().X.percent(1).apply(ax)
        m, s, sk, ku = pf(self.mean), pf(self.std), ff(self.skew), ff(self.kurtosis)
        txt = '$\mathregular{\mu}$=%s   $\mathregular{\sigma}$=%s   skew=%s   kurt=%s' % (m, s, sk, ku)
        bbox = dict(facecolor='white', alpha=0.5)
        ax.text(0, 1, txt, fontdict={'fontweight': 'bold'}, bbox=bbox, ha='left', va='top', transform=ax.transAxes)
        return ax
示例#8
0
文件: table.py 项目: georgebdavis/tia
def pad_positive_wrapper(fmtfct):
    """Ensure that numbers are aligned in table by appending a blank space to postive values if 'parenthesis' are
    used to denote negative numbers"""

    def check_and_append(*args, **kwargs):
        result = fmtfct(*args, **kwargs)
        if fmtfct.parens and not result.endswith(')'):
            result += ' '
        return result

    return check_and_append


IntFormatter = pad_positive_wrapper(fmt.new_int_formatter(nan='-'))
FloatFormatter = pad_positive_wrapper(fmt.new_float_formatter(nan='-'))
PercentFormatter = pad_positive_wrapper(fmt.new_percent_formatter(nan='-'))
ThousandsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(nan='-'))
MillionsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(nan='-'))
BillionsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(nan='-'))
# Don't attempt to pad
DynamicNumberFormatter = fmt.DynamicNumberFormat(nan='-', pcts=1, trunc_dot_zeros=1)

DollarCentsFormatter = pad_positive_wrapper(fmt.new_float_formatter(prefix='$', nan='-'))
DollarFormatter = pad_positive_wrapper(fmt.new_int_formatter(prefix='$', nan='-'))
ThousandDollarsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(prefix='$', nan='-'))
MillionDollarsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(prefix='$', nan='-'))
BillionDollarsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(prefix='$', nan='-'))
YmdFormatter = fmt.new_datetime_formatter('%Y%m%d', True)
Y_m_dFormatter = fmt.new_datetime_formatter('%Y_%m_%d', True)
mdYFormatter = fmt.new_datetime_formatter('%m/%d/%Y', True)
示例#9
0
文件: mplot.py 项目: vanife/tia
 def percent(self, precision=2):
     fct = fmt.new_percent_formatter(precision=precision)
     wrapper = lambda x, pos: fct(x)
     self.axis.set_major_formatter(FuncFormatter(wrapper))
     return self
示例#10
0
文件: table.py 项目: salihcaan/tia
def pad_positive_wrapper(fmtfct):
    """Ensure that numbers are aligned in table by appending a blank space to postive values if 'parenthesis' are
    used to denote negative numbers"""
    def check_and_append(*args, **kwargs):
        result = fmtfct(*args, **kwargs)
        if fmtfct.parens and not result.endswith(')'):
            result += ' '
        return result

    return check_and_append


IntFormatter = pad_positive_wrapper(fmt.new_int_formatter(nan='-'))
FloatFormatter = pad_positive_wrapper(fmt.new_float_formatter(nan='-'))
PercentFormatter = pad_positive_wrapper(fmt.new_percent_formatter(nan='-'))
ThousandsFormatter = pad_positive_wrapper(fmt.new_thousands_formatter(nan='-'))
MillionsFormatter = pad_positive_wrapper(fmt.new_millions_formatter(nan='-'))
BillionsFormatter = pad_positive_wrapper(fmt.new_billions_formatter(nan='-'))
# Don't attempt to pad
DynamicNumberFormatter = fmt.DynamicNumberFormat(method='col',
                                                 nan='-',
                                                 pcts=1,
                                                 trunc_dot_zeros=1)

DollarCentsFormatter = pad_positive_wrapper(
    fmt.new_float_formatter(prefix='$', nan='-'))
DollarFormatter = pad_positive_wrapper(
    fmt.new_int_formatter(prefix='$', nan='-'))
ThousandDollarsFormatter = pad_positive_wrapper(
    fmt.new_thousands_formatter(prefix='$', nan='-'))
示例#11
0
 def percent(self, precision=2):
     fct = fmt.new_percent_formatter(precision=precision)
     wrapper = lambda x, pos: fct(x)
     self.axis.set_major_formatter(FuncFormatter(wrapper))
     return self