by tradeurs » Tue Feb 28, 2012 11:17 pm
Roxtrade. It's a neural network strategies.
- Code: Select all
Devellopped by Niakfu@gmail.com 28/02/2012
Web : http://3w.tradeurs.net/
Compilation : gcc -o roxtrade roxtrade.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
typedef struct element element;
struct element
{
int type;
float capital;
float w[10][30][30];
int id_learn;
int id_list;
struct element *nxt;
};
typedef element* llist;
void sleep(int i);
main(void)
{
FILE* fichier = NULL;
FILE* fichier_poid = NULL;
FILE* fichier_ex = NULL;
element *list_w = NULL;
element *list_wu = NULL;
int caractereActuel = 0;
char tmp[255];
char num[63];
char num_max[63];
char num_min[63];
float data[30000];
float data_max[30000];
float data_min[30000];
float next_data[30];
int nb_data = 0;
int i_data = 0;
int fichier_open = 0;
int i = 0;
int j = 0;
int k = 0;
int iwf = 0;
int in = 0;
int jk=0;
int id_list = 0;
int max_neurone_par_couche = 50;
float input[100] ;
int neurone_par_couche[100] ;
int nb_couche = 6;
float erreur;
float moyenne = 0;
float w[10][30][30];
float ws[30][30];
// initialitation des couches
neurone_par_couche[0] = 20; // nb input
neurone_par_couche[1] = 24;
neurone_par_couche[2] = 30;
neurone_par_couche[3] = 1;
neurone_par_couche[4] = 15;
neurone_par_couche[5] = 1;
neurone_par_couche[6] = 0;
// Initialisation list_w
time(NULL);
element* nouvelElement = malloc(sizeof(element));
nouvelElement->type = 0;
nouvelElement->capital = 0;
nouvelElement->id_list = id_list++;
nouvelElement->nxt = NULL;
list_w = nouvelElement;
list_wu = list_w;
for( j=0; j < 10; j++)
for( i = 0; i< 30; i++)
for( k=0; k<30; k++)
list_wu->w[j][i][k] = ( rand() % 5000 ) * 0.00005;
while(1)
{
// Chargement des rates
nb_data = 0;
fichier = fopen("/roxtrade/taux/5m0.csv", "r");
if (fichier != NULL)
{
caractereActuel = fgetc(fichier);
while (caractereActuel != EOF)
{
if( caractereActuel == '\n' )
{
j = 0;
for( i = 0 ; i < 255; i++)
{
if( tmp[i] == ';')
{
i++;
j++;
}
if( j == 5 )
sprintf(num,"%s%c",num,tmp[i]); // close || 2 : open
if( j == 3 )
sprintf(num_max,"%s%c",num_max,tmp[i]);
if( j == 4 )
sprintf(num_min,"%s%c",num_min,tmp[i]);
}
data[nb_data] = atof(num)/10;
data_max[nb_data] = atof(num_max)/10;
data_min[nb_data++] = atof(num_min)/10;
for( i = 0 ; i < 255; i++)
tmp[i] = '\0';
for( i = 0 ; i < 10; i++)
num[i] = '\0';
for( i = 0 ; i < 10; i++)
num_max[i] = '\0';
for( i = 0 ; i < 10; i++)
num_min[i] = '\0';
}
sprintf(tmp, "%s%c", tmp, caractereActuel);
caractereActuel = fgetc(fichier);
}
fclose(fichier);
}else
return -1;
// Chargement Rate Stream
fichier = fopen("/roxtrade/taux/last_5m0.csv", "r");
if (fichier != NULL)
{
caractereActuel = fgetc(fichier);
while (caractereActuel != EOF)
{
if( caractereActuel == '\n' )
{
j = 0;
for( i = 0 ; i < 255; i++)
{
if( tmp[i] == ';')
{
i++;
j++;
}
if( j == 5 )
sprintf(num,"%s%c",num,tmp[i]); // close || 2 : open
if( j == 3 )
sprintf(num_max,"%s%c",num_max,tmp[i]);
if( j == 4 )
sprintf(num_min,"%s%c",num_min,tmp[i]);
}
data[nb_data] = atof(num)/10;
data_max[nb_data] = atof(num_max)/10;
data_min[nb_data] = atof(num_min)/10;
for( i = 0 ; i < 255; i++)
tmp[i] = '\0';
for( i = 0 ; i < 10; i++)
num[i] = '\0';
for( i = 0 ; i < 10; i++)
num_max[i] = '\0';
for( i = 0 ; i < 10; i++)
num_min[i] = '\0';
}
sprintf(tmp, "%s%c", tmp, caractereActuel);
caractereActuel = fgetc(fichier);
}
fclose(fichier);
}else
return -1;
// start Roxtrade
for( i_data = 200-neurone_par_couche[0] ; i_data <= nb_data ; i_data++ )
{
// initialisation des entrées
i=0;
for( k= i_data - neurone_par_couche[0]; k <= i_data ; k++)
{
moyenne =0;
for( j=0;j <3.00; j++)
moyenne += data[k-j];
input[i++] = ( moyenne/3.00) ;
}
// liason des neurones
for( j = 0 ; j < nb_couche ; j++)
{
if( j == 0 )
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
ws[j][k] += input[i] * list_wu->w[j][i][k];
}
}
}else
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
ws[j][k] += ws[j-1][i] * list_wu->w[j][i][k];
}
}
}
}
if( i_data >= nb_data - 170 )
{
i=0;
for( k= i_data - neurone_par_couche[0]; k <=i_data ; k++)
{
moyenne =0;
for( j=0;j <3.00; j++)
moyenne += data[k-j];
input[i++] = ( moyenne/3.00 ) ;
}
for( j = 0 ; j < nb_couche ; j++)
{
if( j == 0 )
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
ws[j][k] += input[i] * list_wu->w[j][i][k];
}
}
}else
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
ws[j][k] += ws[j-1][i] * list_wu->w[j][i][k];
}
}
}
}
if( fichier_open == 0 )
{
fichier_ex = fopen("/roxtrade/taux/eur_usd_5","w+");
if (fichier_ex != NULL)
fichier_open = 1;
}
printf("[%lf] \t Rox_ws => %lf \t Rox_err => %lf \n", data[i_data]*10 , ws[2][k] * 10 , ((data[i_data]*10)-(ws[2][k]*10))*1000);
sprintf(tmp,"%lf:%lf:%lf:%lf;\0", data[i_data]*10 , data_max[i_data]*10 , data_min[i_data]*10 , ws[2][k]*10 );
if( fichier_open == 1 )
fputs(tmp , fichier_ex);
}
// Corection de l'erreur
moyenne =0;
for( i=0; i <3.00; i++)
moyenne += data[(i_data-i)];
erreur = ( moyenne/3.00) - ws[2][k] ;
if( fichier_open == 1 && i_data > nb_data )
{
if (fichier_ex != NULL)
fclose(fichier_ex);
fichier_open = 0;
}
for( j = 0 ; j < nb_couche ; j++)
{
if( j == 0 )
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
list_wu->w[j][i][k] += (erreur) * input[i] * input[i] ;
}
}
}else
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
list_wu->w[j][i][k] += (erreur) * ws[j][k] * ws[j][k] ;
}
}
}
}
// Prediction
if( i_data == nb_data )
{
i=0;
for( j = 0 ; j< neurone_par_couche[0] ; j++)
{
next_data[j] = data[i_data-(neurone_par_couche[0]-j)];
input[i++] = next_data[j];
}
for( jk=0; jk <= 20 ; jk++)
{
for( j = 0 ; j < nb_couche ; j++)
{
if( j == 0 )
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
ws[j][k] += input[i] * list_wu->w[j][i][k];
}
}
}else
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
ws[j][k] = 0;
for( i = 0 ; i < neurone_par_couche[j] ; i++)
{
for( k = 0 ; k < neurone_par_couche[j+1] ; k++)
{
ws[j][k] += ws[j-1][i] * list_wu->w[j][i][k];
}
}
}
}
printf("[0.000000] \t Rox_ws => %lf \t Rox_err => %lf \n", ws[2][k] * 10 , ((data[i_data]*10)-(ws[2][k]*10))*100);
sprintf(tmp,"%lf:%lf:%lf:%lf;\0", data[i_data]*10 , data[i_data]*10 , data[i_data]*10 , ws[2][k]*10 );
input[neurone_par_couche[0]-1] = ws[2][k];
for( j = 1 ; j< neurone_par_couche[0] ; j++)
{
input[j-1] = input[j];
}
if( fichier_open == 1 )
fputs(tmp , fichier_ex);
}
}
}
if( fichier_open == 1 )
{
if (fichier_ex != NULL)
fclose(fichier_ex);
fichier_open = 0;
}
sleep(3);
}
}
void sleep(int i)
{
time_t t;
int time_fin;
time(&t);
time_fin = t + i;
while( t<=time_fin)
time(&t);
}