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 import java.util.EnumSet;
024
025 import org.otfeed.*;
026 import org.otfeed.command.AggregationSpan;
027 import org.otfeed.command.HistDataCommand;
028 import org.otfeed.command.TimeUnitEnum;
029 import org.otfeed.event.*;
030
031 /**
032 * Requests historical data.
033 */
034 public class HistDataCsv {
035
036 public static void main(String ... args) throws Exception {
037
038 if(args.length != 6) {
039 System.out.println("USAGE: HistData <exchangeName> <symbolName> <intervalUnits> <interval> <startDate> <endDate>");
040 System.exit(-1);
041 }
042
043 DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
044
045 Date start = format.parse(args[4]);
046 Date end = format.parse(args[5]);
047
048 TimeUnitEnum units = null;
049 try {
050 units = TimeUnitEnum.valueOf(args[2]);
051 } catch(IllegalArgumentException ex) {
052 System.out.println("unrecognized resolution: " + args[2]);
053 System.out.println("expected one of: " + EnumSet.allOf(TimeUnitEnum.class));
054 System.exit(-1);
055 }
056
057 int interval = Integer.parseInt(args[3]);
058 System.out.println("# aggregation interval: " + interval + " " + units );
059
060 System.out.println("# generated by " + HistDataCsv.class + " on " + FormatUtil.DATE_FORMAT_AS_STRING.format(new Date()));
061 System.out.println("#");
062 System.out.println("# history data for: " + args[0] + "/" + args[1]);
063
064 System.out.println("# start timestamp: " + FormatUtil.DATE_FORMAT_AS_STRING.format(start));
065 System.out.println("# end timestamp: " + FormatUtil.DATE_FORMAT_AS_STRING.format(end));
066 System.out.println("#");
067
068 final CSVFormatter formatter = new CSVFormatter();
069
070 formatter.setCustomPropertyFormatter(
071 Date.class,
072 FormatUtil.DATE_FORMAT_AS_STRING);
073
074 IConnection connection = Connector.connect();
075
076 HistDataCommand command = new HistDataCommand();
077 command.setExchangeCode(args[0]);
078 command.setSymbolCode(args[1]);
079 command.setStartDate(start);
080 command.setEndDate(end);
081 command.setAggregationSpan(new AggregationSpan(units, interval));
082 command.setDataDelegate(new IDataDelegate<OTOHLC>() {
083 private boolean firstTime = true;
084 public void onData(OTOHLC ohlc) {
085 if(firstTime) {
086 firstTime = false;
087 System.out.println(formatter.header(OTOHLC.class));
088 }
089 System.out.println(formatter.format(ohlc));
090 }
091
092 });
093 command.setCompletionDelegate(new ICompletionDelegate() {
094 public void onDataEnd(OTError error) {
095 System.out.println("# Error: " + error);
096 }
097 });
098
099 try {
100 IRequest request = connection.prepareRequest(command);
101
102 request.submit();
103
104 request.waitForCompletion();
105 } finally {
106 connection.shutdown();
107 connection.waitForCompletion();
108 }
109 }
110 }