def __init__(self, trading_mode, trader, sub_portfolio_percent): AbstractTradingModeCreator.__init__(self, trading_mode) Initializable.__init__(self) self.trader = trader self.parent_portfolio = self.trader.get_portfolio() self.sub_portfolio = SubPortfolio(self.trading_mode.config, self.trader, self.parent_portfolio, sub_portfolio_percent)
def __init__(self, trading_mode, trader, sub_portfolio_percent): super().__init__(trading_mode) self.trader = trader self.parent_portfolio = self.trader.get_portfolio() self.sub_portfolio = SubPortfolio(self.trading_mode.config, self.trader, self.parent_portfolio, sub_portfolio_percent)
class AbstractTradingModeCreatorWithBot(AbstractTradingModeCreator): __metaclass__ = ABCMeta def __init__(self, trading_mode, trader, sub_portfolio_percent): super().__init__(trading_mode) self.trader = trader self.parent_portfolio = self.trader.get_portfolio() self.sub_portfolio = SubPortfolio(self.trading_mode.config, self.trader, self.parent_portfolio, sub_portfolio_percent) @abstractmethod def create_new_order(self, eval_note, symbol, exchange, trader, portfolio, state): raise NotImplementedError("create_new_order not implemented") def get_trader(self): return self.trader def get_parent_portfolio(self): return self.parent_portfolio def get_sub_portfolio(self): return self.sub_portfolio # Can be overwritten def can_create_order(self, symbol, exchange, state, portfolio): return super().can_create_order(symbol, exchange, state, self.get_portfolio()) # force portfolio update def get_portfolio(self, force_update=False): if force_update: self.sub_portfolio.update_from_parent() return self.sub_portfolio
def init_default(): config = load_test_config() exchange_manager = ExchangeManager(config, ccxt.binance, is_simulated=True) exchange_inst = exchange_manager.get_exchange() trader_inst = TraderSimulator(config, exchange_inst, 1) portfolio_inst = Portfolio(config, trader_inst) trader_inst.stop_order_manager() sub_portfolio_inst = SubPortfolio(config, trader_inst, portfolio_inst, TestSubPortfolio.DEFAULT_PERCENT) return config, portfolio_inst, exchange_inst, trader_inst, sub_portfolio_inst
async def test_get_currency_multiple_sub_portfolio(self): config, portfolio_inst, _, trader_inst, _ = await self.init_default() sub_portfolio_inst_2 = SubPortfolio(config, trader_inst, portfolio_inst, 0.2) await sub_portfolio_inst_2.initialize() sub_portfolio_inst_3 = SubPortfolio(config, trader_inst, portfolio_inst, 0.1) await sub_portfolio_inst_3.initialize() sub_portfolio_inst_4 = SubPortfolio(config, trader_inst, portfolio_inst, 0.7) await sub_portfolio_inst_4.initialize() assert sub_portfolio_inst_2.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 * 0.2 assert sub_portfolio_inst_2.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * 0.2 assert sub_portfolio_inst_2.get_currency_portfolio("NANO", Portfolio.TOTAL) == 0 assert sub_portfolio_inst_3.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 * 0.1 assert sub_portfolio_inst_3.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * 0.1 assert sub_portfolio_inst_3.get_currency_portfolio("NANO", Portfolio.TOTAL) == 0 assert sub_portfolio_inst_4.get_currency_portfolio("BTC", Portfolio.AVAILABLE) == 10 * 0.7 assert sub_portfolio_inst_4.get_currency_portfolio("BTC", Portfolio.TOTAL) == 10 * 0.7 assert sub_portfolio_inst_4.get_currency_portfolio("NANO", Portfolio.TOTAL) == 0
def test_get_currency_multiple_sub_portfolio(self): config, portfolio_inst, exchange_inst, trader_inst, sub_portfolio_inst = self.init_default( ) sub_portfolio_inst_2 = SubPortfolio(config, trader_inst, portfolio_inst, 0.2) sub_portfolio_inst_3 = SubPortfolio(config, trader_inst, portfolio_inst, 0.1) sub_portfolio_inst_4 = SubPortfolio(config, trader_inst, portfolio_inst, 0.7) assert sub_portfolio_inst_2.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 10 * 0.2 assert sub_portfolio_inst_2.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 10 * 0.2 assert sub_portfolio_inst_2.get_currency_portfolio( "NANO", Portfolio.TOTAL) == 0 assert sub_portfolio_inst_3.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 10 * 0.1 assert sub_portfolio_inst_3.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 10 * 0.1 assert sub_portfolio_inst_3.get_currency_portfolio( "NANO", Portfolio.TOTAL) == 0 assert sub_portfolio_inst_4.get_currency_portfolio( "BTC", Portfolio.AVAILABLE) == 10 * 0.7 assert sub_portfolio_inst_4.get_currency_portfolio( "BTC", Portfolio.TOTAL) == 10 * 0.7 assert sub_portfolio_inst_4.get_currency_portfolio( "NANO", Portfolio.TOTAL) == 0