def test_guess_formatter(self): for n, t in (3, 'k'), (6, 'M'), (9, 'B'): m = 10**n s = pd.Series([2.1 * m, -20.1 * m, 200.1 * m]) actual = fmt.guess_formatter(s, precision=1)(s) expected = pd.Series(['2.1' + t, '(20.1%s)' % t, '200.1' + t]) pt.assert_series_equal(expected, actual) # percents s = pd.Series([.024, -.561, .987]) actual = fmt.guess_formatter(s, precision=1, pcts=1)(s) expected = pd.Series(['2.4%', '(56.1%)', '98.7%']) pt.assert_series_equal(expected, actual)
def test_guess_formatter(self): for n, t in (3, "k"), (6, "M"), (9, "B"): m = 10**n s = pd.Series([2.1 * m, -20.1 * m, 200.1 * m]) actual = fmt.guess_formatter(s, precision=1)(s) expected = pd.Series(["2.1" + t, "(20.1%s)" % t, "200.1" + t]) pt.assert_series_equal(expected, actual) # percents s = pd.Series([0.024, -0.561, 0.987]) actual = fmt.guess_formatter(s, precision=1, pcts=1)(s) expected = pd.Series(["2.4%", "(56.1%)", "98.7%"]) pt.assert_series_equal(expected, actual)
def test_guess_formatter(self): for n, t in (3, "k"), (6, "M"), (9, "B"): m = 10 ** n s = pd.Series([2.1 * m, -20.1 * m, 200.1 * m]) actual = fmt.guess_formatter(s, precision=1)(s) expected = pd.Series(["2.1" + t, "(20.1%s)" % t, "200.1" + t]) pt.assert_series_equal(expected, actual) # percents s = pd.Series([0.024, -0.561, 0.987]) actual = fmt.guess_formatter(s, precision=1, pcts=1)(s) expected = pd.Series(["2.4%", "(56.1%)", "98.7%"]) pt.assert_series_equal(expected, actual)
def test_guess_formatter(self): for n, t in (3, 'k'), (6, 'M'), (9, 'B'): m = 10 ** n s = pd.Series([2.1 * m, -20.1 * m, 200.1 * m]) actual = fmt.guess_formatter(s, precision=1)(s) expected = pd.Series(['2.1' + t, '(20.1%s)' % t, '200.1' + t]) pt.assert_series_equal(expected, actual) # percents s = pd.Series([.024, -.561, .987]) actual = fmt.guess_formatter(s, precision=1, pcts=1)(s) expected = pd.Series(['2.4%', '(56.1%)', '98.7%']) pt.assert_series_equal(expected, actual)
def plot_ltd(self, ax=None, style='k', label='ltd', show_dd=1, guess_xlabel=1): ltd = self.pl.cumsum() ax = ltd.plot(ax=ax, style=style, label=label) if show_dd: dd = self.drawdowns dd.plot(style='r', label='drawdowns', alpha=.5) ax.fill_between(dd.index, 0, dd.values, facecolor='red', alpha=.25) fmt = lambda x: x # guess the formatter if guess_xlabel: from tia.util.fmt import guess_formatter from tia.util.mplot import AxesFormat fmt = guess_formatter(ltd.abs().max(), precision=1) AxesFormat().Y.apply_format(fmt).apply(ax) 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) return ax
def guess_format(self, rb=1, align=1, **fmt_args): from tia.util.fmt import NumberFormat fct = fmt.guess_formatter(self.actual_values, **fmt_args) if isinstance(fmt, NumberFormat): return self.apply_number_format(fct, rb=rb, align=align) else: return self.apply_format(fct)
def plot_ltd(self, ax=None, style="k", label="ltd", show_dd=1, guess_xlabel=1, title=True): ltd = self.ltd_frame.pl ax = ltd.plot(ax=ax, style=style, label=label) if show_dd: dd = self.drawdowns dd.plot(style="r", label="drawdowns", alpha=0.5) ax.fill_between(dd.index, 0, dd.values, facecolor="red", alpha=0.25) fmt = lambda x: x # guess the formatter if guess_xlabel: from tia.util.fmt import guess_formatter from tia.util.mplot import AxesFormat fmt = guess_formatter(ltd.abs().max(), precision=1) AxesFormat().Y.apply_format(fmt).apply(ax) 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: df = new_dynamic_formatter(precision=1, parens=False, trunc_dot_zeros=True) total = df(ltd.iloc[-1]) vol = df(self.std) mdd = df(self.maxdd) title = "pnl %s vol %s maxdd %s" % (total, vol, mdd) title and ax.set_title(title, fontdict=dict(fontsize=10, fontweight="bold")) return ax
def plot_ltd(self, ax=None, style='k', label='ltd', show_dd=1, guess_xlabel=1, title=True): ltd = self.ltd_frame.pl ax = ltd.plot(ax=ax, style=style, label=label) if show_dd: dd = self.drawdowns dd.plot(style='r', label='drawdowns', alpha=.5) ax.fill_between(dd.index, 0, dd.values, facecolor='red', alpha=.25) fmt = lambda x: x # guess the formatter if guess_xlabel: from tia.util.fmt import guess_formatter from tia.util.mplot import AxesFormat fmt = guess_formatter(ltd.abs().max(), precision=1) AxesFormat().Y.apply_format(fmt).apply(ax) 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: df = new_dynamic_formatter(precision=1, parens=False, trunc_dot_zeros=True) total = df(ltd.iloc[-1]) vol = df(self.std) mdd = df(self.maxdd) title = 'pnl %s vol %s maxdd %s' % (total, vol, mdd) title and ax.set_title(title, fontdict=dict(fontsize=10, fontweight='bold')) return ax
def guess_number_format(self, rb=1, align=1, **fmt_args): """Determine the most appropriate formatter by inspected all the region values""" fct = fmt.guess_formatter(self.actual_values, **fmt_args) return self.apply_number_format(fct, rb=rb, align=align)