-
-
Notifications
You must be signed in to change notification settings - Fork 68
Description
Hi Franklin,
Thank you for this collection of indicators!
Here are some suggestions which you may decide to apply:
- Design each indicator as a simple function
This kind of design should be considered because of multiple reasons:
- Imagine having an infinite stream of data, this will allow to save memory and maybe even increase performance.
- Having simple functions allows composition
- Having simple functions allows easy testing and rewriting to different language.
Please let me give you quick example of simple SMA in Java that covers all three points above:
public static class SMA implements Function<BigDecimal, BigDecimal> {
private final var buffer = new LinkedList<>()
private final int length
private var sum = BigDecimal.ZERO
SMA(int length) {
this.length = length
}
@Override
BigDecimal apply(BigDecimal currentValue) {
if (buffer.size() == length) {
sum -= buffer.removeFirst()
}
sum += currentValue
buffer.addLast(currentValue)
// at the end calculated SMA should be rounded to SOURCE scale
return roundValue(sum / buffer.size(), currentValue)
}
}So it is basically a function, that accepts decimal value and produces new one. It has some minimal internal state like a self buffer etc. Usage is quite simple, it can be applied now to any datasource, streaming or not. RSI from SMA will look like this:
var datasource = candlesticks.map(c -> c.close)
.map(new SMA(3).then(new RSI(14)))I think you may improve your code in this way if you agree.
2. Rounding of values is a problem now. You used 4 decimals by default in any calculation, but I think this is not correct.
Whenever you know the scale you should use it. For example any moving average should use scale of original sourced value. RSI should/may use just 2 decimals. SMA from RSI should have the same 2 decimals for calculated value.
3. I tried to use some cryptocurrency candlestick data and many indicators failed to generate any useful data, I even couldnt find simple signal for RSI with StrongSell/StrongBuy values, which is weird.
The most important part is point with number 1, I do not have much experience with C# to help you with that. Please let me know what do you think.