blob: dfa1ad23a9f63c36e224d51fa98e19d94033045f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Coppock Curve
//
// coppock_curve - WMS
//
// WMA14(ROC14 + ROC11), weighted moving average of combined ROCs
//
// Inputs: 1
// Outputs: 1 (Coppock)
// Constants: 0
#include <math.h>
#include <stdlib.h>
#include "ts.h"
ts_parameter_t const ts_coppock_curve_p = { .n_inputs = 1, .n_outputs = 1, .n_constants = 0, .params_size = 0 };
uint_t ts_coppock_curve_offset( ts_constant_t const *c )
{
( void )c;
return ( 14 );
}
int ts_coppock_curve(
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_coppock_curve_p );
(void)c;
uint_t roc_long = 14;
uint_t roc_short = 11;
uint_t wma_period = 10;
if (n == 0 || n < roc_long) return -1;
fp_t *combined = calloc( n, sizeof( fp_t ) );
if ( !combined ) return -1;
for ( uint_t i = roc_long; i < n; ++i )
{
if ( x[0][i - roc_long] == 0 || x[0][i - roc_short] == 0 )
{
combined[i - roc_long] = 0;
continue;
}
fp_t roc14 = ( x[0][i] - x[0][i - roc_long] ) / x[0][i - roc_long] * 100.0;
fp_t roc11 = ( x[0][i] - x[0][i - roc_short] ) / x[0][i - roc_short] * 100.0;
combined[i - roc_long] = roc14 + roc11;
}
for (uint_t j = 0; j < n && j < roc_long; ++j) y[0][j] = 0;
for ( uint_t i = roc_long; i < n; ++i )
{
uint_t ci = i - roc_long;
fp_t wsum = 0;
fp_t wtotal = 0;
uint_t start = ci + 1 >= wma_period ? ci + 1 - wma_period : 0;
for ( uint_t j = start; j <= ci; ++j )
{
uint_t weight = ci - j + 1;
wsum += combined[j] * (fp_t)weight;
wtotal += (fp_t)weight;
}
y[0][ci + roc_long] = wtotal != 0 ? wsum / wtotal : 0;
}
free( combined );
return 0;
}
|