Repository navigation
Expand file tree
/
Copy pathFuture.java
More file actions
88 lines (71 loc) · 1.99 KB
/
Copy pathFuture.java
File metadata and controls
88 lines (71 loc) · 1.99 KB
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
public class Future {
private int sellerMargin;
private int buyerMargin;
private int sellerAddedMar;
private int buyerAddedMar;
private static int price = 200;
private static final int MAINTENANCE_MARGIN = 10;
private final int marginLevel;
private final int days;
public Future(int margin, int days) {
sellerMargin = margin;
buyerMargin = margin;
marginLevel = margin;
this.days = days;
}
public void trade() {
for(int i = 0; i<days;i++) {
newDay();
}
}
/**
* When it is a new day of trading, set a new price and update margins.
*/
private void newDay() {
int original = Future.generatePrice();
System.out.println("Price: "+ price);
int difference = Math.abs(price - original);
System.out.println("Difference: "+ difference);
if (price < original) {
buyerMargin -= difference;
sellerMargin += difference;
}else if(price > original) {
buyerMargin += difference;
sellerMargin -= difference;
}
System.out.println("Buyer Margin: "+buyerMargin);
System.out.println("Seller Margin: "+sellerMargin);
if (buyerMargin < MAINTENANCE_MARGIN) {
buyerAddedMar += marginLevel - buyerMargin;
System.out.println("Added $"+buyerAddedMar+" to buyer margin account.");
addToMargin(1);
}
if(sellerMargin < MAINTENANCE_MARGIN) {
sellerAddedMar += marginLevel - sellerMargin;
System.out.println("Added $"+sellerAddedMar+" to seller margin account.");
addToMargin(0);
}
}
/**
* If margin is below MAINTENANCE_MARGIN, add money up to the marginLevel
* from the buyer/seller's account to the margin account.
*/
private void addToMargin(int who) {
if (who == 0)
sellerMargin = marginLevel;
else
buyerMargin = marginLevel;
}
/**
* Set a new price using random numbers from 0 to 9.
*/
private static int generatePrice() {
int sign = (int) (Math.random()*10);
int original = price;
if(sign % 2 == 0)
price += (int) (Math.random()*10);
else
price -= (int) (Math.random()*10);
return original;
}
}