Source

opycaml / examples / matplotlib / finance_demo.ml

Full commit
module Api = Opycaml.Api
open Api
open Opycaml.Xapi

let debug = Object.debug

let main () =
  (* from pylab import * *)
  let pylab = import_module "pylab" in

  (* from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY, timezone *)
  let dates = import_module "matplotlib.dates" in

  (* from matplotlib.finance import quotes_historical_yahoo, candlestick,\
     plot_day_summary, candlestick2 *)
  let finance = import_module "matplotlib.finance" in

  (* import datetime *)
  let datetime = import_module "datetime" in

  let date = call_obj (datetime % "date") in

  (* date1 = datetime.date( 2004, 2, 1) *)
  let date1 = date [ Py.of_int 2004; Py.of_int 2; Py.of_int 1 ] in
  debug date1 "date1";

  let date2 = date [ Py.of_int 2004; Py.of_int 4; Py.of_int 12 ] in
  debug date2 "date2";

  begin
    try 
      (* Intentionally calls date function w/o arguments *)
      let date0 = date [] in
      debug date0 "date0";
      assert false
    with
    | Error (ty,detail) -> 
        prerr_endline "Expected type error!";
        debug ty "type";
        debug detail "detail";
  end;

  let monday = dates % "MONDAY" in
  debug monday "monday";

  let dayLocator = dates % "DayLocator" in
  debug dayLocator "DayLocator"; 

  (* Class can be called as a function for New *)
  let dayLocator_f = call_obj (dates % "DayLocator") in

  let alldays = dayLocator_f [] in
  debug alldays "alldays";

  let weekdayLocator = call_obj (dates % "WeekdayLocator") in

  let mondays = weekdayLocator [ monday ] in

  let dateFormatter = call_obj (dates % "DateFormatter") in

  let weekFormatter = dateFormatter [ Py.of_string "%b %d" ] in
  debug weekFormatter "weekFormatter";

  let dayFormatter = dateFormatter [ Py.of_string "%d" ] in
  debug dayFormatter "dayFormatter";

  let quotes_historical_yahoo = call_obj (finance % "quotes_historical_yahoo") in
  debug (finance % "quotes_historical_yahoo") "quotes_historical_yahoo";
 
  let quotes = 
    try
      quotes_historical_yahoo [ !:> (Py.of_string "INTC");
                                date1;
                                date2 ]
    with
    | Api.Error (ty,detail) -> 
        Format.eprintf "%s: %s@." (Object.str_string ty) (Object.str_string detail); raise Exit
  in

  debug quotes "quotes";

  let fig = call_obj (pylab % "figure") [] |> Dict.coerce in
  debug fig "fig";

  (* CR jfuruse: warnings for unit cases. lousy... *)
  call_obj_ (fig % "subplots_adjust") ~kwd:(dict_of_klist [ "bottom", Py.of_float 0.2]) [];
  let ax = call_obj (fig % "add_subplot") [ Py.of_int 111 ] |> Dict.coerce in
  debug ax "ax";

  call_obj_ (ax % "xaxis" %! "set_major_locator") [ mondays ];
  call_obj_ (ax % "xaxis" %! "set_minor_locator") [ alldays ];
  call_obj_ (ax % "xaxis" %! "set_major_formatter") [ weekFormatter ];
  (* call_obj (ax % "xaxis" % "set_minor_formatter") [ dayFormatter ]; *)

  (* #plot_day_summary(ax, quotes, ticksize=3) *)
  call_obj_ (finance % "candlestick") ~kwd:(dict_of_klist [ "", !:> ax; "", !:> quotes; "width", !:> (Py.of_float 0.6) ]) [];

  call_obj_ (ax % "xaxis_date") [];
  call_obj_ (ax % "autoscale_view") [];
  
  call_obj_ (pylab % "setp")
    ~kwd: (dict_of_klist [ "", call_obj (call_obj (pylab % "gca") [] %! "get_xticklabels") []; 
                           "rotation", !:> (Py.of_int 45); 
                           "horizontalalignment", !:> (Py.of_string "right") ])
    [];

  call_obj_ (pylab % "show") []

let () = 
  try main () with
  | Api.Error(ty, detail) -> 
      prerr_endline "Expected type error!";
      debug ty "type";
      debug detail "detail";