kali-hi
Banned
- Registriert
- Sep. 2025
- Beiträge
- 760
Moin, ich versuche gerade, die Anzahl der letzten Heikin-Ashi-Kerzen zu berechnen...
Beispiel:
Ich denke, das Prinzip sollte damit schon mal klar sein.
Ich bin mir aber noch nicht sicher, ob mein Code auch richtig ist (besonders bei der Berechnung der ersten Kerze der Folge), und hatte auch noch keine Gelegenheit, es mit echten Daten zu testen... Vielleicht könnte mir jemand einen 👍 oder 👎 geben.
Code:
Beispiel:
| Folge | Anzahl |
| ... rot, rot, rot, rot | -4 |
| ... rot, grün, rot, rot | -2 |
| ... rot, grün, grün, grün | 3 |
Ich denke, das Prinzip sollte damit schon mal klar sein.
Ich bin mir aber noch nicht sicher, ob mein Code auch richtig ist (besonders bei der Berechnung der ersten Kerze der Folge), und hatte auch noch keine Gelegenheit, es mit echten Daten zu testen... Vielleicht könnte mir jemand einen 👍 oder 👎 geben.
Code:
Java:
public int getHeikinAshiDirection() {
if (series == null) {
updateSeries();
}
int endIndex = series.getEndIndex();
ArrayList<Object[]> haValues = new ArrayList<>();
Bar firstBar = series.getBar(0);
double firstOpen = firstBar.getOpenPrice().doubleValue();
double firstHigh = firstBar.getHighPrice().doubleValue();
double firstLow = firstBar.getLowPrice().doubleValue();
double firstClose = firstBar.getClosePrice().doubleValue();
Object[] firstHa = new Object[] {firstOpen, firstHigh, firstLow, firstClose};
haValues.add(
new Object[] {
calculateOpen(firstHa),
calculateHigh(firstBar),
calculateLow(firstBar),
calculateClose(firstOpen, firstHigh, firstLow, firstClose),
calculateDirection(firstOpen, firstClose)
});
for (int i = 1; i <= endIndex; i++) {
Object[] previousHa = haValues.get(i - 1);
Bar currentBar = series.getBar(i);
double open = calculateOpen(previousHa);
double high = calculateHigh(currentBar);
double low = calculateLow(currentBar);
double close =
calculateClose(
currentBar.getOpenPrice().doubleValue(),
currentBar.getHighPrice().doubleValue(),
currentBar.getLowPrice().doubleValue(),
currentBar.getClosePrice().doubleValue());
int direction = calculateDirection(open, close);
haValues.add(new Object[] {open, high, low, close, direction});
}
int lastHaDirection = (int) haValues.get(endIndex)[4];
int count = 1;
for (int i = endIndex - 1; i >= 0; i--) {
int direction = (int) haValues.get(i)[4];
if (direction == lastHaDirection) {
count++;
} else {
break;
}
}
Main.logger.info("{} Heikin Ashi direction: {} (count: {})", NAME, lastHaDirection, count);
return lastHaDirection * count;
}
private double calculateHigh(Bar second) {
double high = second.getHighPrice().doubleValue();
double open = second.getOpenPrice().doubleValue();
double close = second.getClosePrice().doubleValue();
return Collections.max(List.of(high, open, close));
}
private double calculateLow(Bar second) {
double low = second.getLowPrice().doubleValue();
double open = second.getOpenPrice().doubleValue();
double close = second.getClosePrice().doubleValue();
return Collections.min(List.of(low, open, close));
}
private double calculateOpen(Object[] previousHa) {
double previousOpen = (double) previousHa[0];
double previousClose = (double) previousHa[3];
return (previousOpen + previousClose) * 0.5;
}
private double calculateClose(double open, double high, double low, double close) {
return (open + high + low + close) / 4;
}
private int calculateDirection(double calculatedOpen, double calculatedClose) {
return calculatedClose > calculatedOpen ? 1 : -1;
}