Skip to content

Commit

Permalink
docs:修改文档
Browse files Browse the repository at this point in the history
  • Loading branch information
shinny-limin committed Sep 20, 2019
1 parent 84c7c00 commit 4683dfa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
Binary file added doc/images/qa_pic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ TianQin Python Sdk User Guide
advanced/index.rst
reference/index.rst
demo/index.rst
qa.rst
version.rst

50 changes: 25 additions & 25 deletions doc/usage/backtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@
回测结束后会输出交易记录和每日收盘时的账户资金情况,以及最大回撤、夏普比率等指标,这些数据可以导入到 excel 中或使用其他分析工具进一步处理。


在回测结束时获取回测详细信息
-------------------------------------------------
要在回测结束时调用您自己写的代码, 可以使用 try/except 机制捕获回测结束信号 BacktestFinished, 像这样::

from tqsdk import BacktestFinished

acc = TqSim()

try:
api = TqApi(acc, backtest=TqBacktest(start_dt=date(2018, 5, 1), end_dt=date(2018, 10, 1)))
#策略代码在这里
#...

except BacktestFinished as e:
# 回测结束时会执行这里的代码
print(acc.trade_log)

回测的详细信息保存在回测所用的模拟账户中, 可以直接访问它的成员变量获取, 常用的有:

* trade_log, 格式为 日期->交易记录及收盘时的权益及持仓
* account, 资金账户最终状态
* positions, 账户持仓最终状态
* quotes, 行情最终状态


回测时的成交规则和推进
-------------------------------------------------
策略回测时使用内置模拟账户 :py:class:`~tqsdk.sim.TqSim` , 撮合成交规则为对价成交. 即限价单的价格达到对手盘价格时判定为成交. 不会出现委托单部分成交的情况.
Expand Down Expand Up @@ -85,31 +110,6 @@ TqSdk回测框架使用一套复杂的规则来推进行情:
print(ka.iloc[-1].datetime, kb.iloc[-1].datetime) # 2018/01/01 09:00:20, 2018/01/01 09:00:15
api.wait_update() # 再推一步, 时间推到 09:00:30, ka, kb都更新了
print(ka.iloc[-1].datetime, kb.iloc[-1].datetime) # 2018/01/01 09:00:30, 2018/01/01 09:00:30

在回测结束时获取回测详细信息
-------------------------------------------------
要在回测结束时调用您自己写的代码, 可以使用 try/except 机制捕获回测结束信号 BacktestFinished, 像这样::

from tqsdk import BacktestFinished
acc = TqSim()

try:
api = TqApi(acc, backtest=TqBacktest(start_dt=date(2018, 5, 1), end_dt=date(2018, 10, 1)))
#策略代码在这里
#...
except BacktestFinished as e:
# 回测结束时会执行这里的代码
print(acc.trade_log)

回测的详细信息保存在回测所用的模拟账户中, 可以直接访问它的成员变量获取, 常用的有:

* trade_log, 格式为 日期->交易记录及收盘时的权益及持仓
* account, 资金账户最终状态
* positions, 账户持仓最终状态
* quotes, 行情最终状态


了解更多
Expand Down
22 changes: 17 additions & 5 deletions tqsdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,34 @@ def get_kline_serial(self, symbol, duration_seconds, data_length=200, chart_id=N
* open_oi: int, 27354 (K线起始时刻的持仓量)
* close_oi: int, 27355 (K线结束时刻的持仓量)
Example::
Example1::
# 获取 SHFE.cu1812 的1分钟线
from tqsdk import TqApi, TqSim
api = TqApi(TqSim())
k_serial = api.get_kline_serial("SHFE.cu1812", 60)
klines = api.get_kline_serial("SHFE.cu1812", 60)
while True:
api.wait_update()
print(k_serial.iloc[-1].close)
print(klines.iloc[-1].close)
# 预计的输出是这样的:
50970.0
50970.0
50960.0
...
Example2::
# 将K线的纳秒时间转换为 datetime.datetime 类型
from datetime import datetime
...
klines = api.get_kline_serial("DCE.jd2001", 10)
kline_time = datetime.fromtimestamp(klines.iloc[-1]["datetime"] / 1e9) # datetime.datetime 类型值
print(type(kline_time), kline_time)
print(kline_time.year, kline_time.month, kline_time.day, kline_time.hour, kline_time.minute, kline_time.second)
"""
if symbol not in self._data.get("quotes", {}):
raise Exception("代码 %s 不存在, 请检查合约代码是否填写正确" % (symbol))
Expand Down Expand Up @@ -850,10 +862,10 @@ def is_serial_ready(self, obj) -> bool:
from tqsdk import TqApi, TqSim
api = TqApi(TqSim())
k_serial = api.get_kline_serial("SHFE.cu1812", 60, data_length=3000)
klines = api.get_kline_serial("SHFE.cu1812", 60, data_length=3000)
while True:
api.wait_update()
print(api.is_serial_ready(k_serial))
print(api.is_serial_ready(klines))
# 预计的输出是这样的:
False
Expand Down

0 comments on commit 4683dfa

Please sign in to comment.