Skip to content

Commit

Permalink
Merge e745f96 into 85fd3c6
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhou-JiaJun committed Feb 24, 2023
2 parents 85fd3c6 + e745f96 commit 8fc1898
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
12 changes: 11 additions & 1 deletion rqalpha/mod/rqalpha_mod_sys_accounts/__init__.py
Expand Up @@ -32,7 +32,9 @@
# 融资利率/年
"financing_rate": 0.00,
# 是否开启融资可买入股票的限制
"financing_stocks_restriction_enabled": False
"financing_stocks_restriction_enabled": False,
# 逐日盯市结算价: settlement/close
"futures_settlement_price_type": "close",
}


Expand Down Expand Up @@ -79,3 +81,11 @@ def load_mod():
help="[sys_accounts] enable stock shorting"
)
)

cli.commands['run'].params.append(
click.Option(
('--futures-settlement-price-type', cli_prefix + 'futures_settlement_price_type'),
default="close",
help="[sys_accounts] future settlement price"
)
)
4 changes: 4 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_accounts/mod.py
Expand Up @@ -50,6 +50,10 @@ def start_up(self, env, mod_config):
elif mod_config.financing_rate < 0:
raise ValueError("sys_accounts financing_rate must >= 0")

futures_settlement_price_types = ["settlement", "close"]
if mod_config.futures_settlement_price_type not in futures_settlement_price_types:
raise ValueError("sys_accounts futures_settlement_price_type in {}".format(futures_settlement_price_types))

if DEFAULT_ACCOUNT_TYPE.FUTURE in env.config.base.accounts:
# 注入期货API
# noinspection PyUnresolvedReferences
Expand Down
27 changes: 25 additions & 2 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 @@ -295,9 +307,20 @@ def settlement(self, trading_date):
order_book_id=self._order_book_id
))
self._quantity = self._old_quantity = 0

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.trading_dt)

self._avg_price = self.last_price
return delta_cash

def before_trading(self, trading_date):
# type: (date) -> float
delta = super().before_trading(trading_date)
self._last_price = None
return delta


class StockPositionProxy(PositionProxy):
__repr_properties__ = (
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 8fc1898

Please sign in to comment.