ibx / examples /

open Core.Std
open Core_extended.Std
open Async.Std
open Ibx.Std

let make_tick_printer ~id ~symbol ~color = stage (fun tick ->
    Format.str_formatter "@[<h 0>\\<%s\\>@ id=%s@ symbol=%s@ %a@]"
    (Time.to_string_trimmed ( ()))
    (Query_id.to_string id)
    (Symbol.to_string symbol)
    Market_data.pp tick;
  Format.close_box ();
  let unescape = unstage (String.Escaping.unescape ~escape_char:'\\') in
  let output = unescape (Format.flush_str_formatter ()) in
  if Console.is_color_tty ()
  then Console.Ansi.printf [`Bright; color] "%s\n%!" output
  else print_endline output;
  return ())

let print_market_data ~duration =
  Common.with_tws_client (fun tws ->
    let print_ticks symbol color =
      Tws.market_data tws ~contract:(Contract.stock ~currency:`USD symbol)
      >>= function
      | Error e ->
        print_endline (Error.to_string_hum e);
        return ()
      | Ok (ticks, id) ->
        upon (Clock.after duration) (fun () -> Tws.cancel_market_data tws id);
        Pipe.iter ticks ~f:(unstage (make_tick_printer ~id ~symbol ~color))
    let symbols = ["AAPL"; "MSFT"; "GOOG"] in
    let colors  = [`Red; `Green; `Blue] in
    Deferred.all_unit (List.map2_exn symbols colors ~f:(fun symbol color ->
      print_ticks (Symbol.of_string symbol) color))

let command =
  Command.async_basic ~summary:" print market data"
      +> Common.logging_flag ()
      +> Common.host_arg ()
      +> Common.port_arg ()
      +> Common.duration_arg ()
    (fun enable_logging host port duration () ->
      print_market_data ~enable_logging ~host ~port ~duration
      >>= function
      | Error e -> prerr_endline (Error.to_string_hum e); exit 1
      | Ok () -> return ()

let () = command
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.