Example of altering a threshold
The following code fragment shows part of a monitor that accepts requests from external entities to monitor the value of the trades for a given symbol. After you create a monitor like this, an external entity can, at any time, change the thresholds at which the monitor recognizes the trade as a high value trade.
monitor CountHighValueTicks {
float threshold;
action onload() {
CountHighValueTicksRequest r;
on all CountHighValueTicksRequest():r spawn
monitorHighValueTicks (r);
// Simplified. Assumes no duplicate requests.
}
action monitorHighValueTicks(CountHighValueTicksRequest r) {
threshold := r.threshold;
stream<Tick> filtered := from t in all Ticks(symbol=v.symbol)
where t.price*t.volume > threshold
select t;
integer c;
from t in filtered within 60.0 every 60.0 select count(): c {
print "Count of high value trades in previous minute: " +
c.toString();
}
on all CountHighValueTicksRequestUpdate(symbol=r.symbol) : u {
threshold := u.threshold ; }
}
}
This example uses two queries. The first query filters out any ticks with values below the threshold. The second query accumulates the high-value ticks received in the last minute and outputs the count of high-value ticks in that period. This could have been written as a single query with the filtering performed after the window operation. For example:
from t in all Ticks(symbol=v.symbol) within 60.0 every 60.0
where t.price*t.volume > threshold select count();
However this query’s window contains all of the low value ticks received in the last 60 seconds, as well as the high value ticks. This is not an optimal use of memory resources. Hence the two query approach is preferred.
Alternatively, you can specify an embedded query to amalgamate the two queries into a single statement:
from t in
(from t2 in ticks where t2.price*t2.volume > threshold select t2 )
within 60.0 every 60.0
select count(): c { ... }
The parentheses around the embedded query are optional.