diff options
Diffstat (limited to 'lib/ichimoku.c')
| -rw-r--r-- | lib/ichimoku.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/ichimoku.c b/lib/ichimoku.c new file mode 100644 index 0000000..76f85e6 --- /dev/null +++ b/lib/ichimoku.c @@ -0,0 +1,113 @@ +// Ichimoku +// +// ichimoku +// +// Ichimoku Cloud +// +// Tenkan, Kijun, Senkou Span A/B, Chikou +// +// Inputs: 4 +// Outputs: 5 +// Constants: 3 (tenkan, kijun, senkou) + + +#include <math.h> +#include "ts.h" + + +typedef struct { + uint_t tenkan; + uint_t kijun; + uint_t senkou; +} ts_ichimoku_params_t; + +ts_parameter_t const ts_ichimoku_p = { .n_inputs = 4, .n_outputs = 5, .n_constants = 3, .params_size = sizeof(ts_ichimoku_params_t) }; + +uint_t ts_ichimoku_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( 52 ); +} + +int ts_ichimoku( + fp_t * const *y, uint_t y_n, + fp_t const * const *x, uint_t x_n, + uint_t n, ts_constant_t const *c +) +{ + TS_PROLOGUE( ts_ichimoku_p ); + uint_t tenkan = 9; + uint_t kijun = 26; + uint_t senkou = 52; + + if ( c ) + { + ts_ichimoku_params_t *p = (ts_ichimoku_params_t *)c; + if ( p->tenkan > 0 ) tenkan = p->tenkan; + if ( p->kijun > 0 ) kijun = p->kijun; + if ( p->senkou > 0 ) senkou = p->senkou; + } + + if ( tenkan == 0 || kijun == 0 || senkou == 0 ) return -1; + + for( uint_t i = 0; i < n; ++i ) + { + y[0][i] = 0; + y[1][i] = 0; + y[2][i] = 0; + y[3][i] = 0; + y[4][i] = 0; + } + + for( uint_t i = tenkan - 1; i < n; ++i ) + { + fp_t hh = ts_h( x, i - tenkan + 1 ); + fp_t ll = ts_l( x, i - tenkan + 1 ); + for( uint_t j = i - tenkan + 1; j <= i; ++j ) + { + if( ts_h( x, j ) > hh ) hh = ts_h( x, j ); + if( ts_l( x, j ) < ll ) ll = ts_l( x, j ); + } + y[0][i] = ( hh + ll ) / 2.0; + } + + for( uint_t i = kijun - 1; i < n; ++i ) + { + fp_t hh = ts_h( x, i - kijun + 1 ); + fp_t ll = ts_l( x, i - kijun + 1 ); + for( uint_t j = i - kijun + 1; j <= i; ++j ) + { + if( ts_h( x, j ) > hh ) hh = ts_h( x, j ); + if( ts_l( x, j ) < ll ) ll = ts_l( x, j ); + } + y[1][i] = ( hh + ll ) / 2.0; + } + + for( uint_t i = kijun - 1; i < n; ++i ) + { + uint_t idx = i + kijun; + if( idx < n ) + y[2][idx] = ( y[0][i] + y[1][i] ) / 2.0; + } + + for( uint_t i = senkou - 1; i < n; ++i ) + { + fp_t hh = ts_h( x, i - senkou + 1 ); + fp_t ll = ts_l( x, i - senkou + 1 ); + for( uint_t j = i - senkou + 1; j <= i; ++j ) + { + if( ts_h( x, j ) > hh ) hh = ts_h( x, j ); + if( ts_l( x, j ) < ll ) ll = ts_l( x, j ); + } + uint_t idx = i + kijun; + if( idx < n ) + y[3][idx] = ( hh + ll ) / 2.0; + } + + for( uint_t i = kijun; i < n; ++i ) + { + y[4][i] = ts_c( x, i - kijun ); + } + return 0; +} + |