Run strategy backtest
curl --request POST \
--url https://api.reportify.cn/v1/quant/backtest \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"start_date": "2023-12-25",
"end_date": "2023-12-25",
"market": "cn",
"symbols": [
"<string>"
],
"filter_formula": "<string>",
"entry_formula": "<string>",
"exit_formula": "<string>",
"strategy_code": "<string>",
"initial_cash": 100000,
"commission": 0,
"buy_commission": 0,
"sell_commission": 0,
"min_commission_amount": 0,
"slippage": 0,
"auto_close": true,
"signal_factors": {},
"position_size": 0.2,
"max_positions": 5,
"min_volume": 100
}
'import requests
url = "https://api.reportify.cn/v1/quant/backtest"
payload = {
"start_date": "2023-12-25",
"end_date": "2023-12-25",
"market": "cn",
"symbols": ["<string>"],
"filter_formula": "<string>",
"entry_formula": "<string>",
"exit_formula": "<string>",
"strategy_code": "<string>",
"initial_cash": 100000,
"commission": 0,
"buy_commission": 0,
"sell_commission": 0,
"min_commission_amount": 0,
"slippage": 0,
"auto_close": True,
"signal_factors": {},
"position_size": 0.2,
"max_positions": 5,
"min_volume": 100
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
start_date: '2023-12-25',
end_date: '2023-12-25',
market: 'cn',
symbols: ['<string>'],
filter_formula: '<string>',
entry_formula: '<string>',
exit_formula: '<string>',
strategy_code: '<string>',
initial_cash: 100000,
commission: 0,
buy_commission: 0,
sell_commission: 0,
min_commission_amount: 0,
slippage: 0,
auto_close: true,
signal_factors: {},
position_size: 0.2,
max_positions: 5,
min_volume: 100
})
};
fetch('https://api.reportify.cn/v1/quant/backtest', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.reportify.cn/v1/quant/backtest",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'start_date' => '2023-12-25',
'end_date' => '2023-12-25',
'market' => 'cn',
'symbols' => [
'<string>'
],
'filter_formula' => '<string>',
'entry_formula' => '<string>',
'exit_formula' => '<string>',
'strategy_code' => '<string>',
'initial_cash' => 100000,
'commission' => 0,
'buy_commission' => 0,
'sell_commission' => 0,
'min_commission_amount' => 0,
'slippage' => 0,
'auto_close' => true,
'signal_factors' => [
],
'position_size' => 0.2,
'max_positions' => 5,
'min_volume' => 100
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.reportify.cn/v1/quant/backtest"
payload := strings.NewReader("{\n \"start_date\": \"2023-12-25\",\n \"end_date\": \"2023-12-25\",\n \"market\": \"cn\",\n \"symbols\": [\n \"<string>\"\n ],\n \"filter_formula\": \"<string>\",\n \"entry_formula\": \"<string>\",\n \"exit_formula\": \"<string>\",\n \"strategy_code\": \"<string>\",\n \"initial_cash\": 100000,\n \"commission\": 0,\n \"buy_commission\": 0,\n \"sell_commission\": 0,\n \"min_commission_amount\": 0,\n \"slippage\": 0,\n \"auto_close\": true,\n \"signal_factors\": {},\n \"position_size\": 0.2,\n \"max_positions\": 5,\n \"min_volume\": 100\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.reportify.cn/v1/quant/backtest")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"start_date\": \"2023-12-25\",\n \"end_date\": \"2023-12-25\",\n \"market\": \"cn\",\n \"symbols\": [\n \"<string>\"\n ],\n \"filter_formula\": \"<string>\",\n \"entry_formula\": \"<string>\",\n \"exit_formula\": \"<string>\",\n \"strategy_code\": \"<string>\",\n \"initial_cash\": 100000,\n \"commission\": 0,\n \"buy_commission\": 0,\n \"sell_commission\": 0,\n \"min_commission_amount\": 0,\n \"slippage\": 0,\n \"auto_close\": true,\n \"signal_factors\": {},\n \"position_size\": 0.2,\n \"max_positions\": 5,\n \"min_volume\": 100\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.reportify.cn/v1/quant/backtest")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"start_date\": \"2023-12-25\",\n \"end_date\": \"2023-12-25\",\n \"market\": \"cn\",\n \"symbols\": [\n \"<string>\"\n ],\n \"filter_formula\": \"<string>\",\n \"entry_formula\": \"<string>\",\n \"exit_formula\": \"<string>\",\n \"strategy_code\": \"<string>\",\n \"initial_cash\": 100000,\n \"commission\": 0,\n \"buy_commission\": 0,\n \"sell_commission\": 0,\n \"min_commission_amount\": 0,\n \"slippage\": 0,\n \"auto_close\": true,\n \"signal_factors\": {},\n \"position_size\": 0.2,\n \"max_positions\": 5,\n \"min_volume\": 100\n}"
response = http.request(request)
puts response.read_body{
"success": true,
"initial_cash": 100000,
"commission": 0,
"final_cash": 100000,
"total_return": 0,
"total_return_pct": 0,
"max_drawdown": 0,
"profit_factor": 0,
"win_rate": 0,
"total_trades": 0,
"winning_trades": 0,
"losing_trades": 0,
"trades": [],
"portfolio_value": {},
"benchmark_value": {},
"benchmark_return_pct": 0,
"sharpe_ratio": 0,
"alpha": 0,
"beta": 0,
"max_drawdown_start": "<string>",
"max_drawdown_end": "<string>",
"daily_returns": {},
"drawdown_series": {},
"error_msg": "<string>"
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}量化分析接口
策略回测
运行量化策略回测并获取绩效分析
POST
/
v1
/
quant
/
backtest
Run strategy backtest
curl --request POST \
--url https://api.reportify.cn/v1/quant/backtest \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"start_date": "2023-12-25",
"end_date": "2023-12-25",
"market": "cn",
"symbols": [
"<string>"
],
"filter_formula": "<string>",
"entry_formula": "<string>",
"exit_formula": "<string>",
"strategy_code": "<string>",
"initial_cash": 100000,
"commission": 0,
"buy_commission": 0,
"sell_commission": 0,
"min_commission_amount": 0,
"slippage": 0,
"auto_close": true,
"signal_factors": {},
"position_size": 0.2,
"max_positions": 5,
"min_volume": 100
}
'import requests
url = "https://api.reportify.cn/v1/quant/backtest"
payload = {
"start_date": "2023-12-25",
"end_date": "2023-12-25",
"market": "cn",
"symbols": ["<string>"],
"filter_formula": "<string>",
"entry_formula": "<string>",
"exit_formula": "<string>",
"strategy_code": "<string>",
"initial_cash": 100000,
"commission": 0,
"buy_commission": 0,
"sell_commission": 0,
"min_commission_amount": 0,
"slippage": 0,
"auto_close": True,
"signal_factors": {},
"position_size": 0.2,
"max_positions": 5,
"min_volume": 100
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
start_date: '2023-12-25',
end_date: '2023-12-25',
market: 'cn',
symbols: ['<string>'],
filter_formula: '<string>',
entry_formula: '<string>',
exit_formula: '<string>',
strategy_code: '<string>',
initial_cash: 100000,
commission: 0,
buy_commission: 0,
sell_commission: 0,
min_commission_amount: 0,
slippage: 0,
auto_close: true,
signal_factors: {},
position_size: 0.2,
max_positions: 5,
min_volume: 100
})
};
fetch('https://api.reportify.cn/v1/quant/backtest', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.reportify.cn/v1/quant/backtest",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'start_date' => '2023-12-25',
'end_date' => '2023-12-25',
'market' => 'cn',
'symbols' => [
'<string>'
],
'filter_formula' => '<string>',
'entry_formula' => '<string>',
'exit_formula' => '<string>',
'strategy_code' => '<string>',
'initial_cash' => 100000,
'commission' => 0,
'buy_commission' => 0,
'sell_commission' => 0,
'min_commission_amount' => 0,
'slippage' => 0,
'auto_close' => true,
'signal_factors' => [
],
'position_size' => 0.2,
'max_positions' => 5,
'min_volume' => 100
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.reportify.cn/v1/quant/backtest"
payload := strings.NewReader("{\n \"start_date\": \"2023-12-25\",\n \"end_date\": \"2023-12-25\",\n \"market\": \"cn\",\n \"symbols\": [\n \"<string>\"\n ],\n \"filter_formula\": \"<string>\",\n \"entry_formula\": \"<string>\",\n \"exit_formula\": \"<string>\",\n \"strategy_code\": \"<string>\",\n \"initial_cash\": 100000,\n \"commission\": 0,\n \"buy_commission\": 0,\n \"sell_commission\": 0,\n \"min_commission_amount\": 0,\n \"slippage\": 0,\n \"auto_close\": true,\n \"signal_factors\": {},\n \"position_size\": 0.2,\n \"max_positions\": 5,\n \"min_volume\": 100\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.reportify.cn/v1/quant/backtest")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"start_date\": \"2023-12-25\",\n \"end_date\": \"2023-12-25\",\n \"market\": \"cn\",\n \"symbols\": [\n \"<string>\"\n ],\n \"filter_formula\": \"<string>\",\n \"entry_formula\": \"<string>\",\n \"exit_formula\": \"<string>\",\n \"strategy_code\": \"<string>\",\n \"initial_cash\": 100000,\n \"commission\": 0,\n \"buy_commission\": 0,\n \"sell_commission\": 0,\n \"min_commission_amount\": 0,\n \"slippage\": 0,\n \"auto_close\": true,\n \"signal_factors\": {},\n \"position_size\": 0.2,\n \"max_positions\": 5,\n \"min_volume\": 100\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.reportify.cn/v1/quant/backtest")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"start_date\": \"2023-12-25\",\n \"end_date\": \"2023-12-25\",\n \"market\": \"cn\",\n \"symbols\": [\n \"<string>\"\n ],\n \"filter_formula\": \"<string>\",\n \"entry_formula\": \"<string>\",\n \"exit_formula\": \"<string>\",\n \"strategy_code\": \"<string>\",\n \"initial_cash\": 100000,\n \"commission\": 0,\n \"buy_commission\": 0,\n \"sell_commission\": 0,\n \"min_commission_amount\": 0,\n \"slippage\": 0,\n \"auto_close\": true,\n \"signal_factors\": {},\n \"position_size\": 0.2,\n \"max_positions\": 5,\n \"min_volume\": 100\n}"
response = http.request(request)
puts response.read_body{
"success": true,
"initial_cash": 100000,
"commission": 0,
"final_cash": 100000,
"total_return": 0,
"total_return_pct": 0,
"max_drawdown": 0,
"profit_factor": 0,
"win_rate": 0,
"total_trades": 0,
"winning_trades": 0,
"losing_trades": 0,
"trades": [],
"portfolio_value": {},
"benchmark_value": {},
"benchmark_return_pct": 0,
"sharpe_ratio": 0,
"alpha": 0,
"beta": 0,
"max_drawdown_start": "<string>",
"max_drawdown_end": "<string>",
"daily_returns": {},
"drawdown_series": {},
"error_msg": "<string>"
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}运行策略回测
执行策略回测,返回绩效指标和交易记录。POST /v1/quant/backtest
curl -X POST https://api.reportify.cn/v1/quant/backtest \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"start_date": "2024-01-01",
"end_date": "2025-01-01",
"market": "cn",
"symbols": ["000001"],
"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 10))",
"exit_formula": "CROSS(MA(CLOSE, 10), MA(CLOSE, 5))",
"initial_cash": 100000,
"benchmark": {
"symbol": "000905",
"market": "cn",
"stock_type": "index"
},
"commission": 0.0003,
"position_size": 0.2,
"max_positions": 5,
"min_volume": 100
}'
import requests
response = requests.post(
"https://api.reportify.cn/v1/quant/backtest",
headers={"Authorization": "Bearer <token>"},
json={
"start_date": "2024-01-01",
"end_date": "2025-01-01",
"market": "cn",
"symbols": ["000001"],
"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 10))",
"exit_formula": "CROSS(MA(CLOSE, 10), MA(CLOSE, 5))",
"initial_cash": 100000,
"benchmark": {
"symbol": "000905",
"market": "cn",
"stock_type": "index"
},
"commission": 0.0003,
"position_size": 0.2,
"max_positions": 5,
"min_volume": 100
}
)
print(response.json())
const response = await fetch('https://api.reportify.cn/v1/quant/backtest', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
start_date: '2024-01-01',
end_date: '2025-01-01',
market: 'cn',
symbols: ['000001'],
entry_formula: 'CROSS(MA(CLOSE, 5), MA(CLOSE, 10))',
exit_formula: 'CROSS(MA(CLOSE, 10), MA(CLOSE, 5))',
initial_cash: 100000,
benchmark: {
symbol: '000905',
market: 'cn',
stock_type: 'index'
},
commission: 0.0003,
position_size: 0.2,
max_positions: 5,
min_volume: 100
})
});
const data = await response.json();
请求参数
股票池预筛选公式。在执行每日回测逻辑前,系统会先用此公式对当前市场的股票进行过滤,结果大于 0(即为 True)的股票才会保留在当日的可用股票池中,参与后续的行情数据加载和买卖信号计算。通常用于当前市场范围选股时过滤掉 ST 股、停牌股或不符合基本面要求的股票。
Show 公式示例
Show 公式示例
NOT(IS_ST())- 过滤 ST 股票(PE_TTM() > 0) & (PE_TTM() < 30)- 过滤亏损股且要求 PE 小于 30NOT(IS_ST()) & (ROE() > 10)- 过滤 ST 股票且要求 ROE 大于 10%
买入/开仓公式,结果大于 0 时触发买入(如不提供则必须提供
strategy_code)。支持技术指标与基本面因子。Show 公式示例
Show 公式示例
CROSS(MA(CLOSE, 5), MA(CLOSE, 10))- 均线金叉买入RSI(14) < 30- RSI 超卖买入CLOSE > BOLL(20, 2).upper- 突破布林带上轨买入(RSI(14) < 30) & (CLOSE > MA(CLOSE, 20))- 组合条件(PE_TTM() < 20) & (ROE() > 15)- 基本面因子选股
卖出/平仓公式,结果大于 0 时触发卖出(可选)
Show 公式示例
Show 公式示例
CROSS(MA(CLOSE, 10), MA(CLOSE, 5))- 均线死叉卖出RSI(14) > 70- RSI 超买卖出CLOSE < BOLL(20, 2).lower- 跌破布林带下轨卖出
自定义回测策略的 Python 源码代码。若提供,将执行自定义策略而非公式策略。注意: 策略代码必须定义一个名为
handle_data(context, datas) 的函数作为回测引擎的入口点。预计算因子字典,用于
strategy_code 模式下提前计算买卖所需的指标,或在回测中返回额外的值(可选)。可通过 factors 接口获取可用因子列表。Show signal_factors 示例
Show signal_factors 示例
{
"ma5": "MA(CLOSE, 5)",
"ma10": "MA(CLOSE, 10)",
"rsi": "RSI(14)",
"pe": "PE_TTM()",
"roe": "ROE()"
}
回测对比基准配置。
常见用法:
benchmark 必须是对象,不能直接传字符串代码。Show benchmark 示例
Show benchmark 示例
{
"symbol": "000905",
"market": "cn",
"stock_type": "index"
}
{"symbol": "000905", "market": "cn", "stock_type": "index"}- 中证500{"symbol": "000300", "market": "cn", "stock_type": "index"}- 沪深300{"symbol": "510500", "market": "cn", "stock_type": "etf"}- ETF 基准
"000905"- 错误,接口期望 object,不是 string
响应参数
交易详情列表
Show Trade 字段
Show Trade 字段
id: 交易 IDsymbol: 股票代码type: 交易类型(Long或Short)entry_date: 开仓日期exit_date: 平仓日期entry_price: 开仓价格exit_price: 平仓价格size: 交易数量net_pnl: 净盈亏return_pct: 收益率百分比cumulative_pnl: 累计盈亏
响应示例
{
"success": true,
"initial_cash": 100000,
"commission": 0.0003,
"final_cash": 115600,
"total_return": 15600,
"total_return_pct": 0.156,
"max_drawdown": -0.082,
"profit_factor": 2.35,
"win_rate": 0.65,
"total_trades": 12,
"winning_trades": 8,
"losing_trades": 4,
"trades": [
{
"id": 1,
"symbol": "000001",
"type": "Long",
"entry_date": "2024-01-15",
"exit_date": "2024-02-20",
"entry_price": 11.25,
"exit_price": 12.50,
"size": 8800,
"net_pnl": 10937.3,
"return_pct": 0.111,
"cumulative_pnl": 10937.3
}
],
"portfolio_value": {
"2024-01-15": 100000.0,
"2024-01-16": 101200.5,
"2024-02-20": 110937.3
},
"benchmark_value": {
"2024-01-15": 100000.0,
"2024-01-16": 100850.1,
"2024-02-20": 108430.6
},
"benchmark_return_pct": 0.0843,
"annualized_return_pct": 0.172,
"sharpe_ratio": 1.18,
"alpha": 0.041,
"beta": 0.92,
"max_drawdown_start": "2024-03-11",
"max_drawdown_end": "2024-04-18",
"daily_returns": {
"2024-01-16": 0.0120,
"2024-01-17": -0.0041
},
"drawdown_series": {
"2024-03-11": 0.0000,
"2024-04-18": 0.0820
}
}
策略公式示例
均线金叉死叉策略
5 日均线上穿 10 日均线时买入,下穿时卖出:{
"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 10))",
"exit_formula": "CROSS(MA(CLOSE, 10), MA(CLOSE, 5))",
"symbols": ["000001"],
"start_date": "2024-01-01",
"end_date": "2025-01-01"
}
RSI 超买超卖策略
RSI 低于 30 时买入,高于 70 时卖出:{
"entry_formula": "RSI(14) < 30",
"exit_formula": "RSI(14) > 70",
"symbols": ["600519"],
"start_date": "2024-01-01",
"end_date": "2025-01-01"
}
MACD 金叉死叉策略
MACD DIF 上穿 DEA 时买入,下穿时卖出:{
"entry_formula": "CROSS(MACD().dif, MACD().dea)",
"exit_formula": "CROSS(MACD().dea, MACD().dif)",
"symbols": ["000002"],
"start_date": "2024-01-01",
"end_date": "2025-01-01"
}
布林带突破策略
价格突破布林带上轨时买入,跌破下轨时卖出:{
"entry_formula": "CLOSE > BOLL(20, 2).upper",
"exit_formula": "CLOSE < BOLL(20, 2).lower",
"symbols": ["000001"],
"start_date": "2024-01-01",
"end_date": "2025-01-01"
}
组合条件策略
RSI 超卖且价格在 20 日均线上方买入,RSI 超买或价格跌破均线卖出:{
"entry_formula": "(RSI(14) < 30) & (CLOSE > MA(CLOSE, 20))",
"exit_formula": "(RSI(14) > 70) | (CLOSE < MA(CLOSE, 20))",
"symbols": ["600036"],
"start_date": "2024-01-01",
"end_date": "2025-01-01",
"commission": 0.0003,
"position_size": 0.8
}
当前市场选股与过滤策略
如果不指定symbols,系统将在当前市场(由 market 字段决定,默认为A股 cn)执行回测。可以利用 filter_formula 在当前市场范围内进行过滤筛选,排除不满足基本面或状态要求的股票:
{
"start_date": "2024-01-01",
"end_date": "2025-01-01",
"filter_formula": "NOT(IS_ST()) & (PE_TTM() < 20) & (ROE() > 15)",
"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 20))",
"exit_formula": "CROSS(MA(CLOSE, 20), MA(CLOSE, 5))",
"position_size": 0.1,
"max_positions": 10
}
带 signal_factors 和自定义策略的回测
使用signal_factors 预计算因子并在自定义策略 strategy_code 中调用:
{
"symbols": ["000001"],
"start_date": "2024-01-01",
"end_date": "2025-01-01",
"signal_factors": {
"ma5": "MA(CLOSE, 5)",
"ma10": "MA(CLOSE, 10)",
"rsi": "RSI(14)",
"macd_dif": "MACD().dif",
"pe": "PE_TTM()"
},
"strategy_code": "def handle_data(context, datas):\n for data in datas:\n symbol = data.name\n position = context.portfolio.get_position(symbol)\n if not position:\n if data.ma5 > data.ma10 and data.pe < 20:\n context.order_target_percent(symbol, 0.2)\n elif data.rsi > 70:\n context.order_target_percent(symbol, 0)"
}
参数说明
佣金设置
| 参数 | 值 | 描述 |
|---|---|---|
commission | 0 | 不计算佣金 |
commission | 0.0003 | 买卖统一万三(0.03%) |
buy_commission | 0.0003 | 买入万三 |
sell_commission | 0.0013 | 卖出万三佣金 + 千一印花税(A股推荐) |
min_commission_amount | 5 | 每笔最低 5 元手续费 |
滑点设置
| 值 | 描述 |
|---|---|
0 | 不考虑滑点 |
0.001 | 千一滑点(买入价上浮 0.1%,卖出价下浮 0.1%) |
0.002 | 千二滑点 |
止损设置
| 值 | 描述 |
|---|---|
0 | 不设止损 |
0.05 | 5% 止损 |
0.1 | 10% 止损 |
仓位设置
| 值 | 描述 |
|---|---|
0.99 | 使用 99% 资金(预留部分应对价格波动) |
0.5 | 使用 50% 资金(半仓操作) |
0.3 | 使用 30% 资金(轻仓试探) |
上传 Excel 文件进行回测
上传包含 OHLCV 数据的 Excel 文件进行回测,无需依赖系统内置数据源,使用自有行情数据执行策略回测。POST /v1/quant/backtest/upload
该接口使用
multipart/form-data 格式上传,不支持 JSON body。curl -X POST https://api.reportify.cn/v1/quant/backtest/upload \
-H "Authorization: Bearer <token>" \
-F "file=@backtest_data.xlsx" \
-F 'params={"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 10))", "exit_formula": "CROSS(MA(CLOSE, 10), MA(CLOSE, 5))", "initial_cash": 100000, "commission": 0.0003}'
import requests
response = requests.post(
"https://api.reportify.cn/v1/quant/backtest/upload",
headers={"Authorization": "Bearer <token>"},
files={"file": open("backtest_data.xlsx", "rb")},
data={
"params": '{"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 10))", "exit_formula": "CROSS(MA(CLOSE, 10), MA(CLOSE, 5))", "initial_cash": 100000, "commission": 0.0003}'
}
)
print(response.json())
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('params', JSON.stringify({
entry_formula: 'CROSS(MA(CLOSE, 5), MA(CLOSE, 10))',
exit_formula: 'CROSS(MA(CLOSE, 10), MA(CLOSE, 5))',
initial_cash: 100000,
commission: 0.0003
}));
const response = await fetch('https://api.reportify.cn/v1/quant/backtest/upload', {
method: 'POST',
headers: { 'Authorization': 'Bearer <token>' },
body: formData
});
const data = await response.json();
Excel 文件格式要求
Excel 文件(
可选列:
自定义字段: 支持任意英文字段名(仅字母、数字、下划线,不能以数字开头),值为数字类型。例如
.xlsx 或 .xls 格式),包含回测所需的行情数据。必须包含的列:| 列名 | 类型 | 说明 |
|---|---|---|
date | 日期 | 交易日期 |
open | 数字 | 开盘价 |
high | 数字 | 最高价 |
low | 数字 | 最低价 |
close | 数字 | 收盘价 |
| 列名 | 类型 | 说明 |
|---|---|---|
symbol | 字符串 | 股票代码(多标的回测时必须提供,单标的时缺省为 "UPLOAD") |
volume | 数字 | 成交量 |
amount | 数字 | 成交额 |
pe、market_cap、pb_ratio 等。这些字段可在公式中直接引用。中文列名支持: 系统会自动将常见的中文列名映射为对应的英文列名:| 中文列名 | 映射到 |
|---|---|
| 日期 / 时间 / 交易日期 / 交易日 | date |
| 开盘 / 开盘价 | open |
| 最高 / 最高价 | high |
| 最低 / 最低价 | low |
| 收盘 / 收盘价 | close |
| 成交量 / 交易量 | volume |
| 成交额 / 交易额 | amount |
| 代码 / 股票代码 / 证券代码 | symbol |
自定义字段列名仍须为 ASCII 字符(英文字母、数字、下划线)。
请求参数 (params)
params 为表单字段,值为 JSON 字符串,支持以下参数(与标准回测接口一致,但不含 market/symbols/filter_formula/start_date/end_date):
指标与因子字典,用于预计算买卖信号或自定义字段
Show signal_factors 示例
Show signal_factors 示例
{
"ma5": "MA(CLOSE, 5)",
"ma10": "MA(CLOSE, 10)",
"rsi": "RSI(14)"
}
响应参数
响应格式与标准回测接口完全一致,参见上方 响应参数 章节。Excel 上传回测示例
单标的回测
Excel 文件包含单只股票数据(无需symbol 列):
| date | open | high | low | close | volume |
|---|---|---|---|---|---|
| 2024-01-02 | 10.50 | 10.80 | 10.40 | 10.75 | 5000000 |
| 2024-01-03 | 10.70 | 10.90 | 10.60 | 10.85 | 4500000 |
{
"entry_formula": "CROSS(MA(CLOSE, 5), MA(CLOSE, 10))",
"exit_formula": "CROSS(MA(CLOSE, 10), MA(CLOSE, 5))",
"initial_cash": 100000,
"commission": 0.0003
}
多标的回测
Excel 文件包含多只股票数据(必须有symbol 列):
| date | symbol | open | high | low | close | volume |
|---|---|---|---|---|---|---|
| 2024-01-02 | 000001 | 10.50 | 10.80 | 10.40 | 10.75 | 5000000 |
| 2024-01-02 | 600519 | 1800 | 1815 | 1790 | 1810 | 2000000 |
{
"entry_formula": "RSI(14) < 30",
"exit_formula": "RSI(14) > 70",
"position_size": 0.3,
"max_positions": 2
}
带自定义字段的回测
Excel 文件包含自定义基本面字段(如pe、pb):
| date | symbol | open | high | low | close | volume | pe | pb |
|---|---|---|---|---|---|---|---|---|
| 2024-01-02 | 000001 | 10.50 | 10.80 | 10.40 | 10.75 | 5000000 | 15.2 | 1.3 |
{
"entry_formula": "(CLOSE > MA(CLOSE, 20)) & (pe < 20)",
"exit_formula": "CLOSE < MA(CLOSE, 20)",
"initial_cash": 100000
}
注意事项
- 回测结果仅供参考,历史收益不代表未来表现
- 可通过
slippage参数模拟滑点,buy_commission/sell_commission分别设置买卖手续费以更贴近真实交易成本 - 建议使用合理的佣金和止损参数以更接近真实交易
- 如果不设置
exit_formula,则只有买入信号,卖出依赖止损或auto_close
Body
application/json
Backtest input parameters for strategy execution.
Response
Successful Response
Backtest result containing performance metrics and trade details.
