Author: Copyright � 2008, MetaQuotes Software Corp.

This script is designed to analyze historical market data to predict potential trading signals for the EURUSD currency pair. It uses a sophisticated machine learning approach known as a Support Vector Machine (SVM) to categorize past price movements and generate future predictions.

The logic of the script can be broken down into three primary phases:

1. Data Collection and Feature Engineering

The script starts by scanning historical price data across different timeframes (specifically 1-minute, 5-minute, and 60-minute intervals). Instead of looking at just the raw price, it calculates "features"—specific mathematical relationships between Moving Averages (MA) of varying lengths.

By calculating the differences between these averages across multiple points in time, the script creates a multi-dimensional profile of market behavior. These profiles capture nuances like momentum, volatility, and trend direction, which are converted into numerical values that the machine learning model can interpret.

2. Labeling and Training Set Preparation

The script then organizes this data into a "training set." To do this, it uses a custom logic to determine what a "successful" trade would have looked like in the past. It checks historical price action to see if specific movements met defined criteria for:

  • Buy Signals: Identifying instances where the price reached a target profit level without hitting a designated stop-loss.
  • Sell Signals: Identifying instances where the price dropped to a target profit level without exceeding a stop-loss boundary.

The script assigns a numerical value (a "label") to these historical moments based on whether they resulted in a successful trade and how far into the future that success occurred.

3. Model Training and Prediction

Finally, the script feeds this organized data—the technical indicators (features) and the known outcomes (labels)—into the SVM classifier. The machine learning model "learns" the patterns that typically precede successful trades based on the historical data provided.

Once the model is trained, it applies that knowledge to the most recent "test" data points (market conditions that have occurred but for which the outcome is not yet known). It then outputs a series of predictions, effectively attempting to forecast whether current market conditions resemble past patterns that led to successful trades.

The results of these predictions are then printed to the system log for the user to review.

Price Data Components
Series array that contains close prices for each barSeries array that contains the lowest prices of each barSeries array that contains the highest prices of each bar
Indicators Used
Moving average indicator
8 Views
0 Downloads
0 Favorites
Attrib4a
//+------------------------------------------------------------------+
//|                                                      pointer.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#include <svm288_18_2.mqh>
#define FW_M5 5
#define ROW1_M5 100
#define ROW2_M5 50


//double lines[ROW1_M1];
double lines_attr[][COL];
//double svm_samples_class_dyn_1[ROW1_M1];
double svm_samples_class_dyn_1[];
//double svm_samples_attrib_dyn_1[ROW1_M1][COL];
double svm_samples_attrib_dyn_1[][COL];
//double svm_test_attrib_dyn_1[ROW2_M1][COL];
double svm_test_attrib_dyn_1[][COL];
//double ptr_predict_label__1[ROW2_M1]={0,0,0};
double ptr_predict_label__1[];

string SymbolName="EURUSD";
int PeriodMinutes1=PERIOD_M1;
int PeriodMinutes5=PERIOD_M5;
int PeriodMinutes60=PERIOD_H1;

int TrainPeriod=PERIOD_M5;
int StopLoss=30;		//70
int TakeProfit=30;	//70

double x1,x2,x3,x4,x5,x6,x7,x8,x9;
double y1,y2,y3,y4,y5,y6,y7,y8,y9;
double z1,z2,z3,z4,z5,z6,z7,z8,z9;
double at0,at1,at2,at3,at4,at5,at6,at7,at8;
double at9,at10,at11,at12,at13,at14,at15,at16,at17;
double at18,at19,at20,at21,at22,at23,at24,at25,at26;

