# Path: alephnull/finance/slippage.py
# class VolumeShareSlippage(SlippageModel):
#     def __init__(self,
#                  volume_limit=.25,
#                  price_impact=0.1):
# 
#         self.volume_limit = volume_limit
#         self.price_impact = price_impact
# 
#     def __repr__(self):
#         return """
# {class_name}(
#     volume_limit={volume_limit},
#     price_impact={price_impact})
# """.strip().format(class_name=self.__class__.__name__,
#                    volume_limit=self.volume_limit,
#                    price_impact=self.price_impact)
# 
#     def process_order(self, event, order):
# 
#         max_volume = self.volume_limit * event.volume
# 
#         # price impact accounts for the total volume of transactions
#         # created against the current minute bar
#         remaining_volume = max_volume - self.volume_for_bar
#         if remaining_volume < 1:
#             # we can't fill any more transactions
#             return
# 
#         # the current order amount will be the min of the
#         # volume available in the bar or the open amount.
#         cur_volume = int(min(remaining_volume, abs(order.open_amount)))
# 
#         if cur_volume < 1:
#             return
# 
#         # tally the current amount into our total amount ordered.
#         # total amount will be used to calculate price impact
#         total_volume = self.volume_for_bar + cur_volume
# 
#         volume_share = min(total_volume / event.volume,
#                            self.volume_limit)
# 
#         simulated_impact = (volume_share) ** 2 \
#                            * math.copysign(self.price_impact, order.direction) \
#                            * event.price
# 
#         return create_transaction(
#             event,
#             order,
#             # In the future, we may want to change the next line
#             # for limit pricing
#             event.price + simulated_impact,
#             math.copysign(cur_volume, order.direction)
#         )
# 
# def transact_partial(slippage, commission):
#     return partial(transact_stub, slippage, commission)
# 
# def check_order_triggers(order, event):
#     """
#     Given an order and a trade event, return a tuple of
#     (stop_reached, limit_reached).
#     For market orders, will return (False, False).
#     For stop orders, limit_reached will always be False.
#     For limit orders, stop_reached will always be False.
# 
#     Orders that have been triggered already (price targets reached),
#     the order's current values are returned.
#     """
#     if order.triggered:
#         return (order.stop_reached, order.limit_reached)
# 
#     stop_reached = False
#     limit_reached = False
#     # if the stop price is reached, simply set stop_reached
#     if order.stop is not None:
#         if (order.direction * (event.price - order.stop) <= 0):
#             # convert stop -> limit or market
#             stop_reached = True
# 
#     # if the limit price is reached, we execute this order at
#     # (event.price + simulated_impact)
#     # we skip this order with a continue when the limit is not reached
#     if order.limit is not None:
#         # if limit conditions not met, then continue
#         if (order.direction * (event.price - order.limit) <= 0):
#             limit_reached = True
# 
#     return (stop_reached, limit_reached)


import math
import uuid
import numpy as np
import alephnull.errors
import alephnull.protocol as zp
import alephnull.utils.math_utils as zp_math
from copy import copy
from collections import defaultdict
from logbook import Logger
from alephnull.finance.slippage import (
    VolumeShareSlippage,
    transact_partial,
    check_order_triggers
    )
from alephnull.finance.commission import PerShare
from alephnull.utils.protocol_utils import Enum
        self.filled = filled
        self.status = ORDER_STATUS.OPEN
        self.stop = stop
        self.limit = limit
        self.stop_reached = False
        self.limit_reached = False
        self.direction = math.copysign(1, self.amount)
        self.type = zp.DATASOURCE_TYPE.ORDER


    def make_id(self):
        return uuid.uuid4().hex

    def to_dict(self):
        py = copy(self.__dict__)
        for field in ['type', 'direction']:
            del py[field]
        return py

    def to_api_obj(self):
        pydict = self.to_dict()
        obj = zp.Order(initial_values=pydict)
        return obj

    def check_triggers(self, event):
        """
        Update internal state based on price triggers and the
        trade event's price.
        """
        stop_reached, limit_reached = \

