Skip to main content

Client Libraries

Supported client APIs:

Client languageSupported versionsSupported OSProject GitHub RepositoryArtifacts Repository
Java11-14macOS, Windows, LinuxLinkLink
Python3.6, 3.7, 3.8, 3.9, 3.10macOS, Windows, LinuxLinkLink
C++-macOS, Windows, LinuxLinkLink
Download whl file from the Python artifacts repository/assets and run this command to install a Python client.
pip install timebase_client-6.0.1-py2.py3-none-any.whl
info

Please refer to TimeBase Tutorial for more information.

Prerequisites

Before running samples, please make sure you have a TimeBase Server running on your local machine on port 8011.

tip

Refer to TickDB API and Deployment for more information.

Sample Message

public class MyBarMessage extends InstrumentMessage {
public double closePrice;
public double openPrice;
public double highPrice;
public double lowPrice;
public double volume;
public String exchange;

@Override
public StringBuilder toString(StringBuilder sb) {
sb.append("{ \"$type\": \"MyBarMessage\"");

sb.append(", \"closePrice\": ").append(closePrice);
sb.append(", \"openPrice\": ").append(openPrice);
sb.append(", \"highPrice\": ").append(highPrice);
sb.append(", \"lowPrice\": ").append(lowPrice);
sb.append(", \"volume\": ").append(volume);

return sb;
}

}

Create Stream

public void createStream() {
String connection = "dxtick://localhost:8011";

// Create Timebase connection using connection string
try (DXTickDB db = TickDBFactory.createFromUrl(connection)) {
// It require to open database connection.
db.open(false);

String streamName = "mybars";

// Introspect MyBarMessage.class to define it's schema
Introspector introspector = Introspector.createEmptyMessageIntrospector();
RecordClassDescriptor descriptor = introspector.introspectRecordClass(MyBarMessage.class);

// Define stream Options to creating Durable (Persistent storage) stream with maximum distribution with given descriptor
StreamOptions options = StreamOptions.fixedType(StreamScope.DURABLE, streamName, "Bar Messages", 0, descriptor);

// Create stream in database.
// first check if stream already exists in database, and delete it
DXTickStream bars = db.getStream(streamName);
if (bars != null)
bars.delete();

// create stream
DXTickStream stream = db.createStream(streamName, options);

System.out.println(stream.getKey() + " successfully created using Introspector");
}
}
info

Please refer to TimeBase Tutorial for more information.

Write

public void writeStream() {
String connection = "dxtick://localhost:8011";

// 1. Create Timebase connection using connection string
try (DXTickDB db = TickDBFactory.createFromUrl(connection)) {

// It require to open database connection.
db.open(false);

// 2. Get already created stream from database
String streamName = "mybars";
DXTickStream stream = db.getStream(streamName);

//
// 3. Writing new messages to the stream
//

// 3.1 Create Loader
try (TickLoader loader = stream.createLoader()) {

// 3.2 Create message to send
MyBarMessage message = new MyBarMessage();

// 3.3 set time for this message
LocalDateTime localTime = LocalDateTime.of(2020, Month.MARCH, 9, 0, 0, 0);
Instant utc = localTime.atZone(ZoneId.of("UTC")).toInstant();
message.setTimeStampMs(utc.toEpochMilli());

// 3.4 define field values
message.setSymbol("AAPL"); // Apple
message.openPrice = 263.75;
message.highPrice = 278.09;
message.lowPrice = 263.00;
message.closePrice = 266.17;
message.volume = 71_690_000;
message.exchange = "NYSE";

// 3.5. send first message
System.out.println("Sending 1st message: " + message);
loader.send(message);

// 3.6. reuse message to send another
message.setSymbol("GOOG"); // Apple
message.openPrice = 1205.3;
message.highPrice = 1254.76;
message.lowPrice = 1200.00;
message.closePrice = 1215.56;
message.volume = 33_700_000;

// 3.7. send second message
System.out.println("Sending 2nd message: " + message);
loader.send(message);
}
}
}
info

Please refer to TimeBase Tutorial for more information.

Read

public void readStream() {
String connection = "dxtick://localhost:8011";

// 1. Create Timebase connection using connection string
try (DXTickDB db = TickDBFactory.createFromUrl(connection)) {

// It require to open database connection.
db.open(false);

// 2. Get already created stream from database
String streamName = "mybars";
DXTickStream stream = db.getStream(streamName);

// 3.1. Define List of entities to subscribe (if null, all stream entities will be used)
// Use GOOG
String[] entities = new String[] { "GOOG" };

// 3.2. Define list of types to subscribe - select only "MyBarMessage" messages
String typeName = MyBarMessage.class.getName();
String[] types = new String[] { typeName };

// 3.3. Create a new 'cursor' to read messages
try (TickCursor cursor = stream.select(Long.MIN_VALUE, new SelectionOptions(), types, entities)) {
// 3.4 Iterate cursor and get messages
while (cursor.next()) {
MyBarMessage message = (MyBarMessage) cursor.getMessage();
System.out.println("Read message: " + message.toString());
}
}
}
}
info

Please refer to TimeBase Tutorial for more information.