Search in sources :

Example 6 with Trade

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;
}
Also used : Trade(org.ta4j.core.Trade)

Example 7 with Trade

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;
}
Also used : Trade(org.ta4j.core.Trade) Decimal(org.ta4j.core.Decimal)

Aggregations

Trade (org.ta4j.core.Trade)7 Decimal (org.ta4j.core.Decimal)3 Test (org.junit.Test)2 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)2 Marker (org.jfree.chart.plot.Marker)1 ValueMarker (org.jfree.chart.plot.ValueMarker)1 Minute (org.jfree.data.time.Minute)1 AnalysisCriterion (org.ta4j.core.AnalysisCriterion)1 ExternalCriterionTest (org.ta4j.core.ExternalCriterionTest)1 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)1