Since you basically need the same functionality for multiple line crosses, consider introducing an
enum which does the job
enum Cross {
noCross,
firstOvertakeScnd,
scndOvertakeFirst;
public static Cross getCross(double firstOld, double firstNew, double scndOld, double scndNew){
if(firstOld < scndOld && firstNew > scndNew){
return firstOvertakeScnd;
}
if(scndOld < firstOld && scndNew > firstNew){
return scndOvertakeFirst;
}
return noCross;
}
public String getCrossInfo (String firstLineName, String scndLineName){
if(this == firstOvertakeScnd){
return String.format("%s OVERTOOK %s", firstLineName, scndLineName);
}
if(this == scndOvertakeFirst){
return String.format("%s OVERTOOK %s", scndLineName, firstLineName);
}
return String.format("%s did not cross %s", firstLineName, scndLineName);
}
}
And then you just call it on multiple result arrays:
// calculate crosses
int last = rangeBarCount - 1;
int prev = last - 1;
Cross macdCross = Cross.getCross(macd[MACD][prev], macd[MACD][last], macd[MACD_SIGNAL][prev], macd[MACD_SIGNAL][last]);
Cross emaCross = Cross.getCross(emaFast[prev], emaFast[last], emaSlow[prev], emaSlow[last]);
print("___Cross info: %s; %s",
macdCross.getCrossInfo("macd", "macd signal"),
emaCross.getCrossInfo("ema fast", "ema slow"));