def find_one(self, account_name): accounts = AccountModel.objects(name=account_name).all() if not accounts or len(accounts) <= 0: return None account_model: AccountModel = accounts[0] if account_model.tp == 'IBAccount': account = IBAccount(account_model.name, account_model.initial_cash) elif account_model.tp == 'BacktestAccount': account = BacktestAccount(account_model.name, account_model.initial_cash) elif account_model.tp == "TDAccount": account = TDAccount(account_model.name, account_model.initial_cash) else: raise RuntimeError("wrong account type") account.cash = account_model.cash account.initial_cash = account_model.initial_cash account.positions = account_model.positions account.history_net_value = { self._to_timestamp(dt): account_model.history_net_value[dt] for dt in account_model.history_net_value } account.orders = [self._to_order(om) for om in account_model.orders] # account.current_operation = self._to_operation(account_model.current_operation) # account.history_operations = [self._to_operation(op) for op in account_model.history_operations] logging.info("账户加载成功, 当前持仓:{}, 现金:{}".format(account.positions, account.cash)) return account
# if isinstance(event_definition.time_rule, MarketOpen): # t = Timestamp("2021-01-21 22:30:00", tz='Asia/Shanghai') # return [Event(event_definition, visible_time=t, data={})] # # elif isinstance(event_definition.time_rule, MarketClose): # if event_definition.time_rule.offset == 0: # t = Timestamp("2021-01-22 05:00:00", tz='Asia/Shanghai') # return [Event(event_definition, visible_time=t, data={})] # elif event_definition.time_rule.offset == 30: # t = Timestamp("2021-01-22 05:30:00", tz='Asia/Shanghai') # return [Event(event_definition, visible_time=t, data={})] # # # mocked_current_prices = { # Timestamp("2021-01-21 22:30:00", tz='Asia/Shanghai'): {"SPCE_STK_USD_SMART": 30}, # Timestamp("2021-01-22 05:00:00", tz='Asia/Shanghai'): {"SPCE_STK_USD_SMART": 31}, # Timestamp("2021-01-22 05:30:00", tz='Asia/Shanghai'): {"SPCE_STK_USD_SMART": 31}, # # } # # engine.run(strategy, acc, is_realtime_test=True, mocked_events_generator=mocked_event_generator, # mocked_current_prices=mocked_current_prices) # 实盘 acc = IBAccount("ib_real1", 10000) acc.with_order_callback(strategy).with_client(config.get('ib', 'host'), config.getint('ib', 'port'), config.getint('ib', 'client_id')) engine.run(strategy, acc)
from trading_calendars import get_calendar from se import config, AccountRepo, BeanContainer from se.domain2.engine.engine import Engine, Scope from se.infras.ib import IBAccount from strategies.strategy import TestStrategy3 engine = Engine() scope = Scope(["GSX_STK_USD_SMART"], trading_calendar=get_calendar("NYSE")) strategy = TestStrategy3(scope) account_name = "ib_real2" repo: AccountRepo = BeanContainer.getBean(AccountRepo) acc = repo.find_one(account_name) if not acc: acc = IBAccount(account_name, 10000) acc.with_order_callback(strategy).with_client( config.get('ib_account', 'host'), config.getint('ib_account', 'port'), config.getint('ib_account', 'client_id')) acc.start_save_thread() acc.start_sync_order_executions_thread() engine.run(strategy, acc)