# Path: tia/analysis/model/txn.py
# class Intent(object):
#     Open = 1
#     Close = 2
#     Increase = 3
#     Decrease = 4
# 
#     Labels = {
#         Open: 'Open',
#         Close: 'Close',
#         Increase: 'Increase',
#         Decrease: 'Decrease',
#     }
# 
# class Action(object):
#     Buy = 1
#     Sell = 2
#     SellShort = 3
#     Cover = 4
# 
#     Labels = {
#         Buy: 'Buy',
#         Sell: 'Sell',
#         SellShort: 'SellShort',
#         Cover: 'Cover',
#     }
# 
# Path: tia/analysis/model/interface.py
# class TxnPlColumns(object):
#     DT = 'date'
#     PID = TxnColumns.PID
#     TID = TxnColumns.TID
#     POS = 'pos'
#     TXN_QTY = 'txn_qty'
#     TXN_PX = 'txn_px'
#     TXN_FEES = 'txn_fees'
#     TXN_PREMIUM = 'txn_premium'
#     TXN_INTENT = 'txn_intent'
#     TXN_ACTION = 'txn_action'
#     CLOSE_PX = 'close'
#     OPEN_VAL = 'open_val'
#     MKT_VAL = 'mkt_val'
#     TOT_VAL = 'total_val'
#     DVDS = 'dvds'
#     FEES = 'fees'
#     RPL_GROSS = 'rpl_gross'
#     RPL = 'rpl'
#     UPL = 'upl'
#     PL = 'pl'
# 
#     DESCRIPTIONS = {
#         DT: 'p/l date',
#         POS: 'end of day position quantity',
#         CLOSE_PX: 'end of day closing price',
#         OPEN_VAL: 'open value of the position',
#         MKT_VAL: 'market value',
#         TOT_VAL: 'total of trade premiums',
#         DVDS: 'dividends',
#         FEES: 'fees',
#         RPL_GROSS: 'realized gross p/l (TOT_VAL - OPEN_VAL)',
#         RPL: 'realized pl (RPL_GROSS + FEES + DVDS)',
#         UPL: 'unrealized pl (MKT_VAL + OPEN_VAL)',
#         PL: 'Total p/l (UPL + RPL)'
#     }
# 
# class PositionColumns(object):
#     PID = 'pid'
#     SIDE = 'side'
#     OPEN_DT = 'open_dt'
#     CLOSE_DT = 'close_dt'
#     OPEN_QTY = 'open_qty'
#     OPEN_PX = 'open_px'
#     CLOSE_PX = 'close_px'
#     OPEN_PREMIUM = 'open_premium'
#     PL = 'pl'
#     DURATION = 'duration'
#     NUM_TXNS = 'ntxns'
#     RET = 'ret'
#     STATE = 'state'
# 
# Path: tia/util/decorator.py
# def lazy_property(fct, name=None):
#     name = name or fct.__name__
#     attr_name = '_' + name
#     if attr_name == '_<lambda>':
#         raise Exception("cannot assign <lambda> to lazy property")
# 
#     @property
#     @wraps(fct)
#     def _wrapper(self):
#         if not hasattr(self, attr_name):
#             setattr(self, attr_name, fct(self))
#         return getattr(self, attr_name)
# 
#     return _wrapper


from collections import OrderedDict
from tia.analysis.model.txn import Intent, Action
from tia.analysis.model.interface import TxnPlColumns as TPL, PositionColumns as PC
from tia.util.decorator import lazy_property
        from tia.util.mplot import AxesFormat
        from tia.util.mplot import AxesFormat
        from tia.util.fmt import new_dynamic_formatter
import pandas as pd
import numpy as np
        import matplotlib.pyplot as plt
        import matplotlib.pyplot as plt
                ax.scatter(lframe.index, lframe.ret, s=s, c='k', marker='o', label='Long')
                ax.vlines(lframe.index, min_rets[lframe.index], max_rets[frame.index])
            if len(self.short_pids) > 0:
                sframe = frame.ix[frame.index.isin(self.short_pids)]
                s = sframe.duration + 20 if dur else 20
                ax.scatter(sframe.index, sframe.ret, s=s, c='r', marker='o', label='Short')
                ax.vlines(sframe.index, min_rets[sframe.index], max_rets[sframe.index])

        AxesFormat().Y.percent().apply()
        ax.axhline(color='k', linestyle='--')
        ax.set_xlim(0, frame.index.max() + 3)
        ax.set_xlabel('pid')
        ax.set_ylabel('return')
        ax.legend(loc='upper left')
        return ax


class PositionsStats(object):
    def __init__(self, positions):
        self.positions = positions

    _frame = property(lambda self: self.positions.frame)
    _loser_frame = property(lambda self: self._frame.ix[self._frame[PC.RET] < 0])
    _winner_frame = property(lambda self: self._frame.ix[self._frame[PC.RET] >= 0])

    win_cnt = property(lambda self: len(self._winner_frame.index))
    lose_cnt = property(lambda self: len(self._loser_frame.index))
    cnt = property(lambda self: len(self._frame.index))
    win_pct = property(lambda self: np.divide(float(self.win_cnt), float(self.cnt)))


