Thanks Eugene,

Here's our go at the code (Attached - Vincent wrote it, I can't take 
credit). You can run it as a JUnit test from com.jbooktrader.platform.test. 
This was the response when I ran the test for CL just now (just after 7AM 
Eastern):

Results :
201308 => No security definition has been found for the request
201309 => No security definition has been found for the request
201310 => 36816
201311 => 7256
201312 => 7303
201401 => 1983
201402 => 885
Highest volume for CL-NYMEX: 201310 (36816)

....which seems to be correct. We're not 100% sure to handle bad connection 
issues in JBT on the volume check, but we didn't want to go any further in 
case you wish to use your own version of this code. Let us know what you'd 
like to do.


Many thanks,


Michael.

-- 
You received this message because you are subscribed to the Google Groups 
"JBookTrader" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/jbooktrader.
For more options, visit https://groups.google.com/groups/opt_out.
package com.jbooktrader.platform.test;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

import org.junit.Test;

import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import com.jbooktrader.platform.trader.EWrapperAdapter;
import com.jbooktrader.platform.util.contract.ContractFactory;

public class GenericMostLiquidTest {

	public static final int MONTHS_TO_CHECK = 7; // how many months to compare
	public static final String SYMBOL = "CL"; // symbol
	public static final String EXCHANGE = "NYMEX"; // exchange

	public static final String TWS_HOST = "localhost";
	public static final int TWS_PORT = 7496;
	public static final int TWS_CLIENT_ID = 0;

	@Test
	public void testVolumeRequest() {
		
		// Initialise a calendar
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
		SimpleDateFormat contractDateFormat = new SimpleDateFormat("yyyyMM");

		// Contract months
		// ArrayList<Integer> contractMonths = new ArrayList<>();
		// contractMonths.add(Calendar.MARCH);
		// contractMonths.add(Calendar.JUNE);
		// contractMonths.add(Calendar.SEPTEMBER);
		// contractMonths.add(Calendar.DECEMBER);

		// Wrapper receiving request results
		TestWrapper wraper = new TestWrapper(MONTHS_TO_CHECK);

		// IB API connection
		System.out.println("Connecting to TWS");
		EClientSocket socket = new EClientSocket(wraper);
		socket.eConnect(TWS_HOST, TWS_PORT, TWS_CLIENT_ID);

		// Send all necessary requests
		for (int i = 0; i < MONTHS_TO_CHECK; i++) {
			// Move calendar to the next contract month
			// while (!contractMonths.contains(calendar.get(Calendar.MONTH))) {
			// calendar.add(Calendar.MONTH, 1);
			// }

			// Create contract
			String contractDate = contractDateFormat.format(calendar.getTime());
			Contract contract = ContractFactory.makeFutureContract(SYMBOL, EXCHANGE, contractDate);

			// Request market data for contract
			System.out.println("Requesting market data for " + contractDate);
			wraper.setContractForTicker(contractDate, i);
			socket.reqMktData(i, contract, "", true);
			calendar.add(Calendar.MONTH, 1);
		}

		int timeoutAfter = 20;
		int iterations = 0;
		while (!wraper.isDone() && iterations++ < timeoutAfter) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
		}
		socket.eDisconnect();
		wraper.printResults();
	}

	private class TestWrapper extends EWrapperAdapter {

		private ArrayList<Integer> tickers;
		private int[] volumes;
		private String[] contracts;
		private int nbRequests;
		private int receivedResults;

		public TestWrapper(int nbRequests) {
			super();
			this.nbRequests = nbRequests;
			receivedResults = 0;
			tickers = new ArrayList<>();
			volumes = new int[nbRequests];
			for (int i = 0; i < volumes.length; i++) {
				volumes[i] = -1;
			}
			contracts = new String[nbRequests];
		}

		public void printResults() {
			int max = -1;
			int maxId = -1;
			System.out.println("Results :");
			for (int i = 0; i < contracts.length; i++) {
				System.out.println(contracts[i] + " => " + resultValue(volumes[i]));
				if (volumes[i] > max) {
					max = volumes[i];
					maxId = i;
				}
			}
			System.out.println("Highest volume for " + SYMBOL + "-" + EXCHANGE + ": " + contracts[maxId] + " (" + max + ")");
		}

		public String resultValue(int volume) {
			switch (volume) {
			case -1:
				return "Timeout";
			case -2:
				return "No security definition has been found for the request";
			case -3:
				return "No result";
			default:
				return Integer.toString(volume);
			}
		}

		public void setContractForTicker(String contract, int ticker) {
			tickers.add(ticker);
			contracts[tickers.indexOf(ticker)] = contract;
		}

		public boolean isDone() {
			return receivedResults == nbRequests;
		}

		@Override
		public void error(int id, int errorCode, String errorMsg) {
			if (tickers.indexOf(id) >= 0) { // error corresponds to one of our requests
				System.err.println(contracts[tickers.indexOf(id)] + " => errorCode: " + errorCode + " errorMsg: " + errorMsg);
			} else if (errorCode == 2104) { // Successful connection message
				System.out.println(errorMsg);
			} else { // Other errors
				System.err.println("errorCode: " + errorCode + " errorMsg: " + errorMsg);

			}
			if (errorCode == 200) { // No security definition has been found for
									// the request
				volumes[tickers.indexOf(id)] = -2;
				receivedResults++;
			}
		}

		@Override
		public void tickSnapshotEnd(int reqId) {
			// Snapshot ends without a result
			if(volumes[tickers.indexOf(reqId)] == -1){
				volumes[tickers.indexOf(reqId)] = -3;
			}
		}

		@Override
		public void tickSize(int ticker, int field, int size) {
			if (field == 8) { // Volume
				volumes[tickers.indexOf(ticker)] = size;
				receivedResults++;
			}
		}

	}
}

Reply via email to