use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class WMAIndicatorTest method wmaWithTimeFrameGreaterThanSeriesSize.
@Test
public void wmaWithTimeFrameGreaterThanSeriesSize() {
MockTimeSeries series = new MockTimeSeries(1d, 2d, 3d, 4d, 5d, 6d);
Indicator<Decimal> close = new ClosePriceIndicator(series);
Indicator<Decimal> wmaIndicator = new WMAIndicator(close, 55);
assertDecimalEquals(wmaIndicator.getValue(0), 1);
assertDecimalEquals(wmaIndicator.getValue(1), 1.6667);
assertDecimalEquals(wmaIndicator.getValue(2), 2.3333);
assertDecimalEquals(wmaIndicator.getValue(3), 3);
assertDecimalEquals(wmaIndicator.getValue(4), 3.6666);
assertDecimalEquals(wmaIndicator.getValue(5), 4.3333);
}
use of org.ta4j.core.Decimal in project crypto-bot by jnidzwetzki.
the class EMABot method closeOrder.
/**
* Execute a new close order
* @param symbol
* @param endIndex
* @throws APIException
*/
private void closeOrder(final BitfinexCurrencyPair symbol, final int endIndex) {
final Decimal lastClosePrice = timeSeries.get(symbol).getLastBar().getClosePrice();
final Trade openTrade = getOpenTrade(symbol);
if (openTrade == null) {
logger.error("Unable to close a trade, there is no trade open");
return;
}
openTrade.setExpectedPriceClose(lastClosePrice.doubleValue());
orderManager.closeTrade(openTrade);
}
use of org.ta4j.core.Decimal in project crypto-bot by jnidzwetzki.
the class TradeExecutor method closeTrade.
private void closeTrade(final int i) {
final Decimal priceOut = timeSeries.getBar(i).getOpenPrice();
final Decimal priceIn = timeSeries.getBar(openBarIndex).getOpenPrice();
final double positionValue = priceOut.doubleValue() * openContracts;
calculateFees(positionValue);
double pl = priceOut.minus(priceIn).doubleValue() * openContracts;
// Hard stop loss
if (pl < portfolioValue * STOP_LOSS) {
executedStopLoss++;
pl = portfolioValue * STOP_LOSS;
}
portfolioValue = portfolioValue + pl;
if (pl < 0) {
maxLoose = Math.min(maxLoose, pl);
looser++;
looserInARow++;
} else {
maxWin = Math.max(maxWin, pl);
winner++;
looserInARowList.add(looserInARow);
looserInARow = 0;
}
openContracts = 0;
openBarIndex = -1;
}
use of org.ta4j.core.Decimal in project crypto-bot by jnidzwetzki.
the class DonchianChannelLower method calculate.
@Override
protected Decimal calculate(int index) {
int startIndex = Math.max(0, index - timeFrame + 1);
Decimal result = indicator.getValue(index);
for (int pos = startIndex; pos <= index; pos++) {
final Decimal value = indicator.getValue(pos);
if (value.isLessThanOrEqual(result)) {
result = value;
}
}
return result;
}
use of org.ta4j.core.Decimal in project ta4j by ta4j.
the class ParabolicSarIndicator method calculate.
@Override
protected Decimal calculate(int index) {
Decimal sar = Decimal.NaN;
if (index == series.getBeginIndex()) {
// no trend detection possible for the first value
return sar;
} else if (index == series.getBeginIndex() + 1) {
// start trend detection
currentTrend = series.getBar(series.getBeginIndex()).getClosePrice().isLessThan(series.getBar(index).getClosePrice());
if (!currentTrend) {
// down trend
// put sar on max price of candlestick
sar = maxPriceIndicator.getValue(index);
currentExtremePoint = sar;
minMaxExtremePoint = currentExtremePoint;
} else {
// up trend
// put sar on min price of candlestick
sar = minPriceIndicator.getValue(index);
currentExtremePoint = sar;
minMaxExtremePoint = currentExtremePoint;
}
return sar;
}
Decimal priorSar = getValue(index - 1);
if (currentTrend) {
// if up trend
sar = priorSar.plus(accelerationFactor.multipliedBy((currentExtremePoint.minus(priorSar))));
currentTrend = minPriceIndicator.getValue(index).isGreaterThan(sar);
if (!currentTrend) {
// check if sar touches the min price
// sar starts at the highest extreme point of previous up trend
sar = minMaxExtremePoint;
// switch to down trend and reset values
currentTrend = false;
startTrendIndex = index;
accelerationFactor = accelarationStart;
// put point on max
currentExtremePoint = series.getBar(index).getMinPrice();
minMaxExtremePoint = currentExtremePoint;
} else {
// up trend is going on
currentExtremePoint = new HighestValueIndicator(maxPriceIndicator, index - startTrendIndex).getValue(index);
if (currentExtremePoint.isGreaterThan(minMaxExtremePoint)) {
incrementAcceleration();
minMaxExtremePoint = currentExtremePoint;
}
}
} else {
// downtrend
sar = priorSar.minus(accelerationFactor.multipliedBy(((priorSar.minus(currentExtremePoint)))));
currentTrend = maxPriceIndicator.getValue(index).isGreaterThanOrEqual(sar);
if (currentTrend) {
// check if switch to up trend
// sar starts at the lowest extreme point of previous down trend
sar = minMaxExtremePoint;
accelerationFactor = accelarationStart;
startTrendIndex = index;
currentExtremePoint = series.getBar(index).getMaxPrice();
minMaxExtremePoint = currentExtremePoint;
} else {
// down trend io going on
currentExtremePoint = new LowestValueIndicator(minPriceIndicator, index - startTrendIndex).getValue(index);
if (currentExtremePoint.isLessThan(minMaxExtremePoint)) {
incrementAcceleration();
minMaxExtremePoint = currentExtremePoint;
}
}
}
return sar;
}
Aggregations