【发布时间】:2021-06-21 07:32:23
【问题描述】:
我已经通过这个公式实现了supertend:
BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR
BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR
FINAL UPPERBAND = IF( (Current BASICUPPERBAND < Previous FINAL UPPERBAND) and (Previous Close > Previous FINAL UPPERBAND)) THEN (Current BASIC UPPERBAND) ELSE Previous FINALUPPERBAND)
FINAL LOWERBAND = IF( (Current BASIC LOWERBAND > Previous FINAL LOWERBAND) and (Previous Close < Previous FINAL LOWERBAND)) THEN (Current BASIC LOWERBAND) ELSE Previous FINAL LOWERBAND)
SUPERTREND = IF(Current Close <= Current FINAL UPPERBAND ) THEN Current FINAL UPPERBAND ELSE Current FINAL LOWERBAND
我的代码是:
//returns supertrend value
public double get(int index) {
double finalUpperBand = finalUpperBand(index);
double finalLowerBand = finalLowerBand(index);
if (data.getBar(index).getClose() <= finalUpperBand){
return finalUpperBand;
}else {
return finalLowerBand;
}
}
//calculation upperband
private double finalUpperBand(int index){
double atr = new ATRIndicator(data).get(index);
double multiplier = 3 ;
double max = data.getBar(index).getMax();
double min = data.getBar(index).getMin();
double upperBand = ((max+min)/2) + (multiplier*atr) ;
if (upperBand < finalUpperBand(index-1) && data.getBar(index-1).getClose() > finalUpperBand(index-1) ){
return upperBand;
}else {
return finalUpperBand(index-1);
}
}
//calculation lowerband
private double finalLowerBand(int index){
double atr = new ATRIndicator(data).get(index);
double multiplier = 3 ;
double max = data.getBar(index).getMax();
double min = data.getBar(index).getMin();
double lowerBand = (max+min)/2 - (multiplier*atr) ;
if ( lowerBand > finalLowerBand(index-1) && data.getBar(index-1).getClose() < finalLowerBand(index-1) ){
return lowerBand;
}else {
return finalLowerBand(index-1);
}
}
但它不起作用,我知道问题出在递归方法上,但我无法根据公式找到出路!
我添加了基本操作的代码,但我不确定这是否是超级趋势指标!
if (index == 0) {
return upperBand;
}
【问题讨论】: