[英]Azure Streaming Analytics Calculate OHLC
I am trying to calculate Open, Low, High and Close values for stock prices using Azure Stream Analytics SQL. 我正在尝试使用Azure Stream Analytics SQL计算股票价格的开盘价,低价,最高价和收盘价。
I can get Min and Max fairly easily, but I am having difficulty figuring out how to calculate Open (FIRST) and Close (LAST) of a TumblingWindow. 我可以很容易地获得Min和Max,但是我很难弄清楚如何计算TumblingWindow的Open(FIRST)和Close(LAST)。
I have found the documentation here ( https://docs.microsoft.com/en-us/azure/stream-analytics/stream-analytics-stream-analytics-query-patterns ) to do first and last but I cannot combine them into a single query. 我在这里找到了文档( https://docs.microsoft.com/zh-cn/azure/stream-analytics/stream-analytics-stream-analytics-query-patterns )首先要执行,最后要执行,但是我无法将它们合并为一个查询。
Here's what I have: 这是我所拥有的:
SELECT
DateAdd(second,-5,System.TimeStamp) as WinStartTime,
system.TimeStamp as WinEndTime,
exchange,
Max(price) as H,
Min(price) as L,
Count(*) as EventCount
FROM [input1]
GROUP BY TumblingWindow(second, 5), exchange
I am not terribly advanced in SQL, but is there a way to combine this into a single query? 我在SQL方面不是很先进,但是有没有办法将其组合到单个查询中? Even with the use of subqueries.
即使使用子查询。
Note: 5 seconds is just an example window I chose for testing. 注意:5秒只是我选择进行测试的示例窗口。
According to your scenario, I assumed that you could leverage the Collect aggregate function and user-defined functions from Azure Stream Analytics to achieve your purpose. 根据您的方案,我假设您可以利用Azure Stream Analytics中的Collect聚合功能和用户定义的功能来实现您的目的。 Here are the details, you could refer to them:
以下是详细信息,您可以参考它们:
Assuming your input looks as follows: 假设您的输入如下所示:
[
{"price":1.1,"exchange":10,"Time":"2017-7-24T13:00:00Z"},
{"price":1.2,"exchange":20,"Time":"2017-7-24T13:04:00Z"},
{"price":1.4,"exchange":40,"Time":"2017-7-24T13:03:00Z"},
{"price":1.3,"exchange":30,"Time":"2017-7-24T13:02:00Z"},
{"price":1.5,"exchange":50,"Time":"2017-7-24T13:06:00Z"}
]
UDF UDF
// udf.getLast
function main(objs) {
if(objs==null||objs==undefined||objs.length==0)return null;
return objs[objs.length-1];
}
// udf.getFirst
function main(objs) {
if(objs==null||objs==undefined||objs.length==0)return;
return objs[0];
}
QUERY QUERY
SELECT
DateAdd(minute,-5,System.TIMESTAMP) as WinStartTime,
System.TIMESTAMP as WinEndTime,
UDF.getFirst(Collect()) AS FIRST,
UDF.getLast(Collect()) AS LAST,
Max(price) as H,
Min(price) as L,
Count(*) as EventCount
FROM [input1] TIMESTAMP By Time
GROUP BY TumblingWindow(minute, 5)
RESULT: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.