package com.ib.client.examples;

import com.ib.client.ComboLeg;
import com.ib.client.Contract;
import com.ib.client.TickType;
import com.ib.client.EWrapperMsgGenerator;
import java.text.*;
import java.util.*;
import java.io.*;

/**
 * This program creates a BAG contract and then calls reqMktData() on the contract.
 *
 *  A working example using the "ExampleBase" series of examples included in the IB
 *  API source code.  Just replace the hard coded IP address to one which works for
 *  you.  
 *
 *  This example creates an ES calendar spread, and then calls reqMktData() on the
 *  BAG contract, and the other methods simply print the bid and ask information to
 *  stdout.
 * 
 * $Id$
 */
public class Example9 extends ExampleBase {

  private int requestId = 1;

  public Example9() {
    this.tws_host = "192.168.221.19" ;
  }

  public static void main(String[] args) {
    new Example9().start() ;
  }

  public void run() {
    try {

      // Make connection
      connectToTWS( tws_host, tws_port, tws_clientId );

      // Create a BAG Contract on an ES calendar spread with everything hardcoded!
      Contract contract = createBag() ;

      // Requests market data
      eClientSocket.reqMktData(requestId, contract, "", false);

      while ( true ) {
        sleep(1000 * 1) ;  // sleep 1 second, then sleep some more!
      } 

    } catch (Throwable t) {

      System.err.println("Example9.run() :: Problem occurred during processing: " + t.getMessage());

    } finally {

      eClientSocket.cancelMktData(requestId) ;
      disconnectFromTWS();
    }
  }

  // Create and return a BAG Contract on an ES calendar spread with everything hardcoded!
  private Contract createBag() {
    ComboLeg leg1                       = new ComboLeg(); // for the first leg
    ComboLeg leg2                       = new ComboLeg(); // for the second leg
    Vector addAllLegs                   = new Vector();

    // As returned from reqContractDetails() in contractDetails.m_summary....
    // m_summary.m_localSymbol = ESU2
    // m_summary.m_conId       = 89564182
    // m_summary.m_symbol      = ES
    // m_summary.m_secType     = FUT
    // m_marketName            = ES

    leg1.m_conId                        = 89564182;  // ESU2
    leg1.m_ratio                        = 1;
    leg1.m_action                       = "BUY";
    leg1.m_exchange                     = "GLOBEX";
    leg1.m_openClose                    = 0;
    leg1.m_shortSaleSlot                = 0;
    leg1.m_designatedLocation           = "";

    // As returned from reqContractDetails() in contractDetails.m_summary....
    // m_summary.m_localSymbol = ESZ2
    // m_summary.m_conId       = 94410903
    // m_summary.m_symbol      = ES
    // m_summary.m_secType     = FUT
    // m_marketName            = ES

    leg2.m_conId                        = 94410903;  // ESZ2
    leg2.m_ratio                        = 1;
    leg2.m_action                       = "SELL";
    leg2.m_exchange                     = "GLOBEX";
    leg2.m_openClose                    = 0;
    leg2.m_shortSaleSlot                = 0;
    leg2.m_designatedLocation           = "";

    addAllLegs.add(leg1);
    addAllLegs.add(leg2);

    Contract contract                   = new Contract();
    contract.m_symbol                   = "USD";     // For combo order use USD as the symbol value all the time
    contract.m_secType                  = "BAG";     // BAG is the security type for COMBO order
    contract.m_exchange                 = "GLOBEX";
    contract.m_currency                 = "USD";
    contract.m_comboLegs                = addAllLegs; //including combo order in contract object
    contract.m_symbol                   = "ES";

    return contract;
  }

  public void tickPrice(int tickerId, int field, double price, int canAutoExecute) {
    System.out.println("tickPrice()," +
      TickType.getField(field) + "," + field + "," + price + "," + System.currentTimeMillis()
    ) ;
  }

  public void tickSize(int tickerId, int field, int size) {
    System.out.println("tickSize()," +
      TickType.getField(field) + "," + field + "," + size + "," + System.currentTimeMillis()
    ) ;
  }

  public void tickGeneric(int tickerId, int field, double generic) {
    System.out.println("tickGeneric()," +
      TickType.getField(field) + "," + field + "," + generic + "," + System.currentTimeMillis()
    ) ;
  }

  public void tickString(int tickerId, int field, String value) {
    System.out.println("tickString()," +
      TickType.getField(field) + "," + field + "," + value + "," + System.currentTimeMillis()
    ) ;
  }
}
