Attrib1
Download scriptThis script is designed to perform automated market analysis and predictive modeling by identifying patterns in price movements. It functions as a sophisticated data processing engine that transforms raw market prices into structured "features" used for machine learning classification.
Data Collection and Synchronization
The logic begins by synchronizing data across three different timeframes: 1-minute, 5-minute, and 60-minute intervals. To ensure the analysis is consistent, the script aligns these timeframes so that it can look at a specific point in history and extract relevant information from the smaller and larger timeframes simultaneously.
Feature Engineering
Instead of looking at raw prices (which can be volatile), the script calculates specific differences between Moving Averages (MA). It focuses on three distinct types of data points:
- Short-term Momentum: It calculates the gaps between 5, 10, and 15-period Moving Averages on the 5-minute chart.
- Historical Trends: It looks back at these same averages from two candles prior to calculate the speed and direction of previous price shifts.
- Long-term Context: It repeats this same logic on the 60-minute (hourly) timeframe to capture broader market trends.
By subtracting these moving averages from one another, the script creates a "fingerprint" of the current market state. This fingerprint consists of 18 unique attributes that describe how price is accelerating, decelerating, or consolidating across different time scales.
Training and Prediction Logic
The script organizes this gathered data into two distinct categories:
- Training Samples: These are historical data points used to teach the system what specific market "shapes" look like.
- Test Samples: These are the most recent data points, which the system uses to attempt a prediction based on what it learned from the training samples.
Machine Learning Execution
Finally, the script feeds all of this processed information into a Support Vector Machine (SVM) classification engine. The SVM takes the 18 attributes for both the historical and current data and attempts to assign a label or "prediction" to the current market state.
In short, the script acts as a translator: it takes messy price candles, translates them into a mathematical language of "differences," organizes that language into a structured database, and then uses a machine learning model to identify patterns in that data to predict future outcomes.
//+------------------------------------------------------------------+
//| 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.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;
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;
//+------------------------------------------------------------------+
//| 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]=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]=j;
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.txt";
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 0-
flag2,//0-do nothing 1-create model 2-predict 3-create model and predict
flag3//0-no predict file 1-predict file
);
Print("->",res1);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments