[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Neural Network - Can anyone complete this code?



PureBytes Links

Trading Reference Links

Hi all,

I have been spending some time trying to use ADK to integrate an 
Aussie Dollar NN into Amibroker.
The code i have written so far is below.

Everything up to the AmiVar function works fine, however i am having 
difficulty reading in the input values for the function.

Can anyne help here? cheers..J



include "stdafx.h"
#include "Plugin.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

////////////////////////////////////
// Network Calculation functions
////////////////////////////////////

double input_hidden_weights[4][6]=
{
 {-9.41493586282049e-001, -8.27678659948781e-001, 
5.33357172027228e+000, -8.81433105395710e+000, -
5.62990822935282e+000, 3.33188108288555e+000 },
 {-7.21310882837554e-001, 2.66136821466042e+000, -
1.30616133275411e+000, 2.22745742211765e+000, 1.48166162052262e+000, -
5.02573921885824e-001 },
 {-1.19975838533084e+000, -1.02273814209505e+000, 
6.26326878536705e+000, -8.73585783044836e+000, -
3.61252492344994e+000, 5.07994479550933e+000 },
 {-1.36613217835628e-001, 4.64156076188643e+000, -
1.75930666708638e+000, 3.81695078259574e+000, 1.60987267382251e+000, -
2.16079221557054e+000 } 
};

double hidden_bias[4]={ 2.80708720868898e+000, -
2.38093875656134e+000, 9.60983501333011e-001, -
3.43286402530261e+000 };

double hidden_output_wts[1][4]=
{
 {1.17956836551340e+000, 3.04046922300849e+000, -
1.33236764769559e+000, -2.40961518644655e+000 }
};

double output_bias[1]={ 1.05931141496123e+000 };

double max_input[6]={ 8.38014000000000e+001, 1.91800000000000e-001, 
3.13800000000000e-001, 3.80900000000000e-001, 2.07700000000000e-001, 
4.27100000000000e-001 };

double min_input[6]={ 1.71557000000000e+001, -2.14400000000000e-001, -
4.08600000000000e-001, -2.51700000000000e-001, -2.17500000000000e-
001, -1.99800000000000e-001 };

double max_target[1]={ 1.62800000000000e-001 };

double min_target[1]={ -1.88900000000000e-001 };

double input[6];
double hidden[4];
double output[1];

void FindMax(double* vec, double* max, long* maxIndex,int len)
{
  long i;
  *max = vec[0];
  *maxIndex = 0;
  for(i=1; i<len; i++)
  {
    if(vec[i]>*max)
    {
      *max = vec[i];
      *maxIndex = i;
    }
  }
}

void FindMin(double* vec, double* min, long* minIndex,int len)
{
  long i;
  *min = vec[0];
  *minIndex = 0;
  for(i=1; i<len; i++)
  {
    if(vec[i]<*min)
    {
      *min = vec[i];
      *minIndex = i;
    }
  }
}

void ScaleInputs(double* input, double min, double max, int size)
{
 double delta;
 long i;
 for(i=0; i<size; i++)
 {
	delta = (max-min)/(max_input[i]-min_input[i]);
	input[i] = min - delta*min_input[i]+ delta*input[i];
 }
}

void UnscaleTargets(double* output, double min, double max, int size)
{
  double delta;
  long i;
  for(i=0; i<size; i++)
  {
    delta = (max-min)/(max_target[i]-min_target[i]);
    output[i] = (output[i] - min + delta*min_target[i])/delta;
   }
}

double 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 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] = tanh(V_OUT[row]);
      if(layer==1) V_OUT[row] = logistic(V_OUT[row]);
   }
}

void RunNeuralNet_Regression () 
{
  ComputeFeedForwardSignals((double*)
input_hidden_weights,input,hidden,hidden_bias,6, 4,0);
  ComputeFeedForwardSignals((double*)
hidden_output_wts,hidden,output,output_bias,4, 1,1);
}

// Helper function

int SkipEmptyValues( int nSize, float *Src, float *Dst )
{
	int i;

	for( i = 0; i < nSize && IS_EMPTY( Src[ i ] ); i++ )
	{
		Dst[ i ] = EMPTY_VAL;
	}

	return i;
}

// Neural Network Function

AmiVar VNeuralNetAD( int NumArgs, AmiVar *ArgsTable )
{
       int i;
       int nSize = gSite.GetArraySize();
       
       for (i=250; i<nSize; ++i)
       {       
                     
             input[0] = 45; // dummy value - i need to read in the 
values of the 14 bar RSI here      	
             input[1] = -0.023; // dummy value - i need to read in 
the values of the 50 bar ROC of the Dollar Index here
             input[2] = 0.014; // dummy value - i need to read in the 
values of the 50 bar ROC of the S&P here
             input[3] = 0.0076; // dummy value - i need to read in 
the values of the 50 bar RSI of Feeder Cattle here
             input[4] = -0.054; // dummay value - i need to read in 
the values of the 50 bar LOG return of the Swiss Franc here
             input[5] = 0.0005; // dummy value - i need to read in 
the values of the 50 bar log return of Gold here 
        
             ScaleInputs(input,0,1,6);
             RunNeuralNet_Regression();
             UnscaleTargets(output,0,1,1);   
        
             AmiVar result = gSite.GetVariable("Close"); //the result 
needs to be set to the network output.. i've just used the close so 
that i can compile and test back in AB
             return result;
        }

}


/////////////////////////////////////////////
// Function table 
/////////////////////////////////////////////

FunctionTag gFunctionTable[] = {
							
	"NeuralNetAD",		{ VNeuralNetAD, 0, 0, 0, 0, NULL } 
};

int gFunctionTableSize = sizeof( gFunctionTable )/sizeof( 
FunctionTag );



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/