Commits

Oliver Gu  committed 9381625

Added customized client example

  • Participants
  • Parent commits c7e321b

Comments (0)

Files changed (3)

File examples/client/option_client.ml

+open Core.Std
+open Async.Std
+open Ibx.Std
+
+module Req_intf : sig
+  val req_option_data :
+    (Query.Market_data.t, Tick_option.t) Ib.Streaming_request.t
+end = struct
+  module S = Send_tag
+  module R = Recv_tag
+
+  let req_option_data = Ib.Streaming_request.create
+    ~send_header:(Ib.Header.create ~tag:S.Market_data ~version:9)
+    ~canc_header:(Ib.Header.create ~tag:S.Cancel_market_data ~version:1)
+    ~recv_header:[
+      Ib.Header.create ~tag:R.Tick_option ~version:6;
+    ]
+    ~skip_header:[
+      Ib.Header.create ~tag:R.Tick_price ~version:6;
+      Ib.Header.create ~tag:R.Tick_size ~version:6;
+      Ib.Header.create ~tag:R.Tick_string ~version:6;
+      Ib.Header.create ~tag:R.Tick_generic ~version:6;
+    ]
+    ~tws_query:Query.Market_data.pickler
+    ~tws_response:[
+      Response.Tick_option.unpickler;
+    ] ()
+end
+
+include Ib.Client
+
+let option_data t ~option =
+  let q =
+    Query.Market_data.create
+      ~contract:option
+      ~tick_generics:[]
+      ~snapshot:false
+  in
+  dispatch_streaming_request t Req_intf.req_option_data q
+
+let option_data_exn t ~option = option_data t ~option >>| Or_error.ok_exn
+
+let cancel_option_data t id =
+  cancel_streaming_request t Req_intf.req_option_data id

File examples/client/option_client.mli

+open Core.Std
+open Async.Std
+open Ibx.Std
+
+(** This customized client offers only subscription to option data. *)
+
+type t
+include Client_intf.S with type t := t
+
+val option_data
+  :  t
+  -> option:[ `Option ] Contract.t
+  -> (Tick_option.t Pipe.Reader.t * Query_id.t) Or_error.t Deferred.t
+
+val option_data_exn
+  :  t
+  -> option:[ `Option ] Contract.t
+  -> (Tick_option.t Pipe.Reader.t * Query_id.t) Deferred.t
+
+val cancel_option_data : t -> Query_id.t -> unit

File examples/client/print_option_data.ml

+open Core.Std
+open Async.Std
+open Ibx.Std
+
+let print_option_data ~host ~port ~duration =
+  Option_client.with_client ~host ~port ~on_handler_error:`Raise (fun clt ->
+    (* For current contracts check: http://finance.yahoo.com/q/op?s=GOOG *)
+    let goog_call =
+      Contract.option
+        ~id:(Contract_id.of_string "116034093")
+        ~exchange:`CBOE
+        ~currency:`USD
+        ~option_right:`Call
+        ~expiry:(Date.create_exn ~y:2013 ~m:Month.Jun ~d:21)
+        ~strike:(Price.of_float 850.)
+        (Symbol.of_string "GOOG")
+    in
+    Option_client.option_data_exn clt ~option:goog_call
+    >>= fun (option_ticks, id) ->
+    upon (after duration) (fun () -> Option_client.cancel_option_data clt id);
+    Pipe.iter_without_pushback option_ticks ~f:(fun option_tick ->
+      print_endline (Sexp.to_string_hum (Tick_option.sexp_of_t option_tick)))
+  )
+
+let print_option_data_cmd =
+  Command.async_basic ~summary:"print option data"
+    Command.Spec.(
+      empty
+      +> flag "-host" (optional_with_default "127.0.0.1" string)
+        ~doc:" hostname of TWS or Gateway (default localhost)"
+      +> flag "-port" (optional_with_default 4001 int)
+        ~doc:" TWS port 7496 or Gateway port 4001 (default 4001)"
+      +> flag "-duration" (optional_with_default (sec 30.) time_span)
+        ~doc:" duration of the option data stream (default 30s)"
+    )
+    (fun host port duration () ->
+      Monitor.try_with (fun () ->
+        print_option_data ~host ~port ~duration
+      ) >>= function
+      | Error exn ->
+        let err = Error.of_exn (Monitor.extract_exn exn) in
+        prerr_endline (Error.to_string_hum err);
+        exit 1
+      | Ok () -> return ())
+
+let () = Command.run print_option_data_cmd