forked from sameerlal/OMakeMeAMarket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.ml
164 lines (149 loc) · 7.38 KB
/
gui.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
open Pervasives
(* Make market around this value *)
let tutorial_value = 100
(* TUTORIAL ALGORITHM TO ENSURE PLAYER MAKES PROGRESS TOWARDS THE PROPER
INTERVAL *)
(*Feedback type to be returned to player. *)
type trade_feedback = Low | High | BigSpread | Okay
(* [check_good_interval bid ask] is an option of either None or Some bool
option. If interval is bad, then None. If interval is good but spread
too large then Some false . Otherwise Some true. *)
let check_good_interval (bid:int) (ask:int) =
if (bid <= tutorial_value )
&& (ask >= tutorial_value )
&& ((ask - bid) < 15) then
Some true else
if (bid <= tutorial_value )
&& (ask >= tutorial_value )
&& ((ask - bid) >= 15)then Some false
else None
(* [eval_trade bid ask] is a string message that acts as feedback to a
[bid] and [ask]. *)
let eval_trade bid ask =
match (check_good_interval bid ask) with
| Some x ->
if x = true then "Perfect! Your market is good and has tight spread! Let's start the real game! "
else "\nYour market is good, but your spread is too high.
Traders might not want to trade with you. \n"
| None ->
if bid > tutorial_value then
"\nA lot of traders are hitting your bid (selling to you).
This means they're selling for a high price! Adjust your market downwards. \n"
else
"\nA lot of traders are lifting your offer (buying from you).
This means they're buying for a low price, a bargain! Adjust your market upwards. \n"
(**[parse_input_tut str] is the option that represents the commands input
by the player. *)
let parse_input_tut str =
let wordlst = String.split_on_char ' ' (String.trim str) in
let filtered = List.filter (fun s -> String.length s >= 1) wordlst in
if List.length filtered > 0 then
let cmd = List.hd filtered in
match cmd with
| "set" -> if List.length filtered > 0 then
let obj = begin match filtered with
| [] -> []
| h::t -> t
end
in Some (obj) else None
| "exit" -> Some ["exit"]
| _ -> None
else None
(* Take user input string. Return true to continue game or false to quit *)
let query str =
match (parse_input_tut str) with
| None -> print_endline "Invalid input, try again"; true
| Some text ->
if List.length text = 1 then
false
else
begin
if (List.length text = 1) then (print_endline "Quitting tutorial...";
false)
else
let response = eval_trade (int_of_string (List.nth text 0))
(int_of_string (List.nth text 1)) in
match (String.get response 0) with
| 'P' -> (print_endline response); false
| _ -> (print_endline response); true
end
let rec tutorial_fsm unit =
print_endline ("Enter a bid/offer ");
match query (read_line ()) with
| true -> tutorial_fsm ();
| false -> ()
let tutorial_preamble unit =
ANSITerminal.erase Screen;
ANSITerminal.print_string [ANSITerminal.magenta]
"~ TUTORIAL INTRODUCTION ~\n";
ANSITerminal.print_string [ANSITerminal.yellow]
("The goal of this game is to make a market for traders who want to buy and sell " ^ "CamlCoin. \nFor now, suppose we tell you that the true value of CamlCoin is $10.\n");
ANSITerminal.print_string [ANSITerminal.yellow]
("Your goal is to continuously provide bid/ask spreads and hopefully make a profit on " ^ "your spread.\n");
ANSITerminal.print_string [ANSITerminal.yellow]
("We use the notation 'set x y' to mean you, the marketmaker, will BUY at x and SELL at y \n \n");
ANSITerminal.print_string [ANSITerminal.yellow]
("This means traders will BUY at y and SELL at x! \n \n");
ANSITerminal.print_string [ANSITerminal.yellow]
("Suppose your market is 'set 20 40'. \n \nIn this case, the sell price of $20 is greater than the intrinsic value. \nSellers get a great deal! They can sell something worth $10 for"
^ "$20. Expect a lot of sells. Adjust your interval downwards.\n");
ANSITerminal.print_string [ANSITerminal.yellow]
("\nSuppose your market is 'set 5 8'. \n \nIn this case, the buy price $8 is much less than " ^ "the intrinsic value of $10. Buys are getting a bargain! Expect a lot of buys. Adjust your interval upwards.");
ANSITerminal.print_string [ANSITerminal.yellow]
("\n\nSuppose your market is 'set 9 11'. \n\nYour market is good! With some variance, " ^ "buyers will buy at $11 which is very close to the actual price. Sellers will sell at $9. Expect roughly even " ^ " buyers and sellers. \nYou will make $11 - $9 = $2 per transaction.");
ANSITerminal.print_string [ANSITerminal.yellow]
("\n\n>>>> Starting tutorial <<<< ");
ANSITerminal.print_string [ANSITerminal.yellow]
("\nNow you try. We won't tell you the actual price... \n");
tutorial_fsm ();
()
(* Text to display *)
let preamble unit =
ANSITerminal.erase Above;
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.yellow]
"Welcome! \n";
ANSITerminal.(print_string [black; yellow]
"Welcome to the CamlCoin exchange! Here you will practice your maket making skills \n"); ()
let display_help unit =
ANSITerminal.print_string [ANSITerminal.red; ANSITerminal.blue]
("The list of possible commands are: ");
ANSITerminal.print_string [ANSITerminal.red; ANSITerminal.yellow] ("
set [bid] [ask] \n
inventory \n
history \n
help \n
cheat \n
tutorial\n");
()
let introduction unit =
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("Today, you are the sole market maker of a coin called CamelCoin. \n");
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("You will be making a market for several traders in USD. \n");
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("You are competing with other high frequency trading firms such as Citadel, Jane Street and Optiver, so be fast! \n");
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("Type in 'help' for a list of commands or 'tutorial' to begin the tutorial \n" );
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("We will assume familiarity, so let us begin! \n");
display_help ();
ANSITerminal.print_string [ANSITerminal.red]
("The true cost of a CamelCoin is equal to the answer of the following question: \n \n");
()
let introduction_adv unit =
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("Today, you are the sole market maker of a coin called CamelCoin. \n");
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("You will be making a market for several traders in USD. \n");
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("You are competing with other high frequency trading firms, so be fast! \n");
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("Type in 'help' for a list of commands or 'tutorial' to begin the tutorial \n" );
ANSITerminal.print_string [ANSITerminal.black; ANSITerminal.green]
("We will assume familiarity, so let us begin! \n");
display_help ();
ANSITerminal.print_string [ANSITerminal.red] ("Simulating dice rolls....\n");
ANSITerminal.print_string [ANSITerminal.red]
("Each trader has rolled a dice.\n");
ANSITerminal.print_string [ANSITerminal.red] ("You rolled a : \n \n");
()