diff options
Diffstat (limited to 'lib/channel.c')
| -rw-r--r-- | lib/channel.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/channel.c b/lib/channel.c new file mode 100644 index 0000000..a2d8bd9 --- /dev/null +++ b/lib/channel.c @@ -0,0 +1,64 @@ +// Channel +// +// channel - Donchian-style channel +// +// Upper: rolling highest high, Lower: rolling lowest low over period k +// +// Inputs: 4 (OHLC) +// Outputs: 2 (Upper, Lower) +// Constants: 1 (period) + +#include "ts.h" + +typedef struct { + uint_t k; +} ts_channel_params_t; + +ts_parameter_t const ts_channel_p = { .n_inputs = 4, .n_outputs = 2, .n_constants = 1, .params_size = sizeof(ts_channel_params_t) }; + +uint_t ts_channel_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( c ? ((ts_channel_params_t *)c)->k - 1 : 0 ); +} + +int ts_channel( + 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_channel_p ); + if ( !c ) return -1; + uint_t k = ((ts_channel_params_t *)c)->k; + if (k == 0 || n < k) return -1; + + uint_t xi = k - 1; + uint_t xj = 0; + uint_t yi = k - 1; + + for( ; xi < n; xi++, xj++, yi++ ) + { + y[0][yi] = x[1][xj]; + for( uint_t i = xj; i <= xi; i++ ) + { + if( x[1][i] > y[0][yi] ) + { + y[0][yi] = x[1][i]; + } + } + + y[1][yi] = x[2][xj]; + for( uint_t i = xj; i <= xi; i++ ) + { + if( x[2][i] < y[1][yi] ) + { + y[1][yi] = x[2][i]; + } + } + } + return 0; +} + + + |