-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvestmentStrategiesFromCSV.java
138 lines (118 loc) · 5.58 KB
/
InvestmentStrategiesFromCSV.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class InvestmentStrategiesFromCSV {
public static void main(String[] args) {
String csvFile = "VOO.csv"; // 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
double buyAndHoldReturn = buyAndHoldStrategy(stockPrices, initialInvestment, monthlyInvestment);
double movingAverageReturn = movingAverageStrategy(stockPrices, initialInvestment, monthlyInvestment, movingAveragePeriod);
double buyLowerThanLastPurchaseReturn = buyLowerThanLastPurchaseStrategy(stockPrices, initialInvestment, monthlyInvestment);
double dollarCostAveragingReturn = dollarCostAveragingStrategy(stockPrices, initialInvestment, monthlyInvestment);
System.out.printf("Osta-ja-hoia strateegia tootlus: %.2f%n", buyAndHoldReturn);
System.out.printf("Liikuva keskmise strateegia tootlus: %.2f%n", movingAverageReturn);
System.out.printf("Osta madalama hinnaga kui eelmine ost strateegia tootlus: %.2f%n", buyLowerThanLastPurchaseReturn);
System.out.printf("Dollar cost averaging strateegia tootlus: %.2f%n", dollarCostAveragingReturn);
}
/**
* Loeb CSV failist aktsia hinnad
* @param csvFile name in the project folder.
* @return ArrayList of prices.
*/
public static List<Double> readPricesFromCSV(String csvFile) {
List<Double> prices = new ArrayList<>();
String line;
String cvsSplitBy = ",";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
br.readLine(); // Skip header line
while ((line = br.readLine()) != null) {
String[] data = line.split(cvsSplitBy);
if (data.length > 5 && !data[5].equals("null")) {
prices.add(Double.parseDouble(data[5])); // Assume 'Adj Close' is the 6th column
}
}
} catch (IOException e) {
e.printStackTrace();
}
return prices;
}
// Osta-ja-hoia strateegia
public static double buyAndHoldStrategy(List<Double> prices, double initialInvestment, double monthlyInvestment) {
double shares = initialInvestment / prices.get(0);
for (int i = 1; i < prices.size(); i++) {
shares += monthlyInvestment / prices.get(i);
}
double finalValue = shares * prices.get(prices.size() - 1);
return finalValue;
}
// Liikuva keskmise strateegia
public static double movingAverageStrategy(List<Double> prices, double initialInvestment, double monthlyInvestment, int period) {
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);
}
double finalValue = cash + shares * prices.get(prices.size() - 1);
return finalValue;
}
// Osta madalama hinnaga kui eelmine ost strateegia
public static double buyLowerThanLastPurchaseStrategy(List<Double> prices, double initialInvestment, double monthlyInvestment) {
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);
}
}
double finalValue = cash + shares * prices.get(prices.size() - 1);
return finalValue;
}
// Dollar Cost Averaging strateegia
public static double dollarCostAveragingStrategy(List<Double> prices, double initialInvestment, double monthlyInvestment) {
double cash = initialInvestment;
double shares = 0.0;
for (int i = 0; i < prices.size(); i++) {
if (cash >= monthlyInvestment) {
shares += monthlyInvestment / prices.get(i);
cash -= monthlyInvestment;
}
}
double finalValue = cash + shares * prices.get(prices.size() - 1);
return finalValue;
}
private 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);
}
}