Comments (3)
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.
@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.
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)
- logback 1.3.x supports the Java EE edition (jdk1.8) οΌIs it possible not to upgrade to 11οΌ HOT 9
- Pyramid Trading - Scaling into a position - DCA Trading possible ? HOT 10
- LagIndicator HOT 2
- KDJ
- [BUG] Reviewing process does not work as promised HOT 2
- TradingRecord#getLastPosition(): improve code
- Possible RSI bug HOT 7
- Does the lib support Other TimeFrames HOT 1
- Amount field in the BaseBar class HOT 1
- [BUG]ConvergenceDivergenceIndicator always returns false on 500 data sets with 500 bars each HOT 1
- [BUG] Supertrend Indicator value goes to 0 and stays 0 forever HOT 2
- Create releases more often HOT 23
- Trade style specific question [Support and resistence] HOT 8
- Can ta4j backtest trading multiple stocks simultaneously HOT 2
- [BUG] ProfitLossCriterion: Javadoc says "excludes trading costs" but code uses trading cost HOT 5
- Backtest with fixed entry amount like a new BarSeriesManager or Strategy
- [BUG] VersusEnterAndHoldCriterion should use 'amount' to match comparsion
- [BUG]
- Suggestion for improving the organization of the proyect folders and classes HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ta4j.