Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ** Given the following definitions, your goal is to write an API program to
- * buy the correct number of shares of IBM stock needed to bring the total
- * position of IBM stock in your account up to 1000.
- * - The order should be submitted at the current Ask price of IBM as per
- * the available market data.
- * - You can assume that there are currently less than 1000 shares of IBM
- * in the account, therefore you will always submit a Buy order.
- * - When the order is filled, print out the word "Success."
- *
- * To accomplish this, do the following:
- *
- * Review the IServer and ApiMessageHandler interfaces and ServerFactory class
- * below. These interfaces and class should not be modified in any way.
- *
- * Create a main() method in ApiTest as a starting point for your application.
- *
- * Implement ApiMessageHandler to handle events from the server.
- *
- * Instantiate an IServer object by calling ServerFactory.createServer().
- *
- * Connect to the server and register your message handler. Connect to host
- * "localhost" on port 4444.
- *
- * Request market data for IBM.
- *
- * Request the current account positions.
- *
- * When you have received the current position of IBM and the current Ask price,
- * calculate the number of shares needed and submit the order at the correct price.
- *
- * Wait for the order status and print "Success" when the order is filled.
- *
- * Guiding principle: write the minimum amount of code necessary to accomplish the goal.
- *
- */
- class ConnectionException extends Exception {}
- enum Side {
- Buy, Sell
- };
- enum OrderStatus {
- Open, Filled, Canceled
- }
- /** Calling these methods is how you send messages to the server. */
- interface IServer {
- /** Connects you to the server and also registers your message handler
- * with the server object. The message handler passed in here will receive all
- * future events that are generated from request to server. */
- void connect( String host, int port, ApiMessageHandler handler) throws ConnectionException;
- /** Requests market data for the specified symbol and exchange.
- * Valid values for exchange are NYSE and NASDAQ. After the request is made,
- * market data for the specified contract will flow back into the application
- * through the ApiMessageHandler.marketDataReceived() method. */
- void requestMarketData( String symbol, String exchange);
- /** Requests the current positions in the account. After this request
- * is made, the position data flows back into the app through the
- * ApiMessageHandler.positionReceived() method. */
- void requestPortfolio();
- /** Places an order. The orderId should be a unique number generated by the client. */
- void placeOrder( int orderId, Side side, int quantity, String symbol, String exchange, double price);
- }
- /** Implementing this interface is how you receive messages from the server. */
- interface ApiMessageHandler {
- /** Receives market data for symbols requested via the IServer.requestMarketData method. */
- void marketDataReceived( String symbol, double bid, double ask, double last);
- /** Receives account positions in response to a request via the IServer.requestPortfolio() method. */
- void positionReceived( String symbol, int position);
- /** Receives the status of an order that was placed via the IServer.placeOrder() method. */
- void orderStatusReceived( int orderId, OrderStatus status);
- }
- /** This class allows you to instantiate an object that implements the IServer
- * interface. The actual implementation of createServer() is hidden from view;
- * you can assume that the method is actually there (you don't need to implement it. */
- class ServerFactory {
- public static final ServerFactory INSTANCE = new ServerFactory();
- /** Private constructor to enforce singleton. */
- private ServerFactory() {
- }
- /** This method returns an object that implements the IServer interface. */
- public IServer createServer() { ... }
- }
- class ApiTest {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement