-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMovingAverage.java
More file actions
41 lines (35 loc) · 1.61 KB
/
MovingAverage.java
File metadata and controls
41 lines (35 loc) · 1.61 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
import java.util.List;
public class MovingAverage {
public static boolean isStockGood(double currentValue, double movingAverage, double threshold) {
if (currentValue > threshold) {
System.out.println("Stock is good.");
return true;
} else {
System.out.println("Stock is not good.");
return false;
}
}
public static void main(String[] args) {
List<Double> historicalData = List.of(
351.950012, 358.660004, 357.429993, 370.470001, 369.670013, 374.489990, 367.459991,
368.369995, 376.130005, 383.450012, 381.779999, 385.200012, 390.700012, 393.179993,
394.140015, 401.019989, 400.059998, 390.140015, 394.779999, 474.989990, 459.410004,
454.720001
);
int windowSize = 3; // Number of data points to consider for the moving average
double thresholdPercentage = 0.05; // Percentage factor to adjust the threshold
for (int i = windowSize; i <= historicalData.size(); i++) {
List<Double> subList = historicalData.subList(i - windowSize, i);
double sum = 0;
for (double data : subList) {
sum += data;
}
double movingAverage = sum / windowSize;
double currentValue = historicalData.get(i - 1);
double threshold = movingAverage * (1 + thresholdPercentage);
boolean isGoodStock = isStockGood(currentValue, movingAverage, threshold);
System.out.println("Is the stock good? " + isGoodStock);
System.out.println();
}
}
}