Skip to content

Commit 1f3cc76

Browse files
authored
TrailingStopLossRule ta4j#906 Remove instance variable (ta4j#1056)
* ta4j#906 Remove instance variable for currentStopLossLimitActivation * Format code
1 parent c0c6daf commit 1f3cc76

File tree

3 files changed

+8
-36
lines changed

3 files changed

+8
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Changelog for `ta4j`, roughly following [keepachangelog.com](http://keepachangel
2828
- **ProfitCriterion** fixed excludeCosts functionality as it was reversed
2929
- **LossCriterion** fixed excludeCosts functionality as it was reversed
3030
- **PerformanceReportGenerator** fixed netProfit and netLoss calculations to include costs
31+
- **TrailingStopLossRule** removed instance variable `currentStopLossLimitActivation` because it may not be alway the correct (last) value
3132

3233
### Changed
3334
- **BarSeriesManager** consider finishIndex when running backtest

ta4j-core/src/main/java/org/ta4j/core/rules/TrailingStopLossRule.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ public class TrailingStopLossRule extends AbstractRule {
4747
/** the loss-distance as percentage. */
4848
private final Num lossPercentage;
4949

50-
/** The current stop loss price activation. */
51-
private Num currentStopLossLimitActivation;
52-
5350
/**
5451
* Constructor.
5552
*
@@ -100,20 +97,16 @@ private boolean isBuySatisfied(Num currentPrice, int index, int positionIndex) {
10097
getValueIndicatorBarCount(index, positionIndex));
10198
Num highestCloseNum = highest.getValue(index);
10299
Num lossRatioThreshold = highestCloseNum.numOf(100).minus(lossPercentage).dividedBy(highestCloseNum.numOf(100));
103-
currentStopLossLimitActivation = highestCloseNum.multipliedBy(lossRatioThreshold);
100+
Num currentStopLossLimitActivation = highestCloseNum.multipliedBy(lossRatioThreshold);
104101
return currentPrice.isLessThanOrEqual(currentStopLossLimitActivation);
105102
}
106103

107-
public Num getCurrentStopLossLimitActivation() {
108-
return currentStopLossLimitActivation;
109-
}
110-
111104
private boolean isSellSatisfied(Num currentPrice, int index, int positionIndex) {
112105
LowestValueIndicator lowest = new LowestValueIndicator(priceIndicator,
113106
getValueIndicatorBarCount(index, positionIndex));
114107
Num lowestCloseNum = lowest.getValue(index);
115108
Num lossRatioThreshold = lowestCloseNum.numOf(100).plus(lossPercentage).dividedBy(lowestCloseNum.numOf(100));
116-
currentStopLossLimitActivation = lowestCloseNum.multipliedBy(lossRatioThreshold);
109+
Num currentStopLossLimitActivation = lowestCloseNum.multipliedBy(lossRatioThreshold);
117110
return currentPrice.isGreaterThanOrEqual(currentStopLossLimitActivation);
118111
}
119112

@@ -124,9 +117,8 @@ private int getValueIndicatorBarCount(int index, int positionIndex) {
124117
@Override
125118
protected void traceIsSatisfied(int index, boolean isSatisfied) {
126119
if (log.isTraceEnabled()) {
127-
log.trace("{}#isSatisfied({}): {}. Current price: {}, Current stop loss activation: {}",
128-
getClass().getSimpleName(), index, isSatisfied, priceIndicator.getValue(index),
129-
currentStopLossLimitActivation);
120+
log.trace("{}#isSatisfied({}): {}. Current price: {}", getClass().getSimpleName(), index, isSatisfied,
121+
priceIndicator.getValue(index));
130122
}
131123
}
132124
}

ta4j-core/src/test/java/org/ta4j/core/rules/TrailingStopLossRuleTest.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ this software and associated documentation files (the "Software"), to deal in
4545
*/
4646
package org.ta4j.core.rules;
4747

48-
import static org.junit.Assert.assertEquals;
4948
import static org.junit.Assert.assertFalse;
50-
import static org.junit.Assert.assertNull;
5149
import static org.junit.Assert.assertTrue;
5250

5351
import java.util.function.Function;
@@ -74,32 +72,22 @@ public void isSatisfiedForBuy() {
7472

7573
// 10% trailing-stop-loss
7674
TrailingStopLossRule rule = new TrailingStopLossRule(closePrice, numOf(10));
77-
7875
assertFalse(rule.isSatisfied(0, null));
79-
assertNull(rule.getCurrentStopLossLimitActivation());
80-
8176
assertFalse(rule.isSatisfied(1, tradingRecord));
82-
assertNull(rule.getCurrentStopLossLimitActivation());
8377

8478
// Enter at 114
8579
tradingRecord.enter(2, numOf(114), numOf(1));
8680
assertFalse(rule.isSatisfied(2, tradingRecord));
87-
assertEquals(numOf(120).multipliedBy(numOf(0.9)), rule.getCurrentStopLossLimitActivation());
88-
8981
assertFalse(rule.isSatisfied(3, tradingRecord));
90-
assertEquals(numOf(130).multipliedBy(numOf(0.9)), rule.getCurrentStopLossLimitActivation());
91-
9282
assertTrue(rule.isSatisfied(4, tradingRecord));
93-
assertEquals(numOf(130).multipliedBy(numOf(0.9)), rule.getCurrentStopLossLimitActivation());
83+
9484
// Exit
9585
tradingRecord.exit(5);
9686

9787
// Enter at 128
9888
tradingRecord.enter(5, numOf(128), numOf(1));
9989
assertFalse(rule.isSatisfied(5, tradingRecord));
100-
assertEquals(numOf(130).multipliedBy(numOf(0.9)), rule.getCurrentStopLossLimitActivation());
10190
assertTrue(rule.isSatisfied(6, tradingRecord));
102-
assertEquals(numOf(130).multipliedBy(numOf(0.9)), rule.getCurrentStopLossLimitActivation());
10391
}
10492

10593
@Test
@@ -138,32 +126,23 @@ public void isSatisfiedForSell() {
138126

139127
// 10% trailing-stop-loss
140128
TrailingStopLossRule rule = new TrailingStopLossRule(closePrice, numOf(10));
141-
142129
assertFalse(rule.isSatisfied(0, null));
143-
assertNull(rule.getCurrentStopLossLimitActivation());
144-
145130
assertFalse(rule.isSatisfied(1, tradingRecord));
146-
assertNull(rule.getCurrentStopLossLimitActivation());
147131

148132
// Enter at 84
149133
tradingRecord.enter(2, numOf(84), numOf(1));
150-
assertFalse(rule.isSatisfied(2, tradingRecord));
151-
assertEquals(numOf(80).multipliedBy(numOf(1.1)), rule.getCurrentStopLossLimitActivation());
152134

135+
assertFalse(rule.isSatisfied(2, tradingRecord));
153136
assertFalse(rule.isSatisfied(3, tradingRecord));
154-
assertEquals(numOf(70).multipliedBy(numOf(1.1)), rule.getCurrentStopLossLimitActivation());
155-
156137
assertTrue(rule.isSatisfied(4, tradingRecord));
157-
assertEquals(numOf(70).multipliedBy(numOf(1.1)), rule.getCurrentStopLossLimitActivation());
138+
158139
// Exit
159140
tradingRecord.exit(5);
160141

161142
// Enter at 128
162143
tradingRecord.enter(5, numOf(128), numOf(1));
163144
assertFalse(rule.isSatisfied(5, tradingRecord));
164-
assertEquals(numOf(120).multipliedBy(numOf(1.1)), rule.getCurrentStopLossLimitActivation());
165145
assertTrue(rule.isSatisfied(6, tradingRecord));
166-
assertEquals(numOf(120).multipliedBy(numOf(1.1)), rule.getCurrentStopLossLimitActivation());
167146
}
168147

169148
@Test

0 commit comments

Comments
 (0)