Program: 59456
Project: 59456
Buggy source:
/** 
 * Compute the EMA over a timespan before the a given end time.
 * @see http://www.iexplain.org/ema-how-to-calculate/
 * @see http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:moving_averages#exponential_moving_a
 * @param trades The list of trades.
 * @param startTime The start time as microseconds.
 * @param endTime The end time as microseconds.
 * @param timePeriod The time period as microseconds (day, hour etc).
 * @return The EMA of the trade prices.
 * @throws NotEnoughTradesException if there are not enough trades in the array to perform the computation.
 */
public Price ema(Trade[] trades,long startTime,long endTime,long timePeriod) throws NotEnoughTradesException {
  long timeInterval=endTime - startTime;
  int nPeriods=(int)(timeInterval / timePeriod);
  if (nPeriods == 0) {
    return null;
  }
  double[] weights=new double[nPeriods + 1];
  weights[weights.length - 1]=0.0d;
  for (int currentTimePeriod=weights.length - 2; currentTimePeriod >= 0; --currentTimePeriod) {
    weights[currentTimePeriod]=(1.0d - weights[currentTimePeriod + 1]) * (2.0d / (currentTimePeriod + 2));
  }
  Price totalPrice=new Price("String_Node_Str");
  double totalWeight=0.0d;
  for (int index=0; index < trades.length; ++index) {
    Trade currentTrade=trades[index];
    long currentTimestamp=currentTrade.getTimestamp();
    if ((startTime == -1L) || (currentTimestamp >= startTime)) {
      if ((endTime == -1L) || (currentTimestamp <= endTime)) {
        long endDistance=endTime - currentTimestamp;
        int currentTimePeriod=(int)(endDistance / timePeriod);
        double weight=weights[weights.length - 2 - currentTimePeriod];
        totalPrice=new Price(totalPrice.add(currentTrade.getPrice().multiply(new BigDecimal(weight))));
        totalWeight+=weight;
      }
    }
  }
  return new Price(totalPrice.divide(new BigDecimal(totalWeight,MathContext.DECIMAL128)));
}

Fixed version (ground truth):
/** 
 * Compute the EMA over a timespan before the a given end time.
 * @see http://www.iexplain.org/ema-how-to-calculate/
 * @see http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:moving_averages#exponential_moving_a
 * @param trades The list of trades.
 * @param startTime The start time as microseconds.
 * @param endTime The end time as microseconds.
 * @param timePeriod The time period as microseconds (day, hour etc).
 * @return The EMA of the trade prices.
 * @throws NotEnoughTradesException if there are not enough trades in the array to perform the computation.
 */
public Price ema(Trade[] trades,long startTime,long endTime,long timePeriod) throws NotEnoughTradesException {
  long timeInterval=endTime - startTime;
  int nPeriods=(int)(timeInterval / timePeriod);
  if (nPeriods == 0) {
    return null;
  }
  double[] weights=new double[nPeriods + 1];
  weights[weights.length - 1]=0.0d;
  for (int currentTimePeriod=weights.length - 2; currentTimePeriod >= 0; --currentTimePeriod) {
    weights[currentTimePeriod]=(1.0d - weights[currentTimePeriod + 1]) * (2.0d / (currentTimePeriod + 2));
  }
  Price totalPrice=new Price("String_Node_Str");
  double totalWeight=0.0d;
  for (int index=0; index < trades.length; ++index) {
    Trade currentTrade=trades[index];
    long currentTimestamp=currentTrade.getTimestamp();
    if ((startTime == -1L) || (currentTimestamp >= startTime)) {
      if ((endTime == -1L) || (currentTimestamp <= endTime)) {
        long endDistance=endTime - currentTimestamp;
        int currentTimePeriod=(int)(endDistance / timePeriod);
        double weight=weights[weights.length - 2 - currentTimePeriod];
        totalPrice=new Price(totalPrice.add(currentTrade.getPrice().multiply(new BigDecimal(weight))));
        totalWeight+=weight;
      }
    }
  }
  return new Price(totalPrice.divide(new BigDecimal(totalWeight,MathContext.DECIMAL128),MathContext.DECIMAL128));
}