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
|
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include "ts.h"
bool ts_test_awesome_oscillator( void )
{
uint_t n = 40;
fp_t *o = calloc( n, sizeof( fp_t ) );
fp_t *h = calloc( n, sizeof( fp_t ) );
fp_t *l = calloc( n, sizeof( fp_t ) );
fp_t *c = calloc( n, sizeof( fp_t ) );
if ( !o || !h || !l || !c )
{
free( o ); free( h ); free( l ); free( c );
return false;
}
for ( uint_t i = 0; i < n; ++i )
{
o[i] = (fp_t)(10 + i);
h[i] = (fp_t)(12 + i);
l[i] = (fp_t)(8 + i);
c[i] = (fp_t)(11 + i);
}
fp_t *result = calloc( n, sizeof( fp_t ) );
if ( !result )
{
free( o ); free( h ); free( l ); free( c );
return false;
}
ts_awesome_oscillator(&result, 1, (fp_t const * const []){ o, h, l, c }, 4, n, NULL);
/* Compute expected EMA(5) and EMA(34) of median price */
fp_t fast_a = 2.0 / 6.0;
fp_t slow_a = 2.0 / 35.0;
fp_t sum_fast = 0, sum_slow = 0;
for ( uint_t i = 0; i < 34; ++i )
{
fp_t mp = (h[i] + l[i]) / 2;
sum_slow += mp;
if ( i < 5 ) sum_fast += mp;
}
fp_t ema_fast = sum_fast / 5.0;
fp_t ema_slow = sum_slow / 34.0;
for ( uint_t i = 34; i < n; ++i )
{
fp_t mp = (h[i] + l[i]) / 2;
ema_fast = ema_fast + fast_a * ( mp - ema_fast );
ema_slow = ema_slow + slow_a * ( mp - ema_slow );
}
fp_t expected = ema_fast - ema_slow;
bool pass = fabs( result[n - 1] - expected ) < 1e-10;
free( o ); free( h ); free( l ); free( c ); free( result );
return pass;
}
|