%\VignetteIndexEntry{IBrokersRef}
\documentclass{article}

% IBrokers Reference Card version 0.2-7
% Copyright 2010 Jeffrey A. Ryan
% Extending the excellent vi-ref card design to the R package
% IBrokers
%
% %%%%%%% original GPL and copyright %%%%%%%
% Copyright 2002-2005 Donald Bindner
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

\usepackage{multicol}

\usepackage{color}
\definecolor{eWrapper}{rgb}{1,0.49,0.13}
\definecolor{func}{rgb}{0,0,0.6}

\usepackage[landscape, margin=0.5in]{geometry}
\setlength{\parindent}{0 in}

\begin{document}
\thispagestyle{empty}
\fontsize{9}{10}\selectfont

\newcommand{\key}[2]{#1 \hfill \texttt{\color{func} #2}\par}
\newcommand{\head}[1]{{\normalsize\textbf{#1}}\\}
\newcommand{\ewrappers}[1]{eWrapper methods:\par {\textsl{\color{eWrapper} #1}}\\}
\newcommand{\Rclass}[1]{\mbox{\textit{#1}}}
\newcommand{\Rfunc}[1]{\mbox{\texttt{#1}}}

\begin{multicols}{3}
<<setup, echo=FALSE>>=
options(continue="  ")
@
{\Large IBrokers Reference Card}\\
{\small IBrokers \Sexpr{packageVersion("IBrokers")}; TWS API 9.64}

\vskip 15pt

\vbox{\head{IBrokers R API Overview}
The IBrokers API parallels the official Java API
provided by Interactive Brokers, LLC to access
data and execution services provided to IB
clients. Commands can be run interactively
or automated.\par
\vskip 8pt
The official API documentation is grouped
by \mbox{\textsl{EClientSocket}} methods, \mbox{\textsl{EWrapper}}
methods, and \mbox{\textsl{SocketClient}} objects. This document
combines all related objects and methods into groups
by functionality.
\vskip 8pt
Where appropriate, \textsl{eWrapper} methods for processing
incoming messages from related calls are listed.
}

\vskip 10pt
\vbox{\head{Connection and Server}
Connecting to either the TWS or IB Gateway requires
setting connection parameters external to IBrokers. Once
enabled, the following commands can be used for connections
and details.
\vskip 5pt
\key{connect}{twsConnect, ibgConnect}
\key{disconnect}{twsDisconnect, close}
\key{check connection}{is.twsConnection, isConnected}
\key{set logging level}{setServerLogLevel}
\key{check server version}{serverVersion}
\key{request current time}{reqCurrentTime}
\key{request connection time}{twsConnectionTime}
}

\vskip 10pt

\vbox{\head{Contracts}
All requests require validly constructed \Rclass{twsContract}
objects.  The basic function to create a valid object is
\Rfunc{twsContract}, though IBrokers implements wrapper
functions to simplify commonly requested types such as
equity, cash, and futures.  Depending on the context
the constructors may need more or less detail.
\vskip 5pt
\key{create any contract}{twsContract}
\key{create equity contract}{twsEquity, twsSTK}
\key{create equity option contract}{twsOption, twsOPT}
\key{create future contract}{twsFuture, twsFUT}
\key{create future option contract}{twsFutureOpt, twsFOP}
\key{create currency contract}{twsCurrency, twsCASH}
\key{create combo}{twsBAG, twsComboLeg}
\key{create contract for difference}{twsCFD}
}

\vskip 10pt

\vbox{\head{Contract Details}
Given a full or partial \Rclass{twsContract}, 
returns a list of \Rclass{twsContractDetails} objects;
named lists containing contract details including a 
\texttt{contract} element of class \Rclass{twsContract}.  Many
IBrokers calls will accept \texttt{Contract} arguments of
\Rclass{twsContract} or \Rclass{twsContractDetails}.\par
\vskip 5pt
\key{request contract(s) description}{reqContractDetails}
\key{extract \Rclass{twsContract} from details}{as.twsContract}
\vskip 5pt
\ewrappers{contractDetails, bondContractDetails, contractDetailsEnd}
}

\vskip 10pt

\vbox{\head{Market Data}
Market Data provides for nearly real-time data
from Interactive Brokers.  Data is actually aggregated
into one-third second `snapshot' data from the
exchange, and subsequently passed along to the client.\par
\vskip 5pt
\key{request market data and process}{reqMktData}
\key{request market data (only)}{.reqMktData}
\key{cancel market data}{cancelMktData}
\vskip 5pt
\ewrappers{tickPrice, tickSize,
tickOptionComputation, tickGeneric
tickString, tickEFP, tickSnapshotEnd}
}

\vskip 10pt

\vbox{\head{Market Depth}
Depth of book varies according to contract, and may
not be available for all security types.\par
\vskip 5pt
\key{request market depth data}{reqMktDepth}
\key{cancel market depth data}{cancelMktDepth}
\vskip 5pt
\ewrappers{updateMktDepth, updateMktDepthL2}
}

\vskip 10pt

\vbox{\head{Real Time Bars}
Real-time bars are limited to 5-second bars by the official
API.  All other \texttt{barSize} values will fail.
Realtime bars  may not be available for all security types.\par
\vskip 5pt
\key{request real-time bars}{reqRealTimeBars}
\key{cancel real-time bars}{cancelRealTimeBars}
\vskip 5pt
\ewrappers{realtimeBars}
}

\vskip 10pt

\vbox{\head{Historical Data}
Depending on the contract, only specific combinations 
of \texttt{barSize} and \texttt{duration}
arguments are valid, and some security types have no
historical data.  \Rfunc{reqHistory} is 
an IBrokers only call, allowing for one year of 1 minute
bars, respecting IB timeouts (10 seconds) 
and maximum bars per request (2000).\par
\vskip 5pt
\key{request historical data}{reqHistoricalData}
\key{request maximum history}{reqHistory}
\key{cancel historical request}{cancelHistoricalData}
\vskip 5pt
Valid \texttt{barSize} values include: \texttt{1 secs},
\texttt{15 secs}, \texttt{1 min}, \texttt{2 mins},
\texttt{3 mins}, \texttt{5 mins}, \texttt{15 mins},
\texttt{30 mins}, \texttt{1 hour}, \texttt{1 day},
\texttt{1 week}, \texttt{1 month}, \texttt{3 months},
\texttt{1 year}.
\vskip 5pt
Valid \texttt{duration} form is \textit{`n S'}, where
\textit{n} is the number of periods of \textit{S}. The
second argument may be \texttt{S} (seconds),
\texttt{D} (days), \texttt{W} (weeks),
\texttt{M} (months), \texttt{Y} (year).
Year requests are limited to 1 year.
}

\vskip 10pt

\vbox{\head{Fundamental Data}
Reuters fundamental data
\vskip 5pt
\key{request fundamental data}{reqFundamentalData}
\key{cancel fundamental data}{cancelFundamentalData}
\vskip 5pt
\ewrappers{fundamentalData}
}

\vskip 10pt

\vbox{\head{News Bulletins}
Subscribe to news bulletins from Interactive Brokers.
\vskip 5pt
\key{subscribe}{reqNewsBulletins}
\key{unsubscribe}{cancelNewsBulletins}
\vskip 5pt
\ewrappers{newsBulletins}
}

\vskip 10pt

\vbox{\head{Pricing}
Calculate option values, price and implied volatility,
via the TWS engine.
\vskip 5pt
\key{calculate option price}{calculateOptionPrice}
\key{calculate option volatility}{calculateImpliedVolatility}
\vskip 5pt
\ewrappers{tickOptionCalculation}
}

%\newpage
\vskip 10pt

\vbox{\head{Orders}
Orders via the IB API, and the IBrokers API, require
three primary components: A \Rclass{twsContract} object,
a \Rclass{twsOrder} object, and a \Rfunc{placeOrder} call. Additionally,
a valid \mbox{orderId} is required to the twsOrder object. This is
found by calling \Rfunc{reqIds} on the twsConnection
object. reqIds operates directly on the connection
object by retrieving and then incrementing the next valid
order id in the connection object.\par
\vskip 5pt
\key{next valid order id}{reqIds}
\key{create order object}{twsOrder}
\vskip 5pt
\key{place order}{placeOrder}
\key{cancel order}{cancelOrder}
\vskip 5pt
\key{exercise options}{exerciseOptions}
\vskip 5pt
\key{open orders}{reqOpenOrders}
\key{all open orders}{reqAllOpenOrders, reqAutoOpenOrders}
\vskip 5pt
\ewrappers{orderStatus, openOrder, nextValidId, execDetails}
\vskip 5pt
<<orders, eval=FALSE, keep.source=TRUE>>=
placeOrder(twsconn=tws,
           Contract=twsSTK("AAPL"), 
           Order=twsOrder(reqIds(tws),
                          "BUY", 
                          10, 
                          "MKT"))
@
}

\vskip 10pt

\vbox{\head{Account}
Account data is requested on a subscription basis. The user
subscribes to a continuously updated feed from the TWS
by passing the connection object and the \texttt{subscribe}
argument set to \texttt{TRUE}; unsubscribe with \texttt{FALSE}.
The \texttt{.reqAccountUpdates} function will return
immediately and will begin or end a subscription; account
messages must be handled by the user. \texttt{reqAccountUpdates}
(without the prepended `dot') will subscribe,
collect data, and unsubscribe -- returning an
\texttt{AccountUpdate} object which may be processed
with \texttt{twsPortfolioValue}.
\vskip 5pt
\key{get account data}{reqAccountUpdates}
\key{subscribe account updates (only)}{.reqAccountUpdates}
\key{cancel account updates}{cancelAccountUpdates}
\vskip 5pt
\key{view portfolio}{twsPortfolioValue}
\vskip 5pt
\ewrappers{updateAccountValue, updatePortfolio, \mbox{updateAccountTime},
           accountDownloadEnd}
}

\vskip 10pt

\vbox{\head{Executions}
Returns execution details in a \textit{twsExecution} object.
This method is currently only implemented as a request, with
no built-in mechanism to manage response data apart from
it being discarded.
\vskip 5pt
\key{request execution data}{reqExecutions}
\key{filter argument}{reqExecutionFilter}
\vskip 5pt
\ewrappers{execDetails, execDetailsEnd}
}

\vskip 10pt

\vbox{\head{Financial Advisors}
Funtions for FA-enabled accounts
\vskip 5pt
\key{request list of accounts}{reqManagedAccts}
\key{request FA configuration (XML)}{requestFA}
\key{change FA configuration}{replaceFA}
\vskip 5pt
\ewrappers{managedAccts, receiveFA}
}

\vskip 10pt

\vbox{\head{Scanner}
Interactive Brokers scanner data ...
\vskip 5pt
\key{scanner params (XML)}{reqScannerParameters}
\key{scanner subscription object}{twsScannerSubscription}
\key{return scanner results}{reqScannerSubscription}
\vskip 5pt
\key{subscribe to scanner}{.reqScannerSubscription}
\key{unsubscribe to scanner}{cancelScannerSubscription}
\vskip 5pt
\ewrappers{scannerParameters, scannerData}
}

\vskip 10pt

\vbox{\head{eWrapper}
eWrappers contain the callback methods
for all incoming message types. These are closures
in R that contain functions and data. These functions
are called based on incoming message types from the
TWS.
\vskip 5pt
\key{new eWrapper}{eWrapper}
\key{market data to vector(s)}{eWrapper.data}
\key{market data to csv}{eWrapper.MktData.CSV}
\vskip 5pt
}


\vskip 10pt

\vbox{\head{DISCLAIMER}
IBROKERS IS NOT ENDORSED, AFFILIATED, OR CONNECTED TO
INTERACTIVE BROKERS, LLC.  INTERACTIVE BROKERS IS
TRADEMARKED AND PROPERTY OF INTERACTIVE BROKERS, LLC.
\vskip 5pt
IBROKERS COMES WITH NO WARRANTY, EXPRESSED OR IMPLIED,
AND IS FOR USE \emph{AT YOUR OWN RISK}.
}
\vskip 10pt
\vbox{\small Copyright 2010. Jeffrey A. Ryan}
\end{multicols}
\end{document}