# Path: OnePy/constants.py
# class ActionType(Enum):
# 
#     Buy = 'Buy'
#     Sell = 'Sell'
#     Short = 'Short'
#     Cover = 'Cover'
# 
#     Exit_all = 'Exit_all'
#     Cancel = 'Cancel'
# 
# class OrderStatus(Enum):
# 
#     Created = "Created"
#     Submitted = "Submitted"
#     Partial = "Partial"
#     Completed = "Completed"
#     Canceled = "Canceled"
#     Expired = "Expired"
#     Margin = "Margin"
#     Rejected = "Rejected"
#     Triggered = "Triggered"
# 
# Path: OnePy/sys_module/components/signal_generator.py
# class TriggeredSignalGenerator(OnePyEnvBase):
#     """为触发的挂单生成挂单触发信号"""
#     @classmethod
#     def _generate_bare_signal(cls, order) -> SignalByTrigger:
#         kwargs = {'action_type': order.action_type,
#                   'strategy_name': order.strategy_name,
#                   'size': order.size,
#                   'ticker': order.ticker,
#                   'execute_price': order.target_price,
#                   'first_cur_price': order.first_cur_price,
#                   'mkt_id': order.mkt_id,
#                   'order_type': order.order_type,
#                   'parent_order': order}
# 
#         return SignalByTrigger(**kwargs)
# 
#     @classmethod
#     def _generate_full_signal(cls, order) -> SignalByTrigger:
#         kwargs = {'action_type': order.action_type,
#                   'strategy_name': order.strategy_name,
#                   'size': order.size,
#                   'ticker': order.ticker,
#                   'execute_price': order.target_price,
#                   'price': None,
#                   'price_pct': None,
#                   'takeprofit': order.signal.takeprofit,
#                   'takeprofit_pct': order.signal.takeprofit_pct,
#                   'stoploss': order.signal.stoploss,
#                   'stoploss_pct': order.signal.stoploss_pct,
#                   'trailingstop': order.signal.trailingstop,
#                   'trailingstop_pct': order.signal.trailingstop_pct,
#                   'order_type': order.order_type}
# 
#         return SignalByTrigger(**kwargs)
# 
#     @classmethod
#     def generate_triggered_signal(cls, order: PendingOrderBase) -> Optional[Signal]:
#         if order.ticker not in cls.env.cur_suspended_tickers:
# 
#             if order.is_triggered:
#                 if order.is_with_mkt():
#                     return cls._generate_bare_signal(order)
# 
#                 return cls._generate_full_signal(order)
# 
# Path: OnePy/sys_module/metabase_env.py
# class OnePyEnvBase:
#     """作为基类，提供env共享单例给各个模块"""
#     env = Environment


from collections import defaultdict
from typing import Optional
from OnePy.constants import ActionType, OrderStatus
from OnePy.sys_module.components.signal_generator import \
    TriggeredSignalGenerator
from OnePy.sys_module.metabase_env import OnePyEnvBase
                    if self._is_partial(order, cur_position, acumu_position):
                        self._make_position_cumu_full(order)
                    else:
                        order.status = OrderStatus.Rejected
                        self._delete_from_position_cumu(order)

                        continue
            self.order_pass_checker(order)

    def _check_absolute(self, order_list):
        for order in order_list:
            if order.action_type in [ActionType.Sell, ActionType.Cover]:
                self._add_to_position_cumu(order)
            self.order_pass_checker(order)

    def _check_market_order(self):
        self.cash_acumu = 0
        self.plong_acumu = defaultdict(int)
        self.pshort_acumu = defaultdict(int)

        self._check_absolute(self.env.orders_mkt_absolute_cur)
        self._check_normal(self.env.orders_mkt_normal_cur)

    def _check_pending_order(self):  # TODO: 实盘需要检查
        pass

    def _check_cancel_order(self):
        cancel_orders = self.env.orders_cancel_cur

        for order in cancel_orders:

