Skip to content

Commit

Permalink
Merge 8351796 into 22af07e
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed Sep 2, 2022
2 parents 22af07e + 8351796 commit 2bc02b0
Show file tree
Hide file tree
Showing 13 changed files with 13 additions and 17 deletions.
20 changes: 9 additions & 11 deletions rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py
Expand Up @@ -27,7 +27,7 @@
from rqalpha.data.data_proxy import DataProxy
from rqalpha.utils import INST_TYPE_IN_STOCK_ACCOUNT
from rqalpha.utils.logger import user_system_log
from rqalpha.utils.class_helper import deprecated_property
from rqalpha.utils.class_helper import deprecated_property, cached_property
from rqalpha.utils.i18n import gettext as _


Expand Down Expand Up @@ -101,7 +101,7 @@ def before_trading(self, trading_date):
return delta_cash
if self.direction != POSITION_DIRECTION.LONG:
raise RuntimeError("direction of stock position {} is not supposed to be short".format(self._order_book_id))
data_proxy = Environment.get_instance().data_proxy
data_proxy = self._env.data_proxy
self._handle_dividend_book_closure(trading_date, data_proxy)
delta_cash += self._handle_dividend_payable(trading_date)
self._handle_split(trading_date, data_proxy)
Expand Down Expand Up @@ -152,8 +152,7 @@ def settlement(self, trading_date):
self._quantity = self._old_quantity = 0
return delta_cash

@property
@lru_cache()
@cached_property
def _market_tplus(self):
return self._instrument.market_tplus

Expand All @@ -167,6 +166,8 @@ def _handle_dividend_book_closure(self, trading_date, data_proxy):
if dividend_per_share != dividend_per_share:
raise RuntimeError("Dividend per share of {} is not supposed to be nan.".format(self._order_book_id))
self._avg_price -= dividend_per_share
# 前一天结算发生了除息, 此时 last_price 还是前一个交易日的收盘价,需要改为 除息后收盘价, 否则影响在before_trading中查看盈亏
self._last_price -= dividend_per_share

try:
payable_date = _int_to_date(dividend["payable_date"][0])
Expand Down Expand Up @@ -203,7 +204,6 @@ def _handle_split(self, trading_date, data_proxy):
return
self._avg_price /= ratio
self._last_price /= ratio
self._prev_close /= ratio
ratio = Decimal(ratio)
# int(6000 * 1.15) -> 6899
self._old_quantity = self._quantity = int(Decimal(self._quantity) * ratio)
Expand All @@ -220,15 +220,13 @@ class FuturePosition(Position):
old_quantity = property(lambda self: self._old_quantity)
today_quantity = property(lambda self: self._quantity - self._old_quantity)

@property
@lru_cache()
@cached_property
def contract_multiplier(self):
return self._instrument.contract_multiplier

@property
@lru_cache()
@cached_property
def margin_rate(self):
return self._instrument.margin_rate * Environment.get_instance().config.base.margin_multiplier
return self._instrument.margin_rate * self._env.config.base.margin_multiplier

@property
def equity(self):
Expand Down Expand Up @@ -288,7 +286,7 @@ def settlement(self, trading_date):
super(FuturePosition, self).settlement(trading_date)
if self._quantity == 0:
return 0
data_proxy = Environment.get_instance().data_proxy
data_proxy = self._env.data_proxy
instrument = data_proxy.instrument(self._order_book_id)
next_date = data_proxy.get_next_trading_date(trading_date)
delta_cash = self.equity
Expand Down
10 changes: 4 additions & 6 deletions rqalpha/portfolio/position.py
Expand Up @@ -147,15 +147,13 @@ def equity(self):
@property
def prev_close(self):
if not is_valid_price(self._prev_close):
env = Environment.get_instance()
self._prev_close = env.data_proxy.get_prev_close(self._order_book_id, env.trading_dt)
self._prev_close = self._env.data_proxy.get_prev_close(self._order_book_id, self._env.trading_dt)
return self._prev_close

@property
def last_price(self):
if not self._last_price:
env = Environment.get_instance()
self._last_price = env.data_proxy.get_last_price(self._order_book_id)
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
Expand Down Expand Up @@ -212,8 +210,8 @@ def before_trading(self, trading_date):
self._old_quantity = self._quantity
self._logical_old_quantity = self._old_quantity
self._trade_cost = self._non_closable = 0
self._prev_close = self.last_price
self._transaction_cost = 0
self._prev_close = None
return 0

def apply_trade(self, trade):
Expand Down Expand Up @@ -254,7 +252,7 @@ def calc_close_today_amount(self, trade_amount):
@property
def _open_orders(self):
# type: () -> Iterable[Order]
for order in Environment.get_instance().broker.get_open_orders(self.order_book_id):
for order in self._env.broker.get_open_orders(self.order_book_id):
if order.position_direction == self._direction:
yield order

Expand Down
Binary file modified tests/outs/test_f_buy_and_hold.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_macd.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_macd_signal.pkl
Binary file not shown.
Binary file modified tests/outs/test_f_mean_reverting.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_buy_and_hold.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_dual_thrust.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_scheduler.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_tick_size.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle.pkl
Binary file not shown.
Binary file modified tests/outs/test_s_turtle_signal.pkl
Binary file not shown.
Binary file modified tests/outs/test_sf_buy_and_hold.pkl
Binary file not shown.

0 comments on commit 2bc02b0

Please sign in to comment.