Simple Sequence

Sequence is a state machine implementation that detects consecutive event occurrences from events arrived via one or more event streams over time. Here all matching events need to arrive consecutively, and there should not be any non-matching events in between the matching sequence of events.

Refer the Siddhi query guide for more information.

define stream StockRateStream (symbol string, price float,
    volume int);

@sink(type='log')
define stream PeakStockRateStream (symbol string,
    rateAtPeak float);

partition with (symbol of StockRateStream)
begin

    from every e1=StockRateStream,
        e2=StockRateStream[e1.price < price],
        e3=StockRateStream[e2.price > price]
        within 10 min
    select e1.symbol, e2.price as rateAtPeak
    insert into PeakStockRateStream ;
end;
define stream StockRateStream (symbol string, price float,
    volume int);

Defines StockRateStream having information on stock rate such as symbol, price and volume.

@sink(type='log')
define stream PeakStockRateStream (symbol string,
    rateAtPeak float);

Defines PeakStockRateStream which contains the peak stock rate.

partition with (symbol of StockRateStream)
begin

Partition the StockRateStream events by symbol

    from every e1=StockRateStream,
        e2=StockRateStream[e1.price < price],
        e3=StockRateStream[e2.price > price]

Identifies the peak stock price (top rate of the stock price trend)

        within 10 min
    select e1.symbol, e2.price as rateAtPeak
    insert into PeakStockRateStream ;
end;

This application can be used to detect trends from a stock trades stream; in the above example, peak stock rate identified.

Input

Below events are sent to StockRateStream within 10 minutes,

[mint-leaves, 35, 20]

[mint-leaves, 40, 15]

[mint-leaves, 38, 20]

Output

After processing the above input events, the event arriving at PeakStockRateStream will be as follows:

[mint-leaves, 40]

Top