Source

opycaml / js_of_ocaml / finance_demo_oo.ml

Full commit
open Opycaml
open OOApi

open Opy_of_ocaml

module Xapi = struct

  let import_module name = (Import.importModule name)#getDict
  let (%) o k = o#getItemString k
  let (%!) o k = (o |> Dict.coerce)#getItemString k

  let dict_of_alist kvs =
    let d = Api.Dict.new_ () |> new OPy.o_Dict in
    List.iter (fun (k,v) -> d#setItem k v) kvs;
    d

  let dict_of_klist kvs =
    kvs |> List.map (fun (s,o) -> OPy.String.fromString s, o) |> dict_of_alist
  
  let call_obj o ?kwd args = 
    let kwd = match kwd with
      | None -> None
      | Some xs -> dict_of_klist xs
    in
    (Callable.coerce o)#call o ~kwd

  let call_obj_ o ?kwd args = call_obj o ?kwd args |> ignore

  let (!:>) o = (o :> o_Object)
  
end

class o_Date t = object
  inherit OPy.o_Object t
end

class o_Weekday t = object
  inherit OPy.o_Object t
end

class o_Ticks t = object
  inherit OPy.o_Object t
end

class o_Formatter t = object
  inherit OPy.o_Object t
end

class o_Quotes t = object
  inherit OPy.o_Object t
end

class type dates = object
  method _WeekDayLocator : o_Weekday -> o_Ticks Js.meth
  method _DayLocator : o_Ticks Js.meth
  method _MONDAY : o_Weekday Js.readonly_prop
  method _DateFormatter : o_String -> o_Formatter Js.meth
end

class type datetime = object
  method date : o_Int -> o_Int -> o_Int -> o_Date Js.meth
end

class type finance = object
  method quote_historical_yahoo : o_String -> o_Date -> o_Date -> o_Quotes Js.meth
end

class type figure = object
  method subplots_adjust : kwd: o_Dict -> o_Dict Js.meth
end

class type pylab = object
  method figure : figure Js.meth
end
 
let _ =
  (* from pylab import * *)
  let pylab : pylab Js.t = import_module "pylab" in

  (* from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY, timezone *)
  let dates : dates Js.t = 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 : datetime Js.t = import_module "datetime" in

  (* date1 = datetime.date( 2004, 2, 1) *)
  let date1 = datetime##date( Int.fromLong 2004, Int.fromLong 2, Int.fromLong 1 ) in
  date1#debug "date1";

  let date2 = datetime##date( Int.fromLong 2004, Int.fromLong 4, Int.fromLong 12 ) in
  date2#debug "date2";

  let monday = dates##_MONDAY in
  monday#debug "monday";

  let mondays = dates##_WeekDayLocator( dates##_MONDAY ) in
  mondays#debug "mondays";

  let alldays = dates##_DayLocator() in
  alldays#debug "alldays";

  let weekFormatter = dates##_DateFormatter( OPy.String.fromString "%b %d" ) in  (* Eg, Jan 12 *)
  weekFormatter#debug "weekFormatter";

  let dayFormatter = dates##_DateFormatter( OPy.String.fromString "%d" ) in     (* # Eg, 12 *)
  dayFormatter#debug "dayFormatter";

  (* quotes = quotes_historical_yahoo('INTC', date1, date2) *)
  let quotes = finance##quotes_historical_yahoo( OPy.String.fromString "INTC",
                                                 date1,
                                                 date2 ) 
  in
  quotes#debug "quotes";

  let fig = pylab##figure() in
  fig#debug "fig";

  fig##subplots_adjust ~kwd:(dict_of_klist [ "bottom", Float.fromDouble 0.2]) [];
  ()

(*

quotes = quotes_historical_yahoo(
    'INTC', date1, date2)
if not quotes:
    raise SystemExit

fig = figure()
fig.subplots_adjust(bottom=0.2)
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)

#plot_day_summary(ax, quotes, ticksize=3)
candlestick(ax, quotes, width=0.6)

ax.xaxis_date()
ax.autoscale_view()
setp( gca().get_xticklabels(), rotation=45, horizontalalignment='right')

show()

*)