20个周期移动平均值计算为漂亮的指数价格

时间:2015-02-11 18:30:49

标签: php mysql

  • nifty是印度国家证券交易所的股票指数。 *

我有一个名为“livedata”的数据库,其中表名为nifty,这是印度国家证券交易所的股票指数。我有一行名为

ID作为主键,时间作为时间戳,cmp表示特定的“漂亮”股票指数..白天(下午09:15:01,下午03:29:59),一分钟

(1)我收到大约5个交易条目。

实施例: -

ID |        time        |     cmp | 
-----------------------------------
1  |2014-10-17 10:55:05 | 7743.85 | 
2  |2014-10-17 10:55:15 | 7743.55 | 
3  |2014-10-17 10:55:22 | 7743.35 | 
4  |2014-10-17 10:55:34 | 7744.85 | 
5  |2014-10-17 10:55:45 | 7743.35 | 
6  |2014-10-17 10:55:56 | 7743.25 | this should be the last / closing trade Entry.
7  |2014-10-17 10:56:17 | 7743.15 | 
8  |2014-10-17 10:56:25 | 7742.85 | 
9  |2014-10-17 10:56:35 | 7742.25 | 
10 |2014-10-17 10:56:45 | 7743.35 | 
11 |2014-10-17 10:56:55 | 7742.65 | this should be the last / closing trade Entry.
12 |2014-10-17 10:57:23 | 7743.55 | 
13 |2014-10-17 10:57:37 | 7744.85 | 
14 |2014-10-17 10:57:46 | 7743.55 | 
15 |2014-10-17 10:57:53 | 7741.85 | 
16 |2014-10-17 10:57:56 | 7742.45 | this should be the last / closing trade Entry.
17 |2014-10-17 10:58:05 | 7743.85 | 
18 |2014-10-17 10:58:17 | 7743.55 | 
19 |2014-10-17 10:58:29 | 7743.35 | 
20 |2014-10-17 10:58:37 | 7744.85 | 
21 |2014-10-17 10:58:48 | 7743.35 | 
22 |2014-10-17 10:58:56 | 7743.25 | this should be the last / closing trade Entry.
23 |2014-10-17 10:59:17 | 7743.15 | 
24 |2014-10-17 10:59:21 | 7742.85 | 
25 |2014-10-17 10:59:33 | 7742.25 | 
26 |2014-10-17 10:59:43 | 7743.35 | 
27 |2014-10-17 10:59:50 | 7742.65 | 
28 |2014-10-17 10:59:57 | 7743.55 | this should be the last / closing trade Entry.
29 |2014-10-17 10:60:17 | 7744.85 | 
30 |2014-10-17 10:60:25 | 7743.55 | 
31 |2014-10-17 10:60:36 | 7741.85 | 
32 |2014-10-17 10:60:47 | 7742.45 |this should be the last / closing trade Entry.

我想根据每分钟的最后/收盘价计算移动平均线。在一(1)分钟内,我可以通过许多条目获得此项目,

只需要最后一笔交易。 示例基础所需结果: -

ID |          time      |     cmp |
-----------------------------------
6  |2014-10-17 10:55:56 | 7743.25 | 
11 |2014-10-17 10:56:55 | 7742.65 | 
16 |2014-10-17 10:57:56 | 7742.45 | 
22 |2014-10-17 10:58:56 | 7743.25 | 
28 |2014-10-17 10:59:57 | 7743.55 | 
32 |2014-10-17 10:60:47 | 7742.45 |

