更新:添加每日和每月数据选项,修改相关URL参数,更新JavaScript文件,增加股票指数历史数据获取功能

This commit is contained in:
杨水淼
2025-07-09 07:36:50 +08:00
parent 5d135b24f1
commit 43a202c789
12 changed files with 734 additions and 53 deletions
+98
View File
@@ -1,4 +1,5 @@
<?php
include_once __DIR__."/functions.inc.php";
/**
* 获取PE_TTM,PB,PS等历史数据
* @param $ts_code ts code
@@ -312,4 +313,101 @@ function yearToname($year){
return substr($year,4,4);
}
}
// 函数一:获取股票代码ts_code对应的历史pe_ttm、pb或ps
function getStockHistoryDataByTS($ts_code, $day_st, $day_end = null, $item){
//global $token;
// 参数校验
if (empty($ts_code) || empty($day_st) || !in_array(strtolower($item), ['pe_ttm', 'pb', 'ps'])) {
return "参数错误:股票代码、起始日期不能为空,查询项目必须为pe_ttm、pb或ps。";
}
// 处理日期参数
$params = [
"ts_code" => $ts_code,
"start_date" => $day_st,
"end_date" => $day_end ?? date("Y-m-d") // 如果为空,默认为今天
];
// 调用TuShare API获取数据
$data = callTushareApi('daily_basic', $params);
return true ;
if (empty($data)) {
return "未获取到数据,请检查参数或网络连接。";
}
// 提取指定项目的数据
$result = [];
$tDate = [];
$itemValues = [];
foreach ($data as $row) {
$tDate[] = $row['trade_date'];
$itemValues[] = $row[$item];
}
// 按日期排序(从远到近,默认已经是按日期排序的)
$tDate = array_reverse($tDate); // 默认TuShare按日期顺序是近到远,所以反转得到远到近
$itemValues = array_reverse($itemValues);
// 计算统计值
$dataMax = max($itemValues);
$dataMin = min($itemValues);
$dataAvg = array_sum($itemValues) / count($itemValues);
$dataLast = end($itemValues);
// 返回结果
return [
'tDate' => $tDate,
'data' => $itemValues,
'data_max' => $dataMax,
'data_min' => $dataMin,
'data_avg' => $dataAvg,
'data_last' => $dataLast
];
}
// 函数二:获取指数趋势历史数据
function getIndexHistoryDataByTS($ts_code, $day_st, $day_end = null)
{
global $token;
// 参数校验
if (empty($ts_code) || empty($day_st)) {
return "参数错误:股票代码和起始日期不能为空。";
}
// 处理日期参数
$params = [
"ts_code" => $ts_code,
"start_date" => $day_st,
"end_date" => $day_end ?? date("Y-m-d") // 如果为空,默认为今天
];
// 调用TuShare API获取数据
$data = callTushareApi('index_dailybasic', $params);
return true ;
if (empty($data)) {
return "未获取到数据,请检查参数或网络连接。";
}
// 提取指数点数值
$tDate = [];
$closeValues = [];
foreach ($data as $row) {
$tDate[] = $row['trade_date'];
$closeValues[] = $row['close']; // 假设close字段是指数点数值
}
// 按日期排序
$tDate = array_reverse($tDate);
$closeValues = array_reverse($closeValues);
// 返回结果
return [
'tDate' => $tDate,
'data' => $closeValues
];
}
?>