Skip to content

Commit

Permalink
修复position.last_price为nan时的异常;增加3.11的测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed Mar 23, 2023
1 parent 2721fcb commit 14fe9e1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]
steps:
# Checks out a copy of your repository on the ubuntu-latest machine
- uses: actions/checkout@v2
Expand Down
4 changes: 3 additions & 1 deletion rqalpha/data/bar_dict_price_board.py
Expand Up @@ -17,7 +17,7 @@
from rqalpha.interface import AbstractPriceBoard
from rqalpha.environment import Environment
from rqalpha.core.execution_context import ExecutionContext
from rqalpha.const import EXECUTION_PHASE
from rqalpha.const import EXECUTION_PHASE, POSITION_DIRECTION


class BarDictPriceBoard(AbstractPriceBoard):
Expand All @@ -30,6 +30,8 @@ def _get_bar(self, order_book_id):
return self._env.get_bar(order_book_id)

def get_last_price(self, order_book_id):
if ExecutionContext.phase() == EXECUTION_PHASE.BEFORE_TRADING:
return self._env.portfolio.get_position(order_book_id, POSITION_DIRECTION.LONG).prev_close
return self._get_bar(order_book_id).last

def get_limit_up(self, order_book_id):
Expand Down
4 changes: 1 addition & 3 deletions rqalpha/data/data_proxy.py
Expand Up @@ -130,9 +130,7 @@ def get_prev_close(self, order_book_id, dt):

@lru_cache(10240)
def _get_prev_settlement(self, instrument, dt):
prev_trading_date = self.get_previous_trading_date(dt)
bar = self._data_source.history_bars(instrument, 1, '1d', 'settlement', prev_trading_date,
skip_suspended=False, adjust_orig=dt)
bar = self._data_source.history_bars(instrument, 1, '1d', 'prev_settlement', dt, skip_suspended=False)
if bar is None or len(bar) == 0:
return np.nan
return bar[0]
Expand Down
13 changes: 6 additions & 7 deletions rqalpha/portfolio/position.py
Expand Up @@ -119,12 +119,15 @@ def avg_price(self):
def trading_pnl(self):
# type: () -> float
trade_quantity = self._quantity - self._logical_old_quantity
return (trade_quantity * self.last_price - self._trade_cost) * self._direction_factor
return (trade_quantity * self.last_price - self._trade_cost) * self._direction_factor if trade_quantity else 0

@property
def position_pnl(self):
# type: () -> float
return self._logical_old_quantity * (self.last_price - self.prev_close) * self._direction_factor
if self._logical_old_quantity:
return self._logical_old_quantity * (self.last_price - self.prev_close) * self._direction_factor
else:
return 0

@property
def pnl(self):
Expand Down Expand Up @@ -152,12 +155,8 @@ def prev_close(self):

@property
def last_price(self):
if not self._last_price:
if not is_valid_price(self._last_price):
self._last_price = self._env.data_proxy.get_last_price(self._order_book_id)
if not is_valid_price(self._last_price):
raise RuntimeError(_("invalid price of {order_book_id}: {price}").format(
order_book_id=self._order_book_id, price=self._last_price
))
return self._last_price

@property
Expand Down

0 comments on commit 14fe9e1

Please sign in to comment.