Commits

Oliver Gu committed cbbcd6f

Improved examples

  • Participants
  • Parent commits 6855cbd

Comments (0)

Files changed (6)

examples/buy_market_order.ml

     (Symbol.of_string "AAPL")
   in
   Tws.with_client ~enable_logging ~host ~port
-    ~on_handler_error:(`Call (fun e ->
-      prerr_endline (Error.to_string_hum e);
-      shutdown 1))
-    (fun tws ->
+    ~on_handler_error:`Raise (fun tws ->
       Stream.iter (Tws.execution_reports tws) ~f:(fun exec_report ->
         printf "%s\n\n%!"
           (Sexp.to_string_hum (Execution_report.sexp_of_t exec_report)));
       +> Common.port_arg ()
     )
     (fun enable_logging host port () ->
-      if enable_logging then Common.init_logger ();
-      main ~enable_logging ~host ~port
-    )
+      Monitor.try_with (fun () ->
+        if enable_logging then Common.init_logger ();
+        main ~enable_logging ~host ~port
+      ) >>= 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 main_cmd

examples/demultiplex.ml

       if enable_logging then Common.init_logger ();
       Monitor.try_with (fun () ->
         print_market_data ~enable_logging ~host ~port ~duration
-      ) >>| function
+      ) >>= function
       | Error exn ->
         let err = Error.of_exn (Monitor.extract_exn exn) in
         prerr_endline (Error.to_string_hum err);
-        shutdown 1
-      | Ok () -> ())
+        exit 1
+      | Ok () -> return ())
 
 let () = Command.run print_market_data_cmd

examples/plot_hist_bars.ml

 let sma50 xs = sma ~period:50 xs
 
 let plot_hist_bars ~host ~port ~currency ~symbol =
-  Tws.with_client ~host ~port
-    ~on_handler_error:(`Call (fun e ->
-      prerr_endline (Error.to_string_hum e);
-      shutdown 1))
-    (fun tws ->
-      Tws.historical_data_exn tws
-        ~contract:(Contract.stock ~currency (Symbol.of_string symbol))
-        ~bar_size:`Five_mins
-        ~duration:(`D 1)
-      >>| fun hist_data ->
-      let module Columns = Historical_data.Columns in
-      let hist_data = Historical_data.to_columns hist_data in
-      let num_bars  = Array.length (Columns.timestamps hist_data) in
-      let indices = Array.init num_bars ~f:Int.to_float in
-      let prices = Columns.close_prices hist_data in
-      let g = Gnuplot.create () in
-      Gnuplot.plot g [
-        [ indices;
-          Columns.open_prices  hist_data;
-          Columns.high_prices  hist_data;
-          Columns.low_prices   hist_data;
-          Columns.close_prices hist_data; ],
-        sprintf "with candlesticks title \"%s\"" symbol;
-        [ indices; sma10 prices; ], "w l lc rgb   \"blue\" title \"sma10\"";
-        [ indices; sma20 prices; ], "w l lc rgb  \"green\" title \"sma20\"";
-        [ indices; sma50 prices; ], "w l lc rgb \"yellow\" title \"sma50\"";
-      ];
-      Gnuplot.close g)
+  Tws.with_client ~host ~port ~on_handler_error:`Raise (fun tws ->
+    Tws.historical_data_exn tws
+      ~contract:(Contract.stock ~currency (Symbol.of_string symbol))
+      ~bar_size:`Five_mins
+      ~duration:(`D 1)
+    >>| fun hist_data ->
+    let module Columns = Historical_data.Columns in
+    let hist_data = Historical_data.to_columns hist_data in
+    let num_bars  = Array.length (Columns.timestamps hist_data) in
+    let indices = Array.init num_bars ~f:Int.to_float in
+    let prices = Columns.close_prices hist_data in
+    let g = Gnuplot.create () in
+    Gnuplot.plot g [
+      [ indices;
+        Columns.open_prices  hist_data;
+        Columns.high_prices  hist_data;
+        Columns.low_prices   hist_data;
+        Columns.close_prices hist_data; ],
+      sprintf "with candlesticks title \"%s\"" symbol;
+      [ indices; sma10 prices; ], "w l lc rgb   \"blue\" title \"sma10\"";
+      [ indices; sma20 prices; ], "w l lc rgb  \"green\" title \"sma20\"";
+      [ indices; sma50 prices; ], "w l lc rgb \"yellow\" title \"sma50\"";
+    ];
+    Gnuplot.close g)
 
 let plot_hist_bars_cmd =
   Command.async_basic ~summary:"plot historical bars"
       +> anon ("STOCK-SYMBOL" %: string)
     )
     (fun host port currency symbol () ->
-      plot_hist_bars ~host ~port ~currency ~symbol
-    )
+      Monitor.try_with (fun () ->
+        plot_hist_bars ~host ~port ~currency ~symbol
+      ) >>= 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 plot_hist_bars_cmd