实施例。 (移动平均线= ADD 20期间,即最后20分钟的每分钟的最后交易条目,并将其除以20.该值应该出现在

frontend可能正在使用$ result = mysql_query($ query)或die(mysql_error());请建议..

如果我想获得每分钟的最后一个条目,我该如何编写PHP代码?现在我正在使用

获取条目
$query= "SELECT FROM nifty WHERE time BETWEEN '2014-10-17 10:55:05' AND '2014-10-17 10:60:47'"; 

实际结果: -

ID      Date Stamp        CMP
1   2014-10-17 10:55:05 7743.85
2   2014-10-17 10:55:14 7743.75
3   2014-10-17 10:55:18 7745.30
4   2014-10-17 10:55:21 7745.35
5   2014-10-17 10:55:32 7745.10
6   2014-10-17 10:55:36 7744.85
7   2014-10-17 10:55:46 7744.40
8   2014-10-17 10:55:50 7744.70
9   2014-10-17 10:55:59 7745.60 this should be the last / closing trade Entry.
10  2014-10-17 10:56:06 7745.80
11  2014-10-17 10:56:11 7745.40
12  2014-10-17 10:56:23 7746.50
13  2014-10-17 10:56:32 7747.30
14  2014-10-17 10:56:34 7747.35
15  2014-10-17 10:56:41 7747.65
16  2014-10-17 10:56:52 7747.80
17  2014-10-17 10:56:56 7748.20 this should be the last / closing trade Entry.
18  2014-10-17 10:57:01 7747.80
19  2014-10-17 10:57:13 7747.75
20  2014-10-17 10:57:16 7747.55
21  2014-10-17 10:57:28 7747.50
22  2014-10-17 10:57:32 7746.95
23  2014-10-17 10:57:38 7746.55
24  2014-10-17 10:57:40 7746.70
25  2014-10-17 10:57:50 7747.25
26  2014-10-17 10:57:58 7746.55 this should be the last / closing trade Entry.
27  2014-10-17 10:58:09 7746.70
28  2014-10-17 10:58:11 7746.90
29  2014-10-17 10:58:17 7747.20
30  2014-10-17 10:58:26 7746.75
31  2014-10-17 10:58:33 7746.70
32  2014-10-17 10:58:37 7746.55 this should be the last / closing trade Entry.
33  2014-10-17 11:30:28 7748.35
34  2014-10-17 11:30:32 7748.95
35  2014-10-17 11:30:40 7749.95
36  2014-10-17 11:30:45 7749.05
37  2014-10-17 11:30:58 7748.95 this should be the last / closing trade Entry.
38  2014-10-17 11:31:03 7748.80
39  2014-10-17 11:31:08 7749.90
40  2014-10-17 11:31:12 7751.80
41  2014-10-17 11:31:26 7752.70
42  2014-10-17 11:31:33 7753.80
43  2014-10-17 11:31:33 7753.60
44  2014-10-17 11:31:39 7752.55
45  2014-10-17 11:31:50 7752.65
46  2014-10-17 11:31:56 7754.60 this should be the last / closing trade Entry.
47  2014-10-17 11:32:18 7755.15
48  2014-10-17 11:32:18 7755.35
49  2014-10-17 11:32:26 7755.25
50  2014-10-17 11:32:36 7754.45
51  2014-10-17 11:32:40 7754.90
52  2014-10-17 11:32:56 7754.40 this should be the last / closing trade Entry.
53  2014-10-17 11:33:10 7754.35
54  2014-10-17 11:33:12 7753.65
55  2014-10-17 11:33:15 7753.45
56  2014-10-17 11:33:22 7753.50
57  2014-10-17 11:33:25 7753.35
58  2014-10-17 11:33:34 7753.45
59  2014-10-17 11:33:39 7754.20
60  2014-10-17 11:33:49 7756.35
61  2014-10-17 11:33:50 7755.65
62  2014-10-17 11:33:54 7757.15 this should be the last / closing trade Entry.
63  2014-10-17 11:34:01 7759.05

我们可以使用循环,同时,为此而做吗? 请提前帮助解决这个问题。

使用SELECT LOOP WHILE DO WHILE等预期的结果

1 个答案:

答案 0 :(得分:0)

MySQL不支持分析函数,这使得这类任务比许多其他RDBMS引擎更难。

虽然一个可以仍然使用纯粹的基于集合的SQL方法,但这比使用user variable的快捷方法更难以理解并且可能效率低得多:

SELECT AVG(cmp) FROM (
    SELECT   cmp, @t:=time - INTERVAL SECOND(time) SECOND
    FROM     nifty, (SELECT @t:=CURRENT_TIMESTAMP) init
    WHERE    time < @t
    ORDER BY time DESC
    LIMIT    20
) x

sqlfiddle上查看。

外部查询应该是相当不言自明的:它只取内部查询中cmp值的均值。

由于nifty子句,内部查询在ORDER BY表上向后移动;使用变量@t来跟踪我们已经考虑过的最新分钟,WHERE子句确保只考虑该分钟之前的条目 - SELECT中的第二项每当找到匹配项时,列表会将@t更新到找到的分钟的开头。最后,LIMIT子句确保在找到第20条记录后终止内部查询。

如果存在一个复合索引,其中(time, cmp)是最左边的列,则按此顺序最有效地计算此查询。例如:

ALTER TABLE nifty ADD INDEX (time, cmp)
相关问题