Commits

Oliver Gu  committed 9af20e7

Added Portfolio_update response

  • Participants
  • Parent commits 3a0340e

Comments (0)

Files changed (4)

File lib/response.ml

           (fun t -> `Args $ t.key $ t.value $ t.currency $ t.account_code)))
 end
 
+module Portfolio_update = struct
+  type t = {
+    contract : Contract.Type.t Contract.t;
+    position : int;
+    market_price : Price.t;
+    market_value : Price.t;
+    average_cost : Price.t;
+    unrealized_pnl : Price.t;
+    realized_pnl : Price.t;
+    account_code : Account_code.t;
+  } with sexp, fields
+
+  let create = Fields.create
+
+  let ( = ) t1 t2 : bool =
+    let use op = fun field ->
+      op (Field.get field t1) (Field.get field t2)
+    in
+    Fields.for_all
+      ~contract:(use Contract.(=))
+      ~position:(use (=))
+      ~market_price:(use Price.(=.))
+      ~market_value:(use Price.(=.))
+      ~average_cost:(use Price.(=.))
+      ~unrealized_pnl:(use Price.(=.))
+      ~realized_pnl:(use Price.(=.))
+      ~account_code:(use Account_code.(=))
+
+  let unpickler =
+    let contract_spec =
+      Unpickler.Spec.(
+        step (fun conv id symbol contract_type expiry strike option_right
+          multiplier exchange currency local_symbol ->
+            let contract = Raw_contract.create ?id ~contract_type ?expiry ?strike
+              ?option_right ?multiplier ~exchange ~currency ?local_symbol symbol
+            in
+            conv (Contract.of_raw contract)
+        )
+        ++ value (optional Raw_contract.Id.val_type)
+          ~name:(field_name Raw_contract.Fields.contract_id)
+        ++ value (required Symbol.val_type)
+          ~name:(field_name Raw_contract.Fields.symbol)
+        ++ value (required string)
+          ~name:(field_name Raw_contract.Fields.contract_type)
+        ++ value (optional date)
+          ~name:(field_name Raw_contract.Fields.expiry)
+        ++ value (optional Price.val_type ~none_on_default:"0.0")
+          ~name:(field_name Raw_contract.Fields.strike)
+        ++ value (optional Raw_contract.Option_right.val_type)
+          ~name:(field_name Raw_contract.Fields.option_right)
+        ++ value (optional string)
+          ~name:(field_name Raw_contract.Fields.multiplier)
+        ++ value (required Exchange.val_type)
+          ~name:(field_name Raw_contract.Fields.exchange)
+        ++ value (required Currency.val_type)
+          ~name:(field_name Raw_contract.Fields.currency)
+        ++ value (optional Symbol.val_type)
+          ~name:(field_name Raw_contract.Fields.local_symbol)
+      )
+    in
+    Unpickler.create ~name:"Portfolio_update"
+      Unpickler.Spec.(
+        Fields.fold
+          ~init:(empty ())
+          ~contract:(fun specs -> Fn.const (specs ++ contract_spec))
+          ~position:(fields_value (required int))
+          ~market_price:(fields_value (required Price.val_type))
+          ~market_value:(fields_value (required Price.val_type))
+          ~average_cost:(fields_value (required Price.val_type))
+          ~unrealized_pnl:(fields_value (required Price.val_type))
+          ~realized_pnl:(fields_value (required Price.val_type))
+          ~account_code:(fields_value (required Account_code.val_type)))
+      (fun contract position market_price market_value average_cost
+        unrealized_pnl realized_pnl account_code ->
+          { contract;
+            position;
+            market_price;
+            market_value;
+            average_cost;
+            unrealized_pnl;
+            realized_pnl;
+            account_code;
+          })
+
+  let pickler = Only_in_test.of_thunk (fun () ->
+    let contract_spec =
+      Pickler.Spec.(
+        Raw_contract.Fields.fold
+          ~init:(empty ())
+          ~contract_id:(fields_value (optional Raw_contract.Id.val_type))
+          ~symbol:(fields_value (required Symbol.val_type))
+          ~contract_type:(fields_value (required string))
+          ~expiry:(fields_value (optional date))
+          ~strike:(fields_value (optional Price.val_type ~default_on_none:"0.0"))
+          ~option_right:(fields_value (optional Raw_contract.Option_right.val_type))
+          ~multiplier:(fields_value (optional string))
+          ~exchange:(fields_value (required Exchange.val_type))
+          ~listing_exchange:(fields_value skipped)
+          ~currency:(fields_value (required Currency.val_type))
+          ~local_symbol:(fields_value (optional Symbol.val_type))
+          ~include_expired:(fields_value skipped)
+          ~security_id_type:(fields_value skipped)
+          ~security_id:(fields_value skipped)
+          ~combo_legs:(fields_value skipped))
+      |> wrap_contract_spec
+    in
+    Pickler.create ~name:"Portfolio_value"
+      Pickler.Spec.(
+        wrap (
+          Fields.fold
+            ~init:(empty ())
+            ~contract:(fun specs -> Fn.const (specs ++ contract_spec))
+            ~position:(fields_value (required int))
+            ~market_price:(fields_value (required Price.val_type))
+            ~market_value:(fields_value (required Price.val_type))
+            ~average_cost:(fields_value (required Price.val_type))
+            ~unrealized_pnl:(fields_value (required Price.val_type))
+            ~realized_pnl:(fields_value (required Price.val_type))
+            ~account_code:(fields_value (required Account_code.val_type)))
+          (fun t ->
+            `Args
+              $ (Contract.to_raw t.contract)
+              $ t.position
+              $ t.market_price
+              $ t.market_value
+              $ t.average_cost
+              $ t.unrealized_pnl
+              $ t.realized_pnl
+              $ t.account_code)))
+end
+
 (* +-----------------------------------------------------------------------+
    | Contract specs                                                        |
    +-----------------------------------------------------------------------+ *)

