Attrib2a
Download scriptLogic Overview
This script is designed to analyze financial market data (specifically for the EURUSD currency pair) to identify potential trading opportunities using a machine learning approach. It processes historical price information across different timeframes to "teach" a model how to recognize successful trade patterns and then use that knowledge to provide predictions.
Data Collection and Feature Engineering
The script begins by gathering various technical indicators from three distinct time horizons: 1-minute, 5-minute, and 60-minute periods. Instead of looking at simple price movements, it focuses on the relationships between different Moving Averages (MA).
Specifically, it calculates the differences between multiple Moving Averages (e.g., a 5-period MA vs. a 10-period MA and a 15-period MA) across three consecutive time steps for each timeframe. These differences are converted into a standardized format (divided by the price point) to create a "feature set." This feature set represents the current momentum, trend direction, and volatility of the market at any given moment.
Training Phase: Defining Success
To train the model, the script looks back at historical data. It uses a specific logic to determine if a past market condition resulted in a successful trade. For every historical data point, it checks:
- Buy Signals: Did the price move up by a certain amount (Take Profit) without first dropping by a certain amount (Stop Loss)?
- Sell Signals: Did the price move down by a certain amount without first rising by a certain amount?
If a specific market condition led to a successful trade within a defined "future window," it is labeled as a success. This information is paired with the technical indicators (the features) collected earlier, creating a training dataset of "winning" versus "losing" market states.
Machine Learning Implementation
Once the data is organized into training samples (past results) and test samples (current/recent data), the script utilizes a Support Vector Machine (SVM) algorithm.
The SVM takes the gathered technical indicators and the known historical outcomes to build a mathematical model. This model learns to distinguish between the subtle patterns that preceded successful trades in the past. Finally, the script applies this trained model to the most recent market data to generate a prediction, which is then displayed as a series of labels (outputs) representing the likelihood or direction of future price movements.
Summary of Workflow
- Scan: Collect differences between Moving Averages across 1m, 5m, and 60m charts.
- Label: Review historical data to see which of those conditions actually resulted in hitting a "Take Profit" target vs. a "Stop Loss."
- Learn: Feed these "Success/Failure" labels into a machine learning algorithm (SVM).
- Predict: Use the finished model to analyze current market conditions and provide a prediction output.
//+------------------------------------------------------------------+
//| 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.std";
double C=1;
double g=0;
int flag=0;//0-no file ,1-file ma,2-file ma diff
int normalize=1;//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