-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvestmentStrategiesChart.java
224 lines (195 loc) · 9.53 KB
/
InvestmentStrategiesChart.java
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Author: Paul-Henry Paltmann
* 2024
* No guarantees of the program working.
*/
public class InvestmentStrategiesChart extends ApplicationFrame {
public InvestmentStrategiesChart(String title,
List<Double> investedAmounts,
List<Double> buyAndHoldValues,
List<Double> movingAverageValues,
List<Double> buyLowerThanLastPurchaseValues,
List<Double> dollarCostAveragingValues) {
super(title);
JFreeChart xylineChart = ChartFactory.createXYLineChart(
"Investment Strategies Over Time",
"Time (months)",
"Value",
createDataset(investedAmounts, buyAndHoldValues, movingAverageValues, buyLowerThanLastPurchaseValues, dollarCostAveragingValues),
PlotOrientation.VERTICAL,
true, true, false);
ChartPanel chartPanel = new ChartPanel(xylineChart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
final XYPlot plot = xylineChart.getXYPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
plot.setRenderer(renderer);
setContentPane(chartPanel);
}
/**
* Creates the data lists of prices to work with.
*
* @param investedAmounts
* @param buyAndHoldValues
* @param movingAverageValues
* @param buyLowerThanLastPurchaseValues
* @param dollarCostAveragingValues
* @return
*/
private XYSeriesCollection createDataset(List<Double> investedAmounts,
List<Double> buyAndHoldValues,
List<Double> movingAverageValues,
List<Double> buyLowerThanLastPurchaseValues,
List<Double> dollarCostAveragingValues) {
final XYSeries investedSeries = new XYSeries("Invested Amount");
final XYSeries buyAndHoldSeries = new XYSeries("Buy and Hold");
final XYSeries movingAverageSeries = new XYSeries("Moving Average");
final XYSeries buyLowerThanLastPurchaseSeries = new XYSeries("Buy Lower Than Last Purchase");
final XYSeries dollarCostAveragingSeries = new XYSeries("Dollar Cost Averaging");
for (int i = 0; i < buyAndHoldValues.size(); i++) {
investedSeries.add(i, investedAmounts.get(i));
buyAndHoldSeries.add(i, buyAndHoldValues.get(i));
movingAverageSeries.add(i, movingAverageValues.get(i));
buyLowerThanLastPurchaseSeries.add(i, buyLowerThanLastPurchaseValues.get(i));
dollarCostAveragingSeries.add(i, dollarCostAveragingValues.get(i));
}
final XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(investedSeries);
dataset.addSeries(buyAndHoldSeries);
dataset.addSeries(movingAverageSeries);
dataset.addSeries(buyLowerThanLastPurchaseSeries);
dataset.addSeries(dollarCostAveragingSeries);
return dataset;
}
public static void main(String[] args) {
String csvFile = "VOO.csv"; // Muuda see failitee vastavalt oma CSV failile
List<Double> stockPrices = readPricesFromCSV(csvFile);
if (stockPrices == null || stockPrices.isEmpty()) {
System.out.println("Failed to read stock prices from CSV.");
return;
}
double initialInvestment = 1000.0;
double monthlyInvestment = 200.0;
int movingAveragePeriod = 3; // Liikuva keskmise periood kuudes
List<Double> buyAndHoldValues = simulateBuyAndHold(stockPrices, initialInvestment, monthlyInvestment);
List<Double> movingAverageValues = simulateMovingAverage(stockPrices, initialInvestment, monthlyInvestment, movingAveragePeriod);
List<Double> buyLowerThanLastPurchaseValues = simulateBuyLowerThanLastPurchase(stockPrices, initialInvestment, monthlyInvestment);
List<Double> dollarCostAveragingValues = simulateDollarCostAveraging(stockPrices, initialInvestment, monthlyInvestment);
List<Double> investedAmounts = calculateInvestedAmounts(buyAndHoldValues.size(), initialInvestment, monthlyInvestment);
InvestmentStrategiesChart chart = new InvestmentStrategiesChart("Investment Strategies",
investedAmounts, buyAndHoldValues, movingAverageValues, buyLowerThanLastPurchaseValues, dollarCostAveragingValues);
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
}
public static List<Double> calculateInvestedAmounts(int periods, double initialInvestment, double monthlyInvestment) {
List<Double> investedAmounts = new ArrayList<>();
for (int i = 0; i < periods; i++) {
investedAmounts.add(initialInvestment + (i + 1) * monthlyInvestment);
}
return investedAmounts;
}
public static List<Double> simulateBuyAndHold(List<Double> prices, double initialInvestment, double monthlyInvestment) {
List<Double> values = new ArrayList<>();
double shares = initialInvestment / prices.get(0);
for (int i = 1; i < prices.size(); i++) {
shares += monthlyInvestment / prices.get(i);
values.add(shares * prices.get(i));
}
return values;
}
public static List<Double> simulateMovingAverage(List<Double> prices, double initialInvestment, double monthlyInvestment, int period) {
List<Double> values = new ArrayList<>();
double cash = initialInvestment;
double shares = 0.0;
for (int i = 0; i < prices.size(); i++) {
if (i >= period) {
double movingAverage = calculateMovingAverage(prices, i - period, i);
if (prices.get(i) > movingAverage && cash > 0) {
shares += cash / prices.get(i);
cash = 0;
} else if (prices.get(i) < movingAverage && shares > 0) {
cash += shares * prices.get(i);
shares = 0;
}
}
cash += monthlyInvestment;
shares += monthlyInvestment / prices.get(i);
values.add(cash + shares * prices.get(i));
}
return values;
}
public static List<Double> simulateBuyLowerThanLastPurchase(List<Double> prices, double initialInvestment, double monthlyInvestment) {
List<Double> values = new ArrayList<>();
double cash = initialInvestment;
double shares = 0.0;
double lastPurchasePrice = Double.MAX_VALUE;
for (int i = 0; i < prices.size(); i++) {
if (prices.get(i) < lastPurchasePrice && cash > 0) {
shares += cash / prices.get(i);
cash = 0;
lastPurchasePrice = prices.get(i);
}
cash += monthlyInvestment;
if (prices.get(i) < lastPurchasePrice) {
shares += monthlyInvestment / prices.get(i);
lastPurchasePrice = prices.get(i);
}
values.add(cash + shares * prices.get(i));
}
return values;
}
public static List<Double> simulateDollarCostAveraging(List<Double> prices, double initialInvestment, double monthlyInvestment) {
List<Double> values = new ArrayList<>();
double cash = initialInvestment;
double shares = 0.0;
for (int i = 0; i < prices.size(); i++) {
cash += monthlyInvestment;
shares += monthlyInvestment / prices.get(i);
values.add(cash + shares * prices.get(i));
}
return values;
}
public static List<Double> readPricesFromCSV(String csvFile) {
List<Double> prices = new ArrayList<>();
String line;
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
br.readLine(); // Skip header line
while ((line = br.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length > 4 && fields[4] != null && !fields[4].isEmpty()) {
try {
prices.add(Double.parseDouble(fields[4])); // Adj Close price
} catch (NumberFormatException e){
e.printStackTrace();
System.out.println("NumberFormatException found: "+ fields[4]);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return prices;
}
public static double calculateMovingAverage(List<Double> prices, int start, int end) {
double sum = 0.0;
for (int i = start; i < end; i++) {
sum += prices.get(i);
}
return sum / (end - start);
}
}