Samples :: Getting Started

Getting Started

Description

CME Drop Copy Handler sample that connects to the pre-defined host and port.

Usage

  • Run the sample:
  • win: runSample.bat
  • linux: runSample.sh
  • Clean everything:
  • win: clean.bat
  • linux: clean.sh

Directory Contents

File Location Description
conf/sample/GettingStarted.properties configuration file
conf/sample/CmeDropCopyFixDialect.xml dialect file
conf/logback.xml log configuration file
docs documentation including this file
libs all dependency libraries
src/main/java source files
clean.bat and clean.sh scripts used to clean log and storage directories which will be created while the client is running
GettingStarted.bat and GettingStarted.sh scripts used to run the client
OnixS.lic trial license file
pom.xml maven build configuration file

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import biz.onixs.cme.dropcopy.handler.ErrorEventArgs;
import biz.onixs.cme.dropcopy.handler.Handler;
import biz.onixs.cme.dropcopy.handler.MessageEventArgs;
import biz.onixs.fix.parser.Group;
import biz.onixs.fix.parser.GroupInstance;
import biz.onixs.fix.parser.Message;
import biz.onixs.fix.scheduler.InitiatorConnection;
import biz.onixs.fix.scheduler.MultiDayLengthSchedule;
import biz.onixs.fix.scheduler.SequenceNumberResetPolicy;
import biz.onixs.fix.scheduler.SessionSchedule;
import biz.onixs.fix.scheduler.SessionScheduler;
import biz.onixs.fix.tag.Tag;
import biz.onixs.util.settings.PropertyBasedSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.time.DayOfWeek;
import java.time.LocalTime;
  
public class GettingStarted implements Handler.ErrorListener, Handler.WarningListener, Handler.HandlerListener {
    private static final Logger LOG = LoggerFactory.getLogger(GettingStarted.class);
    private Handler handler;
    private static final String SETTINGS_RESOURCE = "sample/GettingStarted.properties";
  
    public GettingStarted() {
        System.out.println("CME Drop Copy Handler Getting Started Sample.");
    }
  
    private void run() throws Exception {
        final PropertyBasedSettings settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
  
        final boolean isUpToDate = settings.getBoolean("UpToDate");
  
        if (!isUpToDate) {
            throw new Exception("Please update the configuration file (" + SETTINGS_RESOURCE + ") with up-to-date values");
        }
  
        Handler.setLicenseFile(settings.getString("LicenseFile"));
        Handler.setLogDirectory(settings.getString("LogDirectory"));
        Handler.setDialectFile("sample/CmeDropCopyFixDialect.xml");
  
        //Fill the values below with the data for your application:
        final String[] handlerVersion = Handler.getAppVersion().split("\\.");
        Handler.ApplicationSystemVendor = "OnixS";
        Handler.ApplicationSystemName = "OnixS Trading System";
        Handler.ApplicationSystemVersion = "J" + handlerVersion[0] + "." + handlerVersion[1];
  
        String senderCompId = settings.getString("SenderCompID");
        String targetCompId = settings.getString("TargetCompID");
        String senderSubId = settings.getString("SenderSubID");
        String targetSubId = settings.getString("TargetSubID");
        String senderLocationId = settings.getString("SenderLocationID");
  
        handler = new Handler(senderCompId, targetCompId, senderSubId, targetSubId, senderLocationId);
  
        handler.setErrorListener(this);
        handler.setWarningListener(this);
        handler.setHandlerListener(this);
  
        handler.setProcessNextExpectedSeqNumFromCmeLogout(true);
  
        final String host = settings.getString("Host");
        final int port = settings.getInteger("Port");
        final String backupHost = settings.getString("BackupHost", "");
        final int backupPort = settings.getInteger("BackupPort", 0);
  
        final String accessKeyID = settings.getString("AccessKeyID", "");
        final String secretKey = settings.getString("SecretKey", "");
  
        final boolean useScheduler = settings.getBoolean("UseScheduler");
        SessionScheduler sessionScheduler = null;
  
        if (useScheduler) {
            sessionScheduler = new SessionScheduler();
            sessionScheduler.start();
            final SessionSchedule schedule = createSchedule();
            final InitiatorConnection connection = new InitiatorConnection();
            connection.setInitiatorConnectionListener(handler); // This is required to process switching between primary and backup hosts correctly.
            final InetSocketAddress address = InetSocketAddress.createUnresolved(host, port);
            connection.addAddress(address);
            if (backupHost != null) {
                final InetSocketAddress address2 = InetSocketAddress.createUnresolved(backupHost, backupPort);
                connection.addAddress(address2);
            }
  
            Message customLogon = handler.createLogonMessage(accessKeyID, secretKey);
  
            connection.setCustomLogonMessage(customLogon.toString());
            sessionScheduler.register(handler.getSession(), schedule, connection);
        }
  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
        printHelp();
  
        while (true) {
            System.out.print("> ");
            String s = br.readLine();
  
            boolean shouldExit = false;
  
            try {
  
                switch (s) {
  
                    case "exit":
                    case "1":
                        shouldExit = true;
                        break;
  
                    case "help":
                    case "2":
                        printHelp();
                        break;
  
                    case "logon-primary":
                    case "3":
                        handler.logon(host, port, accessKeyID, secretKey);
                        break;
  
                    case "logon-backup":
                    case "4":
                        handler.logon(backupHost, backupPort, accessKeyID, secretKey);
                        break;
  
                    case "logout":
                    case "5":
                        handler.logout();
                        break;
  
                    case "printsessionstate":
                    case "6":
                        System.out.printf("Session [%s]: %s\r\n", handler.getSession().getId(),
                                handler.getSession().getState().toString());
                        break;
  
                    case "reset":
                    case "7":
                        handler.reset();
                        break;
                }
  
            } catch (Exception ex) {
                System.err.println(ex.toString());
            }
  
            if (shouldExit)
                break;
        }
  
        System.out.println("Clean-up resources.");
  
        if (sessionScheduler != null)
            sessionScheduler.stop();
        handler.dispose();
  
        System.out.println("Done.");
    }
  
