Skip to content

Commit

Permalink
Update Version 2.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
shinny-pack authored and shinny-mayanqiong committed Oct 28, 2021
1 parent 5e3f22a commit 9129d7c
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 39 deletions.
2 changes: 1 addition & 1 deletion PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: tqsdk
Version: 2.9.2
Version: 2.9.3
Summary: TianQin SDK
Home-page: https://www.shinnytech.com/tqsdk
Author: TianQin
Expand Down
4 changes: 2 additions & 2 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
# built documents.
#
# The short X.Y version.
version = u'2.9.2'
version = u'2.9.3'
# The full version, including alpha/beta/rc tags.
release = u'2.9.2'
release = u'2.9.3'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
4 changes: 2 additions & 2 deletions doc/profession.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ TqSdk 免费版本提供全部的期货、商品/金融期权和上证50、沪

其他相关函数
-------------------------------------------------
:py:meth:`~tqsdk.api.TqApi.query_symbol_ranking`交易所每日成交持仓排名
:py:meth:`~tqsdk.api.TqApi.query_symbol_ranking` 交易所每日成交持仓排名

:py:meth:`~tqsdk.api.TqApi.get_kline_data_series`以起始日期获取 Dataframe 格式的 kline 数据
:py:meth:`~tqsdk.api.TqApi.get_kline_data_series` 以起始日期获取 Dataframe 格式的 kline 数据

:py:meth:`~tqsdk.api.TqApi.get_trading_status` 获取指定合约的交易状态,帮助用户实现开盘/跨小节抢单

Expand Down
2 changes: 1 addition & 1 deletion doc/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ klines是一个pandas.DataFrame对象. 跟 api.get_quote() 一样, api.get_kline

刚刚注册完成的信易账户的【手机号】/【邮箱地址】/【用户名】和【密码】可以作为 快期模拟 账号,通过 :py:class:`~tqsdk.api.TqKq` 对 auth 传入参数进行登录,这个 快期模拟 账户在快期APP、快期V3 pro 和天勤量化上是互通的

快期模拟的资金可以通过快期APP、快期V3 pro的模拟银行进行出入金::
快期模拟的资金可以通过快期APP、快期专业版的模拟银行进行出入金::

from tqsdk import TqApi, TqAuth, TqKq

Expand Down
1 change: 1 addition & 0 deletions doc/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ TqSdk 模块参考
tqsdk.sim.rst
tqsdk.backtest.rst
tqsdk.algorithm.rst
tqsdk.risk_rule.rst
tqsdk.tools.download.rst
tqsdk.exceptions.rst
6 changes: 6 additions & 0 deletions doc/reference/tqsdk.risk_rule.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _tqsdk.risk_rule:

tqsdk.risk_rule - 风控类模块
------------------------------------------------------------------
.. automodule:: tqsdk.risk_rule
:members:
2 changes: 1 addition & 1 deletion doc/usage/trade.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TqApi 创建成功即代表相应账户已登录成功. 如果在60秒内无法
----------------------------------------------------
如果您需要使用快期模拟账户进行测试,只需在创建TqApi时传入一个 :py:class:`~tqsdk.account.TqKq` 的实例,同时需要传入信易账户 :ref:`sim_trading`。

此账户类型与快期 APP 、天勤官网论坛、快期V3使用相同的模拟账户系统::
此账户类型与快期 APP 、天勤官网论坛、快期专业版使用相同的模拟账户系统::

from tqsdk import TqApi, TqAuth, TqKq

Expand Down
7 changes: 7 additions & 0 deletions doc/version.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

版本变更
=============================
2.9.3 (2021/10/28)

* 增加::py:class:`~tqsdk.risk_rule.TqRuleOpenCountsLimit`、:py:class:`~tqsdk.risk_rule.TqRuleOpenVolumesLimit` 类,
以及 :py:meth:`~tqsdk.api.TqApi.add_risk_rule`、:py:meth:`~tqsdk.api.TqApi.delete_risk_rule` 接口,支持本地风控功能
* 增加::py:class:`~tqsdk.exceptions.TqRiskRuleError` 错误类型,可以捕获风控触发的错误


2.9.2 (2021/10/20)

* 修复:实盘账户无法使用 :py:meth:`~tqsdk.api.TqApi.get_trading_status` 接口的问题
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_tag(self):