# Path: OnePy/constants.py
# class ActionType(Enum):
# 
#     Buy = 'Buy'
#     Sell = 'Sell'
#     Short = 'Short'
#     Cover = 'Cover'
# 
#     Exit_all = 'Exit_all'
#     Cancel = 'Cancel'
# 
# class OrderStatus(Enum):
# 
#     Created = "Created"
#     Submitted = "Submitted"
#     Partial = "Partial"
#     Completed = "Completed"
#     Canceled = "Canceled"
#     Expired = "Expired"
#     Margin = "Margin"
#     Rejected = "Rejected"
#     Triggered = "Triggered"
# 
# Path: OnePy/sys_module/components/signal_generator.py
# class TriggeredSignalGenerator(OnePyEnvBase):
#     """为触发的挂单生成挂单触发信号"""
#     @classmethod
#     def _generate_bare_signal(cls, order) -> SignalByTrigger:
#         kwargs = {'action_type': order.action_type,
#                   'strategy_name': order.strategy_name,
#                   'size': order.size,
#                   'ticker': order.ticker,
#                   'execute_price': order.target_price,
#                   'first_cur_price': order.first_cur_price,
#                   'mkt_id': order.mkt_id,
#                   'order_type': order.order_type,
#                   'parent_order': order}
# 
#         return SignalByTrigger(**kwargs)
# 
#     @classmethod
#     def _generate_full_signal(cls, order) -> SignalByTrigger:
#         kwargs = {'action_type': order.action_type,
#                   'strategy_name': order.strategy_name,
#                   'size': order.size,
#                   'ticker': order.ticker,
#                   'execute_price': order.target_price,
#                   'price': None,
#                   'price_pct': None,
#                   'takeprofit': order.signal.takeprofit,
#                   'takeprofit_pct': order.signal.takeprofit_pct,
#                   'stoploss': order.signal.stoploss,
#                   'stoploss_pct': order.signal.stoploss_pct,
#                   'trailingstop': order.signal.trailingstop,
#                   'trailingstop_pct': order.signal.trailingstop_pct,
#                   'order_type': order.order_type}
# 
#         return SignalByTrigger(**kwargs)
# 
#     @classmethod
#     def generate_triggered_signal(cls, order: PendingOrderBase) -> Optional[Signal]:
#         if order.ticker not in cls.env.cur_suspended_tickers:
# 
#             if order.is_triggered:
#                 if order.is_with_mkt():
#                     return cls._generate_bare_signal(order)
# 
#                 return cls._generate_full_signal(order)
# 
# Path: OnePy/sys_module/metabase_env.py
# class OnePyEnvBase:
#     """作为基类，提供env共享单例给各个模块"""
#     env = Environment


# Path: OnePy/sys_module/components/order_checker.py
from collections import defaultdict
from typing import Optional
from OnePy.constants import ActionType, OrderStatus
from OnePy.sys_module.components.signal_generator import \
    TriggeredSignalGenerator
from OnePy.sys_module.metabase_env import OnePyEnvBase

                    if self._is_partial(order, cur_position, acumu_position):
                        self._make_position_cumu_full(order)
                    else:
                        order.status = OrderStatus.Rejected
                        self._delete_from_position_cumu(order)

                        continue
            self.order_pass_checker(order)

    def _check_absolute(self, order_list):
        for order in order_list:
            if order.action_type in [ActionType.Sell, ActionType.Cover]:
                self._add_to_position_cumu(order)
            self.order_pass_checker(order)

    def _check_market_order(self):
        self.cash_acumu = 0
        self.plong_acumu = defaultdict(int)
        self.pshort_acumu = defaultdict(int)

        self._check_absolute(self.env.orders_mkt_absolute_cur)
        self._check_normal(self.env.orders_mkt_normal_cur)

    def _check_pending_order(self):  # TODO: 实盘需要检查
        pass

    def _check_cancel_order(self):
        cancel_orders = self.env.orders_cancel_cur

        for order in cancel_orders:

            order.status = OrderStatus.Submitted