""" 部分代码 """
def __init__(self, main_engine: MainEngine, event_engine: EventEngine):
""""""
super().__init__(main_engine, event_engine, APP_NAME)
""" 部分代码 """
self.read_write_jsonfile_lock = threading.Lock()
def process_account_event(self, event: Event) -> None:
"""
Dump balance、risk_percent data of account
"""
account = event.data
self.accounts[account.vt_accountid] = account
tqz_vnpy_path = TQZFilePathOperator.grandfather_path(source_path=TQZFilePathOperator.current_file_grandfather_path(file=__file__))
accounts_data_path = tqz_vnpy_path + "/.vntrader/accounts_data/accounts_data.json"
self.read_write_jsonfile_lock.acquire()
try:
account_list = TQZJsonOperator.tqz_load_jsonfile(jsonfile=accounts_data_path)
for account in self.accounts.values():
risk_percent = 0
if account.balance is not 0:
risk_percent = (account.balance - account.available) / account.balance * 100
account_list[account.accountid] = {
"account_id": account.accountid,
"balance": round(account.balance, 2),
"risk_percent": round(risk_percent, 2)
}
TQZJsonOperator.tqz_write_jsonfile(content=account_list, target_jsonfile=accounts_data_path)
except Exception as result:
pass
finally:
self.read_write_jsonfile_lock.release()