Giter VIP home page Giter VIP logo

Comments (3)

francescopeloi avatar francescopeloi commented on June 10, 2024

I think I answered myself.

where I provide all the test data in advance, should the indicator know all values? Or it should answer knowing the values only "incrementally"?

the indicator should know all the values. The barSerie the indicator provides has a beginIndex and endIndex. Even if the caller is asking for e.g. zigzag(1), endIndex could be 10, which means that further points are provided and should be taken into account in the indicator calculations.

Also do you know any already implemented indicator that works similarly so I can take inspiration?

I've found DifferencePercentageIndicator that seems similar/useful. I don't get if the use of the instance variable lastNotification is legit, given that there was a plan to remove them all, but it seems that it has been left there after the refactoring to remove instance variables. Still have to figure out why.

I'll continue developing with these assumptions.

from ta4j.

TheCookieLab avatar TheCookieLab commented on June 10, 2024

@francescopeloi you are correct, the indicator should be aware of all the values. There actually used to be a ZigZagIndicator class in the project but it was removed at some point. I don't know the reason but I assume it's because it didn't work quite as expected. That said here is the original code which may provide some value for you (i.e. things to borrow, things to avoid, etc) during your implementation:


package org.ta4j.core.indicators;

import org.ta4j.core.Indicator;
import org.ta4j.core.num.NaN;
import org.ta4j.core.num.Num;

/**
 * The Zig Zag Indicator.
 * <p>
 * For more information, see
 * <a href="https://www.investopedia.com/terms/z/zig_zag_indicator.asp">Zig Zag
 * Indicator</a>.
 */
public class ZigZagIndicator extends CachedIndicator<Num> {

    /**
     * The threshold ratio (positive number).
     */
    private final Num thresholdRatio;

    /**
     * The indicator to provide values.
     * <p>
     * Can be {@link org.ta4j.core.indicators.helpers.ClosePriceIndicator} or
     * something similar.
     */
    private final Indicator<Num> indicator;

    private Num lastExtreme;

    /**
     * Constructs a ZigZagIndicator.
     *
     * @param indicator the indicator to provide values
     * @param thresholdRatio the threshold ratio, must be positive
     */
    public ZigZagIndicator(Indicator<Num> indicator, Num thresholdRatio) {
        super(indicator);
        this.indicator = indicator;
        validateThreshold(thresholdRatio);
        this.thresholdRatio = thresholdRatio;
    }

    /**
     * Validates the provided threshold.
     *
     * @param threshold the threshold to validate
     * @throws IllegalArgumentException if threshold is null or non-positive
     */
    private void validateThreshold(Num threshold) {
        if (threshold == null) {
            throw new IllegalArgumentException("Threshold ratio is mandatory");
        }
        if (threshold.isNegativeOrZero()) {
            throw new IllegalArgumentException("Threshold ratio value must be positive");
        }
    }

    /**
     * Calculates the Zig Zag indicator's value for the current index.
     *
     * @param index the bar index
     * @return the indicator's value if its ratio from the last extreme is equal
     * to or greater than the threshold, otherwise {@link NaN}
     */
    @Override
    protected Num calculate(int index) {
        if (index == 0) {
            lastExtreme = indicator.getValue(0);
            return lastExtreme;
        } else {
            if (lastExtreme.isZero()) {
                // Treat this case separately
                // because one cannot divide by zero
                return NaN.NaN;
            } else {
                Num indicatorValue = indicator.getValue(index);
                Num differenceRatio = indicatorValue.minus(lastExtreme).dividedBy(lastExtreme);

                if (differenceRatio.abs().isGreaterThanOrEqual(thresholdRatio)) {
                    lastExtreme = indicatorValue;
                    return lastExtreme;
                } else {
                    return NaN.NaN;
                }
            }
        }
    }
}



from ta4j.

francescopeloi avatar francescopeloi commented on June 10, 2024

thanks for your answer @TheCookieLab . I found that indicator you posted, but couldn't make it work properly, so started from scratch. I think this indicator can be implemented easily in a naive way, the problem is that if you don't look close, you're not going to find some hard-to-find edge cases that if you don't solve, what you get is just wrong (some examples: 2 high zigzag points after each other, you need to re-draw the previous line; or another one: price is exactly the same for some time: is it a high swing point o a low swing point? and which of those?). I am in the middle of trying to solve them in my first implementation, than I have to step back and see the whole picture again and probably write it again, as the code now looks awful :)

Can you help me understand one thing: I've found DifferencePercentageIndicator that seems similar. I want to undestand how it works to get some inspiration. I don't get if the use of the instance variable lastNotification there is legit, given that there was a plan to remove them (#906), but it seems that it has been left there after the PR (#1060) to remove instance variables.

Do you know why that instance variable would be acceptable? I thought that all instance variables should be removed from all indicators, given that even if you are using one, you're breaking statelessness, which we should not.

from ta4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.