Implemented API for computing RSI (Relative Strenght Index)
void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values); void ndpi_free_rsi(struct ndpi_rsi_struct *s); float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value);
This commit is contained in:
@@ -3690,6 +3690,30 @@ void rulesUnitTest() {
|
|||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|
||||||
|
void rsiUnitTest() {
|
||||||
|
struct ndpi_rsi_struct s;
|
||||||
|
unsigned int v[] = {
|
||||||
|
2227, 2219, 2208, 2217, 2218, 2213, 2223, 2243, 2224, 2229,
|
||||||
|
2215, 2239, 2238, 2261, 2336, 2405, 2375, 2383, 2395, 2363,
|
||||||
|
2382, 2387, 2365, 2319, 2310, 2333, 2268, 2310, 2240, 2217,
|
||||||
|
};
|
||||||
|
u_int i, n = sizeof(v) / sizeof(unsigned int);
|
||||||
|
|
||||||
|
ndpi_init_rsi(&s, 8);
|
||||||
|
|
||||||
|
for(i=0; i<n; i++) {
|
||||||
|
float rsi = ndpi_rsi_add_value(&s, v[i]);
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
printf("%2d) RSI = %f\n", i, rsi);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
ndpi_free_rsi(&s);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* *********************************************** */
|
||||||
|
|
||||||
void hashUnitTest() {
|
void hashUnitTest() {
|
||||||
ndpi_str_hash *h = ndpi_hash_alloc(16384);
|
ndpi_str_hash *h = ndpi_hash_alloc(16384);
|
||||||
char* dict[] = { "hello", "world", NULL };
|
char* dict[] = { "hello", "world", NULL };
|
||||||
@@ -3731,6 +3755,7 @@ int orginal_main(int argc, char **argv) {
|
|||||||
|
|
||||||
/* Internal checks */
|
/* Internal checks */
|
||||||
// binUnitTest();
|
// binUnitTest();
|
||||||
|
rsiUnitTest();
|
||||||
hashUnitTest();
|
hashUnitTest();
|
||||||
dgaUnitTest();
|
dgaUnitTest();
|
||||||
hllUnitTest();
|
hllUnitTest();
|
||||||
|
|||||||
@@ -1389,6 +1389,14 @@ extern "C" {
|
|||||||
u_int32_t ndpi_data_max(struct ndpi_analyze_struct *s);
|
u_int32_t ndpi_data_max(struct ndpi_analyze_struct *s);
|
||||||
float ndpi_data_ratio(u_int32_t sent, u_int32_t rcvd);
|
float ndpi_data_ratio(u_int32_t sent, u_int32_t rcvd);
|
||||||
|
|
||||||
|
/* ******************************* */
|
||||||
|
|
||||||
|
void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values);
|
||||||
|
void ndpi_free_rsi(struct ndpi_rsi_struct *s);
|
||||||
|
float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value);
|
||||||
|
|
||||||
|
/* ******************************* */
|
||||||
|
|
||||||
const char* ndpi_data_ratio2str(float ratio);
|
const char* ndpi_data_ratio2str(float ratio);
|
||||||
|
|
||||||
void ndpi_data_print_window_values(struct ndpi_analyze_struct *s); /* debug */
|
void ndpi_data_print_window_values(struct ndpi_analyze_struct *s); /* debug */
|
||||||
|
|||||||
@@ -1544,6 +1544,13 @@ struct ndpi_analyze_struct {
|
|||||||
|
|
||||||
/* **************************************** */
|
/* **************************************** */
|
||||||
|
|
||||||
|
struct ndpi_rsi_struct {
|
||||||
|
struct ndpi_analyze_struct *gains, *losses;
|
||||||
|
u_int32_t last_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* **************************************** */
|
||||||
|
|
||||||
typedef struct ndpi_ptree ndpi_ptree_t;
|
typedef struct ndpi_ptree ndpi_ptree_t;
|
||||||
|
|
||||||
/* **************************************** */
|
/* **************************************** */
|
||||||
|
|||||||
@@ -817,3 +817,42 @@ int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
|
/*
|
||||||
|
RSI (Relative Strength Index)
|
||||||
|
|
||||||
|
RSI = 100 − [ 100/ (1 + (Average loss/Average gain)) ]
|
||||||
|
|
||||||
|
https://www.investopedia.com/terms/r/rsi.asp
|
||||||
|
*/
|
||||||
|
|
||||||
|
void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values) {
|
||||||
|
s->gains = ndpi_alloc_data_analysis(num_learning_values);
|
||||||
|
s->losses = ndpi_alloc_data_analysis(num_learning_values);
|
||||||
|
s->last_value = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ndpi_free_rsi(struct ndpi_rsi_struct *s) {
|
||||||
|
ndpi_free_data_analysis(s->gains), ndpi_free_data_analysis(s->losses);
|
||||||
|
}
|
||||||
|
|
||||||
|
float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value) {
|
||||||
|
if(s->gains->num_data_entries == 0)
|
||||||
|
ndpi_data_add_value(s->gains, 0), ndpi_data_add_value(s->losses, 0);
|
||||||
|
else {
|
||||||
|
if(value > s->last_value)
|
||||||
|
ndpi_data_add_value(s->gains, value - s->last_value), ndpi_data_add_value(s->losses, 0);
|
||||||
|
else
|
||||||
|
ndpi_data_add_value(s->losses, s->last_value - value), ndpi_data_add_value(s->gains, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
s->last_value = value;
|
||||||
|
|
||||||
|
if(s->gains->num_data_entries >= s->gains->num_values_array_len) {
|
||||||
|
float relative_strength = ndpi_data_average(s->gains) / ndpi_data_average(s->losses);
|
||||||
|
|
||||||
|
/* printf("RSI: %f\n", relative_strength); */
|
||||||
|
return(100. - (100. / (1. + relative_strength)));
|
||||||
|
} else
|
||||||
|
return(-1); /* Too early */
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user