滑动 / 移动平均 (Moving Average, MA), 又称移动平均线,是技术分析中一种分析时间序列的常用工具。
常见的移动平均包括简单移动平均 (Simple Moving Average, SMA)、权重移动平均 (Weight Moving Average, WMA) 和指数移动平均 (Exponential Moving Average,EMA)等。
它们的主要区别是计算平均值的公式不同。
安装 MySQL 8.0.18
安装 Navicat Premium
由于 MySQL 在 8.0 以后的版本才开始支持窗口函数 (OLAP),所以推荐安装 8.0.18 及以上版本,本文不过多涉及 MySQL 的安装,下载官方的安装包安装即可。
https://www.mysql.com/
安装 Navicat Premium 是为了更加方便编写 SQL 查询语句,否则需要在命令行下编写 SQL 语句。
本篇文章以梳理思路,演示方法为主,所以直接使用以下简单的测试数据即可。
正常情况下的股票历年数据可通过 API 等接口获取。
测试数据:
-- 删除 example_stock 数据表 (如果存在才删除)。
DROP TABLE IF EXISTS example_stock;
-- 创建 example_stock 数据表 (如果不存在才创建)。
CREATE TABLE IF NOT EXISTS example_stock(stk_date DATE, -- Stock Date
stk_code VARCHAR(12), -- Stock Code
stk_open DOUBLE(16,4), -- Stock Open
stk_high DOUBLE(16,4), -- Stock High
stk_low DOUBLE(16,4), -- Stock Low
stk_close DOUBLE(16,4), -- Stock Close
stk_pre_close DOUBLE(16,4), -- Stock Previous Close
stk_change DOUBLE(16,4), -- Stock Change
stk_volume DOUBLE(16,4) -- Stock Volume
);
-- 插入数据到 example_stock 数据表。
INSERT INTO example_stock VALUES(str_to_date('2015-12-31','%Y-%m-%d'), 'SH.000422', 7.93, 7.95, 7.76, 7.77, 7.93, -0.020177, 13915200);
INSERT INTO example_stock VALUES(str_to_date('2015-12-30','%Y-%m-%d'), 'SH.000422', 7.86, 7.93, 7.75, 7.93, 7.84, 0.01148, 16755900);
INSERT INTO example_stock VALUES(str_to_date('2015-12-29','%Y-%m-%d'), 'SH.000422', 7.72, 7.85, 7.69, 7.84, 7.71, 0.016861, 14263800);
INSERT INTO example_stock VALUES(str_to_date('2015-12-28','%Y-%m-%d'), 'SH.000422', 8.03, 8.08, 7.7, 7.71, 8.03, -0.039851, 27672800);
INSERT INTO example_stock VALUES(str_to_date('2015-12-25','%Y-%m-%d'), 'SH.000422', 8.03, 8.05, 7.93, 8.03, 7.99, 0.005006, 18974000);
INSERT INTO example_stock VALUES(str_to_date('2015-12-24','%Y-%m-%d'), 'SH.000422', 7.93, 8.16, 7.87, 7.99, 7.92, 0.008838, 23781900);
INSERT INTO example_stock VALUES(str_to_date('2015-12-23','%Y-%m-%d'), 'SH.000422', 7.97, 8.11, 7.88, 7.92, 7.89, 0.003802, 38033600);
INSERT INTO example_stock VALUES(str_to_date('2015-12-22','%Y-%m-%d'), 'SH.000422', 7.86, 7.93, 7.76, 7.89, 7.83, 0.007663, 24178700);
INSERT INTO example_stock VALUES(str_to_date('2015-12-21','%Y-%m-%d'), 'SH.000422', 7.59, 7.89, 7.56, 7.83, 7.63, 0.026212, 27633600);
INSERT INTO example_stock VALUES(str_to_date('2015-12-18','%Y-%m-%d'), 'SH.000422', 7.71, 7.74, 7.57, 7.63, 7.74, -0.014212, 22234900);
INSERT INTO example_stock VALUES(str_to_date('2015-12-17','%Y-%m-%d'), 'SH.000422', 7.58, 7.75, 7.57, 7.74, 7.55, 0.025166, 25188400);
INSERT INTO example_stock VALUES(str_to_date('2015-12-16','%Y-%m-%d'), 'SH.000422', 7.57, 7.62, 7.53, 7.55, 7.55, 0, 18601600);
INSERT INTO example_stock VALUES(str_to_date('2015-12-15','%Y-%m-%d'), 'SH.000422', 7.63, 7.66, 7.52, 7.55, 7.62, -0.009186, 23256600);
INSERT INTO example_stock VALUES(str_to_date('2015-12-14','%Y-%m-%d'), 'SH.000422', 7.4, 7.64, 7.36, 7.62, 7.51, 0.014647, 18860100);
INSERT INTO example_stock VALUES(str_to_date('2015-12-11','%Y-%m-%d'), 'SH.000422', 7.65, 7.7, 7.41, 7.51, 7.67, -0.02086, 18385900);
INSERT INTO example_stock VALUES(str_to_date('2015-12-10','%Y-%m-%d'), 'SH.000422', 7.78, 7.87, 7.65, 7.67, 7.83, -0.020434, 17931900);
INSERT INTO example_stock VALUES(str_to_date('2015-12-09','%Y-%m-%d'), 'SH.000422', 7.76, 8, 7.75, 7.83, 7.77, 0.007722, 22569700);
INSERT INTO example_stock VALUES(str_to_date('2015-12-08','%Y-%m-%d'), 'SH.000422', 8.08, 8.18, 7.76, 7.77, 8.24, -0.057039, 32948200);
INSERT INTO example_stock VALUES(str_to_date('2015-12-07','%Y-%m-%d'), 'SH.000422', 8.12, 8.39, 7.94, 8.24, 8.23, 0.001215, 57993100);
INSERT INTO example_stock VALUES(str_to_date('2015-12-04','%Y-%m-%d'), 'SH.000422', 7.85, 8.48, 7.8, 8.23, 7.92, 0.039141, 89881900);
INSERT INTO example_stock VALUES(str_to_date('2015-12-03','%Y-%m-%d'), 'SH.000422', 7.42, 8.09, 7.38, 7.92, 7.43, 0.065949, 40777500);
INSERT INTO example_stock VALUES(str_to_date('2015-12-02','%Y-%m-%d'), 'SH.000422', 7.35, 7.48, 7.2, 7.43, 7.36, 0.009511, 14337600);
INSERT INTO example_stock VALUES(str_to_date('2015-12-01','%Y-%m-%d'), 'SH.000422', 7.28, 7.39, 7.23, 7.36, 7.33, 0.004093, 11050700);
INSERT INTO example_stock VALUES(str_to_date('2015-11-30','%Y-%m-%d'), 'SH.000422', 7.18, 7.36, 6.95, 7.33, 7.11, 0.030942, 18247500);
INSERT INTO example_stock VALUES(str_to_date('2015-11-27','%Y-%m-%d'), 'SH.000422', 7.59, 7.59, 6.95, 7.11, 7.6, -0.064474, 24846700);
INSERT INTO example_stock VALUES(str_to_date('2015-11-26','%Y-%m-%d'), 'SH.000422', 7.63, 7.73, 7.58, 7.6, 7.63, -0.003932, 22299800);
INSERT INTO example_stock VALUES(str_to_date('2015-11-25','%Y-%m-%d'), 'SH.000422', 7.56, 7.64, 7.51, 7.63, 7.59, 0.00527, 18782900);
INSERT INTO example_stock VALUES(str_to_date('2015-11-24','%Y-%m-%d'), 'SH.000422', 7.6, 7.63, 7.48, 7.59, 7.62, -0.003937, 13348200);
INSERT INTO example_stock VALUES(str_to_date('2015-11-23','%Y-%m-%d'), 'SH.000422', 7.59, 7.72, 7.55, 7.62, 7.61, 0.001314, 25505000);
INSERT INTO example_stock VALUES(str_to_date('2015-11-20','%Y-%m-%d'), 'SH.000422', 7.59, 7.71, 7.53, 7.61, 7.59, 0.002635, 25389100);
INSERT INTO example_stock VALUES(str_to_date('2015-11-19','%Y-%m-%d'), 'SH.000422', 7.45, 7.62, 7.41, 7.59, 7.39, 0.027064, 34691700);
INSERT INTO example_stock VALUES(str_to_date('2015-11-18','%Y-%m-%d'), 'SH.000422', 7.53, 7.54, 7.38, 7.39, 7.51, -0.015979, 12725000);
INSERT INTO example_stock VALUES(str_to_date('2015-11-17','%Y-%m-%d'), 'SH.000422', 7.53, 7.63, 7.44, 7.51, 7.5, 0.001333, 25714500);
INSERT INTO example_stock VALUES(str_to_date('2015-11-16','%Y-%m-%d'), 'SH.000422', 7.27, 7.52, 7.24, 7.5, 7.38, 0.01626, 14572000);
INSERT INTO example_stock VALUES(str_to_date('2015-11-13','%Y-%m-%d'), 'SH.000422', 7.49, 7.55, 7.36, 7.38, 7.54, -0.02122, 26214400);
INSERT INTO example_stock VALUES(str_to_date('2015-11-12','%Y-%m-%d'), 'SH.000422', 7.65, 7.68, 7.49, 7.54, 7.61, -0.009198, 23794800);
INSERT INTO example_stock VALUES(str_to_date('2015-11-11','%Y-%m-%d'), 'SH.000422', 7.57, 7.64, 7.52, 7.61, 7.57, 0.005284, 23445900);
INSERT INTO example_stock VALUES(str_to_date('2015-11-10','%Y-%m-%d'), 'SH.000422', 7.51, 7.61, 7.45, 7.57, 7.55, 0.002649, 22427700);
INSERT INTO example_stock VALUES(str_to_date('2015-11-09','%Y-%m-%d'), 'SH.000422', 7.51, 7.62, 7.45, 7.55, 7.53, 0.002656, 29959500);
INSERT INTO example_stock VALUES(str_to_date('2015-11-06','%Y-%m-%d'), 'SH.000422', 7.47, 7.53, 7.37, 7.53, 7.45, 0.010738, 33273100);
INSERT INTO example_stock VALUES(str_to_date('2015-11-05','%Y-%m-%d'), 'SH.000422', 7.34, 7.54, 7.32, 7.45, 7.37, 0.010855, 36330200);
INSERT INTO example_stock VALUES(str_to_date('2015-11-04','%Y-%m-%d'), 'SH.000422', 7.1, 7.38, 7.07, 7.37, 7.05, 0.04539, 31260800);
INSERT INTO example_stock VALUES(str_to_date('2015-11-03','%Y-%m-%d'), 'SH.000422', 7.08, 7.13, 7.02, 7.05, 7.06, -0.001416, 13412400);
INSERT INTO example_stock VALUES(str_to_date('2015-11-02','%Y-%m-%d'), 'SH.000422', 7.11, 7.26, 7.05, 7.06, 7.26, -0.027548, 15142100);
INSERT INTO example_stock VALUES(str_to_date('2015-10-30','%Y-%m-%d'), 'SH.000422', 7.22, 7.38, 7.1, 7.26, 7.24, 0.002762, 20490200);
INSERT INTO example_stock VALUES(str_to_date('2015-10-29','%Y-%m-%d'), 'SH.000422', 7.27, 7.33, 7.16, 7.24, 7.16, 0.011173, 23098500);
INSERT INTO example_stock VALUES(str_to_date('2015-10-28','%Y-%m-%d'), 'SH.000422', 7.32, 7.4, 7.09, 7.16, 7.42, -0.03504, 31938500);
INSERT INTO example_stock VALUES(str_to_date('2015-10-27','%Y-%m-%d'), 'SH.000422', 7.21, 7.48, 7.08, 7.42, 7.18, 0.033426, 51769300);
INSERT INTO example_stock VALUES(str_to_date('2015-10-26','%Y-%m-%d'), 'SH.000422', 7.2, 7.25, 7.01, 7.18, 7.17, 0.001395, 33077800);
INSERT INTO example_stock VALUES(str_to_date('2015-10-23','%Y-%m-%d'), 'SH.000422', 6.84, 7.22, 6.81, 7.17, 6.8, 0.054412, 42351500);
INSERT INTO example_stock VALUES(str_to_date('2015-10-22','%Y-%m-%d'), 'SH.000422', 6.68, 6.81, 6.64, 6.8, 6.65, 0.022556, 18503800);
INSERT INTO example_stock VALUES(str_to_date('2015-10-21','%Y-%m-%d'), 'SH.000422', 7.08, 7.11, 6.61, 6.65, 7.09, -0.062059, 35365300);
INSERT INTO example_stock VALUES(str_to_date('2015-10-20','%Y-%m-%d'), 'SH.000422', 7, 7.09, 6.94, 7.09, 7.03, 0.008535, 21972900);
INSERT INTO example_stock VALUES(str_to_date('2015-10-19','%Y-%m-%d'), 'SH.000422', 7.09, 7.13, 6.92, 7.03, 7.08, -0.007062, 28068800);
INSERT INTO example_stock VALUES(str_to_date('2015-10-16','%Y-%m-%d'), 'SH.000422', 6.97, 7.08, 6.91, 7.08, 6.93, 0.021645, 35584700);
INSERT INTO example_stock VALUES(str_to_date('2015-10-15','%Y-%m-%d'), 'SH.000422', 6.77, 6.94, 6.75, 6.93, 6.77, 0.023634, 28412700);
INSERT INTO example_stock VALUES(str_to_date('2015-10-14','%Y-%m-%d'), 'SH.000422', 6.87, 6.94, 6.74, 6.77, 6.89, -0.017417, 24445500);
INSERT INTO example_stock VALUES(str_to_date('2015-10-13','%Y-%m-%d'), 'SH.000422', 6.86, 6.96, 6.8, 6.89, 6.88, 0.001453, 25771900);
INSERT INTO example_stock VALUES(str_to_date('2015-10-12','%Y-%m-%d'), 'SH.000422', 6.62, 6.91, 6.58, 6.88, 6.61, 0.040847, 33254300);
INSERT INTO example_stock VALUES(str_to_date('2015-10-09','%Y-%m-%d'), 'SH.000422', 6.54, 6.65, 6.45, 6.61, 6.54, 0.010703, 16635900);
INSERT INTO example_stock VALUES(str_to_date('2015-10-08','%Y-%m-%d'), 'SH.000422', 6.45, 6.7, 6.37, 6.54, 6.26, 0.044728, 16931000);
INSERT INTO example_stock VALUES(str_to_date('2015-09-30','%Y-%m-%d'), 'SH.000422', 6.25, 6.3, 6.22, 6.26, 6.23, 0.004815, 6579090 );
INSERT INTO example_stock VALUES(str_to_date('2015-09-29','%Y-%m-%d'), 'SH.000422', 6.3, 6.32, 6.18, 6.23, 6.4, -0.026562, 8072900 );
INSERT INTO example_stock VALUES(str_to_date('2015-09-28','%Y-%m-%d'), 'SH.000422', 6.35, 6.42, 6.25, 6.4, 6.34, 0.009464, 7922890 );
INSERT INTO example_stock VALUES(str_to_date('2015-09-25','%Y-%m-%d'), 'SH.000422', 6.51, 6.56, 6.25, 6.34, 6.53, -0.029096, 11298800);
INSERT INTO example_stock VALUES(str_to_date('2015-09-24','%Y-%m-%d'), 'SH.000422', 6.48, 6.56, 6.45, 6.53, 6.45, 0.012403, 10180900);
INSERT INTO example_stock VALUES(str_to_date('2015-09-23','%Y-%m-%d'), 'SH.000422', 6.51, 6.6, 6.41, 6.45, 6.67, -0.032984, 14294100);
INSERT INTO example_stock VALUES(str_to_date('2015-09-22','%Y-%m-%d'), 'SH.000422', 6.58, 6.73, 6.54, 6.67, 6.58, 0.013678, 20970200);
INSERT INTO example_stock VALUES(str_to_date('2015-09-21','%Y-%m-%d'), 'SH.000422', 6.34, 6.61, 6.29, 6.58, 6.44, 0.021739, 15295900);
INSERT INTO example_stock VALUES(str_to_date('2015-09-18','%Y-%m-%d'), 'SH.000422', 6.52, 6.58, 6.3, 6.44, 6.44, 0, 14924700);
INSERT INTO example_stock VALUES(str_to_date('2015-09-17','%Y-%m-%d'), 'SH.000422', 6.59, 6.76, 6.43, 6.44, 6.68, -0.035928, 17523900);
INSERT INTO example_stock VALUES(str_to_date('2015-09-16','%Y-%m-%d'), 'SH.000422', 6.21, 6.76, 6.17, 6.68, 6.15, 0.086179, 17662300);
INSERT INTO example_stock VALUES(str_to_date('2015-09-15','%Y-%m-%d'), 'SH.000422', 6.24, 6.38, 6.05, 6.15, 6.26, -0.017572, 13771200);
INSERT INTO example_stock VALUES(str_to_date('2015-09-14','%Y-%m-%d'), 'SH.000422', 6.89, 6.95, 6.18, 6.26, 6.87, -0.088792, 18559600);
INSERT INTO example_stock VALUES(str_to_date('2015-09-11','%Y-%m-%d'), 'SH.000422', 6.87, 6.96, 6.77, 6.87, 6.84, 0.004386, 9486290 );
INSERT INTO example_stock VALUES(str_to_date('2015-09-10','%Y-%m-%d'), 'SH.000422', 6.95, 7.01, 6.76, 6.84, 7.06, -0.031161, 15229100);
INSERT INTO example_stock VALUES(str_to_date('2015-09-09','%Y-%m-%d'), 'SH.000422', 6.9, 7.09, 6.86, 7.06, 6.88, 0.026163, 25325600);
INSERT INTO example_stock VALUES(str_to_date('2015-09-08','%Y-%m-%d'), 'SH.000422', 6.65, 6.91, 6.55, 6.88, 6.62, 0.039275, 15609100);
INSERT INTO example_stock VALUES(str_to_date('2015-09-07','%Y-%m-%d'), 'SH.000422', 6.5, 6.81, 6.5, 6.62, 6.38, 0.037618, 15602600);
INSERT INTO example_stock VALUES(str_to_date('2015-09-02','%Y-%m-%d'), 'SH.000422', 6.45, 6.88, 6.3, 6.38, 6.74, -0.053412, 19480100);
INSERT INTO example_stock VALUES(str_to_date('2015-09-01','%Y-%m-%d'), 'SH.000422', 6.88, 6.99, 6.67, 6.74, 6.81, -0.010279, 22576700);
INSERT INTO example_stock VALUES(str_to_date('2015-08-31','%Y-%m-%d'), 'SH.000422', 6.9, 6.97, 6.71, 6.81, 7.07, -0.036775, 16069600);
INSERT INTO example_stock VALUES(str_to_date('2015-08-28','%Y-%m-%d'), 'SH.000422', 6.75, 7.08, 6.71, 7.07, 6.67, 0.05997, 23330800);
INSERT INTO example_stock VALUES(str_to_date('2015-08-27','%Y-%m-%d'), 'SH.000422', 6.53, 6.67, 6.34, 6.67, 6.32, 0.05538, 19627900);
INSERT INTO example_stock VALUES(str_to_date('2015-08-26','%Y-%m-%d'), 'SH.000422', 6.31, 6.77, 6.09, 6.32, 6.25, 0.0112, 26190200);
INSERT INTO example_stock VALUES(str_to_date('2015-08-25','%Y-%m-%d'), 'SH.000422', 6.4, 6.77, 6.25, 6.25, 6.94, -0.099424, 25778600);
INSERT INTO example_stock VALUES(str_to_date('2015-08-24','%Y-%m-%d'), 'SH.000422', 7.49, 7.49, 6.94, 6.94, 7.71, -0.09987, 31949900);
INSERT INTO example_stock VALUES(str_to_date('2015-08-21','%Y-%m-%d'), 'SH.000422', 8, 8.11, 7.6, 7.71, 8.17, -0.056304, 28144800);
INSERT INTO example_stock VALUES(str_to_date('2015-08-20','%Y-%m-%d'), 'SH.000422', 8.38, 8.56, 8.14, 8.17, 8.53, -0.042204, 27764200);
INSERT INTO example_stock VALUES(str_to_date('2015-08-19','%Y-%m-%d'), 'SH.000422', 7.73, 8.57, 7.72, 8.53, 7.96, 0.071608, 45619900);
INSERT INTO example_stock VALUES(str_to_date('2015-08-18','%Y-%m-%d'), 'SH.000422', 8.81, 8.86, 7.92, 7.96, 8.8, -0.095455, 49105500);
INSERT INTO example_stock VALUES(str_to_date('2015-08-17','%Y-%m-%d'), 'SH.000422', 8.49, 8.83, 8.42, 8.8, 8.52, 0.032864, 42096900);
INSERT INTO example_stock VALUES(str_to_date('2015-08-14','%Y-%m-%d'), 'SH.000422', 8.48, 8.65, 8.43, 8.52, 8.44, 0.009479, 35985000);
INSERT INTO example_stock VALUES(str_to_date('2015-08-13','%Y-%m-%d'), 'SH.000422', 8.2, 8.45, 8.15, 8.44, 8.24, 0.024272, 26019600);
INSERT INTO example_stock VALUES(str_to_date('2015-08-12','%Y-%m-%d'), 'SH.000422', 8.38, 8.48, 8.21, 8.24, 8.48, -0.028302, 30960700);
INSERT INTO example_stock VALUES(str_to_date('2015-08-11','%Y-%m-%d'), 'SH.000422', 8.41, 8.68, 8.32, 8.48, 8.49, -0.001178, 42343900);
INSERT INTO example_stock VALUES(str_to_date('2015-08-10','%Y-%m-%d'), 'SH.000422', 8.28, 8.58, 8.18, 8.49, 8.21, 0.034105, 36071600);
INSERT INTO example_stock VALUES(str_to_date('2015-08-07','%Y-%m-%d'), 'SH.000422', 8.15, 8.28, 8.08, 8.21, 8.07, 0.017348, 22599800);
INSERT INTO example_stock VALUES(str_to_date('2015-08-06','%Y-%m-%d'), 'SH.000422', 7.88, 8.21, 7.8, 8.07, 8.03, 0.004981, 17546700);
INSERT INTO example_stock VALUES(str_to_date('2015-08-05','%Y-%m-%d'), 'SH.000422', 8.1, 8.35, 7.95, 8.03, 8.18, -0.018337, 23875500);
语句释义:
AVG(stk_close): 求平均值的 AVG 可作为聚合函数使用,表示将所有收盘价 (Close) 汇总成一个平均值。
OVER: OVER 表明 AVG() 是一个窗口函数。OVER 后的括号内为空,表示将所有数据作为一个分区进行汇总。
(PARTITION BY stk_code): 表示按 stk_code 也就是股票代码分区 (PARTITION BY),如果有多个股票,则窗口会分别在各自分区内进行滑动、取值。
(ORDER BY stk_date ASC): 表示按 stk_date 进行升序 (ASC) 排序 (ORDER BY),因为移动平均是按时间顺序计算的。
(ROWS BETWEEN 29 PRECEDING AND CURRENT ROW): 表示窗口大小 (Frame Clause),这里表示取前 29 行到当前 (Current) 行,也即共 30 行。
SELECT
stk_date,
stk_code,
stk_close,
AVG(stk_close) OVER(PARTITION BY stk_code ORDER BY stk_date ASC ROWS BETWEEN 29 PRECEDING AND CURRENT ROW) as SMA30
FROM
example_stock
输出:
+------------+------------+----------+-----------+
|stk_date |stk_code |stk_close |SMA30 |
+------------+------------+----------+-----------+
|2015-08-05 |SH.000422 |8.0300 |8.03000000 |
|2015-08-06 |SH.000422 |8.0700 |8.05000000 |
|2015-08-07 |SH.000422 |8.2100 |8.10333333 |
|2015-08-10 |SH.000422 |8.4900 |8.20000000 |
|2015-08-11 |SH.000422 |8.4800 |8.25600000 |
|2015-08-12 |SH.000422 |8.2400 |8.25333333 |
|2015-08-13 |SH.000422 |8.4400 |8.28000000 |
|2015-08-14 |SH.000422 |8.5200 |8.31000000 |
|2015-08-17 |SH.000422 |8.8000 |8.36444444 |
|2015-08-18 |SH.000422 |7.9600 |8.32400000 |
|2015-08-19 |SH.000422 |8.5300 |8.34272727 |
|2015-08-20 |SH.000422 |8.1700 |8.32833333 |
|2015-08-21 |SH.000422 |7.7100 |8.28076923 |
|2015-08-24 |SH.000422 |6.9400 |8.18500000 |
|2015-08-25 |SH.000422 |6.2500 |8.05600000 |
|2015-08-26 |SH.000422 |6.3200 |7.94750000 |
|2015-08-27 |SH.000422 |6.6700 |7.87235294 |
|2015-08-28 |SH.000422 |7.0700 |7.82777778 |
|2015-08-31 |SH.000422 |6.8100 |7.77421053 |
|2015-09-01 |SH.000422 |6.7400 |7.72250000 |
|2015-09-02 |SH.000422 |6.3800 |7.65857143 |
|2015-09-07 |SH.000422 |6.6200 |7.61136364 |
|2015-09-08 |SH.000422 |6.8800 |7.57956522 |
|2015-09-09 |SH.000422 |7.0600 |7.55791667 |
|2015-09-10 |SH.000422 |6.8400 |7.52920000 |
|2015-09-11 |SH.000422 |6.8700 |7.50384615 |
|2015-09-14 |SH.000422 |6.2600 |7.45777778 |
|2015-09-15 |SH.000422 |6.1500 |7.41107143 |
|2015-09-16 |SH.000422 |6.6800 |7.38586207 |
|2015-09-17 |SH.000422 |6.4400 |7.35433333 |
|2015-09-18 |SH.000422 |6.4400 |7.30133333 |
|2015-09-21 |SH.000422 |6.5800 |7.25166667 |
|2015-09-22 |SH.000422 |6.6700 |7.20033333 |
|2015-09-23 |SH.000422 |6.4500 |7.13233333 |
|2015-09-24 |SH.000422 |6.5300 |7.06733333 |
|2015-09-25 |SH.000422 |6.3400 |7.00400000 |
|2015-09-28 |SH.000422 |6.4000 |6.93600000 |
|2015-09-29 |SH.000422 |6.2300 |6.85966667 |
|2015-09-30 |SH.000422 |6.2600 |6.77500000 |
|2015-10-08 |SH.000422 |6.5400 |6.72766667 |
|2015-10-09 |SH.000422 |6.6100 |6.66366667 |
|2015-10-12 |SH.000422 |6.8800 |6.62066667 |
|2015-10-13 |SH.000422 |6.8900 |6.59333333 |
|2015-10-14 |SH.000422 |6.7700 |6.58766667 |
|2015-10-15 |SH.000422 |6.9300 |6.61033333 |
|2015-10-16 |SH.000422 |7.0800 |6.63566667 |
|2015-10-19 |SH.000422 |7.0300 |6.64766667 |
|2015-10-20 |SH.000422 |7.0900 |6.64833333 |
|2015-10-21 |SH.000422 |6.6500 |6.64300000 |
|2015-10-22 |SH.000422 |6.8000 |6.64500000 |
|2015-10-23 |SH.000422 |7.1700 |6.67133333 |
|2015-10-26 |SH.000422 |7.1800 |6.69000000 |
|2015-10-27 |SH.000422 |7.4200 |6.70800000 |
|2015-10-28 |SH.000422 |7.1600 |6.71133333 |
|2015-10-29 |SH.000422 |7.2400 |6.72466667 |
|2015-10-30 |SH.000422 |7.2600 |6.73766667 |
|2015-11-02 |SH.000422 |7.0600 |6.76433333 |
|2015-11-03 |SH.000422 |7.0500 |6.79433333 |
|2015-11-04 |SH.000422 |7.3700 |6.81733333 |
|2015-11-05 |SH.000422 |7.4500 |6.85100000 |
|2015-11-06 |SH.000422 |7.5300 |6.88733333 |
|2015-11-09 |SH.000422 |7.5500 |6.91966667 |
|2015-11-10 |SH.000422 |7.5700 |6.94966667 |
|2015-11-11 |SH.000422 |7.6100 |6.98833333 |
|2015-11-12 |SH.000422 |7.5400 |7.02200000 |
|2015-11-13 |SH.000422 |7.3800 |7.05666667 |
|2015-11-16 |SH.000422 |7.5000 |7.09333333 |
|2015-11-17 |SH.000422 |7.5100 |7.13600000 |
|2015-11-18 |SH.000422 |7.3900 |7.17366667 |
|2015-11-19 |SH.000422 |7.5900 |7.20866667 |
|2015-11-20 |SH.000422 |7.6100 |7.24200000 |
|2015-11-23 |SH.000422 |7.6200 |7.26666667 |
|2015-11-24 |SH.000422 |7.5900 |7.29000000 |
|2015-11-25 |SH.000422 |7.6300 |7.31866667 |
|2015-11-26 |SH.000422 |7.6000 |7.34100000 |
|2015-11-27 |SH.000422 |7.1100 |7.34200000 |
|2015-11-30 |SH.000422 |7.3300 |7.35200000 |
|2015-12-01 |SH.000422 |7.3600 |7.36100000 |
|2015-12-02 |SH.000422 |7.4300 |7.38700000 |
|2015-12-03 |SH.000422 |7.9200 |7.42433333 |
|2015-12-04 |SH.000422 |8.2300 |7.45966667 |
|2015-12-07 |SH.000422 |8.2400 |7.49500000 |
|2015-12-08 |SH.000422 |7.7700 |7.50666667 |
|2015-12-09 |SH.000422 |7.8300 |7.52900000 |
|2015-12-10 |SH.000422 |7.6700 |7.54333333 |
|2015-12-11 |SH.000422 |7.5100 |7.55166667 |
|2015-12-14 |SH.000422 |7.6200 |7.57033333 |
|2015-12-15 |SH.000422 |7.5500 |7.58700000 |
|2015-12-16 |SH.000422 |7.5500 |7.59300000 |
|2015-12-17 |SH.000422 |7.7400 |7.60266667 |
|2015-12-18 |SH.000422 |7.6300 |7.60600000 |
|2015-12-21 |SH.000422 |7.8300 |7.61533333 |
|2015-12-22 |SH.000422 |7.8900 |7.62600000 |
|2015-12-23 |SH.000422 |7.9200 |7.63633333 |
|2015-12-24 |SH.000422 |7.9900 |7.65133333 |
|2015-12-25 |SH.000422 |8.0300 |7.67300000 |
|2015-12-28 |SH.000422 |7.7100 |7.68000000 |
|2015-12-29 |SH.000422 |7.8400 |7.69100000 |
|2015-12-30 |SH.000422 |7.9300 |7.70900000 |
|2015-12-31 |SH.000422 |7.7700 |7.71500000 |
+------------+------------+----------+-----------+
通过以上的计算,验证后发现,前 29 行,也即 2015-08-05 至 2015-09-16 的数据并不是 30 日均值。
虽然设定了窗口帧范围为前 29 行到当前行,但当前面不足 29 行的时候,则会有多少行取多少行。
也就是 2015-08-05 前面有 0 行,则 2015-08-05 的 SMA30 就是 2015-08-05 的 stk_close 本身 (8.0300 / 1 = 8.0300)
2015-08-06 前面有 1 行,则 2015-08-06 的 SMA30 就是 2015-08-05 至 2015-08-06 的 stk_close 的平均值 ((8.0300 + 8.0700) / 2 = 8.050)
…
以上就是使用 MySQL 窗口函数 (OLAP) 计算简单移动平均 (Simple Moving Average) 值的全部内容。
更多内容可以访问我的代码仓库:
https://gitee.com/goufeng928/public
https://github.com/goufeng928/public