Last active
September 2, 2018 19:11
-
-
Save alf239/0c379c302a59541d7c0b61054ac30e7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Needs["JLink`"]; | |
| ReinstallJava[ | |
| ClassPath -> "C:\\TWS_API\\source\\JavaClient\\TwsApi.jar"]; | |
| LoadJavaClass["com.ib.client.Types$MktDataType"]; | |
| LoadJavaClass["com.ib.client.TickType"]; | |
| myAccounts := {}; | |
| SetSharedVariable[myAccounts]; | |
| (*IConnectionHandler*) | |
| connected[] := Print@{"* connected"}; | |
| disconnected[] := Print@{"* disconnected"}; | |
| accountList[(*List<String>*)list_] := | |
| Block[{lst = list@toArray[]}, myAccounts = lst; | |
| Print@{"* accountList", lst}]; | |
| error[(*Exception*)e_] := Print@{"* error", e}; | |
| message[(*int*)id_,(*int*)errorCode_,(*String*)errorMsg_] := | |
| Print@{"* message", id, errorCode, errorMsg} | |
| show[(*String*)string_] := Print@{"* show", string} | |
| connectionHandler = | |
| ImplementJavaInterface[ | |
| "com.ib.controller.ApiController$IConnectionHandler", {"error" -> | |
| "error", "connected" -> "connected", | |
| "disconnected" -> "disconnected", "accountList" -> "accountList", | |
| "message" -> "message", "show" -> "show"}]; | |
| (*IAccountHandler*) | |
| accountValue[(*String*)account_,(*String*)key_,(*String*) | |
| value_,(*String*)currency_] := | |
| Print@{"# accountValue", account, key, value, currency}; | |
| accountTime[(*String*)timeStamp_] := | |
| Print@{"# accountTime", timeStamp}; | |
| accountDownloadEnd[(*String*)account_] := | |
| Print@{"# accountDownloadEnd", account}; | |
| updatePortfolio[(*Position*)position_] := | |
| Print@{"# updatePortfolio", position}; | |
| accountHandler = | |
| ImplementJavaInterface[ | |
| "com.ib.controller.ApiController$IAccountHandler", {"accountValue" \ | |
| -> "accountValue", "accountTime" -> "accountTime", | |
| "accountDownloadEnd" -> "accountDownloadEnd", | |
| "updatePortfolio" -> "updatePortfolio"}]; | |
| (*ITopMktDataHandler*) | |
| tickPrice[(*TickType*) tickType_,(*double*) price_,(*int*) | |
| canAutoExecute_] := | |
| Print@{"- tickPrice", tickType@name[], price, canAutoExecute}; | |
| tickSize[(*TickType*) tickType_,(*int*) size_] := | |
| Print@{"- tickSize", tickType@name[], size}; | |
| tickString[(*TickType*) tickType_,(*String*) value_] := | |
| Print@{"- tickString", tickType@name[], value}; | |
| tickSnapshotEnd[] := Print@{"- tickSnapshotEnd"}; | |
| marketDataType[(*MktDataType*) marketDataType_] := | |
| Print@{"- marketDataType", marketDataType@name[]}; | |
| topMktDataHandler = | |
| ImplementJavaInterface[ | |
| "com.ib.controller.ApiController$ITopMktDataHandler", { | |
| "tickPrice" -> "tickPrice", | |
| "tickSize" -> "tickSize", | |
| "tickString" -> "tickString", | |
| "tickSnapshotEnd" -> "tickSnapshotEnd", | |
| "marketDataType" -> "marketDataType" | |
| }]; | |
| (*ILogger*) | |
| inLog[(*String*)line_] := Print@{"in", line}; | |
| loggerI = | |
| ImplementJavaInterface[ | |
| "com.ib.controller.ApiConnection$ILogger", {"log" -> "inLog"}]; | |
| (*ILogger*) | |
| outLog[(*String*)line_] := Print@{"out", line}; | |
| loggerO = | |
| ImplementJavaInterface[ | |
| "com.ib.controller.ApiConnection$ILogger", {"log" -> "outLog"}]; | |
| api = JavaNew["com.ib.controller.ApiController", connectionHandler, | |
| loggerI, loggerO]; | |
| api@connect["127.0.0.1", 7497, 2, ""]; | |
| client = api@client[]; | |
| contract = JavaNew["com.ib.client.Contract"]; | |
| contract@symbol["ES"]; | |
| contract@secType["FOP"]; | |
| contract@lastTradeDateOrContractMonth["20181221"]; | |
| contract@strike[2000.0]; | |
| contract@right["P"]; | |
| contract@multiplier["50"]; | |
| contract@exchange["GLOBEX"]; | |
| contract@currency["USD"]; | |
| genericTicklist = ""(*"100"*); | |
| snapshot = True(*False*); | |
| Fields[MktDataType] | |
| api@reqMktDataType[Types$MktDataType`Delayed]; | |
| api@reqTopMktData[contract, genericTicklist, snapshot, | |
| topMktDataHandler]; | |
| api@reqAccountUpdates[True, "DU228249", accountHandler]; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: