def get_portfolio(self): future_account, static_value = self._cache.account start_date = self._env.config.base.start_date future_starting_cash = self._env.config.base.future_starting_cash return Portfolio(start_date, static_value / future_starting_cash, future_starting_cash, {ACCOUNT_TYPE.FUTURE: future_account})
def _init_portfolio(self): accounts = {} config = self._env.config start_date = config.base.start_date total_cash = 0 for account_type, stock_starting_cash in six.iteritems( config.base.accounts): if account_type == FUTU_ACCOUNT_TYPE.FUTU_STOCK.name: # stock_starting_cash = config.base.accounts if stock_starting_cash == 0: raise RuntimeError( _(u"stock starting cash can not be 0, using `--stock-starting-cash 1000`" )) all_positons = self._get_futu_positions(self._env) if all_positons is None: raise RuntimeError("_init_portfolio fail") StockAccount = self._env.get_account_model( FUTU_ACCOUNT_TYPE.FUTU_STOCK.name) accounts[FUTU_ACCOUNT_TYPE.FUTU_STOCK.name] = StockAccount( stock_starting_cash, all_positons) total_cash += stock_starting_cash else: raise NotImplementedError return Portfolio(start_date, 1, total_cash, accounts)
def init_portfolio(env): accounts = {} config = env.config start_date = datetime.datetime.combine(config.base.start_date, datetime.time.min) units = 0 if config.base.init_positions or (DEFAULT_ACCOUNT_TYPE.FUTURE.name in config.base.accounts and config.base.frequency != '1d'): BaseAccount.AGGRESSIVE_UPDATE_LAST_PRICE = True for account_type, starting_cash in six.iteritems(config.base.accounts): if starting_cash == 0: raise RuntimeError( _(u"{} starting cash can not be 0, using `--account {} 100000`" ).format(account_type, account_type)) account_model = env.get_account_model(account_type) position_model = env.get_position_model(account_type) positions = Positions(position_model) if account_type in config.base.init_positions: for order_book_id, quantity in config.base.init_positions[ account_type]: instrument = env.get_instrument(order_book_id) if instrument is None: raise RuntimeError( _(u'invalid order book id {} in initial positions'). format(order_book_id)) if not instrument.listing: raise RuntimeError( _(u'instrument {} in initial positions is not listing' ).format(order_book_id)) bars = env.data_proxy.history_bars( order_book_id, 1, '1d', 'close', env.data_proxy.get_previous_trading_date(start_date), adjust_type='none') if bars is None: raise RuntimeError( _(u'the close price of {} in initial positions is not available' ).format(order_book_id)) price = bars[0] trade = _fake_trade(order_book_id, quantity, price) if order_book_id not in positions: positions[order_book_id] = position_model(order_book_id) positions[order_book_id].apply_trade(trade) # FIXME positions[order_book_id]._last_price = price account = account_model(starting_cash, positions) units += account.total_value accounts[account_type] = account return Portfolio(config.base.start_date, 1, units, accounts)
def get_portfolio(self): future_account, static_value = self._data_factory.make_account_before_init( ) start_date = self._env.config.base.start_date future_starting_cash = self._env.config.base.future_starting_cash return Portfolio(start_date, static_value / future_starting_cash, future_starting_cash, {ACCOUNT_TYPE.FUTURE: future_account})
def init_portfolio(env): accounts = {} config = env.config start_date = datetime.datetime.combine(config.base.start_date, datetime.time.min) units = 0 for account_type, starting_cash in six.iteritems(config.base.accounts): if starting_cash == 0: raise RuntimeError( _(u"{} starting cash can not be 0, using `--account {} 100000`" ).format(account_type, account_type)) account_model = env.get_account_model(account_type) position_model = env.get_position_model(account_type) positions = Positions(position_model) for order_book_id, quantity in _filter_positions(env, account_type): instrument = env.get_instrument(order_book_id) if instrument is None: raise RuntimeError( _(u'invalid order book id {} in initial positions').format( order_book_id)) if not instrument.listing: raise RuntimeError( _(u'instrument {} in initial positions is not listing'). format(order_book_id)) bars = env.data_proxy.history_bars( order_book_id, 1, '1d', 'close', env.data_proxy.get_previous_trading_date(start_date), adjust_type='none') if bars is None: raise RuntimeError( _(u'the close price of {} in initial positions is not available' ).format(order_book_id)) price = bars[0] trade = _fake_trade(order_book_id, quantity, price) if order_book_id not in positions: positions[order_book_id] = position_model(order_book_id) positions[order_book_id].apply_trade(trade) # FIXME positions[order_book_id]._last_price = price # 变成昨仓 for order_book_id, position in positions.items(): position.apply_settlement() account = account_model(starting_cash, positions) units += account.total_value accounts[account_type] = account return Portfolio(config.base.start_date, 1, units, accounts)
def get_portfolio(self): FuturePosition = self._env.get_position_model( DEFAULT_ACCOUNT_TYPE.FUTURE.name) FutureAccount = self._env.get_account_model( DEFAULT_ACCOUNT_TYPE.FUTURE.name) self._cache.set_models(FutureAccount, FuturePosition) future_account, static_value = self._cache.account start_date = self._env.config.base.start_date future_starting_cash = self._env.config.base.future_starting_cash accounts = {DEFAULT_ACCOUNT_TYPE.FUTURE.name: future_account} return Portfolio(start_date, static_value / future_starting_cash, future_starting_cash, accounts)
def create_benchmark_portfolio(env): if env.config.base.benchmark is None: return None BenchmarkAccount = env.get_account_model(const.DEFAULT_ACCOUNT_TYPE.BENCHMARK.name) BenchmarkPosition = env.get_position_model(const.DEFAULT_ACCOUNT_TYPE.BENCHMARK.name) start_date = env.config.base.start_date total_cash = sum(env.config.base.accounts.values()) accounts = { const.DEFAULT_ACCOUNT_TYPE.BENCHMARK.name: BenchmarkAccount(total_cash, Positions(BenchmarkPosition)) } return Portfolio(start_date, 1, total_cash, accounts)
def _create_portfolio(self): config = self._env.config accounts = {} print(self._env.config.base.accounts) total_cash = 0 StockAccount = self._env.get_account_model( DEFAULT_ACCOUNT_TYPE.STOCK.name) positions = self._get_broker_positions() accounts[DEFAULT_ACCOUNT_TYPE.STOCK.name] = StockAccount( total_cash, positions) return Portfolio(self._env.config.base.start_date, 1, total_cash, accounts)
def _init_portfolio(self): config = self._env.config accounts = {} total_cash = 100000 # config.base.stock_starting_cash StockAccount = self._env.get_account_model( DEFAULT_ACCOUNT_TYPE.STOCK.name) positions = self._get_positions(self._env) accounts[DEFAULT_ACCOUNT_TYPE.STOCK.name] = StockAccount( total_cash, positions) return Portfolio(self._env.config.base.start_date, 1, total_cash, accounts)
def init_portfolio(env): accounts = {} config = env.config start_date = config.base.start_date total_cash = 0 for account_type in config.base.account_list: if account_type == ACCOUNT_TYPE.STOCK: stock_starting_cash = config.base.stock_starting_cash accounts[ACCOUNT_TYPE.STOCK] = StockAccount(stock_starting_cash, Positions(StockPosition)) total_cash += stock_starting_cash elif account_type == ACCOUNT_TYPE.FUTURE: future_starting_cash = config.base.future_starting_cash accounts[ACCOUNT_TYPE.FUTURE] = FutureAccount(future_starting_cash, Positions(FuturePosition)) total_cash += future_starting_cash else: raise NotImplementedError return Portfolio(start_date, 1, total_cash, accounts)
def init_portfolio(env): accounts = {} config = env.config start_date = config.base.start_date total_cash = 0 if DEFAULT_ACCOUNT_TYPE.FUTURE.name in config.base.accounts and config.base.frequency != '1d': BaseAccount.AGGRESSIVE_UPDATE_LAST_PRICE = True for account_type, starting_cash in six.iteritems(config.base.accounts): if account_type == DEFAULT_ACCOUNT_TYPE.STOCK.name: if starting_cash == 0: raise RuntimeError( _(u"stock starting cash can not be 0, using `--account stock 100000`" )) StockAccount = env.get_account_model( DEFAULT_ACCOUNT_TYPE.STOCK.name) StockPosition = env.get_position_model( DEFAULT_ACCOUNT_TYPE.STOCK.name) accounts[DEFAULT_ACCOUNT_TYPE.STOCK.name] = StockAccount( starting_cash, Positions(StockPosition)) total_cash += starting_cash elif account_type == DEFAULT_ACCOUNT_TYPE.FUTURE.name: if starting_cash == 0: raise RuntimeError( _(u"future starting cash can not be 0, using `--account future 100000`" )) FutureAccount = env.get_account_model( DEFAULT_ACCOUNT_TYPE.FUTURE.name) FuturePosition = env.get_position_model( DEFAULT_ACCOUNT_TYPE.FUTURE.name) accounts[DEFAULT_ACCOUNT_TYPE.FUTURE.name] = FutureAccount( starting_cash, Positions(FuturePosition)) total_cash += starting_cash else: raise NotImplementedError return Portfolio(start_date, 1, total_cash, accounts)
def get_portfolio(self): future_account = self._data_factory.make_account_before_init() start_date = self._env.config.base.start_date return Portfolio(start_date, 1, future_account._total_cash, {ACCOUNT_TYPE.FUTURE: future_account})