    private static SessionSchedule createSchedule() {
        final MultiDayLengthSchedule schedule = new MultiDayLengthSchedule();
        schedule.setLogonTime(LocalTime.parse("00:00"));
        schedule.setLogoutTime(LocalTime.parse("23:00"));
        schedule.setFirstDay(DayOfWeek.SUNDAY);
        schedule.setLastDay(DayOfWeek.FRIDAY);
        schedule.setResetPolicy(SequenceNumberResetPolicy.WEEKLY);
        return schedule;
    }
  
    private void printHelp() {
        System.out.println("  1. exit               - exit from application");
        System.out.println("  2. help               - print this help");
        System.out.println("  3. logon-primary      - logon to the primary host");
        System.out.println("  4. logon-backup       - logon to the backup host");
        System.out.println("  5. logout             - logout current session");
        System.out.println("  6. printsessionstate  - print session state");
        System.out.println("  7. reset              - reset MsgSeqNum");
    }
  
    public static void main(String[] args) {
        try {
            (new GettingStarted()).run();
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
    }
  
    @Override
    public void onExecutionReportReceived(Object sender, MessageEventArgs args) {
        final Message report =  args.getMessage();
        System.out.printf("Execution Report received: %s.\n", report);
        if(report.contains(Tag.OrdStatus))
            System.out.printf("OrdStatus = %s.\n", report.get(Tag.OrdStatus));
        if(report.contains(Tag.ExecType))
            System.out.printf("ExecType = %s.\n", report.get(Tag.ExecType));
        if(report.contains(Tag.Price))
            System.out.printf("Price = %s.\n", report.get(Tag.Price));
        if(report.contains(Tag.OrderQty))
            System.out.printf("OrderQty = %s.\n", report.get(Tag.OrderQty));
        if(report.contains(Tag.LeavesQty))
            System.out.printf("LeavesQty = %s.\n", report.get(Tag.LeavesQty));
        if(report.contains(Tag.LastPx))
            System.out.printf("LastPx = %s.\n", report.get(Tag.LastPx));
        if(report.contains(Tag.SecurityDesc))
            System.out.printf("SecurityDesc = %s.\n", report.get(Tag.SecurityDesc));
    }
  
    @Override
    public void onMassOrderCancelReportReceived(Object sender, MessageEventArgs args) {
        final Message orderMassActionReport = args.getMessage();
        System.out.printf("Mass Order Cancel Report received: %s.\n", orderMassActionReport);
        if(orderMassActionReport.contains(Tag.MarketSegmentID))
            System.out.printf("MarketSegmentID = %s.\n", orderMassActionReport.get(Tag.MarketSegmentID));
        System.out.printf("MassActionScope = %s.\n", orderMassActionReport.get(Tag.MassActionScope));
        System.out.printf("MassActionResponse = %s.\n", orderMassActionReport.get(Tag.MassActionResponse));
        System.out.printf("TotalAffectedOrders = %s.\n", orderMassActionReport.get(Tag.TotalAffectedOrders));
        if(args.getMessage().hasGroup(Tag.NoAffectedOrders)) {
            System.out.printf("NoAffectedOrders = %s.\n", args.getMessage().get(Tag.NoAffectedOrders));
            final Group noAffectedOrdersGrp = orderMassActionReport.getGroup(Tag.NoAffectedOrders);
            for (final GroupInstance instance: noAffectedOrdersGrp) {
                System.out.printf("OrigClOrdID = %s.\n", instance.get(Tag.OrigClOrdID));
                System.out.printf("CxlQty = %s.\n", instance.get(Tag.CxlQty));
                System.out.printf("AffectedOrderID = %s.\n", instance.get(Tag.AffectedOrderID));
                System.out.printf("---------------\n");
            }
        }
        System.out.printf("LastFragment = %s.\n", orderMassActionReport.get(Tag.LastFragment));
    }
  
    @Override
    public void onError(Object sender, ErrorEventArgs args) {
        LOG.error("{}", args);
    }
  
    @Override
    public void onWarning(Object sender, ErrorEventArgs args) {
        LOG.warn("{}", args);
    }
}