File lib/response.mli

     -> t
 end
 
+module Portfolio_update : sig
+  type t = private
+    { contract : Contract.Type.t Contract.t;
+      position : int;
+      market_price : Price.t;
+      market_value : Price.t;
+      average_cost : Price.t;
+      unrealized_pnl : Price.t;
+      realized_pnl : Price.t;
+      account_code : Account_code.t;
+    }
+  with sexp, fields
+  include Response_intf.S with type t := t
+
+  val create :
+    contract:Contract.Type.t Contract.t
+    -> position:int
+    -> market_price:Price.t
+    -> market_value:Price.t
+    -> average_cost:Price.t
+    -> unrealized_pnl:Price.t
+    -> realized_pnl:Price.t
+    -> account_code:Account_code.t
+    -> t
+end
+
 (** {1 Contract specs} *)
 (*****************************************************************************)
 

File lib_test/pickle_test.ml

       let module R = Response.Account_update in
       gen_test (module R : Response_intf.S with type t = R.t) Rg.R.account_update_g);
 
+    (fun () ->
+      let module R = Response.Portfolio_update in
+      gen_test (module R : Response_intf.S with type t = R.t) Rg.R.portfolio_update_g);
+
     (* ========================== Contract specs =========================== *)
 
     (fun () ->

File lib_test/rg.ml

 
   (* Account and Portfolio *)
 
-  val account_update_g : Response.Account_update.t gen
+  val account_update_g   : Response.Account_update.t gen
+  val portfolio_update_g : Response.Portfolio_update.t gen
 
   (* Contract specs *)
 
       ~currency:(currency_g ())
       ~account_code:(account_code_g ())
 
+  let portfolio_update_g () =
+    let contract_g () = oneof
+      [ always (
+        Contract.stock
+          ?id:(og contract_id_g ())
+          ?listing_exchange:None
+          ?local_symbol:(og symbol_g ())
+          ?security_id:None
+          ?exchange:(og exchange_g ())
+          ~currency:(currency_g ())
+          (symbol_g ()))
+      ; always (
+        Contract.futures
+          ?id:(og contract_id_g ())
+          ?multiplier:(og sg ())
+          ?listing_exchange:None
+          ?local_symbol:(og symbol_g ())
+          ?security_id:None
+          ?include_expired:None
+          ?exchange:(og exchange_g ())
+          ~currency:(currency_g ())
+          ~expiry:(expiry_g ())
+          (symbol_g ()))
+      ; always (
+        Contract.option
+          ?id:(og contract_id_g ())
+          ?multiplier:(og sg ())
+          ?listing_exchange:None
+          ?local_symbol:(og symbol_g ())
+          ?security_id:None
+          ?exchange:(og exchange_g ())
+          ~currency:(currency_g ())
+          ~option_right:(option_right_g ())
+          ~expiry:(expiry_g ())
+          ~strike:(price_g ())
+          (symbol_g ()))
+      ] ()
+    in
+    Response.Portfolio_update.create
+      ~contract:(contract_g ())
+      ~position:(nng ())
+      ~market_price:(price_g ())
+      ~market_value:(price_g ())
+      ~average_cost:(price_g ())
+      ~unrealized_pnl:(price_g ())
+      ~realized_pnl:(price_g ())
+      ~account_code:(account_code_g ())
+
   (* ============================ Contract specs =========================== *)
 
   let contract_specs_g () =