summaryrefslogtreecommitdiffstats
path: root/test/ad_oscillator.c
blob: 623fc44e93f593b3f53cb57d9ed687eb57615645 (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
#include <stddef.h>
#include "ts.h"

bool ts_test_ad_oscillator( void )
{
    fp_t const o[] = { 10, 11, 12, 11, 12 };
    fp_t const h[] = { 12, 13, 14, 12, 14 };
    fp_t const l[] = { 8,  9,  10,  9,  10 };
    fp_t const c[] = { 11, 12, 10, 11, 11 };
    fp_t const v[] = { 100, 200, 300, 400, 500 };

    fp_t const * const x[] = { o, h, l, c, v };

    fp_t fast = 3;
    fp_t slow = 5;
    fp_t a_fast = 1.0  / (fp_t)fast;
    fp_t a_slow = 1.0  / (fp_t)slow;

    fp_t ad = 0;
    fp_t ema_f = 0;
    fp_t ema_s = 0;

    fp_t hl0 = h[0] - l[0];
    fp_t mf0 = hl0 != 0 ? ( (c[0] - l[0]) - (h[0] - c[0]) ) / hl0 * v[0] : 0;
    ad = mf0;
    ema_f = ad;
    ema_s = ad;

    fp_t e[5] = { 0 };

    for( uint_t i = 1; i < 5; ++i )
    {
        fp_t hl = h[i] - l[i];
        fp_t mf = hl != 0 ? ( (c[i] - l[i]) - (h[i] - c[i]) ) / hl * v[i] : 0;
        ad += mf;
        ema_f = ema_f + a_fast * ( ad - ema_f );
        ema_s = ema_s + a_slow * ( ad - ema_s );
        e[i] = ema_f - ema_s;
    }

    fp_t const * const e_arr[1] = { e };

    fp_t constants[2] = { fast, slow };

    return ts_test(ts_ad_oscillator, ts_ad_oscillator_offset, x, e_arr, 5, (ts_constant_t *)constants, &ts_ad_oscillator_p);
}