-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_threshold.ml
49 lines (45 loc) · 2.07 KB
/
simple_threshold.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
open Engine
open Stdlib
(** [check_no_money amount stock_price buy_message] checks if the user has
enough money to buy more of a specified stock. If the user does not have
enough money, it returns true, otherwise it returns false. *)
let check_no_money amount stock_price buy_message =
amount -. stock_price < 0.00
(** [check_counter counter none_message] checks the current volume of a
specified stock in the user's portfolio and returns true if the user does
not have any stock remaining. Otherwise it returns false. *)
let check_counter (counter:int) (none_message:string) =
Stdlib.Int.equal counter 0
(** [threshold counter stock upper lower amount] checks the current price of
[stock] and makes the appropriate transaction based on the inputted [upper],
[lower], and [amount].
Requires:
[stock] is a valid stock ticker in string form.
[upper] is a float >= 0.
[lower] is a float >= 0.
[amount] is an int >= 0.
[upper] >= [lower]. *)
let rec threshold counter stock upper lower amount =
let stock_price = Engine.get_price stock 1 in
let buy_message = "\nYou have no more money to spend :(\n" in
let none_message =
String.concat " " ["\nYou have no more"; stock; "stock\n"] in
let no_purchase_message = "\nNo purchase\n" in
if Engine.compare lower stock_price then
(if check_no_money amount stock_price buy_message then
ANSITerminal.(print_string [green] buy_message)
else (Engine.buy stock 1;
print_string "\n1 share of ";
print_string stock;
print_string " bought!\n";
threshold (counter+1) stock upper lower (amount -. stock_price)))
else if Engine.compare stock_price upper then
(if check_counter counter none_message then
ANSITerminal.(print_string [green] none_message)
else (Engine.sell stock 1;
print_string "\n1 share of ";
print_string stock;
print_string " sold!\n";
threshold (counter - 1) stock upper lower (amount +. stock_price)))
else
ANSITerminal.(print_string [green] no_purchase_message)