Source code

    001/*
    002# Licensed Materials - Property of IBM
    003# Copyright IBM Corp. 2015
    004 */
    005package vwap;
    006
    007import java.util.Map;
    008
    009import com.ibm.streams.operator.StreamSchema;
    010import com.ibm.streams.operator.Type;
    011import com.ibm.streamsx.topology.TStream;
    012import com.ibm.streamsx.topology.Topology;
    013import com.ibm.streamsx.topology.context.StreamsContext;
    014import com.ibm.streamsx.topology.context.StreamsContextFactory;
    015import com.ibm.streamsx.topology.function.Predicate;
    016import com.ibm.streamsx.topology.spl.FileSPLStreams;
    017import com.ibm.streamsx.topology.spl.FileSPLStreams.Compression;
    018import com.ibm.streamsx.topology.spl.SPLStream;
    019
    020import simple.Util;
    021
    022public class Vwap {
    023    private static StreamSchema TQRecT = Type.Factory
    024            .getStreamSchema("tuple<rstring ticker,rstring date, rstring time, int32 gmtOffset,"
    025                    + "rstring ttype, rstring exCntrbID, decimal64 price,"
    026                    + "decimal64 volume, decimal64 vwap, rstring buyerID,"
    027                    + "decimal64 bidprice, decimal64 bidsize, int32 numbuyers,"
    028                    + "rstring sellerID, decimal64 askprice, decimal64 asksize,"
    029                    + "int32 numsellers, rstring qualifiers, int32 seqno,"
    030                    + "rstring exchtime, decimal64 blockTrd, decimal64 floorTrd,"
    031                    + "decimal64 PEratio, decimal64 yield, decimal64 newprice,"
    032                    + "decimal64 newvol, int32 newseqno, decimal64 bidimpvol,"
    033                    + "decimal64 askimpcol, decimal64 impvol>");
    034
    035    /**
    036     * Run the Vwap topology, printing the bargains to stdout.
    037     * @param args
    038     */
    039    public static void main(String[] args) throws Exception {
    040        String type = args[0];
    041        Map<String,Object> configMap = Util.createConfig(args);
    042
    043        TStream<Bargain> bargains = createVwapTopology();
    044
    045        bargains.print();
    046
    047        StreamsContext<?> sc = StreamsContextFactory.getStreamsContext(type);
    048        sc.submit(bargains.topology(), configMap).get();
    049    }
    050
    051    /**
    052     * Create the Vwap topology, returning the resultant bargains stream.
    053     */
    054    public static TStream<Bargain> createVwapTopology() {
    055        Topology topology = new Topology("Vwap");
    056
    057        String vwapDataFile = System.getenv("STREAMS_INSTALL")
    058                + "/samples/spl/application/Vwap/data/TradesAndQuotes.csv.gz";
    059
    060        TStream<String> vwapDataFileName = topology.strings(vwapDataFile);
    061        SPLStream tradeQuotes = FileSPLStreams.csvCompressedReader(
    062                vwapDataFileName, TQRecT, Compression.gzip);
    063
    064        // Convert the SPLStreams into TStream<T> instances,
    065        // unpacking the SPL Tuple into Quote and Trade objects
    066        TStream<Trade> trades = Trade.getTrades(tradeQuotes);
    067        TStream<Quote> quotes = Quote.getQuotes(tradeQuotes);
    068
    069        TStream<Bargain> bargains = VwapProcessing.bargains(trades, quotes);
    070
    071        return bargains;
    072    }
    073
    074    public static TStream<Bargain> realBargains(TStream<Bargain> bargains) {
    075        // Add a filter for actual bargains
    076        return bargains.filter(new Predicate<Bargain>() {
    077            private static final long serialVersionUID = 1L;
    078
    079            @Override
    080            public boolean test(Bargain bargain) {
    081                return bargain.isBargain();
    082            }
    083        });
    084    }
    085}