Skip to content

Instantly share code, notes, and snippets.

@lnostdal
Last active July 31, 2018 22:26
Show Gist options
  • Save lnostdal/097be3db8a4b4aafe4453678b74aab68 to your computer and use it in GitHub Desktop.
Save lnostdal/097be3db8a4b4aafe4453678b74aab68 to your computer and use it in GitHub Desktop.
Interactive Brokers API test in Clojure
(def %bnds (get-thread-bindings)) ;; TODO: Seems kind of hacky, but needed as sometimes (EWrapper/error Exception) is sourced from different threads(!).
(deftype IB
[^:unsynchronized-mutable ^int next-order-id
^:unsynchronized-mutable ^com.ib.client.EClient eclient
^:unsynchronized-mutable ^com.ib.client.EReaderSignal ereader-signal]
com.ib.client.EWrapper
(^void error [this ^int id ^int errorCode ^String errorMsg]
(with-bindings %bnds
(debug :EWrapper/error {:id id :errorCode errorCode :errorMsg errorMsg
:serverVersion (.serverVersion eclient)})
(when (= errorCode 506)
(clojure.stacktrace/print-stack-trace (Exception. "")))))
(^void error [this ^Exception e]
(with-bindings %bnds
(debug :EWrapper/error e)))
(^void error [this ^String e]
(with-bindings %bnds
(debug :EWrapper/error e)))
(^void connectAck [this]
(debug :EWrapper/connectAck {:serverVersion (.serverVersion eclient)})
(let [ereader (com.ib.client.EReader. eclient ereader-signal)]
(.start ereader)
(future
(while (.isConnected eclient)
(try
(.processMsgs ereader)
(catch Throwable e
(debug e :IB/EReader_thread))))
(debug :IB/EReader_thread "thread ending.."))))
(^void nextValidId [this ^int orderId]
(debug :EWrapper/nextValidId orderId)
(set! next-order-id orderId))
(^void accountSummary [this ^int req-id ^String account ^String tag ^String value ^String currency]
(debug :EWrapper/accountSummary {:req-id req-id :account account :tag tag :value value :currency currency}))
(^void accountSummaryEnd [this ^int req-id]
(debug :EWrapper/accountSummaryEnd {:req-id req-id}))
(^void managedAccounts [this ^String accountList]
(debug :EWrapper/managedAccounts {:accountList accountList}))
clojure.lang.IFn
;;(invoke [this socket]) ;; TODO.
(invoke [this host port clientId]
(debug :EWrapper/IFn {:host host :port port :clientId clientId})
(set! eclient (com.ib.client.EClientSocket. this ereader-signal))
(.eConnect ^com.ib.client.EClientSocket eclient host port clientId))
java.io.Closeable ;;IIB
(close [this]
(.eDisconnect ^com.ib.client.EClientSocket eclient))
Object
(finalize [this] ;; Fallback cleanup.
(.close this)))
(defn new-IB ^IB [^String host ^long port ^long clientId]
(let [ib (IB. 0 nil (com.ib.client.EJavaSignal.))]
(ib host port clientId)
ib))
;;;;;;;;;;;;;;;;;;;;;;;;
quantataraxia.core> (def -ib- (new-IB "127.0.0.1" 7497 0))
18-07-31 22:23:23 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:62] - :EWrapper/IFn {:host "127.0.0.1", :port 7497, :clientId 0}
18-07-31 22:23:23 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:34] - :EWrapper/connectAck {:serverVersion 142}
18-07-31 22:23:24 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:20] - :EWrapper/error {:id -1, :errorCode 2104, :errorMsg "Market data farm connection is OK:usfuture", :serverVersion 142}
18-07-31 22:23:24 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:20] - :EWrapper/error {:id -1, :errorCode 2104, :errorMsg "Market data farm connection is OK:cashfarm", :serverVersion 142}
18-07-31 22:23:24 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:20] - :EWrapper/error {:id -1, :errorCode 2104, :errorMsg "Market data farm connection is OK:usfarm", :serverVersion 142}
18-07-31 22:23:24 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:20] - :EWrapper/error {:id -1, :errorCode 2106, :errorMsg "HMDS data farm connection is OK:cashhmds", :serverVersion 142}
18-07-31 22:23:24 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:20] - :EWrapper/error {:id -1, :errorCode 2106, :errorMsg "HMDS data farm connection is OK:ushmds", :serverVersion 142}
18-07-31 22:23:23 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:56] - :EWrapper/managedAccounts {:accountList "DU229529"}
18-07-31 22:23:24 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:46] - :EWrapper/nextValidId 1
quantataraxia.core> (.close -ib-)
18-07-31 22:23:26 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:27] - :EWrapper/error java.net.SocketException: Socket closed (.close -ib-)
18-07-31 22:23:26 lnostdal-Swift-SF514-51 DEBUG [quantataraxia.core:43] - :IB/EReader_thread thread ending..
quantataraxia.core>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment