首页 开发文档 定投收益计算

定投收益计算

定投收益测算。按指定的投入频率(周/月/季/年)把每期投入折算成对应的复利周期,逐期累加计算期末市值,同时给出累计投入本金、总收益、总收益率与年化收益率。支持期初一次性投入与每期定投组合,也可填入负收益率做压力测试。市场有风险,测算结果不构成投资建议。

请求信息
请求地址 https://cx4.cn/openApi/finance/invest 复制
请求方式 GET / POST
接口标识 finance_invest
鉴权方式 必须
四种方式任选其一:Header「X-Api-Key」/ Header「Authorization: Bearer 你的key」/ URL 参数「?key=你的key」/ POST 参数「key」
请求参数
参数名 类型 必填 说明 示例
monthly float 否 每期投入金额(也可用 amount 传),与 initial 至少有一个大于 0 1000
months int 是 投资期数,1-1200 120
annual_rate float 否 预期年化收益率百分比,-100 到 200(也可用 rate 传) 8
initial float 否 期初一次性投入 10000
frequency string 否 投入频率:week 周 / month 月 / quarter 季 / year 年 month
返回参数

下表字段位于返回结构的 data 内;外层 code / message 为全平台统一格式,不在此重复列出。

参数名 类型 说明 示例
ok bool 是否成功 true
frequency string 投入频率 month
periods int 总期数 120
per_period float 每期投入 1000
initial float 期初投入 0
annual_rate float 年化收益率百分比 8
invested float 累计投入本金 120000
final_value float 期末市值 183000
profit float 总收益 63000
return_rate float 总收益率百分比 52.5
annualized_return float 年化收益率百分比 8
note string 说明 定投收益按复利计算,实际收益随市场波动
返回示例

成功

{
    "code": 200,
    "message": "操作成功",
    "data": {
        "ok": true,
        "frequency": "month",
        "periods": 120,
        "per_period": 1000,
        "initial": 0,
        "annual_rate": 8,
        "invested": 120000,
        "final_value": 182946.04,
        "profit": 62946.04,
        "return_rate": 52.46,
        "annualized_return": 7.88,
        "note": "annualized_return 为定投内部收益率(按投入时点加权),与设定的预期年化可能略有差异"
    }
}

失败

{
    "code": 400,
    "message": "每期投入或期初投入至少有一个大于 0",
    "data": null
}
平台通用状态码
状态码 说明
200调用成功
400缺少必填参数或参数格式有误
401密钥缺失或无效
402余额不足,请充值后重试
403未开通该接口,请在本页开通后调用
404接口不存在或已下架
429触发限流,请稍后重试
500接口调用失败(已扣款的会自动退款)
调用示例

cURL 复制

curl -X POST "https://cx4.cn/openApi/finance/invest" -H "X-Api-Key: 你的SecretKey" -d "monthly=1000&months=120&annual_rate=8&initial=10000&frequency=month"

JavaScript 复制

fetch('https://cx4.cn/openApi/finance/invest', {
  method: 'POST',
  headers: {
    'X-Api-Key': '你的SecretKey',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: 'monthly=1000&months=120&annual_rate=8&initial=10000&frequency=month'
})
.then(res => res.json())
.then(data => console.log(data));

PHP 复制

post('https://cx4.cn/openApi/finance/invest', [
    'headers' => ['X-Api-Key' => '你的SecretKey'],
    'form_params' => ['monthly' => '1000', 'months' => '120', 'annual_rate' => '8', 'initial' => '10000', 'frequency' => 'month'],
]);
echo $response->getBody();

Java 复制

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://cx4.cn/openApi/finance/invest"))
    .header("X-Api-Key", "你的SecretKey")
    .header("Content-Type", "application/x-www-form-urlencoded")
    .POST(HttpRequest.BodyPublishers.ofString("monthly=1000&months=120&annual_rate=8&initial=10000&frequency=month"))
    .build();
HttpResponse response = client.send(
        request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Python 复制

import requests

res = requests.post('https://cx4.cn/openApi/finance/invest',
    headers={'X-Api-Key': '你的SecretKey'},
    data={'monthly': '1000', 'months': '120', 'annual_rate': '8', 'initial': '10000', 'frequency': 'month'})
print(res.json())
在线测试 (使用你的密钥真实调用,计费与正常调用一致)

登录后可在线测试: 立即登录

计费说明

¥0.0010

每次调用


每日免费额度:100 次

累计调用:6 次


快速上手

1. 注册并完成邮箱验证:立即注册

2. 登录后复制 Secret Key:去登录

3. 点击右侧「开通接口」按钮

4. 用下方「在线测试」一键体验,或复制下方示例代码直接调用