Skip to content

Commit

Permalink
Merge pull request #874 from ricequant/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Cuizi7 authored Apr 25, 2024
2 parents 9095b0a + cfa6f18 commit 215d9b2
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 52 deletions.
55 changes: 33 additions & 22 deletions messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-12 15:23+0800\n"
"POT-Creation-Date: 2024-04-24 13:56+0800\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -471,8 +471,8 @@ msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_analyser/mod.py:175
msgid ""
"benchmark {} available data start date {} > backtest start date {} or end"
" date {} <= backtest end date {}"
"benchmark {} available data start date {} >= backtest start date {} or "
"end date {} <= backtest end date {}"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_analyser/mod.py:243
Expand Down Expand Up @@ -721,72 +721,79 @@ msgid ""
"trade: [{}...]"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:118
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:335
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:119
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:348
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:69
msgid ""
"Order Cancelled: current security [{order_book_id}] can not be traded in "
"listed date [{listed_date}]"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:125
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:165
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:126
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:166
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:86
msgid "Order Cancelled: {order_book_id} bar no volume"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:150
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:151
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:95
msgid "Order Cancelled: current bar [{order_book_id}] reach the limit_up price."
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:156
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:157
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:104
msgid "Order Cancelled: current bar [{order_book_id}] reach the limit_down price."
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:178
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:417
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:567
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:179
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:436
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:591
msgid ""
"Order Cancelled: market order {order_book_id} volume {order_volume} due "
"to volume limit"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:217
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:218
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:474
msgid ""
"Order Cancelled: not enough money to buy {order_book_id}, needs "
"{cost_money:.2f}, cash {cash:.2f}"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:230
msgid ""
"Order Cancelled: market order {order_book_id} volume {order_volume} is "
"larger than {volume_percent_limit} percent of current bar volume, fill "
"{filled_volume} actually"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:343
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:356
msgid "Order Cancelled: current tick [{order_book_id}] miss market data."
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:368
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:387
msgid "Order Cancelled: current tick [{order_book_id}] reach the limit_up price."
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:374
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:393
msgid ""
"Order Cancelled: current tick [{order_book_id}] reach the limit_down "
"price."
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:381
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:387
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:400
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:406
msgid "Order Cancelled: [{order_book_id}] has no liquidity."
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:462
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:486
msgid ""
"Order Cancelled: market order {order_book_id} volume {order_volume} is "
"larger than {volume_percent_limit} percent of current tick volume, fill "
"{filled_volume} actually"
msgstr ""

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:611
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:635
msgid ""
"Order Cancelled: market order {order_book_id} fill {filled_volume} "
"actually"
Expand Down Expand Up @@ -888,11 +895,15 @@ msgstr ""
msgid "Cash add {}. units {} become to {}"
msgstr ""

#: rqalpha/portfolio/account.py:352
#: rqalpha/portfolio/account.py:350
msgid "Futures account's cash turns negative after settlement"
msgstr ""

#: rqalpha/portfolio/account.py:356
msgid "Trigger Forced Liquidation, current total_value is 0"
msgstr ""

#: rqalpha/portfolio/account.py:502 rqalpha/portfolio/account.py:521
#: rqalpha/portfolio/account.py:506 rqalpha/portfolio/account.py:525
msgid "insufficient cash, current {}, target withdrawal {}"
msgstr ""

Expand Down
2 changes: 2 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_analyser/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def _yearly_indicators(
data["annual_volatility"].append(risk.annual_volatility)
data["max_drawdown"].append(risk.max_drawdown)
data["max_drawdown_days"].append((max_dd.end_date - max_dd.start_date).days)
data["alpha"].append(risk.alpha)
data["beta"].append(risk.beta)
return data


Expand Down
Binary file modified rqalpha/mod/rqalpha_mod_sys_analyser/report/templates/summary.xlsx
Binary file not shown.
38 changes: 31 additions & 7 deletions rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# 详细的授权流程,请联系 [email protected] 获取。
import datetime
from collections import defaultdict
from copy import copy
from rqalpha.const import MATCHING_TYPE, ORDER_TYPE, POSITION_EFFECT, SIDE
from rqalpha.environment import Environment
from rqalpha.core.events import EVENT, Event
Expand Down Expand Up @@ -131,7 +132,7 @@ def match(self, account, order, open_auction):
if reason:
order.mark_rejected(reason)
return

price_board = self._env.price_board
if order.type == ORDER_TYPE.LIMIT:
if order.side == SIDE.BUY and order.price < deal_price:
Expand Down Expand Up @@ -208,6 +209,18 @@ def match(self, account, order, open_auction):
)
trade._commission = self._env.get_trade_commission(trade)
trade._tax = self._env.get_trade_tax(trade)