examples/plot_taq_data.ml

 
 let plot_taq_data ~enable_logging ~host ~port ~duration ~currency ~symbol =
   Tws.with_client ~enable_logging ~host ~port
-    ~on_handler_error:(`Call (fun e ->
-      prerr_endline (Error.to_string_hum e);
-      shutdown 1))
-    (fun tws ->
+    ~on_handler_error:`Raise (fun tws ->
       let stock = Contract.stock ~currency (Symbol.of_string symbol) in
       Tws.taq_data_exn tws ~contract:stock
       >>= fun (taq_data, id) ->
         prerr_endline "Maximum duration is 1 minute";
         exit 1;
       end else
-        plot_taq_data ~enable_logging ~host ~port ~duration ~currency ~symbol
-    )
+        Monitor.try_with (fun () ->
+          plot_taq_data ~enable_logging ~host ~port ~duration ~currency ~symbol
+        ) >>= 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 plot_taq_data_cmd

examples/quote_table.ml

 
 let main ~enable_logging ~host ~port =
   Tws.with_client ~enable_logging ~host ~port
-    ~on_handler_error:(`Call (fun e ->
-      prerr_endline (Error.to_string_hum e);
-      shutdown 1))
-    (fun tws ->
+    ~on_handler_error:`Raise (fun tws ->
       Deferred.all (List.map symbols ~f:(fun symbol ->
         let stock = Contract.stock
           ~exchange:`SMART
     )
     (fun enable_logging host port () ->
       if enable_logging then Common.init_logger ();
-      main ~enable_logging ~host ~port
-    )
+      Monitor.try_with (fun () ->
+        main ~enable_logging ~host ~port
+      ) >>= 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 main_cmd

examples/submit_orders.ml

    Currently IBX cannot handle these open orders messages on connection.
    However, this will be solved in later versions of the library. *)
 
-
 let submit_and_wait_for_fill tws ~timeout ~contract ~order =
   let order_type = Order.Type.to_string (Order.order_type order) in
   printf "Submit %s buy order for %d shares of %s\n%!"
   let ibm = Contract.stock ~exchange:`BATS ~currency:`USD symbol in
   let num_shares = 100 in
   Tws.with_client ~enable_logging ~host ~port
-    ~on_handler_error:(`Call (fun e ->
-      prerr_endline (Error.to_string_hum e);
-      shutdown 1))
-    (fun tws ->
+    ~on_handler_error:`Raise (fun tws ->
       Tws.quote_snapshot_exn tws ~contract:ibm
       >>= fun snapshot ->
       let ask_price = Quote_snapshot.ask_price snapshot in
     )
     (fun enable_logging host port timeout () ->
       if enable_logging then Common.init_logger ();
-      main ~enable_logging ~host ~port ~timeout
-    )
+      Monitor.try_with (fun () ->
+        main ~enable_logging ~host ~port ~timeout
+      ) >>= 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 main_cmd