8
8
9
9
#include <mysql.h>
10
10
#include <ctype.h>
11
+ #include <float.h>
11
12
#include "ta_libmysqludf_ta.h"
12
13
13
14
/*
@@ -19,6 +20,8 @@ typedef struct ta_atr_win_data_ {
19
20
int current ;
20
21
double previous_close ;
21
22
double current_close ;
23
+ double group_high ;
24
+ double group_low ;
22
25
double previous_period_atr ;
23
26
double current_period_atr ;
24
27
double next_period_previous_atr ;
@@ -69,6 +72,8 @@ DLLEXP my_bool ta_atr_win_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
69
72
}
70
73
71
74
data -> current = -1 ;
75
+ data -> group_high = DBL_MIN ;
76
+ data -> group_low = DBL_MAX ;
72
77
data -> previous_close = 0.0 ;
73
78
data -> current_period_atr = 0.0 ;
74
79
data -> previous_period_atr = 0.0 ;
@@ -89,36 +94,33 @@ DLLEXP void ta_atr_win_deinit(UDF_INIT *initid)
89
94
DLLEXP double ta_atr_win (UDF_INIT * initid , UDF_ARGS * args , char * is_null , char * error )
90
95
{
91
96
ta_atr_win_data * data = (ta_atr_win_data * )initid -> ptr ;
92
- double * high = (double * )args -> args [0 ];
93
- double * low = (double * )args -> args [1 ];
94
- double * close = (double * )args -> args [2 ];
97
+ // double *high = (double *)args->args[0];
98
+ // double *low = (double *)args->args[1];
99
+ // double *close = (double *)args->args[2];
95
100
double sum = 0.0 ;
96
101
int * periods = (int * )args -> args [3 ];
97
102
98
- if (close == NULL ) {
99
- * is_null = 1 ;
100
- return 0.0 ;
101
- }
102
-
103
103
if (data -> current > * periods ) {
104
- double current_period_tr = (* high > data -> previous_close ? * high : data -> previous_close ) - (* low < data -> previous_close ? * low : data -> previous_close );
104
+ double current_period_tr = (data -> group_high > data -> previous_close ? data -> group_high : data -> previous_close ) - (data -> group_low < data -> previous_close ? data -> group_low : data -> previous_close );
105
105
double current_period_atr = ((data -> previous_period_atr * (* periods - 1 )) + current_period_tr ) / * periods ;
106
106
data -> next_period_previous_atr = current_period_atr ;
107
107
//fprintf(stderr, "atr\tprevious_atr=%f,current_tr=%f,calc_current_atr=%f\n", data->previous_period_atr, current_period_tr, current_period_atr);
108
108
return current_period_atr ;
109
109
} else if (data -> current == * periods ) {
110
- data -> values [data -> current - 1 ] = (* high > data -> previous_close ? * high : data -> previous_close ) - (* low < data -> previous_close ? * low : data -> previous_close );
110
+ data -> values [data -> current - 1 ] = (data -> group_high > data -> previous_close ? data -> group_high : data -> previous_close ) - (data -> group_low < data -> previous_close ? data -> group_low : data -> previous_close );
111
111
for (int i = 0 ; i < * periods ; i ++ ) {
112
112
sum += data -> values [i ];
113
- //fprintf(stderr, "values[%i] = %f\n", i, data->values[i]);
113
+ //fprintf(stderr, "FIRST CALC values[%i] = %f\n", i, data->values[i]);
114
114
}
115
115
data -> current_period_atr = sum / * periods ;
116
116
data -> previous_period_atr = data -> current_period_atr ;
117
117
data -> next_period_previous_atr = data -> current_period_atr ;
118
118
//fprintf(stderr, "avg = %f\n", data->current_period_atr );
119
119
return data -> current_period_atr ;
120
120
} else if (data -> current > 0 ) {
121
- data -> values [data -> current - 1 ] = (* high > data -> previous_close ? * high : data -> previous_close ) - (* low < data -> previous_close ? * low : data -> previous_close );
121
+ data -> values [data -> current - 1 ] = (data -> group_high > data -> previous_close ? data -> group_high : data -> previous_close ) - (data -> group_low < data -> previous_close ? data -> group_low : data -> previous_close );
122
+ //fprintf(stderr, "atr current value = %f\n", data->values[data->current-1]);
123
+ //fflush(stderr);
122
124
} else {
123
125
* is_null = 1 ;
124
126
return 0.0 ;
@@ -136,7 +138,13 @@ void ta_atr_win_clear(UDF_INIT *initid, char *is_null, char *error) {
136
138
if (data -> current > 0 ) {
137
139
data -> previous_close = data -> current_close ;
138
140
data -> previous_period_atr = data -> next_period_previous_atr ;
141
+ data -> group_high = DBL_MIN ;
142
+ data -> group_low = DBL_MAX ;
139
143
//fprintf(stderr,"clear\tSet previous_period_atr=%f\n", data->previous_period_atr);
144
+ //for (int i = 0; i < 14; i++) {
145
+ //fprintf(stderr, "values[%i] = %f\n", i, data->values[i]);
146
+ //}
147
+ //fflush(stderr);
140
148
} else {
141
149
* is_null = 1 ;
142
150
}
@@ -145,11 +153,17 @@ void ta_atr_win_clear(UDF_INIT *initid, char *is_null, char *error) {
145
153
146
154
void ta_atr_win_add (UDF_INIT * initid , UDF_ARGS * args , char * is_null , char * error ) {
147
155
ta_atr_win_data * data = (ta_atr_win_data * )initid -> ptr ;
148
- // double *high = (double *)args->args[0];
149
- // double *low = (double *)args->args[1];
156
+ double * high = (double * )args -> args [0 ];
157
+ double * low = (double * )args -> args [1 ];
150
158
double * close = (double * )args -> args [2 ];
151
159
152
160
data -> current_close = * close ;
161
+ if (* high > data -> group_high ) {
162
+ data -> group_high = * high ;
163
+ }
164
+ if (* low < data -> group_low ) {
165
+ data -> group_low = * low ;
166
+ }
153
167
154
168
//fprintf(stderr, "add\thigh=%f,low=%f,close=%f,last_c=%f,curr_=%f\n", *high, *low, *close, data->previous_close, data->current_close);
155
169
}
0 commit comments