double CalcBuySell(string _Sym, int _TPd,int SL,int TP,int _FW_M5,int pos)
{
     int i,j;
     double c=EMPTY_VALUE;     
     
   datetime t1=iTime(_Sym,_TPd,_FW_M5);
   int _FW_M1=iBarShift(_Sym,PERIOD_M1,t1,true);
     
     //if(pos<_FW_M1)
     //  return (c);

	  //for(i=_FW_M1+pos,j=0;j<_nr_of_sample_lines-pos;i++,j++)
	  //for(i=pos,j=0;j<_nr_of_sample_lines-pos;i++,j++)
	  //{

        c=0;//EMPTY_VALUE;
        double Point1=MarketInfo(_Sym,MODE_POINT);
        double sp=MarketInfo(_Sym,MODE_SPREAD)*Point1;
        datetime time1=iTime(_Sym,PERIOD_M1,pos);
        int ii=iBarShift(_Sym,_TPd,time1,true);        
        //int ii = pos;//30...504
        double entry_buy = iClose(_Sym,_TPd,ii)+sp;//entry_buy=bid0+sp 
        double entry_sell = iClose(_Sym,_TPd,ii);//entry_sell=bid0 
        bool longOk=true;
        bool shortOk=true;                                                
        for (int fw=1; fw<_FW_M5; fw++) 
        {
          double l=iLow(_Sym,_TPd,ii-fw);
          double h=iHigh(_Sym,_TPd,ii-fw); 
          if (l<=entry_buy-SL*Point1) longOk=false;
          if (h+sp>=entry_sell+SL*Point1) shortOk=false;
          if (longOk && (h>=entry_buy+TP*Point1)) { c=(+1)*fw;  break; }
          if (shortOk && (l+sp<=entry_sell-TP*Point1)) { c=(-1)*fw;  break; }                       
        } 
	  
	  //}
	  
	  return(c);
}
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   int i,j,k;  
  
   int ROW1_M1;
   int ROW2_M1; 
   int FW_M1;
   int diff=0;//2 (5min)

   datetime t1=iTime(SymbolName,PeriodMinutes5,FW_M5);
   FW_M1=iBarShift(SymbolName,PeriodMinutes1,t1,true);
   
   datetime t2=iTime(SymbolName,PeriodMinutes5,ROW2_M5);
   ROW2_M1=iBarShift(SymbolName,PeriodMinutes1,t2,true);      

   datetime t3=iTime(SymbolName,PeriodMinutes5,ROW1_M5);
   ROW1_M1=iBarShift(SymbolName,PeriodMinutes1,t3,true);   
      
   Print(" ROW2_M5=",ROW2_M5);
   Print(" ROW2_M1=",ROW2_M1);
   Print("ROW1_M5-FW_M5=",ROW1_M5-FW_M5," ROW1_M5=",ROW1_M5," FW1_M5=",FW_M5);
   Print("ROW1_M1-FW_M1=",ROW1_M1-FW_M1," ROW1_M1=",ROW1_M1," FW1_M1=",FW_M1);
