Developing Apama Applications > Developing Apama Applications in EPL > Working with Streams and Stream Queries > Defining stream queries > Simple example of a stream network
Simple example of a stream network
Sometimes a single from statement is all that is required to achieve your goal. For example, to obtain a VWAP (Volume-Weighted Average Price) for a stock you can add the following from statement to a monitor:
float vwap;
from t in all Tick(symbol="APMA")
   within period
   select wavg(t.price,t.volume) : vwap {
      processNewVwap(vwap); }
Often, however, you want to use the output from one query as the input to another query. For example, here is an extract from the statistical arbitrage sample application, which you can find in the samples\monitorscript\statarb directory of your Apama installation directory:
action newStatArbOrder(StatArbOrder o) {
   integer BUY:=1, HOLD:=0, SELL:=-1, instruction;
 
   stream<float> spreads:=
      from a in all Price(symbol=o.primary.symbol) retain 1
      from b in all Price(symbol=o.secondary.symbol) retain 1
     select (a.price - b.price);
 
   stream<MeanSd> meanSds := from s in spreads within 20.0
      select MeansSd(mean(s), stddev(s) );
 
   stream<integer> comparison := from s in spreads from m in meanSd
      select compareSpreadAndBands(s, m.mean, m.sd, o.factor);
 
   stream<integer> prevComparison := from c in comparison
      retain 1
      select rstream c;
 
   from c in comparison from p in prevComparison
      where c!=HOLD and c!=p select c: instruction {
         if instruction = BUY {
            buyPrimarySellSecondary();
         } else {
            sellPrimaryBuySecondary();
         }
      }
}
When queries are connected like this, the set of connected queries is referred to as a stream network.
A stream network is strictly within a monitor instance. Routing an event takes that event entirely out of the stream network since the event would not be received in the same network activation even if it is received by the same monitor. Spawning a monitor makes any stream variables point to inert streams so it is not possible to refer to a stream network from a different monitor instance.
Copyright © Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its Subsidiaries and or/its Affiliates and/or their licensors.
Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG.