Download MetaTrader 5

Statistica neural networks

To add comments, please log in or register
altr
10
altr 2013.10.17 21:51 

This article https://www.mql5.com/en/articles/236 has given me idea to try to connect Statistica NN to MT5. I don't have Neurosolutions, but one possible advantage of Statistica to NS is that it automatically sifts through various configurations of NN in order to find the most plausible solution. Once you complete the process, it generates C/C++ code of the network that needs to be modified. 

Here's the code that it printed during recent tests:

 

//Analysis Type - TS_Reg 

#include <stdio.h>

#include <conio.h>

#include <math.h>

#include <stdlib.h>



double data_6_MLP_20_3_1_input_hidden_weights[3][20]=

{

 {-2.47714593443783e+000, 1.47233953482693e-001, -1.65685629128936e-001, 2.44001298714080e+000, -8.41154632460572e-002, -7.47140585636589e-002, -1.21841253487385e-002, 6.61910770022491e-003, 7.30435860961060e-002, 5.89437582566321e-002, 1.48550598991981e-002, 1.62587011942123e-002, -8.42220564828445e-004, 7.34172279089026e-002, -1.54452605590877e-002, 5.82820454487475e-002, -3.07193745336325e-002, 4.50625428349634e-002, 3.10617020305795e-002, -3.28662284131769e-002 },

 {-3.75749331820867e+000, -1.81598080368139e-001, 3.22399498872678e-001, 3.89731410970901e+000, 7.80260219699004e-003, 2.76278232491897e-001, 1.34963552989625e-001, 3.01791709782662e-001, -5.40793151343420e-002, -6.67318230224590e-002, 7.54003491731180e-003, 2.62346717972700e-002, -5.45618350863280e-004, -7.55963083044321e-002, -1.36952088242854e-002, -4.24378253948132e-002, 7.81895634074889e-003, -2.05613764525840e-002, 7.20419404375410e-002, 3.74579949555164e-002 },

 {-2.44743188346030e+000, 1.05761508195988e-001, -4.85221095856977e-001, 2.03982288390680e+000, -7.50975934848673e-002, -4.01037923059316e-001, -1.94960586426769e-001, -4.04099430628550e-001, 2.21156371319495e-002, 4.64210097913586e-002, 1.52123715864792e-003, 3.21290638925572e-002, -1.85810550199894e-002, 8.29190529025398e-002, -4.73213856417796e-002, 4.59491993177758e-002, -1.98240689764085e-002, -1.84761938340701e-002, -8.32890013575084e-002, -2.96768298585089e-002 } 

};


double data_6_MLP_20_3_1_hidden_bias[3]={ 1.71500810253657e-001, 7.05537086532361e-002, -2.46677945337074e-001 };


double data_6_MLP_20_3_1_hidden_output_wts[1][3]=

{

 {4.60538634200486e-001, 8.56989026286234e-001, 6.32219293600482e-001 }

};


double data_6_MLP_20_3_1_output_bias[1]={ -5.87953501383416e-001 };


double data_6_MLP_20_3_1_max_input[20]={ 3.12000000000000e-003, 3.29000000000000e-003, 3.04000000000000e-003, 3.06000000000000e-003, 3.53000000000000e-003, 3.86000000000000e-003, 2.91000000000000e-003, 3.09000000000000e-003, 3.94000000000000e-003, 3.94000000000000e-003, 3.45000000000000e-003, 3.47000000000000e-003, 4.04000000000000e-003, 4.20000000000000e-003, 3.86000000000000e-003, 3.88000000000000e-003, 4.16000000000000e-003, 4.55000000000000e-003, 3.97000000000000e-003, 4.04000000000000e-003 };


double data_6_MLP_20_3_1_min_input[20]={ -6.22000000000000e-003, -6.19000000000000e-003, -6.22000000000000e-003, -6.19000000000000e-003, -8.06000000000000e-003, -7.00000000000000e-003, -8.49000000000000e-003, -7.31000000000000e-003, -1.02000000000000e-002, -9.40000000000000e-003, -1.04500000000000e-002, -1.03800000000000e-002, -1.03300000000000e-002, -1.01900000000000e-002, -1.05100000000000e-002, -1.01900000000000e-002, -1.04100000000000e-002, -1.01100000000000e-002, -1.06200000000000e-002, -1.03400000000000e-002 };


double data_6_MLP_20_3_1_max_target[1]={ 4.95000000000000e-003 };


double data_6_MLP_20_3_1_min_target[1]={ -2.72000000000000e-003 };


double data_6_MLP_20_3_1_input[20];

double data_6_MLP_20_3_1_hidden[3];

double data_6_MLP_20_3_1_output[1];


double data_6_MLP_20_3_1_MeanInputs[20]={ -7.57857142857143e-006, 1.82241071428571e-004, -1.84851785714286e-004, -7.32142857142857e-007, -1.30839285714286e-005, 1.75344642857143e-004, -1.92389285714286e-004, -9.10535714285714e-006, -1.76839285714286e-005, 1.69892857142857e-004, -1.95255357142857e-004, -1.41375000000000e-005, -2.04285714285714e-005, 1.66589285714286e-004, -1.98266071428571e-004, -1.79982142857143e-005, -2.74125000000000e-005, 1.60964285714286e-004, -2.04307142857143e-004, -2.09446428571428e-005 };


void data_6_MLP_20_3_1_ScaleInputs(double* input, double minimum, double maximum, int nCategoricalInputs, int nContInputs, int steps)

{

 double delta;

 long i,j,n;

 for(j=0,n=0; j<steps; j++, n+=nCategoricalInputs)

 {

   for(i=0; i<nContInputs; i++)

   {

delta = (maximum-minimum)/(data_6_MLP_20_3_1_max_input[i]-data_6_MLP_20_3_1_min_input[i]);

input[n] = minimum - delta*data_6_MLP_20_3_1_min_input[i]+ delta*input[n];

    n++;

   }

 }

}


void data_6_MLP_20_3_1_UnscaleTargets(double* output, double minimum, double maximum, int size)

{

  double delta;

  long i;

  for(i=0; i<size; i++)

  {

    delta = (maximum-minimum)/(data_6_MLP_20_3_1_max_target[i]-data_6_MLP_20_3_1_min_target[i]);

    output[i] = (output[i] - minimum + delta*data_6_MLP_20_3_1_min_target[i])/delta;

   }

}


double data_6_MLP_20_3_1_logistic(double x)

{

  if(x > 100.0) x = 1.0;

  else if (x < -100.0) x = 0.0;

  else x = 1.0/(1.0+exp(-x));

  return x;

}


void data_6_MLP_20_3_1_ComputeFeedForwardSignals(double* MAT_INOUT,double* V_IN,double* V_OUT, double* V_BIAS,int size1,int size2,int layer)

{

  int row,col;

  for(row=0;row < size2; row++) 

    {

      V_OUT[row]=0.0;

      for(col=0;col<size1;col++)V_OUT[row]+=(*(MAT_INOUT+(row*size1)+col)*V_IN[col]);

      V_OUT[row]+=V_BIAS[row];

      if(layer==0) V_OUT[row] = data_6_MLP_20_3_1_logistic(V_OUT[row]);

   }

}


void data_6_MLP_20_3_1_RunNeuralNet_TS_Reg () 

{

  data_6_MLP_20_3_1_ComputeFeedForwardSignals((double*)data_6_MLP_20_3_1_input_hidden_weights,data_6_MLP_20_3_1_input,data_6_MLP_20_3_1_hidden,data_6_MLP_20_3_1_hidden_bias,20, 3,0);

  data_6_MLP_20_3_1_ComputeFeedForwardSignals((double*)data_6_MLP_20_3_1_hidden_output_wts,data_6_MLP_20_3_1_hidden,data_6_MLP_20_3_1_output,data_6_MLP_20_3_1_output_bias,3, 1,1);

}


int main()

{

  int i=0;

  int keyin=1;

  int stepcntr;

  int inputindex;

  int cont_inps_idx;

  int nsteps;

  while(1)

  {

stepcntr=1;

inputindex=0;

printf("\n%s\n","Enter values for Continuous inputs (To skip a continuous input please enter -9999)");

    for(nsteps=0;nsteps<1;nsteps++)

    {

     printf("\n%s%d\n","Enter Input values for Step ", stepcntr++);

     printf("%s","Cont. Input-0(Open1): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-1(High1): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-2(Low1): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-3(Close1): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-4(Open2): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-5(High2): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-6(Low2): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-7(Close2): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-8(Open3): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-9(High3): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-10(Low3): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-11(Close3): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-12(Open4): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-13(High4): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-14(Low4): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-15(Close4): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-16(Open5): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-17(High5): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-18(Low5): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

     printf("%s","Cont. Input-19(Close5): ");

     scanf("%lg",&data_6_MLP_20_3_1_input[inputindex++]);

inputindex-=20;

     //Substitution of missing continuous variables

for(cont_inps_idx=0;cont_inps_idx<20;cont_inps_idx++)

{

      if(data_6_MLP_20_3_1_input[inputindex] == -9999)

  data_6_MLP_20_3_1_input[inputindex]=data_6_MLP_20_3_1_MeanInputs[cont_inps_idx];

 inputindex++;

}

    }

    data_6_MLP_20_3_1_ScaleInputs(data_6_MLP_20_3_1_input,0,1,0,20,1);

data_6_MLP_20_3_1_RunNeuralNet_TS_Reg();

data_6_MLP_20_3_1_UnscaleTargets(data_6_MLP_20_3_1_output,0,1,1);

printf("\n%s%.14e","Predicted Output of Close0 = ",data_6_MLP_20_3_1_output[0]);

printf("\n\n%s\n","Press any key to make another prediction or enter 0 to quit the program.");

keyin=getch();

if(keyin==48)break;

  }

return 0;

}

 

As far as I see it  "data_6_MLP_20_3_1_output" is the number holding the prediction, so it would be easy to instruct MT5  if >0 then buy, if < 0 then sell.

What is more complicated is how to send OHLC values of the previous bars to the network.  In the example it is here

 {

stepcntr=1;

inputindex=0;

printf("\n%s\n","Enter values for Continuous inputs (To skip a continuous input please enter -9999)");

    for(nsteps=0;nsteps<1;nsteps++)

    {

     printf("\n%s%d\n","Enter Input values for Step ", stepcntr++);

     printf("%s","Cont. Input-0(Open1): ");

 

but obviously this needs to be done automatically and not manually. 

I hope that someone can help about this and all will benefit from this effort.


Connecting NeuroSolutions Neuronets
Connecting NeuroSolutions Neuronets
  • 2011.02.14
  • Andrew
  • www.mql5.com
In addition to creation of neuronets, the NeuroSolutions software suite allows exporting them as DLLs. This article describes the process of creating a neuronet, generating a DLL and connecting it to an Expert Advisor for trading in MetaTrader 5.
To add comments, please log in or register