Skip to content
This repository has been archived by the owner on May 12, 2019. It is now read-only.

Momentum Indicator? #196

Closed
nimo23 opened this issue Sep 13, 2017 · 4 comments
Closed

Momentum Indicator? #196

nimo23 opened this issue Sep 13, 2017 · 4 comments

Comments

@nimo23
Copy link
Contributor

nimo23 commented Sep 13, 2017

I need a typical Price Momentum Indicator (https://en.wikipedia.org/wiki/Momentum_(technical_analysis)).

I cannot find it in the ta4j-API with such name. Does it exist?

@team172011
Copy link
Contributor

what about:

int n = 10;
ClosePriceIndicator cp = new ClosePriceIndicator(series);
DifferenceIndicator momentum = new DifferenceIndicator(cp, new PreviousValueIndicator(cp,n));

@nimo23
Copy link
Contributor Author

nimo23 commented Sep 13, 2017

Thank you. For a typical Price-Momentum-Indicator, the actual momentum should be a relative number, where the number oscillates around 0 (=100%).

I want to make a PR, so do you find anything to better this?:

public class MomentumIndicator extends CachedIndicator<Decimal> {

    private final int timeFrame;
    
    private final Indicator<Decimal> indicator;
    

    public MomentumIndicator(Indicator<Decimal> indicator, int timeFrame) {
        super(indicator);
        this.indicator = indicator;
        this.timeFrame = timeFrame;
    }

    @Override
    protected Decimal calculate(int index) {
    	
    	PreviousValueIndicator prev = new PreviousValueIndicator(indicator, timeFrame);
    	DifferenceIndicator momentum = new DifferenceIndicator(indicator, prev);
    	
        // Decimal prevVal = momentum.getValue(index - timeFrame)
    	Decimal prevVal = prev.getValue(index - timeFrame);
        Decimal momentumVal = momentum.getValue(index);

        return momentumVal.dividedBy(prevVal)
                .multipliedBy(Decimal.HUNDRED);
    }
}

@team172011
Copy link
Contributor

Looks good. But regarding #192 PR will not be merged.

@nimo23
Copy link
Contributor Author

nimo23 commented Sep 13, 2017

Okay, no need for that. Because ta4j already has such a momentum-indicator called "ROCIndicator".

@nimo23 nimo23 closed this as completed Sep 13, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants