【精选】PHP将股票日K线数据转换为周K线数据
可以使用PHP来将股票日K线数据转换为周K线数据。具体实现方法如下:
获取日K线数据,例如从数据库中查询出所有日期的股票数据,保存在一个数组中,每个元素包含如下信息:
$day_data = [
['date' => '2021-10-01', 'open' => 11.2, 'high' => 12.4, 'low' => 10.8, 'close' => 11.6],
['date' => '2021-10-04', 'open' => 11.5, 'high' => 11.7, 'low' => 10.6, 'close' => 10.8],
['date' => '2021-10-05', 'open' => 10.7, 'high' => 11.2, 'low' => 10.5, 'close' => 11.0],
['date' => '2021-10-06', 'open' => 11.3, 'high' => 11.6, 'low' => 10.8, 'close' => 11.5],
// ...
];
对日K线数据按周进行分组,即将每周的数据合并成一个元素。对于每周末休市的数据,可以简单判断日期是否连续,不连续则为1周数据
这样就可以将日K线数据转换为周K线数据了。转换后的数据类似如下格式:
$week_data = [
'2021-10-04' => [
'week_start' => '2021-10-04',
'open' => 11.2,
'high' => 12.4,
'low' => 10.6,
'close' => 10.8,
'rate' => null,
'volume' => 123456,
],
'2021-10-11' => [
'week_start' => '2021-10-11',
'open' => 10.7,
'high' => 11.6,
'low' => 10.5,
'close' => 11.5,
'rate' => 0.06481579,
'volume' => 98765,
],
// ...
];
PHP
其中,每个元素的键名为该周的起始日期。