ta4j/src/main/java/eu/verdelhan/ta4j/indicators/TrailingStopLossIndicator.java
@Override
    protected Decimal calculate(int index) {
        if (stopLossLimit.isNaN()) {
            // Case without initial stop-loss limit value
            stopLossLimit = indicator.getValue(0).minus(stopLossDistance);
        }
        Decimal currentValue = indicator.getValue(index);
        Decimal referenceValue = stopLossLimit.plus(stopLossDistance);
        
        if (currentValue.isGreaterThan(referenceValue)) {
            stopLossLimit = currentValue.minus(stopLossDistance);
        }
        return stopLossLimit;
    }
@Override
    protected Decimal calculate(int index) {
        if (stopLossLimit.isNaN()) {
            // Case without initial stop-loss limit value
            stopLossLimit = indicator.getValue(0).minus(stopLossDistance);
        }
        Decimal currentValue = indicator.getValue(index);
        Decimal referenceValue = stopLossLimit.plus(stopLossDistance);
        
        if (currentValue.isGreaterThan(referenceValue)) {
            stopLossLimit = currentValue.minus(stopLossDistance);
        }
        return stopLossLimit;
    }
@ Override protected Decimal calculate ( int index ) { if ( stopLossLimit . isNaN ( ) ) { // Case without initial stop-loss limit value stopLossLimit = indicator . getValue ( 0 ) . minus ( stopLossDistance ) ; } Decimal currentValue = indicator . getValue ( index ) ; Decimal referenceValue = stopLossLimit . plus ( stopLossDistance ) ; if ( currentValue . isGreaterThan ( referenceValue ) ) { stopLossLimit = currentValue . minus ( stopLossDistance ) ; } return stopLossLimit ; }
Simple implementation of the trailing stop-loss concept.
Logic:
IF CurrentPrice - StopLossDistance > StopLossLimit THEN StopLossLimit = CurrentPrice - StopLossDistance
@param index
@return Decimal
Simple implementation of the trailing stop - loss concept . Logic : IF CurrentPrice - StopLossDistance > StopLossLimit THEN StopLossLimit = CurrentPrice - StopLossDistance