IBCSharp

IBCSharp consists of an improved version of Karl Schulze's
Interactive Brokers C# API (IBClient.dll), a WinForms C# algorithmic daytrading program (DayTrade.exe), a WinForms C# long-term trading program (LongTermTrade.exe), and a historical data downloader that downloads intraday data from IB and end-of-day data from moneycentral.msn.com in eSignal tick replay format (HistoricalDataDownloader.exe).
The main improvement of IBCSharp's IBClient over Karl's IBClient is that IBCSharp's IBClient events are called with EventArgs that include the Contract responsible for why the event is being called. In Karl's version events are called with EventArgs that include int request Ids but no Contract. These int request Ids with no Contract lead to the programmer needing to engage in Dictionary<int, Contract> management to obtain the Contract. My IBClient takes care of that Dictionary<int, Contract> management for you and gives you EventArgs with int request Id and Contract.
IBCSharp's IBClient also features optimized network code by way of preallocated byte arrays, doing work outside of lock statements instead of inside of lock statements, and sending requests to Trader Workstation (TWS) using one byte array send per request instead of multiple byte array sends per request.
IBCSharp's IBClient corresponds to IB API 9.63.
DayTrade.exe subscribes to stock, future, or currency data and then submits orders based on trading decisions made by an instance of a subclass of abstract class TradingStrategy. Once an opening order is filled, the opened position is bracketed with a profit limit order, a trailing stop order, and a time stop order. Trading in a margin-safe way is only supported for stocks and futures. Currency trading happens without pre-order-submission margin checks because I do not understand
IB's unclear, single-example explanation of how IB calculates currency margin.
This is DayTrade.exe with Trader Workstation in the background:

For testing DayTrade.exe there is TestingTradingStrategy.cs, which is a moving average crossover strategy:

Indicators that come with DayTrade.exe:

DayTrade.exe writes incoming five-second PriceBars into eSignal tick replay format .epf files. Strategy results are written to .txt files:

You will want to be thoroughly good at eSignal and eSignal's EFS language (a modified JavaScript) if you are to one day make money using IBCSharp. There is this tutorial to get you started:
"How to setup eSignal for backtesting trading strategies and for confirming that DayTrade.exe makes the same trades that eSignal makes".
LongTermTrade.exe submits limit orders for stocks with limit prices derived from yesterday's closing prices. Once the opening limit order is filled the new position is bracketed with the same three bracket orders as above but the time stop order becomes active after some number of days instead of minutes. The idea with LongTermTrade.exe is to decide upon trades tonight and then tomorrow morning before market open submit limit orders and then when the market opens the limit orders fill.
This is LongTermTrade.exe on 12/22/2009 6:25 AM PST after having submit three orders with limit prices derived from yesterday's closing prices. These three orders will fill at market open 6:30 AM PST (ideally. Sometimes takes up to two minutes for the orders to fill):

This is LongTermTrade.exe on 12/22/2009 right after 6:30 AM PST. Note the 10-days-in-the-future Start Time of the time stop orders.

This is how the "Intraday Data From IB" tab of HistoricalDataDownloader.exe works:


1 year of intraday data can be downloaded for stocks and 1.5 years of intraday data can be downloaded for futures YM, ES, and NQ. Here is the code that handles the symbol to be downloaded:

This is how the "EOD Data from moneycentral.msn.com" tab of HistoricalDataDownloader.exe works. You provide a list of stock symbols and then the good people at Microsoft send back 5 years of end-of-day data for each symbol. All the 5 years of data for each symbol is written to a single eSignal tick replay format .epf file. eSignal backtesting file Strategy.efs available in
How to setup eSignal for backtesting trading strategies and for confirming that DayTrade.exe makes the same trades that eSignal makes has code in it that is able to handle data from multiple symbols inside of a single .epf file.


Command Window aliases I find useful while developing IBCSharp. I have View.CommandWindow assigned to Ctrl+/.
alias account open account.cs
alias contract open contract.cs
alias five open fivesecondpricebarsubscription.cs
alias ib open ibclient.cs
alias main open daytrademainform.cs
alias man open fivesecondpricebarsubscriptionmanager.cs
alias fac open daytradefacade.cs
alias my open mytradingstrategy.cs
alias openorder open openordermanager.cs
alias creator open ordercreator.cs
alias ordersub open ordersubmitter.cs
alias port open portfolio.cs
alias program open daytradeprogram.cs
alias test open testingtradingstrategy.cs
alias trade open tradingstrategy.cs
IBCSharp Is Unit Tested, For Your Protection And Mine!
