Exemplo n.º 1
0
    async def __set_starting_simulated_portfolio(self):
        # load new portfolio from config settings
        portfolio_amount_dict = self.config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO]

        try:
            await self.handle_balance_update(Portfolio.get_portfolio_from_amount_dict(portfolio_amount_dict))
        except Exception as e:
            self.logger.warning(f"Error when loading trading history, will reset history. ({e})")
            self.logger.exception(e)
            self.trader.get_previous_state_manager.reset_trading_history()
            await self.handle_balance_update(Portfolio.get_portfolio_from_amount_dict(
                self.config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO]))
    async def __reset_portfolio(self):
        if self.exchange_manager.is_margin:
            self.portfolio = MarginPortfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        else:
            self.portfolio = Portfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        await self.__load_portfolio()

        self.portfolio_profitability = PortfolioProfitabilty(
            self.config, self.trader, self, self.exchange_manager)
        self.reference_market = self.portfolio_profitability.reference_market
    async def _reset_portfolio(self):
        """
        Reset the portfolio and portfolio profitability instances
        """
        if self.exchange_manager.is_margin:
            self.portfolio = MarginPortfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        else:
            self.portfolio = Portfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        await self.portfolio.initialize()
        self._load_portfolio()

        self.portfolio_profitability = PortfolioProfitabilty(
            self.config, self.trader, self, self.exchange_manager)
        self.reference_market = self.portfolio_profitability.reference_market
class PortfolioManager(Initializable):
    def __init__(self, config, trader, exchange_manager):
        super().__init__()
        self.logger = get_logger(self.__class__.__name__)
        self.config, self.trader, self.exchange_manager = config, trader, exchange_manager

        self.portfolio = None
        self.portfolio_profitability = None
        self.reference_market = None

    async def initialize_impl(self):
        await self.__reset_portfolio()

    async def __reset_portfolio(self):
        if self.exchange_manager.is_margin:
            self.portfolio = MarginPortfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        else:
            self.portfolio = Portfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        await self.__load_portfolio()

        self.portfolio_profitability = PortfolioProfitabilty(
            self.config, self.trader, self, self.exchange_manager)
        self.reference_market = self.portfolio_profitability.reference_market

    async def handle_balance_update(self, balance) -> bool:
        if self.trader.is_enabled:
            return await self.portfolio.update_portfolio_from_balance(balance)
        return False

    async def handle_balance_update_from_order(self, order):
        if self.trader.is_enabled and self.trader.simulate:
            await self.portfolio.update_portfolio_from_order(order)
            return True
        return False

    # Load simulated portfolio from config if required
    async def __load_portfolio(self):
        if self.trader.is_enabled:
            if self.trader.simulate:
                await self.__set_starting_simulated_portfolio()
            self.logger.info(
                f"{CURRENT_PORTFOLIO_STRING} {self.portfolio.portfolio}")

    async def __set_starting_simulated_portfolio(self):
        # load new portfolio from config settings
        portfolio_amount_dict = self.config[CONFIG_SIMULATOR][
            CONFIG_STARTING_PORTFOLIO]

        try:
            await self.handle_balance_update(
                self.portfolio.get_portfolio_from_amount_dict(
                    portfolio_amount_dict))
        except Exception as e:
            self.logger.exception(
                e, True,
                f"Error when loading trading history, will reset history. ({e})"
            )
            self.trader.get_previous_state_manager.reset_trading_history()
            await self.handle_balance_update(
                self.portfolio.get_portfolio_from_amount_dict(
                    self.config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO]))
class PortfolioManager(Initializable):
    """
    Manage the portfolio and portfolio profitability instances
    """
    def __init__(self, config, trader, exchange_manager):
        super().__init__()
        self.logger = get_logger(self.__class__.__name__)
        self.config, self.trader, self.exchange_manager = config, trader, exchange_manager

        self.portfolio = None
        self.portfolio_profitability = None
        self.reference_market = None

    async def initialize_impl(self):
        """
        Reset the portfolio instance
        """
        await self._reset_portfolio()

    def handle_balance_update(self, balance):
        """
        Handle a balance update request
        :param balance: the new balance
        :return: True if the portfolio was updated
        """
        if self.trader.is_enabled and balance is not None:
            return self.portfolio.update_portfolio_from_balance(balance)
        return False

    def handle_balance_update_from_order(self, order):
        """
        Handle a balance update from an order request
        :param order: the order
        :return: True if the portfolio was updated
        """
        if self.trader.is_enabled and self.trader.simulate:
            self.portfolio.update_portfolio_from_order(order)
            return True
        return False

    async def _reset_portfolio(self):
        """
        Reset the portfolio and portfolio profitability instances
        """
        if self.exchange_manager.is_margin:
            self.portfolio = MarginPortfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        else:
            self.portfolio = Portfolio(
                self.exchange_manager.get_exchange_name(),
                self.trader.simulate)
        await self.portfolio.initialize()
        self._load_portfolio()

        self.portfolio_profitability = PortfolioProfitabilty(
            self.config, self.trader, self, self.exchange_manager)
        self.reference_market = self.portfolio_profitability.reference_market

    def _load_portfolio(self):
        """
        Load simulated portfolio from config if required
        """
        if self.trader.is_enabled:
            if self.trader.simulate:
                self._set_starting_simulated_portfolio()
            self.logger.info(
                f"{CURRENT_PORTFOLIO_STRING} {self.portfolio.portfolio}")

    def _set_starting_simulated_portfolio(self):
        """
        Load new portfolio from config settings
        """
        portfolio_amount_dict = self.config[CONFIG_SIMULATOR][
            CONFIG_STARTING_PORTFOLIO]

        try:
            self.handle_balance_update(
                self.portfolio.get_portfolio_from_amount_dict(
                    portfolio_amount_dict))
        except Exception as e:
            self.logger.exception(
                e, True,
                f"Error when loading trading history, will reset history. ({e})"
            )
            self.handle_balance_update(
                self.portfolio.get_portfolio_from_amount_dict(
                    self.config[CONFIG_SIMULATOR][CONFIG_STARTING_PORTFOLIO]))