001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     */
017    
018    package org.otfeed.samples;
019    
020    import java.text.DateFormat;
021    import java.text.SimpleDateFormat;
022    import java.util.Date;
023    
024    import org.otfeed.*;
025    import org.otfeed.command.HistTicksCommand;
026    import org.otfeed.event.*;
027    
028    /**
029     * Requests historcal data at tick resolution.
030     */
031    public class HistTicks {
032    
033            public static void main(String ... args) throws Throwable {
034    
035                    if(args.length != 5) {
036                            System.out.println("USAGE: HistTicks <exchangeName> <symbolName> <resolution> <startDate> <endDate>");
037                            System.exit(-1);
038                    }
039    
040                    DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
041                    Date start = format.parse(args[3]);
042                    Date end = format.parse(args[4]);
043                    System.out.println("# generated by " + HistTicks.class + " on " + (new Date()));
044                    System.out.println("#");
045                    System.out.println("# history data for: " + args[0] + "/" + args[1]);
046    
047                    HistTicksCommand command = new HistTicksCommand();
048                    command.setExchangeCode(args[0]);
049                    command.setSymbolCode(args[1]);
050                    command.setStartDate(start);
051                    command.setEndDate(end);
052    
053                    if("quote".equals(args[2])) {
054                            System.out.println("# event type: quote");
055                            command.setQuoteDelegate(new IDataDelegate<OTQuote>() {
056                                    public void onData(OTQuote data) {
057                                            System.out.println(data);
058                                    }
059                            });
060                    } else if("mmquote".equals(args[2])) {
061                            System.out.println("# event type: mmquote");
062                            command.setMmQuoteDelegate(new IDataDelegate<OTMMQuote>() {
063                                    public void onData(OTMMQuote data) {
064                                            System.out.println(data);
065                                    }
066                            });
067                    } else if("trade".equals(args[2])) {
068                            System.out.println("# event type: trade");
069                            command.setTradeDelegate(new IDataDelegate<OTTrade>() {
070                                    public void onData(OTTrade data) {
071                                            System.out.println(data);
072                                    }
073                            });
074                    } else if("bbo".equals(args[2])) {
075                            System.out.println("# event type: bbo");
076                            command.setBboDelegate(new IDataDelegate<OTBBO>() {
077                                    public void onData(OTBBO data) {
078                                            System.out.println(data);
079                                    }
080                            });
081                    } else {
082                            System.out.println("unrecognized event type: " + args[2]);
083                            System.out.println("expected one of: quote, mmquote, trade, bbo");
084                            System.exit(-1);
085                    }
086    
087                    command.setCompletionDelegate(new ICompletionDelegate() {
088                            public void onDataEnd(OTError error) {
089                                    if(error != null) {
090                                            System.out.println("# Error: " + error);
091                                    }
092                            }
093                    });
094                    
095                    System.out.println("# start timestamp: " + start);
096                    System.out.println("# end   timestamp: " + end);
097                    System.out.println("#");
098                    
099                    IConnection connection = Connector.connect();
100    
101                    try {
102                            IRequest request = connection.prepareRequest(command);
103                            request.submit();
104    
105                            request.waitForCompletion();
106                    } finally {
107                            connection.shutdown();
108                            connection.waitForCompletion();
109                    }
110            }
111    }