use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class MaximumDrawdownCriterion method calculateMaximumDrawdown.
/**
* Calculates the maximum drawdown from a cash flow over a series.
* @param series the time series
* @param cashFlow the cash flow
* @return the maximum drawdown from a cash flow over a series
*/
private Decimal calculateMaximumDrawdown(TimeSeries series, CashFlow cashFlow) {
Decimal maximumDrawdown = Decimal.ZERO;
Decimal maxPeak = Decimal.ZERO;
if (!series.isEmpty()) {
// The series is not empty
for (int i = series.getBeginIndex(); i <= series.getEndIndex(); i++) {
Decimal value = cashFlow.getValue(i);
if (value.isGreaterThan(maxPeak)) {
maxPeak = value;
}
Decimal drawdown = maxPeak.minus(value).dividedBy(maxPeak);
if (drawdown.isGreaterThan(maximumDrawdown)) {
maximumDrawdown = drawdown;
}
}
}
return maximumDrawdown;
}
use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class MaximumDrawdownCriterion method calculate.
@Override
public double calculate(TimeSeries series, Trade trade) {
if (trade != null && trade.getEntry() != null && trade.getExit() != null) {
CashFlow cashFlow = new CashFlow(series, trade);
Decimal maximumDrawdown = calculateMaximumDrawdown(series, cashFlow);
return maximumDrawdown.doubleValue();
}
return 0;
}
use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class TotalProfitCriterion method calculateProfit.
/**
* Calculates the profit of a trade (Buy and sell).
* @param series a time series
* @param trade a trade
* @return the profit of the trade
*/
private double calculateProfit(TimeSeries series, Trade trade) {
Decimal profit = Decimal.ONE;
if (trade.isClosed()) {
// use price of entry/exit order, if NaN use close price of underlying time series
Decimal exitClosePrice = trade.getExit().getPrice().isNaN() ? series.getBar(trade.getExit().getIndex()).getClosePrice() : trade.getExit().getPrice();
Decimal entryClosePrice = trade.getEntry().getPrice().isNaN() ? series.getBar(trade.getEntry().getIndex()).getClosePrice() : trade.getEntry().getPrice();
if (trade.getEntry().isBuy()) {
profit = exitClosePrice.dividedBy(entryClosePrice);
} else {
profit = entryClosePrice.dividedBy(exitClosePrice);
}
}
return profit.doubleValue();
}
use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class StochasticOscillatorKIndicator method calculate.
@Override
protected Decimal calculate(int index) {
HighestValueIndicator highestHigh = new HighestValueIndicator(maxPriceIndicator, timeFrame);
LowestValueIndicator lowestMin = new LowestValueIndicator(minPriceIndicator, timeFrame);
Decimal highestHighPrice = highestHigh.getValue(index);
Decimal lowestLowPrice = lowestMin.getValue(index);
return indicator.getValue(index).minus(lowestLowPrice).dividedBy(highestHighPrice.minus(lowestLowPrice)).multipliedBy(Decimal.HUNDRED);
}
use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class WMAIndicator method calculate.
@Override
protected Decimal calculate(int index) {
if (index == 0) {
return indicator.getValue(0);
}
Decimal value = Decimal.ZERO;
if (index - timeFrame < 0) {
for (int i = index + 1; i > 0; i--) {
value = value.plus(Decimal.valueOf(i).multipliedBy(indicator.getValue(i - 1)));
}
return value.dividedBy(Decimal.valueOf(((index + 1) * (index + 2)) / 2));
}
int actualIndex = index;
for (int i = timeFrame; i > 0; i--) {
value = value.plus(Decimal.valueOf(i).multipliedBy(indicator.getValue(actualIndex)));
actualIndex--;
}
return value.dividedBy(Decimal.valueOf((timeFrame * (timeFrame + 1)) / 2));
}
Aggregations