class InMarket(Criteria):
    """
    Criteria used to define if the strategy currently holds a position
    in the market with a specified symbol.
    """
    def __init__(self, symbol):
        Criteria.__init__(self)
        self.symbol = str(symbol)
        self.label = 'InMarket_%s' %symbol
        self.num_bars_required = 1
        self.logger.info('Initialized - %s' %self)
    def __str__(self):
        return 'InMarket(symbol=%s)' %self.symbol
    def __repr__(self):
        return self.label
    def apply(self, data_frame):
        """
        Apply the InMarket criteria to the data_frame provided.
        @return Series(bool) The criteria status
        """
        try:
            return data_frame['STATUS_%s' %self.symbol][-1] != 0
        except KeyError:
            return False class IsLong(Criteria):
    """
    Criteria used to define if the strategy currently holds a Long position
    in the market with a specified symbol.
    """
    def __init__(self, symbol):
        Criteria.__init__(self)
        self.symbol = str(symbol)
        self.label = 'IsLong_%s' %symbol
        self.num_bars_required = 1
        self.logger.info('Initialized - %s' %self)
    def __str__(self):
        return 'IsLong(symbol=%s)' %self.symbol
    def __repr__(self):
        return self.label
    def apply(self, data_frame):
        """
        Apply the IsLong criteria to the data_frame provided.
        @return Series(bool) The criteria status
        """
        try:
            return data_frame['STATUS_%s' %self.symbol][-1] > 0
        except KeyError:
            return False class IsShort(Criteria):
    """
    Criteria used to define if the strategy currently holds a Short position
    in the market with a specified symbol.
    """
    def __init__(self, symbol):
        Criteria.__init__(self)
        self.symbol = str(symbol)
        self.label = 'IsShort_%s' %symbol
        self.num_bars_required = 1
        self.logger.info('Initialized - %s' %self)
    def __str__(self):
        return 'IsShort(symbol=%s)' %self.symbol
    def __repr__(self):
        return self.label
    def apply(self, data_frame):
        """
        Apply the IsShort criteria to the data_frame provided.
        @return Series(bool) The criteria status
        """
        try:
            return data_frame['STATUS_%s' %self.symbol][-1] < 0
        except KeyError:
            return False class Not(Criteria):
    """
    Returns the inverses of another criteria's value.
    """
    def __init__(self, criteria):
        Criteria.__init__(self)
        self.criteria = criteria
        self.label = 'Not_%s' %criteria
        self.num_bars_required = criteria.num_bars_required
        self.logger.info('Initialized - %s' %self)
    def __str__(self):
        return 'Not(criteria=%s)' %self.criteria
    def __repr__(self):
        return self.label
    def apply(self, data_frame):
        """
        Apply the Not criteria to the data_frame provided.
        @return Series(bool) The criteria status
        """
        if not self.criteria.apply(data_frame):
            return True
        return False class Long(Action):
    """
    Long Action represents going long in the market.
    """
    def __init__(self):
        Action.__init__(self, 'long')
    def raw(self):
        """
        A raw (int) representation of the action.
        """
        return LONG class LongExit(Action):
    """
    Long Exit Action represents exiting a long position from the market.
    """
    def __init__(self):
        Action.__init__(self, 'longexit')
    def __eq__(self, other):
        other = str(other).replace(' ', '').replace('_', '').replace('-', '').lower()
        return other == 'longexit' or other == 'exitlong'
    def raw(self):
        """
        A raw (int) representation of the action.
        """
        return LONG_EXIT class Short(Action):
    """
    Short Action represents shorting in the market.
    """
    def __init__(self):
        Action.__init__(self, 'short')
    def raw(self):
        """
        A raw (int) representation of the action.
        """
        return SHORT class ShortExit(Action):
    """
    Short Exit Action represents exiting a short position from the market.
    """
    def __init__(self):
        Action.__init__(self, 'shortexit')
    def __eq__(self, other):
        other = str(other).replace(' ', '').replace('_', '').replace('-', '').lower()
        return other == 'shortexit' or other == 'exitshort'
    def raw(self):
        """
        A raw (int) representation of the action.
        """
        return SHORT_EXIT LONG = 1 LONG_EXIT = -1 SHORT = 2 SHORT_EXIT = -2 NO_ACTION = 0 CRITICAL = logging.CRITICAL
ERROR = logging.ERROR
WARNING = logging.WARNING
INFO = logging.INFO
DEBUG = logging.DEBUG
class Logger(object):
    def __init__(self, name, log_file=configuration.LOG_FILE, \
                 console_lvl=configuration.LOGGING_DEFAULT_CONSOLE, \
                 file_lvl=configuration.LOGGING_DEFAULT_FILE):
    def get_logger_level(self, name, default, out):
    def get_console_level(self, name, default):
    def get_file_level(self, name, default):
    def set_file_level(self, lvl):
    def set_console_level(self, lvl):
    def debug(self, message):
    def info(self, message):
    def warning(self, message):
    def error(self, message):
    def critical(self, message):
    def exception(self, message):
    def __getstate__(self):
    def __setstate__(self, data):
from nowtrade.criteria import InMarket, IsLong, IsShort, Not
from nowtrade.action import Long, LongExit, Short, ShortExit, \
                            LONG, LONG_EXIT, SHORT, SHORT_EXIT, NO_ACTION
from nowtrade import logger
"""
Criteria group module associates various criteria with corresponding actions.
"""

class InvalidAction(Exception):
    """
    Exception raised when an invalid action is submitted to a criteria group.
    """
    pass

class CriteriaGroup(object):
    """
    For now, since we haven't implemented adding to positions,
    the default is InMarket, NotInMarket, IsLong, IsShort gets
    added automatically to criteria groups as needed.
    """
    def __init__(self, criteria_list, action, symbol):
        self.criteria_list = criteria_list
        self.action = action
        self._action = 0
        self.symbol = str(symbol)
        # Add Actions Criteria Automatically
        if self.action == Long():
            self.criteria_list.append(Not(InMarket(self.symbol)))
            self._action = LONG
        elif self.action == LongExit():
            self.criteria_list.append(IsLong(self.symbol))
            self._action = LONG_EXIT

        elif self.action == Short():