Samples :: Getting Started

Getting Started

Description

CME iLink 3 Handler that connects to the pre-defined host and port. When the session is established, the ‘SingleOrder - New’(MsgType=‘D’) SME message is sent to the counterparty.

Usage

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

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
import biz.onixs.cme.ilink3.handler.ApplicationLayerMsgType;
import biz.onixs.cme.ilink3.handler.CME;
import biz.onixs.cme.ilink3.handler.Handler;
import biz.onixs.cme.ilink3.handler.Session;
import biz.onixs.cme.ilink3.handler.SessionState;
import biz.onixs.cme.ilink3.handler.Tag;
import biz.onixs.cme.ilink3.handler.session.*;
import biz.onixs.sbe.ByteDecoder;
import biz.onixs.sbe.IMessage;
import biz.onixs.util.ScaledDecimal;
import biz.onixs.util.Utils;
import biz.onixs.util.settings.PropertyBasedSettings;
import biz.onixs.util.settings.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  
import java.util.Arrays;
  
/**
 * Getting Started sample.
 */
public class GettingStarted implements InboundSessionMessageListener, InboundApplicationMessageListener,
        OutboundSessionMessageListener, OutboundApplicationMessageListener,
        StateChangeListener, WarningListener, ErrorListener, Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(GettingStarted.class);
    private static final String SETTINGS_RESOURCE = "sample/GettingStarted.properties";
    private static final long PartyDetailsListReqId = 11012;
    private final int marketSegmentId;
    private final String host;
    private final int port;
    private Session session = null;
    private Settings settings = null;
  
    public GettingStarted(final int marketSegmentId, final String host, final int port) {
        this.marketSegmentId = marketSegmentId;
        this.host = host;
        this.port = port;
    }
  
    public void run() {
        try {
            LOG.info("Getting Started Sample");
            LOG.info("The application is starting...");
            //
            LOG.info("Loading settings from: {}", SETTINGS_RESOURCE);
            settings = new PropertyBasedSettings(SETTINGS_RESOURCE);
            //
            LOG.info("Starting the Handler...");
            final Handler handler = Handler.init(settings);
            //
            createSession();
            //
            subscribeListeners();
            //
            establishConnection();
            //
            Utils.waitForEnter("\nPress \"Enter\" to send the order.\n");
            //
            final IMessage order = createLimitOrder(session.getDecoder(), 205787, 100, 5);
            session.send(order);
            //
            Utils.waitForEnter("\nPress \"Enter\" to disconnect the session and terminate the application.\n");
            session.terminate();
            //
            LOG.info("Handler shutdown...");
            handler.shutdown();
        } catch (final RuntimeException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            LOG.info("The application is stopped.");
        }
    }
  
    private void createSession() {
        final SessionSettings sessionSettings = new SessionSettings();
        sessionSettings.init(settings);
        session = new Session(sessionSettings, marketSegmentId);
    }
  
    private void subscribeListeners() {
        session.addStateChangeListener(this);
        session.setOutboundSessionMessageListener(this);
        session.setOutboundApplicationMessageListener(this);
        session.setInboundSessionMessageListener(this);
        session.setInboundApplicationMessageListener(this);
        session.setErrorListener(this);
        session.setWarningListener(this);
    }
  
    private void establishConnection() {
        LOG.info("Establishing connection to {}:{} ...", host, port);
        session.logon(host, port);
    }
  
    public static IMessage createLimitOrder(final ByteDecoder decoder, final int securityId, final int securityPrice,
                                            final int orderQuantity) {
        final byte[] buffer = new byte[1024];
        Arrays.fill(buffer, (byte) 0);
        final IMessage message = decoder.encode(buffer, 0, buffer.length, ApplicationLayerMsgType.NewOrderSingleId);
        setCommonOrderFields(message, securityId, orderQuantity);
        message.setChar(Tag.OrdType, CME.OrderTypeReq.Limit);
        final ScaledDecimal priceDecimal = new ScaledDecimal(securityPrice, -9);
        message.setDecimal(Tag.Price, priceDecimal);
        return message;
    }
  
    private static void setCommonOrderFields(final IMessage message, final int securityId, final int orderQuantity) {
        message.setLong(Tag.PartyDetailsListReqID, PartyDetailsListReqId);
        message.setInt(Tag.SecurityID, securityId);
        message.setUnsignedInt(Tag.OrderQty, orderQuantity);
        message.setUnsignedByte(Tag.Side, CME.SideReq.Sell);
        message.setString(Tag.SenderID, "GFP");
        message.setString(Tag.ClOrdID, "SomeOrderID");
        message.setLong(Tag.OrderRequestID, 1L);
        message.setString(Tag.Location, "UK");
        message.setUnsignedByte(Tag.TimeInForce, CME.TimeInForce.Day);
        message.setUnsignedByte(Tag.ManualOrderIndicator, CME.ManualOrdIndReq.Automated);
        message.setUnsignedByte(Tag.ExecInst, CME.ExecInst.AON);
        message.setUnsignedByte(Tag.ExecutionMode, CME.ExecMode.Aggressive);
    }
  
    @Override
    public void onInboundSessionMessage(final Object sender, final InboundSessionMessageArgs args) {
        final IMessage message = args.getMsg();
        LOG.info("Incoming session-level message: {}", message);
        // Processing of the incoming session-level message...
    }
  
    @Override
    public void onInboundApplicationMessage(final Object sender, final InboundApplicationMessageArgs args) {
        final IMessage message = args.getMsg();
        LOG.info("Incoming application-level message: {}", message);
        // Processing of the incoming application-level message...
    }
  
    @Override
    public void onOutboundSessionMessage(final Object sender, final OutboundSessionMessageArgs args) {
        final IMessage message = args.getMsg();
        LOG.info("Outgoing session-level message: {}", message);
    }
  
    @Override
    public void onOutboundApplicationMessage(final Object sender, final OutboundApplicationMessageArgs args) {
        final IMessage message = args.getMsg();
        LOG.info("Outgoing application-level message: {}", message);
    }
  
    @Override
    public void onStateChange(final Object sender, final StateChangeArgs args) {
        final SessionState newState = args.getNewState();
        final SessionState prevState = args.getPrevState();
        LOG.info("Session state changed from {} to {}", prevState, newState);
    }
  
    @Override
    public void onWarning(final Object sender, final WarningArgs args) {
        LOG.warn("{}", args.getDescription());
    }
  
    @Override
    public void onError(final Object sender, final ErrorArgs args) {
        LOG.error("{}", args.getDescription());
    }
  
    public static void main(final String[] args) {
        if (3 > args.length) {
            LOG.error("Usage: [MarketSegmentId] [Host] [Port]");
        } else {
            final int marketSegmentId = Integer.parseInt(args[0]);
            final String host = args[1];
            final int port = Integer.parseInt(args[2]);
            final GettingStarted gettingStarted = new GettingStarted(marketSegmentId, host, port);
            gettingStarted.run();
        }
    }
}