Skip to content

Commit

Permalink
fix and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed Feb 24, 2023
1 parent 9a1d319 commit e745f96
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
19 changes: 15 additions & 4 deletions rqalpha/mod/rqalpha_mod_sys_accounts/position_model.py
Expand Up @@ -19,13 +19,12 @@

from decimal import Decimal

from rqalpha.utils.functools import lru_cache
from rqalpha.model.trade import Trade
from rqalpha.const import POSITION_DIRECTION, SIDE, POSITION_EFFECT, DEFAULT_ACCOUNT_TYPE, INSTRUMENT_TYPE
from rqalpha.environment import Environment
from rqalpha.portfolio.position import Position, PositionProxy
from rqalpha.data.data_proxy import DataProxy
from rqalpha.utils import INST_TYPE_IN_STOCK_ACCOUNT
from rqalpha.utils import INST_TYPE_IN_STOCK_ACCOUNT, is_valid_price
from rqalpha.utils.logger import user_system_log
from rqalpha.utils.class_helper import deprecated_property, cached_property
from rqalpha.utils.i18n import gettext as _
Expand Down Expand Up @@ -281,6 +280,19 @@ def apply_trade(self, trade):
trade.last_price - self._avg_price
) * trade.last_quantity * self.contract_multiplier * self._direction_factor

@property
def prev_close(self):
if not is_valid_price(self._prev_close):
if self._env.config.mod.sys_accounts.futures_settlement_price_type == "settlement":
self._prev_close = self._env.data_proxy.get_prev_settlement(self._order_book_id, self._env.trading_dt)
else:
self._prev_close = super().prev_close
if not is_valid_price(self._prev_close):
raise RuntimeError(_("invalid price of {order_book_id}: {price}").format(
order_book_id=self._order_book_id, price=self._prev_close
))
return self._prev_close

def settlement(self, trading_date):
# type: (date) -> float
super(FuturePosition, self).settlement(trading_date)
Expand All @@ -298,8 +310,7 @@ def settlement(self, trading_date):

if self._env.config.mod.sys_accounts.futures_settlement_price_type == "settlement":
# 逐日盯市按照结算价结算
self._last_price = self._env.data_proxy.get_settle_price(self._order_book_id, self._env.calendar_dt)
self._prev_close = self._env.data_proxy.get_prev_settlement(self._order_book_id, self._env.calendar_dt)
self._last_price = self._env.data_proxy.get_settle_price(self._order_book_id, self._env.trading_dt)

self._avg_price = self.last_price
return delta_cash
Expand Down
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# 版权所有 2019 深圳米筐科技有限公司(下称“米筐科技”)
#
# 除非遵守当前许可,否则不得使用本软件。
#
# * 非商业用途(非商业用途指个人出于非商业目的使用本软件,或者高校、研究所等非营利机构出于教育、科研等目的使用本软件):
# 遵守 Apache License 2.0(下称“Apache 2.0 许可”),您可以在以下位置获得 Apache 2.0 许可的副本:http://www.apache.org/licenses/LICENSE-2.0。
# 除非法律有要求或以书面形式达成协议,否则本软件分发时需保持当前许可“原样”不变,且不得附加任何条件。
#
# * 商业用途(商业用途指个人出于任何商业目的使用本软件,或者法人或其他组织出于任何目的使用本软件):
# 未经米筐科技授权,任何个人不得出于任何商业目的使用本软件(包括但不限于向第三方提供、销售、出租、出借、转让本软件、本软件的衍生产品、引用或借鉴了本软件功能或源代码的产品或服务),任何法人或其他组织不得出于任何目的使用本软件,否则米筐科技有权追究相应的知识产权侵权责任。
# 在此前提下,对本软件的使用同样需要遵守 Apache 2.0 许可,Apache 2.0 许可与本许可冲突之处,以本许可为准。
# 详细的授权流程,请联系 public@ricequant.com 获取。

from rqalpha.apis import *

__config__ = {
"base": {
"start_date": "2016-01-01",
"end_date": "2016-01-31",
"frequency": "1d",
"accounts": {
"future": 1000000,
}
},
"mod": {
"sys_accounts": {
"futures_settlement_price_type": "settlement"
}
}
}


def test_futures_settlement_price_type():

def init(context):
context.fixed = True
context.symbol = "IC1603"
context.total = 0

def handle_bar(context, bar_dict):
if context.fixed:
buy_open(context.symbol, 1)
context.fixed = False
context.total += 1

def after_trading(context):
pos = get_position(context.symbol)
# close - prev_settlement
if context.total == 2:
assert pos.position_pnl == (6364.6 - 6657.0) * 200
elif context.total == 3:
assert pos.position_pnl == (6468 - 6351.2) * 200
return locals()

0 comments on commit e745f96

Please sign in to comment.