if order.side == SIDE.BUY and self._slippage_decider.decider.rate != 0:
# 标的价格经过滑点处理后,账户资金可能不够买入,需要进行验证
cost_money = instrument.calc_cash_occupation(price, order.quantity, order.position_direction, order.trading_datetime.date())
cost_money += trade.transaction_cost
if cost_money > account.cash + order.init_frozen_cash:
reason = _(u"Order Cancelled: not enough money to buy {order_book_id}, needs {cost_money:.2f}, cash {cash:.2f}").format(
order_book_id=order_book_id, cost_money=cost_money, cash = account.cash + order.init_frozen_cash
)
order.mark_rejected(reason)
return

order.fill(trade)
self._turnover[order.order_book_id] += fill

Expand Down Expand Up @@ -344,6 +357,12 @@ def match(self, account, order, open_auction): # type: (Account, Order, bool) -
order_book_id=order.order_book_id)
order.mark_rejected(reason)
return

# 对价格进行滑点处理
if instrument.during_call_auction(self._env.calendar_dt):
price = deal_price
else:
price = self._slippage_decider.get_trade_price(order, deal_price)

price_board = self._env.price_board
if order.type == ORDER_TYPE.LIMIT:
Expand Down Expand Up @@ -434,12 +453,6 @@ def match(self, account, order, open_auction): # type: (Account, Order, bool) -
# 平今的数量
ct_amount = account.calc_close_today_amount(order_book_id, fill, order.position_direction, order.position_effect)

# 对价格进行滑点处理
if instrument.during_call_auction(self._env.calendar_dt):
price = deal_price
else:
price = self._slippage_decider.get_trade_price(order, deal_price)

# 成交记录
trade = Trade.__from_create__(
order_id=order.order_id,
Expand All @@ -453,6 +466,17 @@ def match(self, account, order, open_auction): # type: (Account, Order, bool) -
)
trade._commission = self._env.get_trade_commission(trade)
trade._tax = self._env.get_trade_tax(trade)

if order.side == SIDE.BUY and self._slippage_decider.decider.rate != 0:
cost_money = instrument.calc_cash_occupation(price, order.quantity, order.position_direction, order.trading_datetime.date())
cost_money += trade.transaction_cost
if cost_money > account.cash + order.init_frozen_cash:
reason = _(u"Order Cancelled: not enough money to buy {order_book_id}, needs {cost_money:.2f}, cash {cash:.2f}").format(
order_book_id=order_book_id, cost_money=cost_money, cash=account.cash + order.init_frozen_cash
)
order.mark_rejected(reason)
return

order.fill(trade)
self._turnover[order.order_book_id] += fill

Expand Down
4 changes: 4 additions & 0 deletions rqalpha/portfolio/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ def _on_settlement(self, event):
self._management_fees += fee
self._total_cash -= fee

# 如果期货结算结束时 cash 为负数,抛出提醒给到用户
if self._type == "FUTURE" and self.cash < 0:
user_system_log.warn(_("Futures account's cash turns negative after settlement"))

# 如果 total_value <= 0 则认为已爆仓,清空仓位,资金归0
forced_liquidation = self._env.config.base.forced_liquidation
if self.total_value <= 0 and forced_liquidation:
Expand Down
Binary file modified rqalpha/utils/translations/zh_Hans_CN/LC_MESSAGES/messages.mo
Binary file not shown.
55 changes: 33 additions & 22 deletions rqalpha/utils/translations/zh_Hans_CN/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-04-12 15:23+0800\n"
"POT-Creation-Date: 2024-04-24 13:56+0800\n"
"PO-Revision-Date: 2016-10-24 21:20+0800\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: zh_Hans_CN\n"
Expand Down Expand Up @@ -494,8 +494,8 @@ msgstr "基准标的 {} 在回测起始日期 {} 结束日期 {} 间缺失数据

#: rqalpha/mod/rqalpha_mod_sys_analyser/mod.py:175
msgid ""
"benchmark {} available data start date {} >= backtest start date {} or end"
" date {} <= backtest end date {}"
"benchmark {} available data start date {} >= backtest start date {} or "
"end date {} <= backtest end date {}"
msgstr "基准标的 {} 的可用行情数据开始日期 {} >= 回测起始日期 {} 或结束日期 {} <= 回测结束日期 {}"

