Skip to content

Commit 7b73bba

Browse files
committed
Fix atr window function
1 parent 39a0ee4 commit 7b73bba

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/ta_atr_win.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <mysql.h>
1010
#include <ctype.h>
11+
#include <float.h>
1112
#include "ta_libmysqludf_ta.h"
1213

1314
/*
@@ -19,6 +20,8 @@ typedef struct ta_atr_win_data_ {
1920
int current;
2021
double previous_close;
2122
double current_close;
23+
double group_high;
24+
double group_low;
2225
double previous_period_atr;
2326
double current_period_atr;
2427
double next_period_previous_atr;
@@ -69,6 +72,8 @@ DLLEXP my_bool ta_atr_win_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
6972
}
7073

7174
data->current = -1;
75+
data->group_high = DBL_MIN;
76+
data->group_low = DBL_MAX;
7277
data->previous_close = 0.0;
7378
data->current_period_atr = 0.0;
7479
data->previous_period_atr = 0.0;
@@ -89,36 +94,33 @@ DLLEXP void ta_atr_win_deinit(UDF_INIT *initid)
8994
DLLEXP double ta_atr_win(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
9095
{
9196
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];
95100
double sum = 0.0;
96101
int *periods = (int *)args->args[3];
97102

98-
if (close == NULL) {
99-
*is_null = 1;
100-
return 0.0;
101-
}
102-
103103
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);
105105
double current_period_atr = ((data->previous_period_atr * (*periods-1)) + current_period_tr) / *periods;
106106
data->next_period_previous_atr = current_period_atr;
107107
//fprintf(stderr, "atr\tprevious_atr=%f,current_tr=%f,calc_current_atr=%f\n", data->previous_period_atr, current_period_tr, current_period_atr);
108108
return current_period_atr;
109109
} 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);
111111
for (int i = 0; i < *periods; i++) {
112112
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]);
114114
}
115115
data->current_period_atr = sum / *periods;
116116
data->previous_period_atr = data->current_period_atr;
117117
data->next_period_previous_atr = data->current_period_atr;
118118
//fprintf(stderr, "avg = %f\n", data->current_period_atr );
119119
return data->current_period_atr;
120120
} 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);
122124
} else {
123125
*is_null = 1;
124126
return 0.0;
@@ -136,7 +138,13 @@ void ta_atr_win_clear(UDF_INIT *initid, char *is_null, char *error) {
136138
if (data->current > 0) {
137139
data->previous_close = data->current_close;
138140
data->previous_period_atr = data->next_period_previous_atr;
141+
data->group_high = DBL_MIN;
142+
data->group_low = DBL_MAX;
139143
//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);
140148
} else {
141149
*is_null = 1;
142150
}
@@ -145,11 +153,17 @@ void ta_atr_win_clear(UDF_INIT *initid, char *is_null, char *error) {
145153

146154
void ta_atr_win_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
147155
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];
150158
double *close = (double *)args->args[2];
151159

152160
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+
}
153167

154168
//fprintf(stderr, "add\thigh=%f,low=%f,close=%f,last_c=%f,curr_=%f\n", *high, *low, *close, data->previous_close, data->current_close);
155169
}

0 commit comments

Comments
 (0)