# Path: OnePy/constants.py
# class ActionType(Enum):
# 
#     Buy = 'Buy'
#     Sell = 'Sell'
#     Short = 'Short'
#     Cover = 'Cover'
# 
#     Exit_all = 'Exit_all'
#     Cancel = 'Cancel'
# 
# class OrderType(Enum):
# 
#     Market = 'Market'
#     Limit = 'Limit'
#     Stop = 'Stop'
#     Trailing_stop = 'Trailing_stop'
# 
#     Limit_pct = 'Limit_pct'
#     Stop_pct = 'Stop_pct'
#     Trailing_stop_pct = 'Trailing_stop_pct'
# 
# Path: OnePy/sys_module/metabase_env.py
# class OnePyEnvBase:
#     """作为基类，提供env共享单例给各个模块"""
#     env = Environment
# 
# Path: OnePy/utils/memo_for_cache.py
# def memo(key):
#     cache = Environment.cache
# 
#     def decorate(func):
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             if key not in cache:
#                 cache[key] = func(*args, **kwargs)
# 
#             return cache[key]
# 
#         return wrapper
# 
#     return decorate


import json
import arrow
import numpy as np
import pandas as pd
from collections import defaultdict
from OnePy.constants import ActionType, OrderType
from OnePy.sys_module.metabase_env import OnePyEnvBase
from OnePy.utils.memo_for_cache import memo
        from OnePy.custom_module.trade_log_analysis import APP
                        run_up = -(np.min(price) - log.entry_price) / \
                            log.entry_price

                else:
                    returns_diff = (
                        (log.exit_price - log.entry_price) / log.entry_price)
                    # diff of price
                    drawdown = -(np.min(data_low) - log.entry_price) / \
                        log.entry_price
                    run_up = (np.max(data_high) - log.entry_price) / \
                        log.entry_price

                    if 'Trailing_stop' in log.exit_type:
                        price = log.sell.signal.execute_price + log.sell.signal.parent_order.difference
                        run_up = (np.max(price) - log.entry_price) / \
                            log.entry_price

                if drawdown <= 0:
                    drawdown = 0

            log_dict['drawdown'].append(drawdown)
            log_dict['run_up'].append(run_up)
            log_dict['returns_diff'].append(returns_diff)

        return pd.DataFrame(log_dict)

    def trade_analysis(self, full_trade_log: pd.DataFrame=None):
        if full_trade_log:
            OnePyEnvBase.full_trade_log = full_trade_log
        else:

# Path: OnePy/constants.py
# class ActionType(Enum):
# 
#     Buy = 'Buy'
#     Sell = 'Sell'
#     Short = 'Short'
#     Cover = 'Cover'
# 
#     Exit_all = 'Exit_all'
#     Cancel = 'Cancel'
# 
# class OrderType(Enum):
# 
#     Market = 'Market'
#     Limit = 'Limit'
#     Stop = 'Stop'
#     Trailing_stop = 'Trailing_stop'
# 
#     Limit_pct = 'Limit_pct'
#     Stop_pct = 'Stop_pct'
#     Trailing_stop_pct = 'Trailing_stop_pct'
# 
# Path: OnePy/sys_module/metabase_env.py
# class OnePyEnvBase:
#     """作为基类，提供env共享单例给各个模块"""
#     env = Environment
# 
# Path: OnePy/utils/memo_for_cache.py
# def memo(key):
#     cache = Environment.cache
# 
#     def decorate(func):
#         @wraps(func)
#         def wrapper(*args, **kwargs):
#             if key not in cache:
#                 cache[key] = func(*args, **kwargs)
# 
#             return cache[key]
# 
#         return wrapper
# 
#     return decorate


# Path: OnePy/custom_module/analysis.py
import json
import arrow
import numpy as np
import pandas as pd
from collections import defaultdict
from OnePy.constants import ActionType, OrderType
from OnePy.sys_module.metabase_env import OnePyEnvBase
from OnePy.utils.memo_for_cache import memo
        from OnePy.custom_module.trade_log_analysis import APP

                        run_up = -(np.min(price) - log.entry_price) / \
                            log.entry_price

                else:
                    returns_diff = (
                        (log.exit_price - log.entry_price) / log.entry_price)
                    # diff of price
                    drawdown = -(np.min(data_low) - log.entry_price) / \
                        log.entry_price
                    run_up = (np.max(data_high) - log.entry_price) / \
                        log.entry_price

                    if 'Trailing_stop' in log.exit_type:
                        price = log.sell.signal.execute_price + log.sell.signal.parent_order.difference
                        run_up = (np.max(price) - log.entry_price) / \
                            log.entry_price

                if drawdown <= 0:
                    drawdown = 0

            log_dict['drawdown'].append(drawdown)
            log_dict['run_up'].append(run_up)
            log_dict['returns_diff'].append(returns_diff)

        return pd.DataFrame(log_dict)

    def trade_analysis(self, full_trade_log: pd.DataFrame=None):
        if full_trade_log:
            OnePyEnvBase.full_trade_log = full_trade_log
        else:

            OnePyEnvBase.full_trade_log = self.get_full_trade_log()