Commit b4fe0011 authored by denis's avatar denis

freeze for branch 'bstring_integration'

parent ce7da832
Pipeline #2201220 skipped
......@@ -8,7 +8,7 @@ DB_PASSWORD=slayer1
DB_NAME=ichinscratchy_test
# Ichimoku analysis related settings
ICHI_DAYS_TO_ANALYZE=40
ICHI_DAYS_TO_ANALYZE=25
ICHI_PERIOD_SHORT=9;
ICHI_PERIOD_MID=26;
ICHI_PERIOD_LONG=52;
......@@ -122,7 +122,15 @@ CREATE TABLE `ichimoku_daily` (
PRIMARY KEY (`date`,`symbol`));
```
#### Example queries
tbd
Update Orange Juice Chikou Span in Database:
```
INSERT INTO ichimoku_daily
(date, symbol, chikou)
VALUES
(2016-02-08,'OJuice',128.600006),
(2016-02-09,'OJuice',127.849998),
(2016-02-10,'OJuice',127.550003);
```
Table `indicators_daily`
----------------
......
......@@ -8,10 +8,10 @@ CFLAGS = -lm -Wall -O2 -L/usr/lib/mysql/ -lmysqlclient -pedantic -Wextra
all:ichinscratchy
ichinscratchy: $(OBJPATH)
$(CC) -o $@ $^ $(LIBS)
$(CC) -g -o $@ $^ $(LIBS)
build/%.o: src/%.c
$(CC) $(CFLAGS) -c $(INCLUDES) -o $@ $<
$(CC) $(CFLAGS) -g -c $(INCLUDES) -o $@ $<
clean:
rm -f $(OBJPATH)
......
......@@ -35,7 +35,7 @@ float **init_2d_array_float(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
float **array;
float **array=NULL;
array = malloc(N*sizeof(float *));
for(i = 0 ; i < N ; i++)
array[i] = malloc( M*sizeof(float) );
......@@ -53,7 +53,7 @@ float **init_2d_array_float(int N, int M) /* Allocate the array */
* ===================================================================*/
float *init_1d_array_float(int N)
{
float *array;
float *array=NULL;
array = malloc(N*sizeof(float));
return array;
}
......@@ -71,7 +71,7 @@ char **init_1d_array_string(int N) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i;
char **array;
char **array=NULL;
array = malloc(N*sizeof(char *));
for(i = 0 ; i < N ; i++)
array[i] = malloc(256*sizeof(char));
......
......@@ -396,10 +396,16 @@ void mysql_updatequotes(char *symbol)
}
/*
/* ---------------------------------------------------------------------
* void mysql_get_lagged_dates(char *symbol, int days, char **lagged_dates)
* input: char *symbol: pointer to symbol string
* int days: period of days which should be retrieved
* char **lagged_dates: pointer to matrix containing dates
* output: char **lagged_dates: matrix with lagged dates
* ---------------------------------------------------------------------
* gets the dates for ICHI_PERIOD_MID (default 26d) lagged days;
* needed for Chikou-Span, returns lagged dates for ICHI_DAYS_TO_ANALYZE
*/
* ===================================================================*/
void mysql_get_lagged_dates(char *symbol, int days, char **lagged_dates)
{
extern struct parameters parms; /* parms are declared global in ichinscratchy.c */
......@@ -471,9 +477,9 @@ void mysql_get_lagged_dates(char *symbol, int days, char **lagged_dates)
// loop through all rows of result
while((row = mysql_fetch_row(result)) !=0)
{
/* for each row extract field results */
// for each row extract field results
// never ever do that: lagged_dates[row_nr]=row[0];
// 2hrs bughunting grml. fucking pointers. C is relentless.
// 2hrs bughunting grml. stupid me. fucking pointers. C is relentless.
strcpy(lagged_dates[row_nr],row[0]);
//printout for debug:
//printf("%s\n",lagged_dates[row_nr]);
......@@ -487,4 +493,71 @@ void mysql_get_lagged_dates(char *symbol, int days, char **lagged_dates)
free(tmp);
free(query);
}
void mysql_update_indicator(char *indicator, char *tablename, char *symbol, \
int days, char **dates, float **quotes, int quote_field)
{
int i=0;
char quote_close[256];
/*
prepare query statement:
INSERT INTO ichimoku_daily
(date, symbol, chikou)
VALUES
(dates[0], symbol, quotes[0][COL_QUOTES_DAILY_CLOSE),
(dates[1], symbol, quotes[1][COL_QUOTES_DAILY_CLOSE),
...
ON DUPLICATE KEY UPDATE chikou=chikou;
everything after VALUES will be stored in on, sequential string
'datequote'
*/
// pointer to string that will hold the values to insert
char *datequote=NULL;
// everything will be stored in on, sequential string
datequote = (char*) malloc(days* (sizeof(char)*256));
// create this string
datequote=strcpy(datequote," ");
for(i=0;i<days;i++)
{
datequote=strcat(datequote,"('");
datequote=strcat(datequote, dates[i]);
datequote=strcat(datequote, "','");
datequote=strcat(datequote, symbol);
datequote=strcat(datequote, "',");
// cast the close quote from float to string
sprintf(quote_close,"%f",quotes[i][quote_field]);
datequote=strcat(datequote,quote_close);
if(i<(days-1))
datequote=strcat(datequote, ") , ");
else
datequote=strcat(datequote, ") ");
}
//datequote[strlen(datequote)-3]="\0";
//printf("\n%s\n",datequote);
char *querystring=NULL;
querystring = (char*) malloc(sizeof(datequote)+256*sizeof(char));
querystring = strcpy(querystring, "INSERT INTO ");
querystring = strcat(querystring, tablename);
querystring = strcat(querystring, " (date, symbol, ");
querystring = strcat(querystring, indicator);
querystring = strcat(querystring, ") VALUES ");
querystring = strcat(querystring, datequote);
querystring = strcat(querystring, " ON DUPLICATE KEY UPDATE ");
querystring = strcat(querystring, indicator);
querystring = strcat(querystring, "=");
querystring = strcat(querystring, indicator);
querystring = strcat(querystring, "\0");
// uncomment to see quer statement printed out
// printf("\n%s",querystring);
mysql_query(mysql, querystring);
check_mysql_error();
// cleanup
free(datequote);
// free(querystring); // segfaults?!
}
/* End of file */
......@@ -17,10 +17,13 @@
*/
void check_mysql_error();
void connect_mysql_database(char *host, char *username, char *password, char *dbname, char *port, char *socket, char *flags);
void connect_mysql_database(char *host, char *username, char *password,\
char *dbname, char *port, char *socket, char *flags);
void close_mysql_connection();
void mysql_getquotes(char *symbol, int days, float **res, char **dates);
void mysql_updatequotes(char *symbol);
void mysql_get_lagged_dates(char *symbol, int days, char **dates);
void mysql_update_indicator(char *indicator, char *tablename, \
char *symbol,int days,char **dates,float **quotes, int quote_field);
/* End of file */
......@@ -29,16 +29,21 @@ struct parameters parms; /* make struct that holds config global */
int main ()
{
float **daily_quotes; /* dynamic 2d array that will hold the daily quotes (o,h,l,c) */
//float **daily_quotes; /* dynamic 2d array that will hold the daily quotes (o,h,l,c) */
/* declare pointers to dynamic arrays that will hold the
* highest highs/lowest lows for short, medium term
* and Tenkan-Sen, Kijun-Sen */
float *hh9, *ll9, *hh26, *ll26, *tenkan, *kijun;
///* declare pointers to dynamic arrays that will hold the
//* highest highs/lowest lows for short, medium term
//* and Tenkan-Sen, Kijun-Sen */
//float *hh9=NULL;
//float *ll9=NULL;
//float *hh26=NULL;
//float *ll26=NULL;
//float *tenkan=NULL;
//float *kijun=NULL;
char **quote_dates; /* dynamic 1d array that will hold dates */
char **tenkan_dates;
char *symbol; /* pointer to symbol string */
//char **quote_dates=NULL; /* dynamic 1d array that will hold dates */
//char **chikou_dates=NULL;
//char *symbol=NULL; /* pointer to symbol string */
/* set default/fallback config parameters */
init_parameters(&parms);
......@@ -48,81 +53,89 @@ int main ()
if(parse_config(&parms)==0)
fprintf(stdout, "done!\n");
/* connect to mysql Server */
fprintf(stdout,"Connecting to database %s ...", parms.DB_NAME);
connect_mysql_database(parms.DB_HOSTNAME,
parms.DB_USERNAME,
parms.DB_PASSWORD,
parms.DB_NAME,
parms.DB_PORT,
parms.DB_SOCKET,
parms.DB_FLAGS);
fprintf(stdout,"connected!\n");
///* connect to mysql Server */
//fprintf(stdout,"Connecting to database %s ...", parms.DB_NAME);
//connect_mysql_database(parms.DB_HOSTNAME,
//parms.DB_USERNAME,
//parms.DB_PASSWORD,
//parms.DB_NAME,
//parms.DB_PORT,
//parms.DB_SOCKET,
//parms.DB_FLAGS);
//fprintf(stdout,"connected!\n");
fprintf(stdout,"Allocating memory ...");
/* create 2d array to store quotes */
daily_quotes=init_2d_array_float(parms.ICHI_DAYS_TO_ANALYZE,3);
//fprintf(stdout,"Allocating memory ...");
///* create 2d array to store quotes */
//daily_quotes=init_2d_array_float(parms.ICHI_DAYS_TO_ANALYZE,3);
/* create 1d arrays */
hh9 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
ll9 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
hh26 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
ll26 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
tenkan = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
kijun = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
///* create 1d arrays */
//hh9 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
//ll9 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
//hh26 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
//ll26 = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
//tenkan = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
//kijun = init_1d_array_float(parms.ICHI_DAYS_TO_ANALYZE);
/* create string array to store dates */
quote_dates=init_1d_array_string(parms.ICHI_DAYS_TO_ANALYZE);
tenkan_dates=init_1d_array_string(parms.ICHI_DAYS_TO_ANALYZE);
///* create string array to store dates */
//quote_dates=init_1d_array_string(parms.ICHI_DAYS_TO_ANALYZE);
//chikou_dates=init_1d_array_string(parms.ICHI_DAYS_TO_ANALYZE);
//printf("\n%i\n%i",&dates, &tenkan_dates);
////printf("\n%i\n%i",&dates, &tenkan_dates);
fprintf(stdout,"done!\n"); // Allocating memory
//fprintf(stdout,"done!\n"); // Allocating memory
/* set symbol */
symbol="OJuice";
///* set symbol */
//symbol="OJuice";
/* Update quotes */
fprintf(stdout,"Updating quote database...");
mysql_updatequotes(symbol);
fprintf(stdout, "done!\n");
///* Update quotes */
//fprintf(stdout,"Updating quote database...");
//mysql_updatequotes(symbol);
//fprintf(stdout, "done!\n");
/* Query database for last quotes */
fprintf(stdout,"Querying...");
mysql_getquotes(symbol, parms.ICHI_DAYS_TO_ANALYZE, daily_quotes, quote_dates); // ToDo: Error handling!
fprintf(stdout, "done!\n");
///* Query database for last quotes */
//fprintf(stdout,"Querying...");
//mysql_getquotes(symbol, parms.ICHI_DAYS_TO_ANALYZE, daily_quotes, quote_dates); // ToDo: Error handling!
//fprintf(stdout, "done!\n");
/* display results (dates + quotes) */
//print_quote_table(dates,symbol,daily_quotes, parms.ICHI_DAYS_TO_ANALYZE);
///* display results (dates + quotes) */
////print_quote_table(dates,symbol,daily_quotes, parms.ICHI_DAYS_TO_ANALYZE);
fprintf(stdout, "Calculating indicators: hh/ll..");
lowest_low(parms.ICHI_PERIOD_SHORT, parms.ICHI_DAYS_TO_ANALYZE, daily_quotes, ll9);
highest_high(parms.ICHI_PERIOD_SHORT,parms.ICHI_DAYS_TO_ANALYZE, daily_quotes,hh9);
lowest_low(parms.ICHI_PERIOD_MID, parms.ICHI_DAYS_TO_ANALYZE, daily_quotes, ll26);
highest_high(parms.ICHI_PERIOD_MID,parms.ICHI_DAYS_TO_ANALYZE, daily_quotes,hh26);
fprintf(stdout, "Tenkan-Sen..");
ichimoku_tenkan(parms.ICHI_DAYS_TO_ANALYZE,hh9,ll9,tenkan);
fprintf(stdout, "Kijun-Sen..");
ichimoku_kijun(parms.ICHI_DAYS_TO_ANALYZE, hh26,ll26, kijun);
fprintf(stdout, "Chikou-Span..");
mysql_get_lagged_dates(symbol, parms.ICHI_DAYS_TO_ANALYZE, tenkan_dates);
fprintf(stdout, "done!\n");
//fprintf(stdout, "Calculating indicators: hh/ll..");
//lowest_low(parms.ICHI_PERIOD_SHORT, parms.ICHI_DAYS_TO_ANALYZE, daily_quotes, ll9);
//highest_high(parms.ICHI_PERIOD_SHORT,parms.ICHI_DAYS_TO_ANALYZE, daily_quotes,hh9);
//lowest_low(parms.ICHI_PERIOD_MID, parms.ICHI_DAYS_TO_ANALYZE, daily_quotes, ll26);
//highest_high(parms.ICHI_PERIOD_MID,parms.ICHI_DAYS_TO_ANALYZE, daily_quotes,hh26);
//fprintf(stdout, "Tenkan-Sen..");
//ichimoku_tenkan(parms.ICHI_DAYS_TO_ANALYZE,hh9,ll9,tenkan);
//fprintf(stdout, "Kijun-Sen..");
//ichimoku_kijun(parms.ICHI_DAYS_TO_ANALYZE, hh26,ll26, kijun);
//fprintf(stdout, "Chikou-Span..");
//mysql_get_lagged_dates(symbol, parms.ICHI_DAYS_TO_ANALYZE, chikou_dates);
//fprintf(stdout, "done!\n");
//// update Database with different indicators
//fprintf(stdout, "Updating indicator database..");
//fprintf(stdout, "Chikou-Span..");
//mysql_update_indicator("chikou","ichimoku_daily", symbol, \
//parms.ICHI_DAYS_TO_ANALYZE, chikou_dates, daily_quotes, \
//COL_QUOTES_DAILY_CLOSE);
//fprintf(stdout, "done!\n");
//print_1d_array_char(tenkan_dates,parms.ICHI_DAYS_TO_ANALYZE);
print_1d_array_char(quote_dates,parms.ICHI_DAYS_TO_ANALYZE);
// print_quote_table(quote_dates,symbol,daily_quotes, parms.ICHI_DAYS_TO_ANALYZE);
/* cleanup */
////print_1d_array_char(tenkan_dates,parms.ICHI_DAYS_TO_ANALYZE);
////print_1d_array_char(quote_dates,parms.ICHI_DAYS_TO_ANALYZE);
//// print_quote_table(quote_dates,symbol,daily_quotes, parms.ICHI_DAYS_TO_ANALYZE);
///* cleanup */
free_2d_array_float(daily_quotes,parms.ICHI_DAYS_TO_ANALYZE);
free_1d_array_float(hh9);
free_1d_array_float(ll9);
free_1d_array_float(hh26);
free_1d_array_float(ll26);
free_1d_array_float(tenkan);
free_1d_array_float(kijun);
free_1d_array_char(quote_dates,parms.ICHI_DAYS_TO_ANALYZE);
free_1d_array_char(tenkan_dates,parms.ICHI_DAYS_TO_ANALYZE);
close_mysql_connection();
//free_2d_array_float(daily_quotes,parms.ICHI_DAYS_TO_ANALYZE);
//free_1d_array_float(hh9);
//free_1d_array_float(ll9);
//free_1d_array_float(hh26);
//free_1d_array_float(ll26);
//free_1d_array_float(tenkan);
//free_1d_array_float(kijun);
//free_1d_array_char(quote_dates,parms.ICHI_DAYS_TO_ANALYZE);
//free_1d_array_char(chikou_dates,parms.ICHI_DAYS_TO_ANALYZE);
//close_mysql_connection();
return EXIT_SUCCESS;
}
......
......@@ -141,6 +141,6 @@ void ichimoku_kijun(int nr_days, float *hh_mid, float *ll_mid, \
for (i=0; i<parms.ICHI_PERIOD_MID;i++)
kijun_sen[i]=0.0;
}
}
/* End of file */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment