def update_account_close(cls, event): """ 用bar_close更新总资产 :param event: :return: """ current_timetag = event.event_data_dict["strategy_data"].timetag current_date = millisecond_to_date(current_timetag, format='%Y-%m-%d') data_class = GetData() if Environment.bar_position_data_list: for account in Environment.bar_account_data_list: # 分资金账号update hold_balance = 0 for position_data in Environment.bar_position_data_list: if account.account_id == position_data.account_id: stock_code = position_data.instrument + "." + position_data.exchange current_close_price = data_class.get_market_data( Environment.daily_data, stock_code=[stock_code], field=["close"], start=current_date, end=current_date) hold_balance += position_data.position * current_close_price account.total_balance = account.available + hold_balance
def get_benchmark_net_asset_value(self, event): period = event.event_data_dict["strategy"].period data_class = GetData() benchmark = event.event_data_dict["strategy"].benchmark if period == Period.DAILY.value: start_time = millisecond_to_date(millisecond=Environment.benchmark_index[0], format="%Y-%m-%d") end_time = millisecond_to_date(millisecond=Environment.benchmark_index[-1], format="%Y-%m-%d") benchmark_close = data_class.get_market_data(Environment.daily_data, stock_code=[benchmark], field=["close"], start=start_time, end=end_time) elif period == Period.ONE_MIN.value: start_time = millisecond_to_date(millisecond=Environment.benchmark_index[0], format="%Y-%m-%d %H:%M:%S") end_time = millisecond_to_date(millisecond=Environment.benchmark_index[-1], format="%Y-%m-%d %H:%M:%S") benchmark_close = data_class.get_market_data(Environment.daily_data, stock_code=[benchmark], field=["close"], start=start_time, end=end_time) benchmark_close = list(benchmark_close) benchmark_net_asset_value = [current_close / benchmark_close[0] for current_close in benchmark_close] return benchmark_net_asset_value
def update_position_close(cls, event): """ 更新bar_close持仓盈亏 :param event: :return: """ if Environment.bar_position_data_list: current_timetag = event.event_data_dict["strategy_data"].timetag current_date = millisecond_to_date(current_timetag, format='%Y-%m-%d') data_class = GetData() for position_data in Environment.bar_position_data_list: stock_code = position_data.instrument + "." + position_data.exchange current_close_price = data_class.get_market_data( Environment.daily_data, stock_code=[stock_code], field=["close"], start=current_date, end=current_date) position_data.position_profit = position_data.position * ( current_close_price - position_data.average_price)