setuptools.setup(
name='tqsdk',
version="2.9.2",
version="2.9.3",
description='TianQin SDK',
author='TianQin',
author_email='[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion tqsdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tqsdk.auth import TqAuth
from tqsdk.channel import TqChan
from tqsdk.backtest import TqBacktest, TqReplay
from tqsdk.exceptions import BacktestFinished, TqTimeoutError
from tqsdk.exceptions import BacktestFinished, TqTimeoutError, TqRiskRuleError
from tqsdk.lib import TargetPosScheduler, TargetPosTask, InsertOrderUntilAllTradedTask, InsertOrderTask, TqNotify
from tqsdk.sim import TqSim
from tqsdk.multiaccount import TqMultiAccount
Expand Down
2 changes: 1 addition & 1 deletion tqsdk/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.9.2'
__version__ = '2.9.3'
93 changes: 64 additions & 29 deletions tqsdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
from tqsdk.objs import SecurityAccount, SecurityOrder, SecurityTrade, SecurityPosition
from tqsdk.objs_not_entity import QuoteList, TqDataFrame, TqSymbolDataFrame, SymbolList, SymbolLevelList, \
TqSymbolRankingDataFrame, TqOptionGreeksDataFrame
from tqsdk.risk_manager import TqRiskManager
from tqsdk.risk_rule import TqRiskRule
from tqsdk.sim import TqSim
from tqsdk.symbols import TqSymbols
from tqsdk.trading_status import TqTradingStatus
Expand Down Expand Up @@ -243,6 +245,7 @@ def __init__(self, account: Union[TqMultiAccount, TqAccount, TqSim, None] = None
self._td_url = _td_url

# 内部关键数据
self._risk_manager = TqRiskManager()
self._requests = {
"trading_status": set(),
"quotes": set(),
Expand Down Expand Up @@ -376,13 +379,13 @@ def get_quote(self, symbol: str) -> Quote:
注意:
1. 在 tqsdk 还没有收到行情数据包时, 此对象中各项内容为 NaN 或 0
2. 天勤接口从0.8版本开始,合约代码格式变更为 交易所代码.合约代码 的格式. 可用的交易所代码如下:
* CFFEX: 中金所
* SHFE: 上期所
* DCE: 大商所
* CZCE: 郑商所
* INE: 能源交易所(原油)
* SSE: 上交所
* SZSE: 深交所
* CFFEX: 中金所
* SHFE: 上期所
* DCE: 大商所
* CZCE: 郑商所
* INE: 能源交易所(原油)
* SSE: 上交所
* SZSE: 深交所
Example1::
Expand Down Expand Up @@ -443,13 +446,13 @@ def get_quote_list(self, symbols: List[str]) -> List[Quote]:
注意:
1. 在 tqsdk 还没有收到行情数据包时, 此对象中各项内容为 NaN 或 0
2. 天勤接口从0.8版本开始,合约代码格式变更为 交易所代码.合约代码 的格式. 可用的交易所代码如下:
* CFFEX: 中金所
* SHFE: 上期所
* DCE: 大商所
* CZCE: 郑商所
* INE: 能源交易所(原油)
* SSE: 上交所
* SZSE: 深交所
* CFFEX: 中金所
* SHFE: 上期所
* DCE: 大商所
* CZCE: 郑商所
* INE: 能源交易所(原油)
* SSE: 上交所
* SZSE: 深交所
Example::
Expand Down Expand Up @@ -1082,6 +1085,37 @@ def query_his_cont_quotes(self, symbol: Union[str, List[str]], n: int = 200):
df.reset_index(inplace=True, drop=True)
return df

# ----------------------------------------------------------------------
def add_risk_rule(self, rule: TqRiskRule):
"""
添加一项风控规则实例,此接口为 TqSdk 专业版提供。
如需使用此功能,可以点击 `天勤量化专业版 <https://www.shinnytech.com/tqsdk_professional/>`_ 申请试用或购买
Args:
rule (TqRiskRule): 风控规则实例,必须是 TqRiskRule 的子类型
"""
if not self._auth._has_feature("tq_lc_rk"):
raise Exception("本地风控功能仅限专业版用户使用,如需购买专业版或者申请试用,请访问 https://www.shinnytech.com/tqsdk_professional/")
if not isinstance(rule, TqRiskRule):
raise Exception("传入参数对象必须是 TqRiskRule 的类型")
self._risk_manager.append(rule)

def delete_risk_rule(self, rule: TqRiskRule):
"""
删除一项风控规则实例,此接口为 TqSdk 专业版提供。
如需使用此功能,可以点击 `天勤量化专业版 <https://www.shinnytech.com/tqsdk_professional/>`_ 申请试用或购买
Args:
rule (TqRiskRule): 风控规则实例,必须是 TqRiskRule 的子类型
"""
if not isinstance(rule, TqRiskRule):
raise Exception("传入参数对象必须是 TqRiskRule 的类型")
self._risk_manager.remove(rule)

# ----------------------------------------------------------------------
def insert_order(self, symbol: str, direction: str, offset: str = "", volume: int = 0,
limit_price: Union[str, float, None] = None,
Expand Down Expand Up @@ -1322,7 +1356,8 @@ def _get_insert_order_pack(self, symbol, direction, offset, volume, limit_price,
msg["contingent_condition"] = "UNKNOWN"
msg["volume_condition"] = "UNKNOWN"
msg["time_condition"] = "UNKNOWN"

self._risk_manager._could_insert_order(msg)
self._risk_manager._on_insert_order(msg)
return msg

async def _insert_order_async(self, symbol, direction, offset, volume, limit_price, advanced, order_id,
Expand Down Expand Up @@ -2194,20 +2229,20 @@ def query_symbol_ranking(self, symbol: str, ranking_type: str, days: int = 1, st
Returns:
pandas.DataFrame: 本函数返回 pandas.DataFrame 实例。行数为 days * 20,每行为一条成交量/多头持仓量/空头持仓量的排名信息。返回值不会再更新。包含以下列:
* datetime (查询日期)
* symbol (合约代码,以交易所列出的期货公司名称为准)
* exchange_id (交易所)
* instrument_id (交易所内合约代码)
* broker (期货公司)
* volume (成交量)
* volume_change (成交量变化)
* volume_ranking (成交量排名)
* long_oi (多头持仓量)
* long_change (多头持仓增减量)
* long_ranking (多头持仓量排名)
* short_oi (空头持仓量)
* short_change (空头持仓增减量)
* short_ranking (空头持仓量排名)
* datetime (查询日期)
* symbol (合约代码,以交易所列出的期货公司名称为准)
* exchange_id (交易所)
* instrument_id (交易所内合约代码)
* broker (期货公司)
* volume (成交量)
* volume_change (成交量变化)
* volume_ranking (成交量排名)
* long_oi (多头持仓量)
* long_change (多头持仓增减量)
* long_ranking (多头持仓量排名)
* short_oi (空头持仓量)
* short_change (空头持仓增减量)
* short_ranking (空头持仓量排名)
注意:
1. 返回值中 datetime、symbol、exchange_id、instrument_id、broker 这几列一定为有效值。其他列会根据不同的 ranking_type 参数值,可能返回 nan:
Expand Down
10 changes: 10 additions & 0 deletions tqsdk/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ class TqBacktestPermissionError(Exception):
def __init__(self, message):
super().__init__(message)
self.message = message


class TqRiskRuleError(Exception):
"""
风控触发的报错
"""

def __init__(self, message):
super().__init__(message)
self.message = message
30 changes: 30 additions & 0 deletions tqsdk/risk_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!usr/bin/env python3
# -*- coding:utf-8 -*-

__author__ = 'mayanqiong'

from tqsdk.exceptions import TqRiskRuleError


class TqRiskManager(list):

def append(self, rule):
if rule not in self:
super(TqRiskManager, self).append(rule)

def remove(self, rule):
if rule in self:
super(TqRiskManager, self).remove(rule)

def _could_insert_order(self, pack):
# 是否可以下单
for r in self:
is_valid, err_msg = r._could_insert_order(pack)
if not is_valid:
raise TqRiskRuleError(err_msg)
return True

def _on_insert_order(self, pack):
# 需要更新风控对象内部统计值
for r in self:
r._on_insert_order(pack)
Loading

0 comments on commit 9129d7c

Please sign in to comment.