import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Marble {
// 주어진 기간동안의 종가를 기반으로 상대강도지수(RSI)를 계산함
public static Double calculateRSI(Double[] closingPrices, int period) {
// 연속된 종가 간의 가격 변동을 계산함
Double[] priceChanges = calculatePriceChanges(closingPrices);
// 상승폭과 하락폭을 각각의 배열로 분리함
List<Double> gains = new ArrayList<>();
List<Double> losses = new ArrayList<>();
for (int i = 1; i < priceChanges.length; i++) {
if (priceChanges[i] > 0) {
gains.add(priceChanges[i]);
losses.add((double) 0);
} else if (priceChanges[i] < 0) {
losses.add(-priceChanges[i]);
gains.add((double) 0);
} else {
gains.add((double) 0);
losses.add((double) 0);
}
}
// 상승폭과 하락폭을 배열로 변환함
Double[] Au = gains.toArray(new Double[0]);
Double[] Ad = losses.toArray(new Double[0]);
// 상승폭과 하락폭의 지수이동평균을 계산함
Double avgGain = calculateExponentialMovingAverage(Au, period);
Double avgLoss = calculateExponentialMovingAverage(Ad, period);
// 상대강도와 RSI 값을 계산함
Double relativeStrength = avgGain / (avgLoss + avgGain) * 100;
Double rsiValues = 100 - (100 / (1 + relativeStrength));
return rsiValues;
}
// 연속된 종가 간의 가격 변동을 계산함
private static Double[] calculatePriceChanges(Double[] closingPrices) {
Double[] priceChanges = new Double[closingPrices.length - 1];
for (int i = 1; i < closingPrices.length; i++) {
priceChanges[i - 1] = closingPrices[i] - closingPrices[i - 1];
}
return priceChanges;
}
// 주어진 기간동안 값 배열의 지수이동평균을 계산함
private static Double calculateExponentialMovingAverage(Double[] values, int period) {
// 지수이동평균을 위한 평활화 상수를 계산함
Double smoothingConstant = 1.0 / (1 + (period - 1));
// 첫 번째 값으로 지수이동평균을 초기화함
Double emaValues = values[0];
for (int i = 0; i < 14; i++) {
emaValues = values[i] + emaValues;
}
emaValues = emaValues / 14;
// 나머지 값에 대해 지수이동평균을 업데이트함
for (int i = 14; i < values.length; i++) {
emaValues = (values[i] + emaValues * (period - 1)) / period;
}
return emaValues;
}
}
rsi 검증용 엑셀도 올려봄
'프로그래밍 > 자바' 카테고리의 다른 글
자바에서 파이썬 파일 실행하고 결과 받아 오기 (0) | 2024.02.09 |
---|---|
[자바] sleep함수 (0) | 2024.02.09 |
자바에서 javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake (0) | 2024.02.08 |
자바에서 크롬 웹드라이버 실행 파일을 따로 설치하지 않고 크롤링하기[vscode] (0) | 2024.01.17 |
java.net.BindException: Address already in use: bind 에러 (1) | 2023.12.26 |