use of org.ta4j.core.Trade in project ta4j by ta4j.
the class LinearTransactionCostCriterion method calculate.
@Override
public double calculate(TimeSeries series, TradingRecord tradingRecord) {
double totalCosts = 0d;
double tradedAmount = initialAmount;
for (Trade trade : tradingRecord.getTrades()) {
double tradeCost = getTradeCost(series, trade, tradedAmount);
totalCosts += tradeCost;
// To calculate the new traded amount:
// - Remove the cost of the *first* order
// - Multiply by the profit ratio
// - Remove the cost of the *second* order
tradedAmount = (tradedAmount - getOrderCost(trade.getEntry(), tradedAmount));
tradedAmount *= profit.calculate(series, trade);
tradedAmount -= getOrderCost(trade.getExit(), tradedAmount);
}
// Special case: if the current trade is open
Trade currentTrade = tradingRecord.getCurrentTrade();
if (currentTrade.isOpened()) {
totalCosts += getOrderCost(currentTrade.getEntry(), tradedAmount);
}
return totalCosts;
}
use of org.ta4j.core.Trade in project ta4j by ta4j.
the class StopLossRule method isSatisfied.
@Override
public boolean isSatisfied(int index, TradingRecord tradingRecord) {
boolean satisfied = false;
// No trading history or no trade opened, no loss
if (tradingRecord != null) {
Trade currentTrade = tradingRecord.getCurrentTrade();
if (currentTrade.isOpened()) {
Decimal entryPrice = currentTrade.getEntry().getPrice();
Decimal currentPrice = closePrice.getValue(index);
Decimal threshold = entryPrice.multipliedBy(lossRatioThreshold);
if (currentTrade.getEntry().isBuy()) {
satisfied = currentPrice.isLessThanOrEqual(threshold);
} else {
satisfied = currentPrice.isGreaterThanOrEqual(threshold);
}
}
}
traceIsSatisfied(index, satisfied);
return satisfied;
}
Aggregations