# Path: zaifbot/trade/trade.py
# class Trade:
#     def __init__(self):
#         self.currency_pair = None
#         self.entry_datetime = None
#         self.entry_price = None
#         self.amount = None
#         self.action = None
#         self.exit_price = None
#         self.exit_datetime = None
#         self.id_ = None
#         self.closed = False
#         self._trade_api = BotTradeApi()
#         self._dao = TradesDao()
#         self.strategy_name = None
#         self.process_id = None
# 
#     def entry(self, currency_pair, amount, action):
#         self.currency_pair = CurrencyPair(currency_pair)
#         self.amount = amount
#         self.entry_price = self._rounded_last_price()
#         self.action = Action(action)
#         self.entry_datetime = datetime.now()
# 
#         self._trade_api.trade(currency_pair=self.currency_pair,
#                               amount=self.amount,
#                               price=self.entry_price,
#                               action=self.action)
# 
#         trade_obj = self._dao.create(currency_pair=self.currency_pair.name,
#                                      amount=self.amount,
#                                      entry_price=self.entry_price,
#                                      action=self.action.name,
#                                      strategy_name=self.strategy_name,
#                                      process_id=self.process_id)
# 
#         self.id_ = trade_obj.id_
#         self._logging_entry()
# 
#     def exit(self):
#         self.exit_price = self._rounded_last_price()
#         self.exit_datetime = datetime.now()
# 
#         self._trade_api.trade(currency_pair=self.currency_pair,
#                               amount=self.amount,
#                               price=self.exit_price,
#                               action=self.action.opposite_action())
# 
#         self.closed = True
# 
#         self._dao.update(id_=self.id_,
#                          exit_price=self.exit_price,
#                          exit_datetime=self.exit_datetime,
#                          profit=self.profit(),
#                          closed=self.closed)
# 
#         self._logging_exit()
# 
#     def profit(self):
#         if self.action == Buy:
#             return self.exit_price - self.entry_price
#         else:
#             return self.entry_price - self.exit_price
# 
#     @property
#     def is_short(self):
#         return self.action == Sell
# 
#     @property
#     def is_long(self):
#         return self.action == Buy
# 
#     @property
#     def is_closed(self):
#         return self.closed
# 
#     def _logging_entry(self):
#         log_frame = "Entry: {{trade_id: {}, currency_pair: {}, action: {}," \
#                     " amount: {}, entry_price: {}, entry_datetime: {}}}"
#         log_msg = log_frame.format(self.id_, self.currency_pair, self.action,
#                                    self.amount, self.entry_price, self.entry_datetime)
#         trade_logger.info(log_msg, extra={'strategyid': self._strategy_descriptor()})
# 
#     def _logging_exit(self):
#         log_frame = "Exit: {{trade_id: {}, currency_pair: {}, exit_price: {}, exit_datetime: {}}}"
#         log_msg = log_frame.format(self.id_, self.currency_pair, self.exit_price, self.exit_datetime)
#         trade_logger.info(log_msg, extra={'strategyid': self._strategy_descriptor()})
# 
#     def _strategy_descriptor(self):
#         return self.strategy_name or self.process_id[:12] or ''
# 
#     def _rounded_last_price(self):
#         tick = Tick(self.currency_pair)
#         price = last_price(self.currency_pair)
#         return tick.truncate_price(price)
# 
# Path: zaifbot/logger.py
# def _bot_console_handler():
# def _bot_file_handler(file=None):
# def _bot_logger():
# def _trade_logger():
# def add_logging_directions(webhook_url=None, email={}):


import uuid
import datetime
from collections import OrderedDict
from threading import Event, Condition
from zaifbot.trade.trade import Trade
from zaifbot.logger import trade_logger


class Strategy:
    def __init__(self, entry_rule, exit_rule, stop_rule=None, name=None):
        self.entry_rule = entry_rule
        self.exit_rule = exit_rule
        self.stop_rule = stop_rule
        self.name = name
        self.id_ = _generate_id()
        self.total_profit = 0
        self.total_trades_counts = 0
        self.started = None

        self._status = Status()
        self._have_position = Event()
        self._trade = None
        self._cv = Condition()

    @property
    def have_position(self):
        return self._have_position.is_set()

    @property
    def status(self):
        return self._status.status

    def start(self, *, sec_wait=1):
        self.started = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self._status.to_running()

