Scripting Engine

Some samples of what can be done in Groovy, using the classes provided in the org.otfeed Driver package.

Example 1

//
// Demostrates a simple request to retrieve historical quotes
// for 'GOOG'. Result is printed to the screen.
//
import org.otfeed.*;
import org.otfeed.event.*;
import org.otfeed.command.*;
import org.otfeed.support.*;

// command-line parameters are in the pre-defined variable called "args".
println("script called with parameters: $args");
 
// create Connection factory
def cf = new OTConnectionFactory(
	username: args[0],
	password: args[1],

	hosts: [
        	new OTHost("feed1.opentick.com", 10010)
	//        , new OTHost("feed2.opentick.com", 10010)
	]
);
 
// helper: convenient date input
def dateParser = new DateFormat("MM/dd/yyyy");

// handles incoming data: just print it out (make date look nice too)
def dataHandler = {
	def t = dateParser.format(it.timestamp);
	println("$t, $it.openPrice, $it.highPrice, $it.lowPrice, $it.closePrice"); 
}
 
// prepare a command.
def command = new HistDataCommand(
	exchangeCode:      "Q",
	symbolCode:        "GOOG",
	startDate:          dateParser.parse("1/1/2006"),
	endDate:            dateParser.parse("5/5/2006"),
	aggregationSpan:    AggregationSpan.days(), 
	dataDelegate:       dataHandler as IDataDelegate
);

// connect
def connection = cf.connect(null, new SimpleConnectionStateListener());

// prepare and submit request. Wait for request completion.
def r = connection.prepareRequest(command);
r.submit();
r.waitForCompletion();
if(r.error != null) {
	println "ERROR: $r.error"
}

// disconnect
connection.shutdown();
connection.waitForCompletion();
Runs historical OHLC request for ticker "Q/GOOG". Result is printed to the screen.

To run the script, use command:

$ java -jar lib/otfeed-script-X.X.X.jar samples/exa01.groovy username password

Example 2

//
// Demonstrates download of two historical data streams.
// Data is saved in a file 'data.csv', in CSV format
// Note that download requests are executed in parallel.
//
import org.otfeed.*;
import org.otfeed.event.*;
import org.otfeed.command.*;
import org.otfeed.support.*;

// define Connection factory. Take username/password from the command line
def connectionFactory = new OTConnectionFactory(
	username: args[0],
	password: args[1],
	hosts: [
		new OTHost("feed1.opentick.com", 10015)
	//	, new OTHost("feed2.opentick.com", 10010)
	]
);

// helper: nice date input/output
dateFormat = new DateFormat("MM/dd/yyyy");

// create file
def file = new FileWriter('data.csv');
file.write("# ticker, openPrice, highPrice, lowPrice, closePrice\n");

// closure that defines how we process data 
// events (write in CSV format to the file)
def writer = { f, id, o ->
	def t = dateFormat.format(o.timestamp);
	f.write("$id, $t, $o.openPrice, $o.highPrice, $o.lowPrice, $o.closePrice\n");
}

// list of commands to execute
def commandList = [
	new HistDataCommand(
		exchangeCode:   'Q',
		symbolCode:     'GOOG', 
		startDate:       dateFormat.parse("1/1/2006"),
		endDate:         dateFormat.parse("5/5/2006"), 
		aggregationSpan: AggregationSpan.days(),
		dataDelegate:    writer.curry(file, 'GOOG') as IDataDelegate
	),

	new HistDataCommand(
		exchangeCode:   "Q",
		symbolCode:     "MSFT", 
		startDate:       dateFormat.parse("1/1/2006"),
		endDate:         dateFormat.parse("5/5/2006"), 
		aggregationSpan: AggregationSpan.days(),
		dataDelegate:    writer.curry(file, 'MSFT') as IDataDelegate
	)
];


// connect to the server
def connection = connectionFactory.connect(null, new SimpleConnectionStateListener());
def requests = [];

// submit all commands to the server
commandList.each() { c -> 
	def r = connection.prepareRequest(c);
	r.submit();
	requests << r;
}

// wait for the completion of all commands
requests.each() { r ->
	r.waitForCompletion();
	if(r.error != null) println("ERROR: $r.error");
}

// disconnect
connection.shutdown();
connection.waitForCompletion();

file.close()
The code above will issue two requests for the historical data: for ticker Q/GOOG and Q/MSFT. Each requests OHLC (open/high/low/close) quotes aggregated over the period of one day. Data is saved in file "data.csv" in a comma-separated-values (csv) format.

To run the script, use command:

$ java -jar lib/otfeed-script-X.X.X.jar samples/exa02.groovy username password