/*
   |<---------------------------ROW1_M5-------------------------->|
   |                                                              |
   |                                                   |<--FW_M5->|
   |                                                   |          |
   +-------------------------------------------+-------+----------+
   t3                                          t2      t1         t0
                                               |                  |
                                               |<-----ROW2_M5---->|
                                               
                                               |                  |
   lines_attr ...                    lines_attr|lines_attr      lines_attr                                            
   [ROW1_M1-1                           [FW_M1]|[FW_M1-1]        [0]
*/  

   int  	nr_of_sample_lines=ROW1_M1-FW_M1-diff; 
   int	nr_of_test_lines=ROW2_M1;
         
   ArrayResize(lines_attr,ROW1_M1);
   ArrayResize(svm_samples_class_dyn_1,nr_of_sample_lines);
   ArrayResize(svm_samples_attrib_dyn_1,nr_of_sample_lines);
   ArrayResize(svm_test_attrib_dyn_1,nr_of_test_lines);   
   ArrayResize(ptr_predict_label__1,nr_of_test_lines);

   for (i=ROW1_M1-1;i>=0;i--)
   {
      datetime time1=iTime(SymbolName,PeriodMinutes1,i);
      
      int b5=iBarShift(SymbolName,PeriodMinutes5,time1,true);
      int b60=iBarShift(SymbolName,PeriodMinutes60,time1,true);
      if(b5>=0)
      {  
        x1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5);
        x2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5);
        x3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5);
        at0=x1-x2;
        at1=x2-x3;
        at2=x1-x3;
        y1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5+1);
        y2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5+1);
        y3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5+1);
        at9=y1-y2;
        at10=y2-y3;
        at11=y1-y3;    
        z1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5+2);
        z2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5+2);
        z3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5+2);   
        at18=z1-z2;
        at19=z2-z3;
        at20=z1-z3;            
      }
      
      if(b60>=0)
      {        
        x4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60);
        x5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60);
        x6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60);
        at3=x4-x5;
        at4=x5-x6;
        at5=x4-x6;
        y4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60+1);
        y5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60+1);
        y6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60+1);
        at12=y4-y5;
        at13=y5-y6;
        at14=y4-y6;
        z4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60+2);
        z5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60+2);
        z6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60+2);
        at21=z4-z5;
        at22=z5-z6;
        at23=z4-z6;
      }       
      //for (j=0;j<COL;j++)
      lines_attr[i][0]=i;//at0/Point;
      lines_attr[i][1]=at1/Point;
      lines_attr[i][2]=at2/Point;
      
      lines_attr[i][3]=at9/Point;
      lines_attr[i][4]=at10/Point;
      lines_attr[i][5]=at11/Point; 
      
      lines_attr[i][6]=at18/Point;
      lines_attr[i][7]=at19/Point;
      lines_attr[i][8]=at20/Point;   
        
      lines_attr[i][9]=at3/Point;
      lines_attr[i][10]=at4/Point;
      lines_attr[i][11]=at5/Point;
      
      lines_attr[i][12]=at12/Point;
      lines_attr[i][13]=at13/Point;
      lines_attr[i][14]=at14/Point;
        
      lines_attr[i][15]=at21/Point;     
      lines_attr[i][16]=at22/Point;
      lines_attr[i][17]=at23/Point;              
        
   }

      
   for(i=FW_M1,j=0;j<nr_of_sample_lines;i++,j++)  
   {
      svm_samples_class_dyn_1[j]=CalcBuySell(SymbolName, TrainPeriod,StopLoss,TakeProfit,FW_M5,i);
      for(  k = 0; k < COL; k++)
        svm_samples_attrib_dyn_1[j][k]=lines_attr[i][k];
   }
  


   for( i=0; i < nr_of_test_lines;i++)
   {      
      ptr_predict_label__1[i]=	0.0;   
      for(  k = 0; k < COL; k++)
        svm_test_attrib_dyn_1[i][k]=lines_attr[i][k];
   }

    
	int FutureWindow=FW_M1;
	int  nr_of_attrib=COL;
   string file_name="c:/"+SymbolName+"_file_18_1.not";
   double C=1;
   double g=0;  						    
   int flag=0;//0-no file ,1-file ma,2-file ma diff
	int normalize=0;//1-normalize 0-
	int flag2=0;//0-do nothing 1-create model 2-predict 3-create model and predict
   int flag3=0;//0-no predict file 1-predict file

   int res1=0;

   res1= svm_classify(
	svm_samples_class_dyn_1,
	svm_samples_attrib_dyn_1,
	svm_test_attrib_dyn_1,	
	nr_of_sample_lines, //2200
   nr_of_test_lines, //800
   nr_of_attrib,
	FutureWindow,	//200
   file_name,
   C,
   g,
   ptr_predict_label__1,//predict output							    
   flag,//0-no file ,1-file ma,2-file ma diff
	normalize,//1-normalize with std, 3-normalize with rng, 0-don't normalize
	flag2,//0-do nothing 1-create model 2-predict 3-create model and predict
   flag3//0-no predict file 1-predict file
   );
   
   Print(ptr_predict_label__1[0]," ",
            ptr_predict_label__1[1]," ",
            ptr_predict_label__1[2]," ",
            ptr_predict_label__1[3]," ... ",ptr_predict_label__1[nr_of_test_lines-1]);
   
   Print("->",res1);  
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments