9
$\begingroup$

TWS is the trading platform of Interactive Brokers. Accessing it programmatically could be useful for various purposes. How could I access it from Mathematica ?

$\endgroup$
10
$\begingroup$

Here's a minimal example using the Java API of IB. Anything else can be done in theory by analogy. It works in Windows, but should work on other platforms as well as it's using Java.

Results are displayed in the Mathematica messages window.

Prerequisite
Read the beginning of https://www.interactivebrokers.com/download/JavaAPIGettingStarted.pdf , download the API and export the source\JavaClient project provided into a JAR file. You can do this in Eclipse for example, which is easy as the provided source code is an Eclipse project.

References
https://www.interactivebrokers.com/en/software/api/api_Left.htm
https://www.interactivebrokers.com/en/software/tws/twsguide_Left.htm
Api calls' examples: http://tradingbot.blogspot.fr/2009/07/proof-of-concept-8-of-8g-complex.html
Overview of the API functions:
http://cran.r-project.org/web/packages/IBrokers/vignettes/IBrokersREFCARD.pdf
http://cran.r-project.org/web/packages/IBrokers/IBrokers.pdf

Needs["JLink`"]
ReinstallJava[ClassPath->"yourPath\\twsJar.jar"] ;

LoadJavaClass["java.util.Collections"];
emptyList=Collections`emptyList[];

LoadJavaClass["com.ib.client.TickType"];

(*Defining some interfaces (implementations of abstract methods) for eWrapper, look at the AnyWrapper and EWrapper class for a list of interfaces.*)  
(*The main thing to understand is that the arguments of these Mathematica functions ARE the results, so you can do whatever you want with them.*)  
(*You can modify these functions even after eWrapper has been defined, which makes development easier.*)
error[m___]:=Print@{"error",m};
tickPrice[(*int*)tickerId_,(*int*)field_,(*double*)price_,(*int*)canAutoExecute_]:=Print@{"tickPrice",tickerId,TickType`getField[field],price};
tickOptionComputation[(*int*) tickerId_,(*int*) field_,(*double*) impliedVol_,(*double*) delta_,(*double*) optPrice_,(*double*) pvDividend_,(*double*) gamma_,(*double*) vega_,(*double*) theta_,(*double*) undPrice_]:=Print@{"tickOptionComputation",tickerId,TickType`getField[field],impliedVol};

(*Wrapper needed for replies from the server, each function in the wrapper can be called by the client upon an answer from the server and passed to a Mathematica implementation*)
eWrapper=ImplementJavaInterface["com.ib.client.EWrapper",{"error"->"error","tickPrice"->"tickPrice","tickOptionComputation"->"tickOptionComputation"}];
eWrapper@error["hello"];

eClient=JavaNew["com.ib.client.EClientSocket",eWrapper];
eClient@eConnect["127.0.0.1",7496,0];
(*press yes in TWS to accept the connection*)
eClient@isConnected[];

contract=JavaNew["com.ib.client.Contract"];
contract@mUsymbol="ES";
contract@mUsecType="FOP";
contract@mUexpiry="20141219";
contract@mUstrike=2000.0;
contract@mUright="P";
contract@mUmultiplier="50";
contract@mUexchange="GLOBEX";
contract@mUcurrency="USD";

tickerId=0;
genericTicklist=""(*"100"*);
snapshot=True(*False*);
eClient@reqMktData[tickerId,contract,genericTicklist,snapshot,emptyList]
$\endgroup$
  • $\begingroup$ Could you provide some additional pointers? Even something as trivial as compilation is a challenge for non Java programmers. I read the Java API documentation and it seems relatively straightforward, once I get the initial setup done. $\endgroup$ – Jonathan Kinlay Nov 6 '16 at 17:20
  • $\begingroup$ Two years I wrote this post already ... Time goes fast. I've spent so much time implementing a bridge between IB and MM, now that it works, I'm not even thinking about it anymore. The biggest difficulty is that many things are not documented by IB, the only way to understand how it works is by trial and error. $\endgroup$ – faysou Nov 6 '16 at 18:43
  • $\begingroup$ Do you distribute this code? $\endgroup$ – Igor Rivin Nov 6 '16 at 19:22
  • $\begingroup$ No I don't currently. $\endgroup$ – faysou Nov 6 '16 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.