# Path: zaifbot/trade/trade.py
# class Trade:
#     def __init__(self):
#         self.currency_pair = None
#         self.entry_datetime = None
#         self.entry_price = None
#         self.amount = None
#         self.action = None
#         self.exit_price = None
#         self.exit_datetime = None
#         self.id_ = None
#         self.closed = False
#         self._trade_api = BotTradeApi()
#         self._dao = TradesDao()
#         self.strategy_name = None
#         self.process_id = None
# 
#     def entry(self, currency_pair, amount, action):
#         self.currency_pair = CurrencyPair(currency_pair)
#         self.amount = amount
#         self.entry_price = self._rounded_last_price()
#         self.action = Action(action)
#         self.entry_datetime = datetime.now()
# 
#         self._trade_api.trade(currency_pair=self.currency_pair,
#                               amount=self.amount,
#                               price=self.entry_price,
#                               action=self.action)
# 
#         trade_obj = self._dao.create(currency_pair=self.currency_pair.name,
#                                      amount=self.amount,
#                                      entry_price=self.entry_price,
#                                      action=self.action.name,
#                                      strategy_name=self.strategy_name,
#                                      process_id=self.process_id)
# 
#         self.id_ = trade_obj.id_
#         self._logging_entry()
# 
#     def exit(self):
#         self.exit_price = self._rounded_last_price()
#         self.exit_datetime = datetime.now()
# 
#         self._trade_api.trade(currency_pair=self.currency_pair,
#                               amount=self.amount,
#                               price=self.exit_price,
#                               action=self.action.opposite_action())
# 
#         self.closed = True
# 
#         self._dao.update(id_=self.id_,
#                          exit_price=self.exit_price,
#                          exit_datetime=self.exit_datetime,
#                          profit=self.profit(),
#                          closed=self.closed)
# 
#         self._logging_exit()
# 
#     def profit(self):
#         if self.action == Buy:
#             return self.exit_price - self.entry_price
#         else:
#             return self.entry_price - self.exit_price
# 
#     @property
#     def is_short(self):
#         return self.action == Sell
# 
#     @property
#     def is_long(self):
#         return self.action == Buy
# 
#     @property
#     def is_closed(self):
#         return self.closed
# 
#     def _logging_entry(self):
#         log_frame = "Entry: {{trade_id: {}, currency_pair: {}, action: {}," \
#                     " amount: {}, entry_price: {}, entry_datetime: {}}}"
#         log_msg = log_frame.format(self.id_, self.currency_pair, self.action,
#                                    self.amount, self.entry_price, self.entry_datetime)
#         trade_logger.info(log_msg, extra={'strategyid': self._strategy_descriptor()})
# 
#     def _logging_exit(self):
#         log_frame = "Exit: {{trade_id: {}, currency_pair: {}, exit_price: {}, exit_datetime: {}}}"
#         log_msg = log_frame.format(self.id_, self.currency_pair, self.exit_price, self.exit_datetime)
#         trade_logger.info(log_msg, extra={'strategyid': self._strategy_descriptor()})
# 
#     def _strategy_descriptor(self):
#         return self.strategy_name or self.process_id[:12] or ''
# 
#     def _rounded_last_price(self):
#         tick = Tick(self.currency_pair)
#         price = last_price(self.currency_pair)
#         return tick.truncate_price(price)
# 
# Path: zaifbot/logger.py
# def _bot_console_handler():
# def _bot_file_handler(file=None):
# def _bot_logger():
# def _trade_logger():
# def add_logging_directions(webhook_url=None, email={}):


# Path: zaifbot/trade/strategy.py
import uuid
import datetime
from collections import OrderedDict
from threading import Event, Condition
from zaifbot.trade.trade import Trade
from zaifbot.logger import trade_logger



class Strategy:
    def __init__(self, entry_rule, exit_rule, stop_rule=None, name=None):
        self.entry_rule = entry_rule
        self.exit_rule = exit_rule
        self.stop_rule = stop_rule
        self.name = name
        self.id_ = _generate_id()
        self.total_profit = 0
        self.total_trades_counts = 0
        self.started = None

        self._status = Status()
        self._have_position = Event()
        self._trade = None
        self._cv = Condition()

    @property
    def have_position(self):
        return self._have_position.is_set()

    @property
    def status(self):
        return self._status.status

    def start(self, *, sec_wait=1):
        self.started = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self._status.to_running()

        trade_logger.info('process started',