-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvestSimulator.java
More file actions
179 lines (133 loc) · 5.99 KB
/
InvestSimulator.java
File metadata and controls
179 lines (133 loc) · 5.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package symposium;
import java.awt.*;
import java.time.YearMonth;
import java.util.List;
import javax.swing.*;
public class InvestSimulator extends ParentSimulator {
double contribution;
private double totalContribution = 0;
public InvestSimulator(double startAmount, double monthlyContribution, double startAlloc, double endAlloc,
double fixedYield, double annualIncrease, YearMonth startDate, YearMonth endDate) {
super(startAmount, startAlloc, endAlloc, fixedYield, annualIncrease, startDate, endDate);
contribution = monthlyContribution;
}
// Divide contribution and split it to stock and fixed income according to current allocation
public void allocateContribution() {
double stockContribution = contribution * allocation;
double fixedContribution = contribution * (1 - allocation);
stockBalance += stockContribution;
fixedBalance += fixedContribution;
}
// Increase annual contribution
public void increaseContribution() {
contribution *= (1 + annualIncrease / 100);
//contribution = contribution + contribution * annualIncrease / 100;
}
// Print formatted monthly update of values
public void printValues() {
System.out.printf("Date: %s | Stock Balance: %.2f | Fixed Balance: %.2f | Allocation: %.2f/%.2f | Contribution: %.2f | Total: %.2f%n",
currentDate.format(formatter), stockBalance, fixedBalance, allocation * 100, (1 - allocation) * 100, contribution, fixedBalance + stockBalance);
}
// Run simulation
public void runSimulation() {
// Setup to be run once
adjustAllocation();
rebalance();
allocateContribution();
//printValues();
nextMonth();
// Main simulation loop
while (currentDate.isBefore(endDate) || currentDate.equals(endDate)) {
YearMonth previousMonth = currentDate.minusMonths(1);
double percentChange = 0;
try {
// Calculate the percent change in stock price for the current month
percentChange = stockData.calculatePercentChange(currentDate, previousMonth);
} catch (IllegalArgumentException e) {
System.out.println("Stock data not available for the specified months.");
break;
}
// Track the total contribution
totalContribution += contribution;
applyStockMarket(percentChange);
// Actions to be performed every January
if (currentDate.getMonthValue() == 1) {
adjustAllocation();
rebalance();
increaseContribution();
}
// Actions to be performed every month
dividend(); // Checks month in method
applyFixedIncomeYield();
allocateContribution();
// Update logged data
dates.add(currentDate);
stockBalances.add(stockBalance);
fixedBalances.add(fixedBalance);
//printValues();
nextMonth();
}
// Rebalance neatly for the end
allocation = endAlloc;
rebalance();
//printValues();
// Display graph
//SwingUtilities.invokeLater(this::displayChart);
}
public double getFinalAmount() {
return stockBalance + fixedBalance;
}
public double getTotalContribution() {
return totalContribution;
}
private void displayChart() {
JFrame frame = new JFrame("Investment Growth");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(800, 600);
frame.add(new GraphPanel(dates, stockBalances, fixedBalances));
frame.setVisible(true);
}
static class GraphPanel extends JPanel {
List<YearMonth> dates;
List<Double> stockBalances;
List<Double> fixedBalances;
GraphPanel(List<YearMonth> dates, List<Double> stockBalances, List<Double> fixedBalances) {
this.dates = dates;
this.stockBalances = stockBalances;
this.fixedBalances = fixedBalances;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (dates.isEmpty() || stockBalances.isEmpty() || fixedBalances.isEmpty()) {
return; // Prevent division by zero if no data exists
}
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
int padding = 50;
int graphWidth = width - 2 * padding;
int graphHeight = height - 2 * padding;
double maxBalance = stockBalances.stream().max(Double::compareTo).orElse(1.0) + fixedBalances.stream().max(Double::compareTo).orElse(1.0);
int numBars = dates.size();
if (numBars == 0) {
return;
}
int barWidth = graphWidth / (numBars * 2);
int spacing = barWidth / 2;
g2.drawLine(padding, height - padding, padding + graphWidth, height - padding);
g2.drawLine(padding, height - padding, padding, padding);
for (int i = 0; i < numBars; i++) {
int xStock = padding + i * (barWidth * 2 + spacing);
int xFixed = xStock + barWidth;
int stockBarHeight = (int) (((stockBalances.get(i) + fixedBalances.get(i)) / maxBalance) * graphHeight);
g2.setColor(Color.BLUE);
g2.fillRect(xStock, height - padding - stockBarHeight, barWidth, stockBarHeight);
}
// Legend
g2.setColor(Color.BLUE);
g2.drawString("Total", width - 150, 30);
}
}
}