Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. ** Given the following definitions, your goal is to write an API program to
  2.  *  buy the correct number of shares of IBM stock needed to bring the total
  3.  *  position of IBM stock in your account up to 1000.
  4.  *  - The order should be submitted at the current Ask price of IBM as per
  5.  *    the available market data.
  6.  *  - You can assume that there are currently less than 1000 shares of IBM
  7.  *    in the account, therefore you will always submit a Buy order.
  8.  *  - When the order is filled, print out the word "Success."
  9.  *  
  10.  *  To accomplish this, do the following:
  11.  *  
  12.  *  Review the IServer and ApiMessageHandler interfaces and ServerFactory class
  13.  *  below. These interfaces and class should not be modified in any way.
  14.  *
  15.  *  Create a main() method in ApiTest as a starting point for your application.
  16.  *  
  17.  *  Implement ApiMessageHandler to handle events from the server.
  18.  *  
  19.  *  Instantiate an IServer object by calling ServerFactory.createServer().
  20.  *  
  21.  *  Connect to the server and register your message handler. Connect to host
  22.  *  "localhost" on port 4444.
  23.  *  
  24.  *  Request market data for IBM.
  25.  *  
  26.  *  Request the current account positions.
  27.  *  
  28.  *  When you have received the current position of IBM and the current Ask price,
  29.  *  calculate the number of shares needed and submit the order at the correct price.
  30.  *  
  31.  *  Wait for the order status and print "Success" when the order is filled.
  32.  *  
  33.  *  Guiding principle: write the minimum amount of code necessary to accomplish the goal.
  34.  *
  35.  */
  36.  
  37. class ConnectionException extends Exception {}
  38.  
  39. enum Side {
  40.     Buy, Sell
  41. };
  42.  
  43. enum OrderStatus {
  44.     Open, Filled, Canceled
  45. }
  46.  
  47. /** Calling these methods is how you send messages to the server. */
  48. interface IServer {
  49.     /** Connects you to the server and also registers your message handler
  50.      *  with the server object. The message handler passed in here will receive all
  51.      *  future events that are generated from request to server. */
  52.     void connect( String host, int port, ApiMessageHandler handler) throws ConnectionException;
  53.    
  54.     /** Requests market data for the specified symbol and exchange.
  55.      *  Valid values for exchange are NYSE and NASDAQ. After the request is made,
  56.      *  market data for the specified contract will flow back into the application
  57.      *  through the ApiMessageHandler.marketDataReceived() method. */
  58.     void requestMarketData( String symbol, String exchange);
  59.    
  60.     /** Requests the current positions in the account. After this request
  61.      *  is made, the position data flows back into the app through the
  62.      *  ApiMessageHandler.positionReceived() method. */
  63.     void requestPortfolio();
  64.    
  65.     /** Places an order. The orderId should be a unique number generated by the client. */
  66.     void placeOrder( int orderId, Side side, int quantity, String symbol, String exchange, double price);
  67. }
  68.  
  69. /** Implementing this interface is how you receive messages from the server. */
  70. interface ApiMessageHandler {
  71.     /** Receives market data for symbols requested via the IServer.requestMarketData method. */
  72.     void marketDataReceived( String symbol, double bid, double ask, double last);
  73.    
  74.     /** Receives account positions in response to a request via the IServer.requestPortfolio() method. */
  75.     void positionReceived( String symbol, int position);
  76.    
  77.     /** Receives the status of an order that was placed via the IServer.placeOrder() method. */
  78.     void orderStatusReceived( int orderId, OrderStatus status);
  79. }
  80.  
  81. /** This class allows you to instantiate an object that implements the IServer
  82.  *  interface. The actual implementation of createServer() is hidden from view;
  83.  *  you can assume that the method is actually there (you don't need to implement it. */
  84. class ServerFactory {
  85.     public static final ServerFactory INSTANCE = new ServerFactory();
  86.    
  87.     /** Private constructor to enforce singleton. */
  88.     private ServerFactory() {
  89.     }
  90.    
  91.     /** This method returns an object that implements the IServer interface. */
  92.     public IServer createServer() { ... }
  93. }
  94.  
  95. class ApiTest {
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement