TulipCharts / tulipindicators

Technical Analysis Indicator Function Library in C
https://tulipindicators.org/
GNU Lesser General Public License v3.0
841 stars 157 forks source link

Chandelier Exit indicator #58

Closed rdbuf closed 5 years ago

rdbuf commented 5 years ago

Sources:

  1. True Range:

ce-1

  1. Average True Range:

ce-2

  1. Chandelier Exit, ce_high:

ce-4

  1. Chandelier Exit on short positions, ce_low:

ce-5

codeplea commented 5 years ago

This looks pretty good. Great job with the documentation!

Here's my suggestion:

Instead of using your MAX3 and TR macros, just include "truerange.h" and use the CALC_TRUERANGE macro. I just benchmarked it, and ce went from 59 mfps to 77 mfps with this change (using -O2). It also keeps it more consistent with the rest of the codebase.

You'll need 'TI_REAL truerange;` at the top, then the first loop would look like this:

    for (i = 1; i < period; ++i) {
        CALC_TRUERANGE();
        atr += truerange;
    }

and the second like this:

    for (i = period; i < size; ++i) {
        CALC_TRUERANGE();
        atr = atr * smth + truerange * per;
        ...
rdbuf commented 5 years ago

Done. By the way, what's your approach for quick microbenchmarking within this library?

codeplea commented 5 years ago

You can remove your #undefs too.

Ok, I think you might have a serious bug. Imagine if high[4] is the local high point, and the period is 5. Would HP ever get set to high[4]? It seem that HP would skip straight to high[5] for the first bar. Right?

You might want to look at stoch.c as an example of maintaining the highest and lowest. This comes up in a lot of indicators. Also, the method used by stoch.c seem to benchmark faster (it pushed ce past 100 mfps for me, but I'm not sure if I introduced any new bugs or not).

Let me know your thoughts.

codeplea commented 5 years ago

Done. By the way, what's your approach for quick microbenchmarking within this library?

I'm just doing make benchmark -B && benchmark ce.

Make sure to remove -std=c89 -pedantic before benchmarking too. It makes a large difference.

rdbuf commented 5 years ago

Yes, my bad regarding the maintainance of max/min. I’ll push an update once I’m back to my pc.

That’s interesting because I found my version consistently performing a few percent better. I was benchmarking inside a VM though and using an ad-hoc bench code, not benchmark.c.

What’s to benchmark.c, it has a dependency on ta-lib, and I haven’t still figured out how to obtain it.

rdbuf commented 5 years ago

...yet it must be pretty easy, right?

codeplea commented 5 years ago

ta-lib is from https://www.ta-lib.org/hdr_dw.html

Benchmark will do comparisons between TI and ta-lib. We should probably update benchmark in the future so the ta-lib comparison is optional.

rdbuf commented 5 years ago

I've spot another bug in my code. Gonna make a _ref implementation (#59)

codeplea commented 5 years ago

I laid the ground work for streaming indicators and reference indicators. Check out https://github.com/TulipCharts/tulipindicators/pull/60

I think the reference indicators will be a big help. There are a lot of edge cases in optimized indicator code.

rdbuf commented 5 years ago

I've fixed everything I got wrong last time and implemented the new interfaces. Benchmarks show that there is potentially a room for improvement, so I will probably revise it later. These are available in the CI build log (thanks to #63).