Module Tws

module Tws: sig .. end
A TWS client

module Query_id: Core.Std.Unique_id 
type t 
A TWS client

Connection and server


val with_client : ?do_logging:bool ->
?client_id:Client_id.t ->
host:string ->
port:int ->
on_handler_error:[ `Call of Core.Std.Error.t -> unit | `Ignore | `Raise ] ->
(t -> unit Async.Std.Deferred.t) -> unit Async.Std.Deferred.t
with_client ~host ~port ~on_handler_error handler connects to the TWS software on (host, port) and runs the handler until an exception is raised or the returned Deferred.t is determined.

on_handler_error determines what happens if the handler raises an exception.

The standard port for the TWS software is 7496. You can connect IB Gateway on port 4001 as an low-resource alternative compared to TWS.

val with_client_or_error : ?do_logging:bool ->
?client_id:Client_id.t ->
host:string ->
port:int ->
(t -> unit Async.Std.Deferred.t) ->
unit Core.Std.Or_error.t Async.Std.Deferred.t
Same as with_client, but returns an Error if the connection was not successful or an exception was raised in the handler.
val is_connected : t -> bool
is_connected t checks whether the TWS client t is connected.
val state : t -> [ `Connected | `Connecting | `Disconnected ]
state t returns the connection of the TWS client t.
val set_server_log_level : t ->
level:[ `Detail | `Error | `Information | `System | `Warning ] -> unit
set_server_log_level level sets the log entry detail level of the TWS software when processing API requests.
val server_time : t -> Core.Std.Time.t Core.Std.Or_error.t Async.Std.Deferred.t
server_time t returns the current time from the TWS server or an Error.
val server_time_exn : t -> Core.Std.Time.t Async.Std.Deferred.t
Same as server_time, but raises an exception if an Error was returned.
val server_version : t -> int option
server_version t returns the version of the TWS server upon successful connection of the TWS client t, otherwise None is returned.
val connection_time : t -> Core.Std.Time.t option
connection_time t returns the time the client t was connected to TWS or None when no connection was established.
val account_code : t -> Account_code.t option
account_code t returns the code of the Interactive Brokers account upon successful connection of the TWS client t, otherwise None is returned.

Market data


module Market_data: sig .. end
val market_data : ?snapshot:bool ->
?tick_types:Tick_type.t list ->
t ->
contract:[< Security_type.t ] Contract.t ->
(Market_data.t Tws_result.t Async.Std.Pipe.Reader.t * Query_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
market_data t contract requests streaming market data from TWS for the given contract. Streaming market data is written to a Pipe.t that is returned together with a query id used to cancel the request via cancel_market_data from below.

When snapshot is set to True the market data pipe contains only the latest snapshot of the data.

If futher tick_types are supplied the marke data pipe is extended with their current values.

val market_data_exn : ?snapshot:bool ->
?tick_types:Tick_type.t list ->
t ->
contract:[< Security_type.t ] Contract.t ->
(Market_data.t Async.Std.Pipe.Reader.t * Query_id.t) Async.Std.Deferred.t
Same as market_data, but raises an exception instead of returning an Error explicitly.
val cancel_market_data : t -> Query_id.t -> unit
cancel_market_data t query_id cancels the market data request that corresponds to the given query_id.
val option_price : t ->
contract:[ `Option ] Contract.t ->
volatility:float ->
underlying_price:Price.t -> Price.t Core.Std.Or_error.t Async.Std.Deferred.t
option_price t contract volatility price asks TWS to calculate the option price for the given contract based on the given volatility and current price of the option's underlying. An Error is returned whenever something goes wrong.
val option_price_exn : t ->
contract:[ `Option ] Contract.t ->
volatility:float -> underlying_price:Price.t -> Price.t Async.Std.Deferred.t
Same as option_price, but raises an exception in case of an Error.
val implied_volatility : t ->
contract:[ `Option ] Contract.t ->
option_price:Price.t ->
underlying_price:Price.t -> float Core.Std.Or_error.t Async.Std.Deferred.t
implied_volatility t contract option_price under_price asks TWS to calculate the implied volatility for the given option contract based on the price of the option and the underlying. An Error is returned whenever something goes wrong.
val implied_volatility_exn : t ->
contract:[ `Option ] Contract.t ->
option_price:Price.t ->
underlying_price:Price.t -> float Async.Std.Deferred.t
Same as implied_volatility, but raises an exception in case of an Error.

Orders


val submit_order : t ->
contract:[< Security_type.t ] Contract.t ->
order:([< Order_action.t ], [< Order_type.t ]) Order.t ->
(Std_internal.Order_status.t Tws_result.t Async.Std.Pipe.Reader.t *
Std_internal.Order_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
val submit_order_exn : t ->
contract:[< Security_type.t ] Contract.t ->
order:([< Order_action.t ], [< Order_type.t ]) Order.t ->
(Std_internal.Order_status.t Async.Std.Pipe.Reader.t *
Std_internal.Order_id.t)
Async.Std.Deferred.t
val cancel_order_status : t -> Std_internal.Order_id.t -> unit

Account and portfolio


val account_updates : t ->
Std_internal.Account_update.t Async.Std.Pipe.Reader.t Core.Std.Or_error.t
Async.Std.Deferred.t
val account_updates_exn : t ->
Std_internal.Account_update.t Async.Std.Pipe.Reader.t Async.Std.Deferred.t
val portfolio : t ->
Std_internal.Position.t Async.Std.Pipe.Reader.t Core.Std.Or_error.t
Async.Std.Deferred.t
val portfolio_exn : t -> Std_internal.Position.t Async.Std.Pipe.Reader.t Async.Std.Deferred.t
val commissions : t -> Std_internal.Commission.t Async.Std.Pipe.Reader.t

Execution reports


val executions : t -> Std_internal.Execution.t Async.Std.Pipe.Reader.t
val filter_executions : ?time:Core.Std.Time.t ->
t ->
contract:[< Security_type.t ] Contract.t ->
action:Order_action.t ->
Std_internal.Execution.t Tws_result.t Async.Std.Pipe.Reader.t
Core.Std.Or_error.t Async.Std.Deferred.t
val filter_executions_exn : ?time:Core.Std.Time.t ->
t ->
contract:[< Security_type.t ] Contract.t ->
action:Order_action.t ->
Std_internal.Execution.t Async.Std.Pipe.Reader.t Async.Std.Deferred.t

Contract details


val contract_details : t ->
?con_id:Std_internal.Contract_id.t ->
?multiplier:int ->
?listing_exchange:Exchange.t ->
?local_symbol:Symbol.t ->
?sec_id:Std_internal.Security_id.t ->
?include_expired:bool ->
?exchange:Exchange.t ->
?option_right:Std_internal.Option_right.t ->
?expiry:Core.Std.Date.t ->
?strike:Price.t ->
currency:Currency.t ->
sec_type:Security_type.t ->
Symbol.t ->
Std_internal.Contract_data.t Tws_result.t Async.Std.Pipe.Reader.t
Core.Std.Or_error.t Async.Std.Deferred.t
The request returns a pipe containing additional information for all contracts that met the criteria specified by the given parameters.
val contract_details_exn : t ->
?con_id:Std_internal.Contract_id.t ->
?multiplier:int ->
?listing_exchange:Exchange.t ->
?local_symbol:Symbol.t ->
?sec_id:Std_internal.Security_id.t ->
?include_expired:bool ->
?exchange:Exchange.t ->
?option_right:Std_internal.Option_right.t ->
?expiry:Core.Std.Date.t ->
?strike:Price.t ->
currency:Currency.t ->
sec_type:Security_type.t ->
Symbol.t ->
Std_internal.Contract_data.t Async.Std.Pipe.Reader.t Async.Std.Deferred.t
Same as contract_details, but raises an exception instead of returning an Error.
val contract_data : t ->
contract:[< Security_type.t ] Contract.t ->
Std_internal.Contract_data.t Core.Std.Or_error.t Async.Std.Deferred.t
contract_data t contract returns additional information for the given the contract or an Error.
val contract_data_exn : t ->
contract:[< Security_type.t ] Contract.t ->
Std_internal.Contract_data.t Async.Std.Deferred.t
Same as contract_data, but raises an exception in case of an Error.

Futures and option chains


val futures_chain : t ->
?con_id:Std_internal.Contract_id.t ->
?multiplier:int ->
?listing_exchange:Exchange.t ->
?local_symbol:Symbol.t ->
?sec_id:Std_internal.Security_id.t ->
?include_expired:bool ->
?exchange:Exchange.t ->
currency:Currency.t ->
Symbol.t ->
[< Security_type.t > `Futures ] Contract.t list Core.Std.Or_error.t
Async.Std.Deferred.t
Requests a futures chain for the given contract specifications and returns the chain as a list of futures contracts or an Error. It is a special version of contract_details from above.
val futures_chain_exn : t ->
?con_id:Std_internal.Contract_id.t ->
?multiplier:int ->
?listing_exchange:Exchange.t ->
?local_symbol:Symbol.t ->
?sec_id:Std_internal.Security_id.t ->
?include_expired:bool ->
?exchange:Exchange.t ->
currency:Currency.t ->
Symbol.t ->
[< Security_type.t > `Futures ] Contract.t list Async.Std.Deferred.t
Same as futures_chain, but raises an exception in case of an Error.
val option_chain : t ->
?con_id:Std_internal.Contract_id.t ->
?multiplier:int ->
?listing_exchange:Exchange.t ->
?local_symbol:Symbol.t ->
?sec_id:Std_internal.Security_id.t ->
?include_expired:bool ->
?exchange:Exchange.t ->
?expiry:Core.Std.Date.t ->
?strike:Price.t ->
option_right:Std_internal.Option_right.t ->
currency:Currency.t ->
Symbol.t ->
[< Security_type.t > `Option ] Contract.t list Core.Std.Or_error.t
Async.Std.Deferred.t
Requests an option chain for the given contract specifications and returns the chain as a list of option contracts or an Error. It is a special version of contract_details from above.
val option_chain_exn : t ->
?con_id:Std_internal.Contract_id.t ->
?multiplier:int ->
?listing_exchange:Exchange.t ->
?local_symbol:Symbol.t ->
?sec_id:Std_internal.Security_id.t ->
?include_expired:bool ->
?exchange:Exchange.t ->
?expiry:Core.Std.Date.t ->
?strike:Price.t ->
option_right:Std_internal.Option_right.t ->
currency:Currency.t ->
Symbol.t ->
[< Security_type.t > `Option ] Contract.t list Async.Std.Deferred.t
Same as option_chain, but raises an exception in case of an Error.

Market depth


val market_depth : ?num_rows:int ->
t ->
contract:[< Security_type.t ] Contract.t ->
(Std_internal.Book_update.t Tws_result.t Async.Std.Pipe.Reader.t * Query_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
val market_depth_exn : ?num_rows:int ->
t ->
contract:[< Security_type.t ] Contract.t ->
(Std_internal.Book_update.t Async.Std.Pipe.Reader.t * Query_id.t)
Async.Std.Deferred.t
val cancel_market_depth : t -> Query_id.t -> unit

History


val history : ?bar_size:[ `Fifteen_min
| `Fifteen_sec
| `Five_min
| `Five_sec
| `One_day
| `One_hour
| `One_min
| `One_sec
| `Thirty_min
| `Thirty_sec
| `Three_min
| `Two_min ] ->
?bar_span:[ `Day of int
| `Month of int
| `Sec of int
| `Week of int
| `Year of int ] ->
?use_tradehours:bool ->
?tick_type:[ `Ask
| `Bid
| `Bid_ask
| `Historical_volatility
| `Implied_volatility
| `Midpoint
| `Option_volume
| `Trades ] ->
?until:Core.Std.Time.t ->
t ->
contract:[< Security_type.t ] Contract.t ->
Std_internal.History.t Tws_result.t Core.Std.Or_error.t Async.Std.Deferred.t
val history_exn : ?bar_size:[ `Fifteen_min
| `Fifteen_sec
| `Five_min
| `Five_sec
| `One_day
| `One_hour
| `One_min
| `One_sec
| `Thirty_min
| `Thirty_sec
| `Three_min
| `Two_min ] ->
?bar_span:[ `Day of int
| `Month of int
| `Sec of int
| `Week of int
| `Year of int ] ->
?use_tradehours:bool ->
?tick_type:[ `Ask
| `Bid
| `Bid_ask
| `Historical_volatility
| `Implied_volatility
| `Midpoint
| `Option_volume
| `Trades ] ->
?until:Core.Std.Time.t ->
t ->
contract:[< Security_type.t ] Contract.t ->
Std_internal.History.t Async.Std.Deferred.t

Realtime bars


val realtime_bars : ?bar_size:[ `Fifteen_min
| `Fifteen_sec
| `Five_min
| `Five_sec
| `One_min
| `Thirty_min
| `Thirty_sec
| `Three_min
| `Two_min ] ->
?tick_type:[ `Ask | `Bid | `Midpoint | `Trades ] ->
?use_tradehours:bool ->
t ->
contract:[< Security_type.t ] Contract.t ->
(Std_internal.Bar.t Tws_result.t Async.Std.Pipe.Reader.t * Query_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
val realtime_bars_exn : ?bar_size:[ `Fifteen_min
| `Fifteen_sec
| `Five_min
| `Five_sec
| `One_min
| `Thirty_min
| `Thirty_sec
| `Three_min
| `Two_min ] ->
?tick_type:[ `Ask | `Bid | `Midpoint | `Trades ] ->
?use_tradehours:bool ->
t ->
contract:[< Security_type.t ] Contract.t ->
(Std_internal.Bar.t Async.Std.Pipe.Reader.t * Query_id.t)
Async.Std.Deferred.t
val cancel_realtime_bars : t -> Query_id.t -> unit

TAQ data


module Trade: sig .. end
val trades : t ->
contract:[< Security_type.t ] Contract.t ->
(Trade.t Tws_result.t Async.Std.Pipe.Reader.t * Query_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
val trades_exn : t ->
contract:[< Security_type.t ] Contract.t ->
(Trade.t Async.Std.Pipe.Reader.t * Query_id.t) Async.Std.Deferred.t
val cancel_trades : t -> Query_id.t -> unit
module Quote: sig .. end
val quotes : t ->
contract:[< Security_type.t ] Contract.t ->
(Quote.t Tws_result.t Async.Std.Pipe.Reader.t * Query_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
val quotes_exn : t ->
contract:[< Security_type.t ] Contract.t ->
(Quote.t Async.Std.Pipe.Reader.t * Query_id.t) Async.Std.Deferred.t
val cancel_quotes : t -> Query_id.t -> unit
module TAQ: sig .. end
val taq_data : t ->
contract:[< Security_type.t ] Contract.t ->
(TAQ.t Tws_result.t Async.Std.Pipe.Reader.t * Query_id.t)
Core.Std.Or_error.t Async.Std.Deferred.t
val taq_data_exn : t ->
contract:[< Security_type.t ] Contract.t ->
(TAQ.t Async.Std.Pipe.Reader.t * Query_id.t) Async.Std.Deferred.t
val cancel_taq_data : t -> Query_id.t -> unit

TAQ snapshots


val latest_quote : t ->
contract:[< Security_type.t ] Contract.t ->
Quote.t Core.Std.Or_error.t Async.Std.Deferred.t
latest_quote t contract returns either the latest quote for the given contract or an Error.
val latest_quote_exn : t ->
contract:[< Security_type.t ] Contract.t -> Quote.t Async.Std.Deferred.t
Same as latest_quote but raises an exception in case of an Error.
val latest_trade : t ->
contract:[< Security_type.t ] Contract.t ->
Trade.t Core.Std.Or_error.t Async.Std.Deferred.t
latest_trade t contract returns either the latest trade for the given contract or an Error.
val latest_trade_exn : t ->
contract:[< Security_type.t ] Contract.t -> Trade.t Async.Std.Deferred.t
Same as latest_trade but raises an exception in case of an Error.

Close snapshot


module Close: sig .. end
val latest_close : t ->
contract:[< Security_type.t ] Contract.t ->
Close.t Core.Std.Or_error.t Async.Std.Deferred.t
latest_close t contract returns either the latest closing price for the given contract or an Error.
val latest_close_exn : t ->
contract:[< Security_type.t ] Contract.t -> Close.t Async.Std.Deferred.t
Same as latest_close but raises an exception in case of an Error.