//+------------------------------------------------------------------+ //| Schnick.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2011, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| This script demonstrates the capabilities of the Support Vector //| Machine Learning Tool //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| The following statement imports all of the functions included in //| the Support Vector Machine Tool 'svMachineTool.ex5' //+------------------------------------------------------------------+ #import "svMachineTool.ex5" enum ENUM_TRADE {BUY,SELL}; enum ENUM_OPTION {OP_MEMORY,OP_MAXCYCLES,OP_TOLERANCE}; int initSVMachine(void); void setIndicatorHandles(int handle,int &indicatorHandles[],int offset,int N); void setParameter(int handle,ENUM_OPTION option,double value); bool genOutputs(int handle,ENUM_TRADE trade,int StopLoss,int TakeProfit,double duration); bool genInputs(int handle); bool setInputs(int handle,double &Inputs[],int nInputs); bool setOutputs(int handle,bool &Outputs[]); bool training(int handle); bool classify(int handle); bool classify(int handle,int offset); bool classify(int handle,double &iput[]); void deinitSVMachine(void); #import //--- The number of inputs we will be using for the svm int N_Inputs=7; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnStart() { double inputs[]; //empty double array to be used for creating training inputs bool outputs[]; //empty bool array to be used for creating training inputs int N_TrainingPoints=5000; //defines the number of training samples to be generated int N_TestPoints=5000; //defines the number of samples to used when testing genTrainingData(inputs,outputs,N_TrainingPoints); //generates the inputs and outputs to be used for training the svm int handle1=initSVMachine(); //initializes a new support vector machine and returns a handle setInputs(handle1,inputs,7); //passes the inputs (without errors) to the support vector machine setOutputs(handle1,outputs); //passes the outputs (without errors) to the support vector machine setParameter(handle1,OP_TOLERANCE,0.01); //sets the error tolerance parameter to <5% training(handle1); //trains the support vector machine using the inputs/outputs passed insertRandomErrors(inputs,outputs,500); //takes the original inputs/outputs generated and adds random errors to the data int handle2=initSVMachine(); //initializes a new support vector machine and returns a handle setInputs(handle2,inputs,7); //passes the inputs (with errors) to the support vector machine setOutputs(handle2,outputs); //passes the outputs (with errors) to the support vector machine setParameter(handle2,OP_TOLERANCE,0.01); //sets the error tolerance parameter to <5% training(handle2); //trains the support vector machine using the inputs/outputs passed double t1=testSVM(handle1,N_TestPoints); //tests the accuracy of the trained support vector machine and saves it to t1 double t2=testSVM(handle2,N_TestPoints); //tests the accuracy of the trained support vector machine and saves it to t2 Print("The SVM accuracy is ",NormalizeDouble(t1,2),"% (using training inputs/outputs without errors)"); Print("The SVM accuracy is ",NormalizeDouble(t2,2),"% (using training inputs/outputs with errors)"); deinitSVMachine(); //Cleans up all of the memory used in generating the SVM to avoid memory leakage } //+------------------------------------------------------------------+ //| This function takes the observation properties of the observed //| animal and based on the critera we have chosen, returns //| true/false whether it is a schnick //+------------------------------------------------------------------+ bool isItASchnick(double height,double weight,double N_legs,double N_eyes,double L_arm,double av_speed,double f_call) { if(height < 1000 || height > 1100) return(false); //If the height is outside the parameters > return(false) if(weight < 40 || weight > 50) return(false); //If the weight is outside the parameters > return(false) if(N_legs < 8 || N_legs > 10) return(false); //If the N_Legs is outside the parameters > return(false) if(N_eyes < 3 || N_eyes > 4) return(false); //If the N_eyes is outside the parameters > return(false) if(L_arm < 400 || L_arm > 450) return(false); //If the L_arm is outside the parameters > return(false) if(av_speed < 2 || av_speed > 2.5) return(false); //If the av_speed is outside the parameters > return(false) if(f_call < 11000 || f_call > 15000) return(false); //If the f_call is outside the parameters > return(false) return(true); //Otherwise > return(true) } //+------------------------------------------------------------------+ //| This function takes an empty double array and empty boolean array //| and generates the inputs/outputs to be used for training the SVM //+------------------------------------------------------------------+ void genTrainingData(double &inputs[],bool &outputs[],int N) { double in[]; //creates an empty double array to be used //for temporarily storing the inputs generated ArrayResize(in,N_Inputs); //resize the in[] array to N_Inputs ArrayResize(inputs,N*N_Inputs); //resize the inputs[] array to have a size of N*N_Inputs ArrayResize(outputs,N); //resize the outputs[] array to have a size of N for(int i=0;i0.5) randomOutput=true; else randomOutput=false; //--- copy the new random inputs generated into the training input array ArrayCopy(inputs,in,index*N_Inputs,0,N_Inputs); //--- copy the new random output generated into the training output array outputs[index]=randomOutput; } } //+------------------------------------------------------------------+ //| This function is used to create a random value between t1 and t2 //+------------------------------------------------------------------+ double randBetween(double t1,double t2) { return((t2-t1)*((double)MathRand()/(double)32767)+t1); } //+------------------------------------------------------------------+