Commits

Oliver Gu committed a053951

Improved examples

  • Participants
  • Parent commits e07bc76

Comments (0)

Files changed (2)

examples/demultiplex.ml

 open Async.Std
 open Ibx.Std
 
-let make_tick_printer id symbol ~color = stage (fun tick ->
-  Format.fprintf Format.str_formatter "@[<h 0>\\<%s\\>@ id=%s@ symbol=%s@ %a@]"
-    (Time.to_string (Time.now ()))
+let make_tick_printer ~id ~symbol ~color = stage (fun tick ->
+  Format.fprintf
+    Format.str_formatter "@[<h 0>\\<%s\\>@ id=%s@ symbol=%s@ %a@]"
+    (Time.to_string_trimmed (Time.now ()))
     (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.string_with_attr [`Bright; color] output
-  else output)
+  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 process_market_data symbol color =
+    let print_ticks symbol color =
       Tws.market_data tws ~contract:(Contract.stock ~currency:`USD symbol)
       >>= function
       | Error e ->
-        prerr_endline (Error.to_string_hum e);
-        let pipe_r, pipe_w = Pipe.create () in
-        Pipe.close pipe_w;
-        return pipe_r
+        print_endline (Error.to_string_hum e);
+        return ()
       | Ok (ticks, id) ->
         upon (Clock.after duration) (fun () -> Tws.cancel_market_data tws id);
-        return (Pipe.map ticks ~f:(unstage (make_tick_printer id symbol ~color)))
+        Pipe.iter ticks ~f:(unstage (make_tick_printer ~id ~symbol ~color))
     in
     let symbols = ["AAPL"; "MSFT"; "GOOG"] in
     let colors  = [`Red; `Green; `Blue] in
-    Deferred.all (List.map2_exn symbols colors ~f:(fun symbol color ->
-      process_market_data (Symbol.of_string symbol) color))
-    >>= fun pipes ->
-    Pipe.iter_without_pushback (Pipe.interleave pipes) ~f:print_endline)
+    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"

examples/quote_table.ml

 
 module Ascii_table = Core_extended.Ascii_table
 
-let symbols = ["AAPL";"AMZN";"CSCO";"FB";"GOOG";"IBM";"MSFT";"ORCL";"SAP";"YHOO"]
-
 let print_quote_table quotes =
   let module Q = Quote_snapshot in
   let get_symbol    quote = sprintf "%s"    (Q.symbol quote |! Symbol.to_string) in
     create_col "Ask price" get_ask_price;
   ] quotes
 
+let symbols = ["AAPL";"AMZN";"CSCO";"FB";"GOOG";"IBM";"MSFT";"ORCL";"SAP";"YHOO"]
+
 let run () =
   Common.with_tws_client (fun tws ->
-    Deferred.all (List.map symbols ~f:(fun symbol ->
+    Deferred.List.map symbols ~how:`Parallel ~f:(fun symbol ->
       let stock = Contract.stock
         ~exchange:`SMART
         ~currency:`USD
         (Symbol.of_string symbol)
       in
-      Tws.quote_snapshot_exn tws ~contract:stock))
+      Tws.quote_snapshot_exn tws ~contract:stock)
     >>| fun quotes -> print_quote_table quotes)
 
 let command =