From e3f9f9412a43a7ebc88014ff0cbc4ab45d43987c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E5=98=89=E4=BF=8A?= <654181984@qq.com> Date: Tue, 11 Oct 2022 16:26:45 +0800 Subject: [PATCH] fix and add test --- rqalpha/mod/rqalpha_mod_sys_analyser/plot/utils.py | 3 +-- rqalpha/portfolio/__init__.py | 2 +- rqalpha/portfolio/account.py | 3 +-- tests/api_tests/test_api_base.py | 6 ++++++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/rqalpha/mod/rqalpha_mod_sys_analyser/plot/utils.py b/rqalpha/mod/rqalpha_mod_sys_analyser/plot/utils.py index 4d70d659a..47c376960 100644 --- a/rqalpha/mod/rqalpha_mod_sys_analyser/plot/utils.py +++ b/rqalpha/mod/rqalpha_mod_sys_analyser/plot/utils.py @@ -74,8 +74,7 @@ def max_ddd(arr: array, index: DatetimeIndex) -> IndexRange: def weekly_returns(portfolio: DataFrame) -> Series: - return portfolio.unit_net_value.reset_index().resample( - "W", on="date").last().set_index("date").unit_net_value.dropna() - 1 + return portfolio.unit_net_value.resample("W").last().dropna() - 1 def trading_dates_index(trades: DataFrame, position_effect, index: DatetimeIndex): diff --git a/rqalpha/portfolio/__init__.py b/rqalpha/portfolio/__init__.py index 8f85e6f21..18deab5a8 100644 --- a/rqalpha/portfolio/__init__.py +++ b/rqalpha/portfolio/__init__.py @@ -279,7 +279,7 @@ def deposit_withdraw(self, account_type, amount, receiving_days=0): unit_net_value = self.unit_net_value self._accounts[account_type].deposit_withdraw(amount, receiving_days) _units = self.total_value / unit_net_value - user_log.info(_("Cash add {}. units {} become to {}".format(amount, self._units ,_units))) + user_log.info(_("Cash add {}. units {} become to {}".format(amount, self._units, _units))) self._units = _units def finance_repay(self, amount, account_type): diff --git a/rqalpha/portfolio/account.py b/rqalpha/portfolio/account.py index 9be4fd6e2..df7ace100 100644 --- a/rqalpha/portfolio/account.py +++ b/rqalpha/portfolio/account.py @@ -19,7 +19,6 @@ from datetime import date from typing import Callable, Dict, Iterable, List, Optional, Union, Tuple -import rqdatac import six from rqalpha.const import POSITION_DIRECTION, POSITION_EFFECT, DEFAULT_ACCOUNT_TYPE, DAYS_CNT from rqalpha.environment import Environment @@ -490,7 +489,7 @@ def deposit_withdraw(self, amount: float, receiving_days: int = 0): if (amount < 0) and (self.cash < amount * -1): raise ValueError(_('insufficient cash, current {}, target withdrawal {}').format(self._total_cash, amount)) if receiving_days >= 1: - receiving_date = rqdatac.get_next_trading_date(self._env.trading_dt.date(), n=receiving_days) + receiving_date = self._env.data_proxy.get_next_trading_date(self._env.trading_dt.date(), n=receiving_days) self._pending_deposit_withdraw.append((receiving_date, amount)) self._pending_deposit_withdraw.sort(key=lambda i: i[0]) else: diff --git a/tests/api_tests/test_api_base.py b/tests/api_tests/test_api_base.py index c53f2d6eb..eaf83be4f 100644 --- a/tests/api_tests/test_api_base.py +++ b/tests/api_tests/test_api_base.py @@ -482,4 +482,10 @@ def handle_bar(context, bar_dict): else: assert False, "未报出当前账户可取出金额不足异常" + deposit("STOCK", 10000, 3) + context.cash = context.portfolio.accounts["STOCK"].cash + + elif context.counter == 9: + assert int(context.portfolio.accounts["STOCK"].cash) == int(context.cash) + 10000 + return locals()