Trading Evolution

Markets are never wrong - opinions often are

How to send an order to IB TWS in just 68 lines

Below is C# code snippet to send buy 100 shares of SPY at market order. Code is pretty much self-explanatory. Connect, PlaceOrder, Disconnect and we are done.

class Program
	static IBWrapper _ibWrapper;
	static IBApi.Contract _ibContract;
	static IBApi.Order _ibOrder;

	static ManualResetEvent _connectedEvent = new ManualResetEvent(false);
	static ManualResetEvent _orderSubmittedEvent = new ManualResetEvent(false);

	static void Main(string[] args)
			_ibWrapper = new IBWrapper();

			// Subscribe to events
			_ibWrapper.ClientConnected      += IbWrapper_ClientConnected;
			_ibWrapper.OrderStatusEvent     += IbWrapper_OrderStatusEvent;
			_ibWrapper.Error                += IbWrapper_Error;

			// Connect to TWS
			_ibWrapper.Connect("", 7496, 0);
			if (!_ibWrapper.IBClient.IsConnected())
				Console.WriteLine("Failed to connect to TWS");


			_ibContract = new IBApi.Contract() { Symbol = "SPY", SecType="STK", Exchange = "SMART", PrimaryExch = "Nasdaq" };
			_ibOrder = new IBApi.Order() { Action = "BUY", OrderType = "MKT", Tif = "DAY", TotalQuantity = 100, Transmit = true, OrderId = _ibWrapper.NextOrderId++ };

			_ibWrapper.IBClient.placeOrder(_ibOrder.OrderId, _ibContract, _ibOrder);

		catch (Exception e)

	private static void IbWrapper_ClientConnected(object sender, EventArgs e)

	private static void IbWrapper_OrderStatusEvent(object sender, OrderStatusEventArgs e)
		if (_ibOrder != null && _ibOrder.OrderId == e.OrderId)
			Console.WriteLine("Order status: " + e.Status);

	private static void IbWrapper_Error(object sender, ErrorEventArgs e)
		Console.WriteLine(string.Format("Error: {0}.", e.Error));


Download historical Forex data from Interactive Brokers

I added few more arguments to IbCmd tool for historical data download. For example to download last 10 days of Open, High, Low, Close data for EUR/USD pair use following list of parameters:

IbCmd.exe /s:EUR /t:Cash /e:IDEALPRO /f:EURUSD.csv /d:10

The tool will output EURUSD.csv file which can be opened in Excel or any other spreadsheet editor.

12/15/2015 12:00:00 AM,1.099625,1.105975,1.090475,1.093025
12/16/2015 12:00:00 AM,1.0925,1.101175,1.0888,1.091275
12/17/2015 12:00:00 AM,1.0909,1.09145,1.080275,1.0826
12/18/2015 12:00:00 AM,1.083425,1.08745,1.080525,1.0864
12/21/2015 12:00:00 AM,1.085975,1.0939,1.084825,1.09145
12/22/2015 12:00:00 AM,1.091375,1.098425,1.090225,1.09565
12/23/2015 12:00:00 AM,1.095225,1.0957,1.087025,1.091175
12/24/2015 12:00:00 AM,1.090975,1.096775,1.090425,1.096375
12/28/2015 12:00:00 AM,1.0964,1.099275,1.095625,1.09685
12/29/2015 12:00:00 AM,1.0967,1.098025,1.096625,1.097925

If you want 10 days of 5 minute bars then add /b:FiveMins


IbCmd.exe /s:EUR /t:Cash /e:IDEALPRO /f:EURUSD.csv /d:10 /b:FiveMins

Interactive Brokers Limitations

When you use this tool you need to be familiar with Interactive Brokers historical data download limitations. The following table lists the valid duration and bar size settings for historical data requests. Requesting the same historical data in a short period of time can cause pacing violations from Interactive Brokers.

More details can be found here: Historical Data Limitations

Command line tool for Interactive Brokers

Many times in the past I came across people who needed quick and simple way to send orders to Interactive Brokers. I decided to share command line tool that I use to send orders to TWS and IB Gateway from command line. It can be used in automated trading systems or scripts.


Few simple examples: 

Buy 100 shares of Facebook at Market

IbCmd.exe /s:FB /pe:Nasdaq

Buy 250 shares of Google at $752.5

IbCmd.exe /s:GOOG /pe:Nasdaq /q:250 /p:752.5

Sell 1 ES (E-Mini S&P 500) at Market

IbCmd.exe /s:ES /e:Globex /t:Future /a:Sell

Full list of parameters for IbCmd 

/ServerIp:<string> IP address of copumter that hosts TWS or TWS Gateway. Default value:'' (short form /srv)

/ServerPort:<int> Port number. Default value:'7496' (short form /port)

/ClientId:<int> API client id. Default value:'10' (short form /cid)

/Action:{Buy|Sell} Buy or sell. Default value:'Buy' (short form /a)

/Symbol:<string> Symbol of the underlying asset. (short form /s)

/SecurityType:{Stock|Future} Security type. (short form /t)

/Exchange:<string> The order destination, such as SMART. Default value:'SMART' (short form /e)

/PrimaryExchange:<string> To clarify any ambiguity for Smart-routed contracts, include the primary exchange, along with the Smart designation. (short form /pe)

/Quantity:<int> The order quantity. Default is 100 for stocks and 1 for futures. Default value:'0' (short form /q)

/OrderType:{Market|Limit} The order type. Default value:'Market' (short form /ot)

/Price:<decimal>  The price for limit orders. (short form /p)