#: rqalpha/mod/rqalpha_mod_sys_analyser/mod.py:243
Expand Down Expand Up @@ -744,39 +744,46 @@ msgid ""
"trade: [{}...]"
msgstr "订单创建失败,当前存在可能导致自成交的挂单:[{}...]"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:118
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:335
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:119
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:348
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:69
msgid ""
"Order Cancelled: current security [{order_book_id}] can not be traded in "
"listed date [{listed_date}]"
msgstr "订单被撤销: [{order_book_id}] 上市首日无法交易"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:125
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:165
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:126
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:166
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:86
msgid "Order Cancelled: {order_book_id} bar no volume"
msgstr "订单被撤销:{order_book_id} 当前无成交量"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:150
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:151
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:95
msgid "Order Cancelled: current bar [{order_book_id}] reach the limit_up price."
msgstr "订单被拒单: [{order_book_id}] 已涨停。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:156
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:157
#: rqalpha/mod/rqalpha_mod_sys_simulation/signal_broker.py:104
msgid "Order Cancelled: current bar [{order_book_id}] reach the limit_down price."
msgstr "订单被拒单: [{order_book_id}] 已跌停。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:178
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:417
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:567
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:179
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:436
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:591
msgid ""
"Order Cancelled: market order {order_book_id} volume {order_volume} due "
"to volume limit"
msgstr "订单被撤销: 订单 [{order_book_id}] 的下单量 {order_volume} 超过了成交量限制。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:217
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:218
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:474
msgid ""
"Order Cancelled: not enough money to buy {order_book_id}, needs "
"{cost_money:.2f}, cash {cash:.2f}"
msgstr "订单创建失败: 可用资金不足。当前资金: {cash:.2f},{order_book_id} 下单所需资金: {cost_money:.2f}"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:230
msgid ""
"Order Cancelled: market order {order_book_id} volume {order_volume} is "
"larger than {volume_percent_limit} percent of current bar volume, fill "
Expand All @@ -785,26 +792,26 @@ msgstr ""
"{order_book_id} 下单量 {order_volume} 超过当前 Bar 成交量的 "
"{volume_percent_limit}%,实际成交 {filled_volume}"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:343
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:356
msgid "Order Cancelled: current tick [{order_book_id}] miss market data."
msgstr "订单被拒单: [{order_book_id}] 当前缺失市场数据。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:368
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:387
msgid "Order Cancelled: current tick [{order_book_id}] reach the limit_up price."
msgstr "订单被拒单: [{order_book_id}] 已涨停。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:374
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:393
msgid ""
"Order Cancelled: current tick [{order_book_id}] reach the limit_down "
"price."
msgstr "订单被拒单: [{order_book_id}] 已跌停。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:381
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:387
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:400
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:406
msgid "Order Cancelled: [{order_book_id}] has no liquidity."
msgstr "合约 [{order_book_id}] 流动性不足,拒单。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:462
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:486
#, fuzzy
msgid ""
"Order Cancelled: market order {order_book_id} volume {order_volume} is "
Expand All @@ -814,7 +821,7 @@ msgstr ""
"{order_book_id} 下单量 {order_volume} 超过当前 Tick 成交量的 "
"{volume_percent_limit}%,实际成交 {filled_volume}"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:611
#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:635
msgid ""
"Order Cancelled: market order {order_book_id} fill {filled_volume} "
"actually"
Expand Down Expand Up @@ -916,11 +923,15 @@ msgstr "不合法的账户类型 {}, 请在以下账户类型中选择 {}"
msgid "Cash add {}. units {} become to {}"
msgstr "入金{}元,份额由{}变动为{}"

#: rqalpha/portfolio/account.py:352
#: rqalpha/portfolio/account.py:350
msgid "Futures account's cash turns negative after settlement"
msgstr "期货账户的现金在结算后变为负数"

#: rqalpha/portfolio/account.py:356
msgid "Trigger Forced Liquidation, current total_value is 0"
msgstr "触发强制清算,当前总权益为 0"

#: rqalpha/portfolio/account.py:502 rqalpha/portfolio/account.py:521
#: rqalpha/portfolio/account.py:506 rqalpha/portfolio/account.py:525
msgid "insufficient cash, current {}, target withdrawal {}"
msgstr "现金不足,当前现金 {},目标出金 {}"

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[metadata]
name = rqalpha
version = 5.3.9
version = 5.3.10

[versioneer]
VCS = git
Expand Down

0 comments on commit 215d9b2

Please sign in to comment.