# Path: alephnull/protocol.py
# DATASOURCE_TYPE = Enum(
#     'AS_TRADED_EQUITY',
#     'MERGER',
#     'SPLIT',
#     'DIVIDEND',
#     'TRADE',
#     'TRANSACTION',
#     'ORDER',
#     'EMPTY',
#     'DONE',
#     'CUSTOM',
#     'BENCHMARK',
#     'COMMISSION'
# )
# 
# Path: alephnull/gens/utils.py
# def assert_sort_unframe_protocol(event):
#     """Same as above."""
#     assert isinstance(event.source_id, basestring)
#     assert event.type in DATASOURCE_TYPE
# 
# def hash_args(*args, **kwargs):
#     """Define a unique string for any set of representable args."""
#     arg_string = '_'.join([str(arg) for arg in args])
#     kwarg_string = '_'.join([str(key) + '=' + str(value)
#                              for key, value in kwargs.iteritems()])
#     combined = ':'.join([arg_string, kwarg_string])
# 
#     hasher = md5()
#     hasher.update(combined)
#     return hasher.hexdigest()
# 
# Path: alephnull/finance/trading.py
# class TradingEnvironment(object):
# class SimulationParameters(object):
#     def __init__(
#         self,
#         load=None,
#         bm_symbol='^GSPC',
#         exchange_tz="US/Eastern",
#         max_date=None,
#         extra_dates=None
#     ):
#     def __enter__(self, *args, **kwargs):
#     def __exit__(self, exc_type, exc_val, exc_tb):
#     def normalize_date(self, test_date):
#     def utc_dt_in_exchange(self, dt):
#     def exchange_dt_in_utc(self, dt):
#     def is_market_hours(self, test_date):
#     def is_trading_day(self, test_date):
#     def next_trading_day(self, test_date):
#     def days_in_range(self, start, end):
#     def next_open_and_close(self, start_date):
#     def get_open_and_close(self, day):
#     def market_minutes_for_day(self, midnight):
#     def trading_day_distance(self, first_date, second_date):
#     def get_index(self, dt):
#     def __init__(self, period_start, period_end,
#                  capital_base=10e3,
#                  emission_rate='daily',
#                  data_frequency='daily'):
#     def calculate_first_open(self):
#     def calculate_last_close(self):
#     def days_in_period(self):
#     def __repr__(self):


import types
import logbook
from numbers import Integral
from datetime import datetime
from collections import deque
from abc import ABCMeta, abstractmethod
from alephnull.protocol import DATASOURCE_TYPE
from alephnull.gens.utils import assert_sort_unframe_protocol, hash_args
from alephnull.finance import trading
                and event.type not in (
                    DATASOURCE_TYPE.TRADE,
                    DATASOURCE_TYPE.CUSTOM)):
            return

        self.assert_well_formed(event)
        # Add new event and increment totals.
        self.ticks.append(event)

        # Subclasses should override handle_add to define behavior for
        # adding new ticks.
        self.handle_add(event)
        # Clear out any expired events.
        #
        #                              oldest               newest
        #                                |                    |
        #                                V                    V
        while self.drop_condition(self.ticks[0].dt, self.ticks[-1].dt):

            # popleft removes and returns the oldest tick in self.ticks
            popped = self.ticks.popleft()

            # Subclasses should override handle_remove to define
            # behavior for removing ticks.
            self.handle_remove(popped)

    def out_of_market_window(self, oldest, newest):
        oldest_index = \
            trading.environment.trading_days.searchsorted(oldest)
        newest_index = \

# Path: alephnull/protocol.py
# DATASOURCE_TYPE = Enum(
#     'AS_TRADED_EQUITY',
#     'MERGER',
#     'SPLIT',
#     'DIVIDEND',
#     'TRADE',
#     'TRANSACTION',
#     'ORDER',
#     'EMPTY',
#     'DONE',
#     'CUSTOM',
#     'BENCHMARK',
#     'COMMISSION'
# )
# 
# Path: alephnull/gens/utils.py
# def assert_sort_unframe_protocol(event):
#     """Same as above."""
#     assert isinstance(event.source_id, basestring)
#     assert event.type in DATASOURCE_TYPE
# 
# def hash_args(*args, **kwargs):
#     """Define a unique string for any set of representable args."""
#     arg_string = '_'.join([str(arg) for arg in args])
#     kwarg_string = '_'.join([str(key) + '=' + str(value)
#                              for key, value in kwargs.iteritems()])
#     combined = ':'.join([arg_string, kwarg_string])
# 
#     hasher = md5()
#     hasher.update(combined)
#     return hasher.hexdigest()
# 
# Path: alephnull/finance/trading.py
# class TradingEnvironment(object):
# class SimulationParameters(object):
#     def __init__(
#         self,
#         load=None,
#         bm_symbol='^GSPC',
#         exchange_tz="US/Eastern",
#         max_date=None,
#         extra_dates=None
#     ):
#     def __enter__(self, *args, **kwargs):
#     def __exit__(self, exc_type, exc_val, exc_tb):
#     def normalize_date(self, test_date):
#     def utc_dt_in_exchange(self, dt):
#     def exchange_dt_in_utc(self, dt):
#     def is_market_hours(self, test_date):
#     def is_trading_day(self, test_date):
#     def next_trading_day(self, test_date):
#     def days_in_range(self, start, end):
#     def next_open_and_close(self, start_date):
#     def get_open_and_close(self, day):
#     def market_minutes_for_day(self, midnight):
#     def trading_day_distance(self, first_date, second_date):
#     def get_index(self, dt):
#     def __init__(self, period_start, period_end,
#                  capital_base=10e3,
#                  emission_rate='daily',
#                  data_frequency='daily'):
#     def calculate_first_open(self):
#     def calculate_last_close(self):
#     def days_in_period(self):
#     def __repr__(self):


# Path: alephnull/transforms/utils.py
import types
import logbook
from numbers import Integral
from datetime import datetime
from collections import deque
from abc import ABCMeta, abstractmethod
from alephnull.protocol import DATASOURCE_TYPE
from alephnull.gens.utils import assert_sort_unframe_protocol, hash_args
from alephnull.finance import trading

                and event.type not in (
                    DATASOURCE_TYPE.TRADE,
                    DATASOURCE_TYPE.CUSTOM)):
            return

        self.assert_well_formed(event)
        # Add new event and increment totals.
        self.ticks.append(event)

        # Subclasses should override handle_add to define behavior for
        # adding new ticks.
        self.handle_add(event)
        # Clear out any expired events.
        #
        #                              oldest               newest
        #                                |                    |
        #                                V                    V
        while self.drop_condition(self.ticks[0].dt, self.ticks[-1].dt):

            # popleft removes and returns the oldest tick in self.ticks
            popped = self.ticks.popleft()

            # Subclasses should override handle_remove to define
            # behavior for removing ticks.
            self.handle_remove(popped)

    def out_of_market_window(self, oldest, newest):
        oldest_index = \
            trading.environment.trading_days.searchsorted(oldest)
        newest_index = \

            trading.environment.trading_days.searchsorted(newest)