# Path: tia/analysis/model/txn.py
# class Intent(object):
#     Open = 1
#     Close = 2
#     Increase = 3
#     Decrease = 4
# 
#     Labels = {
#         Open: 'Open',
#         Close: 'Close',
#         Increase: 'Increase',
#         Decrease: 'Decrease',
#     }
# 
# class Action(object):
#     Buy = 1
#     Sell = 2
#     SellShort = 3
#     Cover = 4
# 
#     Labels = {
#         Buy: 'Buy',
#         Sell: 'Sell',
#         SellShort: 'SellShort',
#         Cover: 'Cover',
#     }
# 
# Path: tia/analysis/model/interface.py
# class TxnPlColumns(object):
#     DT = 'date'
#     PID = TxnColumns.PID
#     TID = TxnColumns.TID
#     POS = 'pos'
#     TXN_QTY = 'txn_qty'
#     TXN_PX = 'txn_px'
#     TXN_FEES = 'txn_fees'
#     TXN_PREMIUM = 'txn_premium'
#     TXN_INTENT = 'txn_intent'
#     TXN_ACTION = 'txn_action'
#     CLOSE_PX = 'close'
#     OPEN_VAL = 'open_val'
#     MKT_VAL = 'mkt_val'
#     TOT_VAL = 'total_val'
#     DVDS = 'dvds'
#     FEES = 'fees'
#     RPL_GROSS = 'rpl_gross'
#     RPL = 'rpl'
#     UPL = 'upl'
#     PL = 'pl'
# 
#     DESCRIPTIONS = {
#         DT: 'p/l date',
#         POS: 'end of day position quantity',
#         CLOSE_PX: 'end of day closing price',
#         OPEN_VAL: 'open value of the position',
#         MKT_VAL: 'market value',
#         TOT_VAL: 'total of trade premiums',
#         DVDS: 'dividends',
#         FEES: 'fees',
#         RPL_GROSS: 'realized gross p/l (TOT_VAL - OPEN_VAL)',
#         RPL: 'realized pl (RPL_GROSS + FEES + DVDS)',
#         UPL: 'unrealized pl (MKT_VAL + OPEN_VAL)',
#         PL: 'Total p/l (UPL + RPL)'
#     }
# 
# class PositionColumns(object):
#     PID = 'pid'
#     SIDE = 'side'
#     OPEN_DT = 'open_dt'
#     CLOSE_DT = 'close_dt'
#     OPEN_QTY = 'open_qty'
#     OPEN_PX = 'open_px'
#     CLOSE_PX = 'close_px'
#     OPEN_PREMIUM = 'open_premium'
#     PL = 'pl'
#     DURATION = 'duration'
#     NUM_TXNS = 'ntxns'
#     RET = 'ret'
#     STATE = 'state'
# 
# Path: tia/util/decorator.py
# def lazy_property(fct, name=None):
#     name = name or fct.__name__
#     attr_name = '_' + name
#     if attr_name == '_<lambda>':
#         raise Exception("cannot assign <lambda> to lazy property")
# 
#     @property
#     @wraps(fct)
#     def _wrapper(self):
#         if not hasattr(self, attr_name):
#             setattr(self, attr_name, fct(self))
#         return getattr(self, attr_name)
# 
#     return _wrapper


# Path: tia/analysis/model/pos.py
from collections import OrderedDict
from tia.analysis.model.txn import Intent, Action
from tia.analysis.model.interface import TxnPlColumns as TPL, PositionColumns as PC
from tia.util.decorator import lazy_property
        from tia.util.mplot import AxesFormat
        from tia.util.mplot import AxesFormat
        from tia.util.fmt import new_dynamic_formatter
import pandas as pd
import numpy as np
        import matplotlib.pyplot as plt
        import matplotlib.pyplot as plt

                ax.scatter(lframe.index, lframe.ret, s=s, c='k', marker='o', label='Long')
                ax.vlines(lframe.index, min_rets[lframe.index], max_rets[frame.index])
            if len(self.short_pids) > 0:
                sframe = frame.ix[frame.index.isin(self.short_pids)]
                s = sframe.duration + 20 if dur else 20
                ax.scatter(sframe.index, sframe.ret, s=s, c='r', marker='o', label='Short')
                ax.vlines(sframe.index, min_rets[sframe.index], max_rets[sframe.index])

        AxesFormat().Y.percent().apply()
        ax.axhline(color='k', linestyle='--')
        ax.set_xlim(0, frame.index.max() + 3)
        ax.set_xlabel('pid')
        ax.set_ylabel('return')
        ax.legend(loc='upper left')
        return ax


class PositionsStats(object):
    def __init__(self, positions):
        self.positions = positions

    _frame = property(lambda self: self.positions.frame)
    _loser_frame = property(lambda self: self._frame.ix[self._frame[PC.RET] < 0])
    _winner_frame = property(lambda self: self._frame.ix[self._frame[PC.RET] >= 0])

    win_cnt = property(lambda self: len(self._winner_frame.index))
    lose_cnt = property(lambda self: len(self._loser_frame.index))
    cnt = property(lambda self: len(self._frame.index))
    win_pct = property(lambda self: np.divide(float(self.win_cnt), float(self.cnt)))


    @lazy_property