Skip to content

Commit 60daa9d

Browse files
committed
Validation of trade and generation of accessors in trade
1 parent 8e96230 commit 60daa9d

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/model/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
(library
22
(name model)
3+
(preprocess (pps ppx_fields_conv))
34
(libraries base calendar unix str validator))

lib/model/trade.ml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ open Instrumentid
44
open Common
55
open CalendarLib
66
open Market
7-
open Validator
87
open List
98
open Taxfee
109

@@ -24,9 +23,40 @@ module Trade(TaxFeeForMarket: TaxFee) : Trade_sig = struct
2423
value_date: Calendar.t option;
2524
tax_fees: (TaxFeeForTrade.t * money) list;
2625
net_amount: money option;
26+
} [@@deriving fields ~getters]
27+
28+
let build_trade account_no isin market buy_sell unit_price quantity trade_date value_date = {
29+
account_no;
30+
isin;
31+
market;
32+
buy_sell;
33+
unit_price;
34+
quantity;
35+
trade_date;
36+
value_date;
37+
tax_fees = [];
38+
net_amount = None;
2739
}
2840

29-
let create_trade ~account_no ~isin ~market ~buy_sell ~unit_price ~quantity ~trade_date ~value_date = failwith "TODO"
41+
let validate_trade ~account_no ~isin ~market ~buy_sell ~unit_price ~quantity ~trade_date ~value_date =
42+
let open Validator in
43+
let open Tvalidator in
44+
let valid = build build_trade
45+
|> keep account_no
46+
|> keep isin
47+
|> keep market
48+
|> keep buy_sell
49+
|> validate unit_price (TradingValidator.float_min 1.0 "Unit price must be > 0")
50+
|> validate quantity (TradingValidator.float_min 1.0 "Quantity must be > 0")
51+
|> keep trade_date
52+
|> keep value_date in
53+
match valid with
54+
| Ok trade -> Ok trade
55+
| Error e -> Error e
56+
57+
58+
let create_trade ~account_no ~isin ~market ~buy_sell ~unit_price ~quantity ~trade_date ~value_date =
59+
validate_trade ~account_no ~isin ~market ~buy_sell ~unit_price ~quantity ~trade_date ~value_date
3060

3161
let principal t = t.unit_price *. t.quantity
3262

lib/model/trade_sig.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open Validator
88

99
module type Trade_sig = sig
1010
module TaxFeeForTrade : TaxFee
11-
type t
11+
type t
1212
type money
1313

1414
(* create a validated trade *)
@@ -31,6 +31,7 @@ module type Trade_sig = sig
3131
(* get the net amount of the trade *)
3232
val net_amount: t -> money
3333

34+
(* get the tax/fee list along with values for the trade *)
3435
val tax_fees: t -> (TaxFeeForTrade.t * money) list
3536

3637
end

0 commit comments

Comments
 (0)