# Path: alephnull/finance/slippage.py
# class VolumeShareSlippage(SlippageModel):
#     def __init__(self,
#                  volume_limit=.25,
#                  price_impact=0.1):
# 
#         self.volume_limit = volume_limit
#         self.price_impact = price_impact
# 
#     def __repr__(self):
#         return """
# {class_name}(
#     volume_limit={volume_limit},
#     price_impact={price_impact})
# """.strip().format(class_name=self.__class__.__name__,
#                    volume_limit=self.volume_limit,
#                    price_impact=self.price_impact)
# 
#     def process_order(self, event, order):
# 
#         max_volume = self.volume_limit * event.volume
# 
#         # price impact accounts for the total volume of transactions
#         # created against the current minute bar
#         remaining_volume = max_volume - self.volume_for_bar
#         if remaining_volume < 1:
#             # we can't fill any more transactions
#             return
# 
#         # the current order amount will be the min of the
#         # volume available in the bar or the open amount.
#         cur_volume = int(min(remaining_volume, abs(order.open_amount)))
# 
#         if cur_volume < 1:
#             return
# 
#         # tally the current amount into our total amount ordered.
#         # total amount will be used to calculate price impact
#         total_volume = self.volume_for_bar + cur_volume
# 
#         volume_share = min(total_volume / event.volume,
#                            self.volume_limit)
# 
#         simulated_impact = (volume_share) ** 2 \
#                            * math.copysign(self.price_impact, order.direction) \
#                            * event.price
# 
#         return create_transaction(
#             event,
#             order,
#             # In the future, we may want to change the next line
#             # for limit pricing
#             event.price + simulated_impact,
#             math.copysign(cur_volume, order.direction)
#         )
# 
# def transact_partial(slippage, commission):
#     return partial(transact_stub, slippage, commission)
# 
# def check_order_triggers(order, event):
#     """
#     Given an order and a trade event, return a tuple of
#     (stop_reached, limit_reached).
#     For market orders, will return (False, False).
#     For stop orders, limit_reached will always be False.
#     For limit orders, stop_reached will always be False.
# 
#     Orders that have been triggered already (price targets reached),
#     the order's current values are returned.
#     """
#     if order.triggered:
#         return (order.stop_reached, order.limit_reached)
# 
#     stop_reached = False
#     limit_reached = False
#     # if the stop price is reached, simply set stop_reached
#     if order.stop is not None:
#         if (order.direction * (event.price - order.stop) <= 0):
#             # convert stop -> limit or market
#             stop_reached = True
# 
#     # if the limit price is reached, we execute this order at
#     # (event.price + simulated_impact)
#     # we skip this order with a continue when the limit is not reached
#     if order.limit is not None:
#         # if limit conditions not met, then continue
#         if (order.direction * (event.price - order.limit) <= 0):
#             limit_reached = True
# 
#     return (stop_reached, limit_reached)


# Path: alephnull/finance/blotter.py
import math
import uuid
import numpy as np
import alephnull.errors
import alephnull.protocol as zp
import alephnull.utils.math_utils as zp_math
from copy import copy
from collections import defaultdict
from logbook import Logger
from alephnull.finance.slippage import (
    VolumeShareSlippage,
    transact_partial,
    check_order_triggers
    )
from alephnull.finance.commission import PerShare
from alephnull.utils.protocol_utils import Enum

        self.filled = filled
        self.status = ORDER_STATUS.OPEN
        self.stop = stop
        self.limit = limit
        self.stop_reached = False
        self.limit_reached = False
        self.direction = math.copysign(1, self.amount)
        self.type = zp.DATASOURCE_TYPE.ORDER


    def make_id(self):
        return uuid.uuid4().hex

    def to_dict(self):
        py = copy(self.__dict__)
        for field in ['type', 'direction']:
            del py[field]
        return py

    def to_api_obj(self):
        pydict = self.to_dict()
        obj = zp.Order(initial_values=pydict)
        return obj

    def check_triggers(self, event):
        """
        Update internal state based on price triggers and the
        trade event's price.
        """
        stop_reached, limit_reached = \

            check_order_triggers(self, event)