Choosing Simulation Accounts and Historical Backtests in TqSdk
Summary
This reference distinguishes local simulation accounts from remote Quick simulated accounts for futures and stocks. It describes TqSim as a local futures simulation option for development and backtests, TqKq as a Quick linked futures account, and corresponding stock choices. It highlights an important difference for stock workflows: TargetPosTask is unsupported for stock trading, and mixed futures and stock backtests require a multi-account setup.
Historical strategy execution uses TqBacktest with a suitable local simulation account, with separate choices for futures and stocks. The example illustrates a simple moving average comparison in a futures backtest, while the guidance notes that quote updates during backtests can differ from live trading and that update calls may process order state before advancing market time. Backtests are framed as strategy execution over a date range, not long-range data export; the document directs users to a separate downloader for CSV extraction.
Key ideas
- TqSim and TqSimStock are local account options for futures and stock simulations and backtests.
- TqKq and TqKqStock provide simulated account workflows connected to Quick.
- Stock trading does not support TargetPosTask in the described setup.
- Historical backtests pair TqBacktest with the appropriate local simulation account.
- Backtest update timing can differ from live trading, and long-range data export requires a downloader.
Tags
Full text
# Simulation And Backtest
# Simulation And Backtest
## Use This Reference For
- Explaining the difference among `TqSim`, `TqKq`, `TqSimStock`, `TqKqStock`, and `TqBacktest`
- Writing backtest examples
- Knowing which tool to choose for local simulation, Quick simulation, stock simulation, or historical backtest
## Table Of Contents
- Simulation choices
- Backtest
- Choosing among them
## Simulation Choices
### `TqSim`
Use for:
- local futures simulation
- strategy development
- futures backtest account object
- disposable examples that should not depend on a persistent remote simulated account
Characteristics:
- local simulated matching
- in backtest mode, only `TqSim` or `TqSimStock` can trade
- good default when `TqApi` is created without an explicit account
### `TqKq`
Use for:
- Quick futures simulation
- simulated trading tied to the Quick ecosystem
- scenarios where the user expects account data to match the official Quick clients
Characteristics:
- remote simulated account behavior
- requires `auth=TqAuth(...)`
### `TqSimStock`
Use for:
- local stock simulation
- stock backtest
- mixed futures and stock backtest when paired with `TqMultiAccount`
Important boundary:
- stock trading rules differ from futures
- `TargetPosTask` is not supported for stock trading
### `TqKqStock`
Use for:
- Quick stock simulation
- stock account workflows that should stay in the Quick ecosystem
## Backtest
Backtest uses `TqBacktest(...)` together with `TqSim()` or `TqSimStock()`.
Futures backtest example:
```python
from datetime import date
from tqsdk import TqApi, TqAuth, TqBacktest, TqSim, TargetPosTask
api = TqApi(
TqSim(),
backtest=TqBacktest(start_dt=date(2025, 1, 1), end_dt=date(2025, 1, 31)),
auth=TqAuth("快期账户", "账户密码"),
)
klines = api.get_kline_serial("DCE.m2609", 60, data_length=200)
target_pos = TargetPosTask(api, "DCE.m2609")
while True:
api.wait_update()
if api.is_changing(klines.iloc[-1], "datetime"):
if klines.close.iloc[-1] > klines.close.iloc[-20:].mean():
target_pos.set_target_volume(1)
else:
target_pos.set_target_volume(0)
```
Important boundaries:
- backtest is for historical strategy execution, not arbitrary long-range data export
- quote update behavior in backtest differs from live trading
- one `wait_update()` call may update only order state, and the next one may advance market time
- TqSdk can output account statistics at backtest end
Stock backtest boundaries:
- use `TqSimStock()` for stock backtest
- stock backtest does not use `TargetPosTask`
- if the user wants both futures and stock in one backtest, use `TqMultiAccount([TqSim(), TqSimStock()])`
## Choosing Among Them
- need an account for live-ish strategy development without persistence requirements: `TqSim`
- need a Quick futures simulated account: `TqKq`
- need a Quick stock simulated account: `TqKqStock`
- need a local stock simulated account: `TqSimStock`
- need historical futures strategy execution over a date range: `TqBacktest` with `TqSim`
- need historical stock strategy execution over a date range: `TqBacktest` with `TqSimStock`
- need long-range historical CSV export: `DataDownloader`, not backtest
## Repository Sources
- `tqsdk/demo/tutorial/backtest.py`
- `doc/usage/backtest.rst`
- `doc/reference/tqsdk.sim.rst`
- `doc/reference/tqsdk.tqkq.rst`
- `tqsdk/api.py`Shown in full with attribution under the source's licence. Licence: Apache-2.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.