def test_dynamic_formatter(self): kwargs = dict(precision=1, commas=1, parens=1, pcts=1, trunc_dot_zeros=1) byrow = fmt.new_dynamic_formatter("row", **kwargs) bycol = fmt.new_dynamic_formatter("col", **kwargs) bycell = fmt.new_dynamic_formatter("cell", **kwargs) todt = pd.to_datetime f = pd.DataFrame( dict( pcts=[0.1, 0.2343, -0.9234], flt=[123.0, 1234.0, -12345.0], ts=[todt("1/1/2012"), todt("1/1/2013"), todt("1/1/2014")], ) ) # by column expected_bycol = { "pcts": ["10%", "23.4%", "(92.3%)"], "flt": ["123", "1,234", "(12,345)"], "ts": ["2012-01-01", "2013-01-01", "2014-01-01"], } pt.assert_frame_equal(pd.DataFrame(expected_bycol), bycol(f)) pt.assert_frame_equal(pd.DataFrame(expected_bycol).T, byrow(f.T)) expected_bycell = { "pcts": ["10%", "23.4%", "(92.3%)"], "flt": ["123", "1.2k", "(12.3k)"], "ts": ["2012-01-01", "2013-01-01", "2014-01-01"], } pt.assert_frame_equal(pd.DataFrame(expected_bycell), bycell(f)) # ensure nothing different pt.assert_frame_equal(pd.DataFrame(expected_bycell).T, bycell(f.T))
def test_dynamic_formatter(self): kwargs = dict(precision=1, commas=1, parens=1, pcts=1, trunc_dot_zeros=1) byrow = fmt.new_dynamic_formatter('row', **kwargs) bycol = fmt.new_dynamic_formatter('col', **kwargs) bycell = fmt.new_dynamic_formatter('cell', **kwargs) todt = pd.to_datetime f = pd.DataFrame(dict(pcts=[.1, .2343, -.9234], flt=[123., 1234., -12345.], ts=[todt('1/1/2012'), todt('1/1/2013'), todt('1/1/2014')])) # by column expected_bycol = {'pcts': ['10%', '23.4%', '(92.3%)'], 'flt': ['123', '1,234', '(12,345)'], 'ts': ['2012-01-01', '2013-01-01', '2014-01-01']} pt.assert_frame_equal(pd.DataFrame(expected_bycol), bycol(f)) pt.assert_frame_equal(pd.DataFrame(expected_bycol).T, byrow(f.T)) expected_bycell = {'pcts': ['10%', '23.4%', '(92.3%)'], 'flt': ['123', '1.2k', '(12.3k)'], 'ts': ['2012-01-01', '2013-01-01', '2014-01-01']} pt.assert_frame_equal(pd.DataFrame(expected_bycell), bycell(f)) # ensure nothing different pt.assert_frame_equal(pd.DataFrame(expected_bycell).T, bycell(f.T))
def _repr_html_(self): from tia.util.fmt import new_dynamic_formatter fmt = new_dynamic_formatter( method="row", precision=2, pcts=1, trunc_dot_zeros=1, parens=1 ) return fmt(self.series.to_frame())._repr_html_()
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 _repr_html_(self): from tia.util.fmt import new_dynamic_formatter fmt = new_dynamic_formatter(method='row', precision=2, pcts=1, trunc_dot_zeros=1, parens=1) df = self.summary.to_frame() return fmt(df)._repr_html_()
def test_dynamic_formatter(self): kwargs = dict(precision=1, commas=1, parens=1, pcts=1, trunc_dot_zeros=1) byrow = fmt.new_dynamic_formatter("row", **kwargs) bycol = fmt.new_dynamic_formatter("col", **kwargs) bycell = fmt.new_dynamic_formatter("cell", **kwargs) todt = pd.to_datetime f = pd.DataFrame( dict( pcts=[0.1, 0.2343, -0.9234], flt=[123.0, 1234.0, -12345.0], ts=[todt("1/1/2012"), todt("1/1/2013"), todt("1/1/2014")], )) # by column expected_bycol = { "pcts": ["10%", "23.4%", "(92.3%)"], "flt": ["123", "1,234", "(12,345)"], "ts": ["2012-01-01", "2013-01-01", "2014-01-01"], } pt.assert_frame_equal(pd.DataFrame(expected_bycol), bycol(f)) pt.assert_frame_equal(pd.DataFrame(expected_bycol).T, byrow(f.T)) expected_bycell = { "pcts": ["10%", "23.4%", "(92.3%)"], "flt": ["123", "1.2k", "(12.3k)"], "ts": ["2012-01-01", "2013-01-01", "2014-01-01"], } pt.assert_frame_equal(pd.DataFrame(expected_bycell), bycell(f)) # ensure nothing different pt.assert_frame_equal(pd.DataFrame(expected_bycell).T, bycell(f.T))
def test_dynamic_formatter(self): kwargs = dict(precision=1, commas=1, parens=1, pcts=1, trunc_dot_zeros=1) byrow = fmt.new_dynamic_formatter('row', **kwargs) bycol = fmt.new_dynamic_formatter('col', **kwargs) bycell = fmt.new_dynamic_formatter('cell', **kwargs) todt = pd.to_datetime f = pd.DataFrame( dict(pcts=[.1, .2343, -.9234], flt=[123., 1234., -12345.], ts=[todt('1/1/2012'), todt('1/1/2013'), todt('1/1/2014')])) # by column expected_bycol = { 'pcts': ['10%', '23.4%', '(92.3%)'], 'flt': ['123', '1,234', '(12,345)'], 'ts': ['2012-01-01', '2013-01-01', '2014-01-01'] } pt.assert_frame_equal(pd.DataFrame(expected_bycol), bycol(f)) pt.assert_frame_equal(pd.DataFrame(expected_bycol).T, byrow(f.T)) expected_bycell = { 'pcts': ['10%', '23.4%', '(92.3%)'], 'flt': ['123', '1.2k', '(12.3k)'], 'ts': ['2012-01-01', '2013-01-01', '2014-01-01'] } pt.assert_frame_equal(pd.DataFrame(expected_bycell), bycell(f)) # ensure nothing different pt.assert_frame_equal(pd.DataFrame(expected_bycell).T, bycell(f.T))
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 _repr_html_(self): from tia.util.fmt import new_dynamic_formatter fmt = new_dynamic_formatter(method='row', precision=2, pcts=1, trunc_dot_zeros=1, parens=1) return fmt(self.series.to_frame())._repr_html_()