Update of /cvsroot/tail/Tail/src/java/net/sf/tail/indicator/helper
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv2946/src/java/net/sf/tail/indicator/helper
Added Files:
AverageGainIndicator.java HighestValueIndicator.java
StandardDeviationIndicator.java AverageLossIndicator.java
CrossIndicator.java LowestValueIndicator.java
Log Message:
Refatoração de testes
--- NEW FILE: LowestValueIndicator.java ---
package net.sf.tail.indicator.helper;
import net.sf.tail.Indicator;
public class LowestValueIndicator implements Indicator<Double> {
private final Indicator<? extends Number> indicator;
private final int timeFrame;
public LowestValueIndicator(Indicator<? extends Number> indicator, int timeFrame) {
this.indicator = indicator;
this.timeFrame = timeFrame;
}
public Double getValue(int index) {
int start = Math.max(0, index - timeFrame + 1);
Double lowest = (Double) indicator.getValue(start);
for (int i = start + 1; i <= index; i++) {
if (lowest.doubleValue() > indicator.getValue(i).doubleValue())
lowest = (Double) indicator.getValue(i);
}
return lowest;
}
}
--- NEW FILE: AverageLossIndicator.java ---
package net.sf.tail.indicator.helper;
import net.sf.tail.Indicator;
public class AverageLossIndicator implements Indicator<Double> {
private final Indicator<? extends Number> indicator;
private final int timeFrame;
public AverageLossIndicator(Indicator<? extends Number> indicator, int timeFrame) {
this.indicator = indicator;
this.timeFrame = timeFrame;
}
public Double getValue(int index) {
double result = 0;
for (int i = Math.max(1, index - timeFrame + 1); i <= index; i++) {
if (indicator.getValue(i).doubleValue() < indicator.getValue(i - 1).doubleValue())
result += indicator.getValue(i - 1).doubleValue() - indicator.getValue(i).doubleValue();
}
return result / Math.min(timeFrame, index + 1);
}
}
--- NEW FILE: HighestValueIndicator.java ---
package net.sf.tail.indicator.helper;
import net.sf.tail.Indicator;
public class HighestValueIndicator implements Indicator<Double> {
private final Indicator<? extends Number> indicator;
private final int timeFrame;
public HighestValueIndicator(Indicator<? extends Number> indicator, int timeFrame) {
this.indicator = indicator;
this.timeFrame = timeFrame;
}
public Double getValue(int index) {
int start = Math.max(0, index - timeFrame + 1);
Double highest = (Double) indicator.getValue(start);
for (int i = start + 1; i <= index; i++) {
if (highest.doubleValue() < indicator.getValue(i).doubleValue())
highest = (Double) indicator.getValue(i);
}
return highest;
}
}
--- NEW FILE: CrossIndicator.java ---
package net.sf.tail.indicator.helper;
import net.sf.tail.Indicator;
public class CrossIndicator implements Indicator<Boolean> {
private final Indicator<? extends Number> low;
private final Indicator<? extends Number> up;
public CrossIndicator(Indicator<? extends Number> up, Indicator<? extends Number> low) {
this.up = up;
this.low = low;
}
public Boolean getValue(int index) {
if (index == 0 || up.getValue(index).doubleValue() >= (low.getValue(index).doubleValue()))
return false;
index--;
if (up.getValue(index).doubleValue() > low.getValue(index).doubleValue())
return true;
else {
while (index > 0 && up.getValue(index).doubleValue() == low.getValue(index).doubleValue())
index--;
if (index == 0)
return false;
if (up.getValue(index).doubleValue() > low.getValue(index).doubleValue())
return true;
return false;
}
}
public Indicator<? extends Number> getLow() {
return low;
}
public Indicator<? extends Number> getUp() {
return up;
}
}
--- NEW FILE: AverageGainIndicator.java ---
package net.sf.tail.indicator.helper;
import net.sf.tail.Indicator;
public class AverageGainIndicator implements Indicator<Double> {
private final Indicator<? extends Number> indicator;
private final int timeFrame;
public AverageGainIndicator(Indicator<? extends Number> indicator, int timeFrame) {
this.indicator = indicator;
this.timeFrame = timeFrame;
}
public Double getValue(int index) {
double result = 0;
for (int i = Math.max(1, index - timeFrame + 1); i <= index; i++) {
if (indicator.getValue(i).doubleValue() >= indicator.getValue(i - 1).doubleValue())
result += indicator.getValue(i).doubleValue() - indicator.getValue(i - 1).doubleValue();
}
return result / Math.min(timeFrame, index + 1);
}
}
--- NEW FILE: StandardDeviationIndicator.java ---
package net.sf.tail.indicator.helper;
import net.sf.tail.Indicator;
import net.sf.tail.indicator.tracker.SMAIndicator;
public class StandardDeviationIndicator implements Indicator<Double> {
private Indicator<? extends Number> indicator;
private int timeFrame;
private SMAIndicator sma;
public StandardDeviationIndicator(Indicator<? extends Number> indicator, int timeFrame) {
this.indicator = indicator;
this.timeFrame = timeFrame;
sma = new SMAIndicator(indicator, timeFrame);
}
public Double getValue(int index) {
double standardDeviation = 0.0;
double average = sma.getValue(index);
for (int i = Math.max(0, index - timeFrame + 1); i <= index; i++) {
standardDeviation += Math.pow(indicator.getValue(i).doubleValue() - average, 2.0);
}
return Math.sqrt(standardDeviation);
}
}
|