NeuroProba
Download scriptThis script functions as a self-learning artificial intelligence tool designed for financial charting. Rather than relying on fixed mathematical formulas to predict market trends, it uses a "Neural Network" approach—a method inspired by how biological brains process information to recognize patterns and learn from experience.
Core Logic Overview
The script operates in two distinct phases: the Learning Phase (which happens when you first load the indicator) and the Application Phase (which occurs as the market moves in real-time).
1. The Learning Phase (Data Processing)
Before the indicator starts providing active signals, it performs a "study" of historical data.
- Data Collection: It looks back at a specific window of history (defined by your settings) and collects price movements.
- The Teacher: It uses an external signal (a "Teacher") as its reference point. Think of this as a grading system; the script looks at what happened in the past and identifies what the "correct" outcome was based on that teacher's guidance.
- Normalization: To ensure it doesn't get overwhelmed by large price fluctuations, it converts raw price changes into a standardized format. This allows the AI to focus on the shape of the movement rather than just the magnitude.
- Optimization (Training): The script runs through multiple "Study Cycles." During these cycles, it makes a guess about the market, compares that guess to the Teacher's result, calculates the error, and then automatically adjusts its internal "weights" (its internal logic) to be more accurate next time.
2. The Application Phase (Real-Time Analysis)
Once the training is complete, the indicator begins to run live on your chart.
- Pattern Matching: As new candles form, the script feeds current price data into its trained network.
- Signal Generation: The neural network processes these inputs through its optimized layers to produce a result. This result is displayed as a visual output on your chart.
- Error Tracking: The script continues to monitor the difference between its own predictions and the "Teacher's" signals, providing you with an error buffer that shows how accurately the AI is currently performing.
Key Components Simplified
- Weights: These are the "memory" of the AI. They represent the importance the script assigns to different types of price movements. The goal of the training is to find the perfect balance of these weights.
- Sigma Function: This is a mathematical curve used to squash the AI's raw calculations into a smooth, usable range (usually between -1 and 1), making it easier to interpret as a clear directional signal.
- Gradient Descent: This is the "learning" mechanic. It is the process of slightly nudging the internal weights in the direction that reduces the error until the AI's guesses closely match the teacher’s results.
Summary for the User
Essentially, this script isn't just a standard moving average or oscillator. It is a dynamic model that attempts to "mimic" a specific trading style by studying history, correcting its own mistakes during an initial training period, and then applying those learned behaviors to live market data.
//+------------------------------------------------------------------+
//| NeuroProba.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Blue
#property indicator_color3 Red
//---- input parameters
extern int BeginBar=300; // íà÷àëüíûé áàð â îáó÷àþùåé âûáîðêå
extern int EndBar=201; // êîíå÷íûé áàð â îáó÷àþùåé âûáîðêå
extern int StudyNumber=100; // êîëè÷åñòâî îáó÷àþùèõ öèêëîâ
extern double Betta=0.5; // êðóòèçíà ñèãìîèäàëüíîé ôóíêöèè
extern double StudyCoeff=0.1; // êîýôôèöèåíò îáó÷åíèÿ
extern double MaxRandom=0.66;//2.0/MathSqrt(9);
//---- buffers
double NeuroExitBuffer[]; // âûõîäíûå çíà÷åíèÿ íåéðîñåòè
double TeacherBuffer[]; // ýòàëîííûå çíà÷åíèÿ íåéðîñåòè (îáó÷àþùèå ñèãíàëû)
double ErrorBuffer[]; // îòêëîíåíèÿ íà êàæäîì áàðå
//----
double deviation; // îòêëîíåíèå íà êàæäîì øàãå îáó÷åíèÿ
double GradientFirstLay[9][9];// äëÿ ðàñ÷åòà ãðàäèåíòà 1-ãî ñëîÿ
double GradientSecondLay[9]; // äëÿ ðàñ÷åòà ãðàäèåíòà 2-ãî ñëîÿ
double OutputFirstLay[9]; // ñóììà âûõîäà êàæäîãî íåéðîíà
double SecondSum;// ñóììà âûõîäíîãî ñëîÿ - àðãóìåíò ñèãìîèäàëüíîé ôóíêöèè
double FirstLayWeights[9][9]; // âåñà íåéðîíîâ ïåðâîãî (ñêðûòîãî ñëîÿ)
double OutputSecondWeights[9]; // âåñà âûõîäíîãî íåéðîíà
double InputNormalX[100][9]; // íîðìàëèçîâàííàÿ âûáîðêà âõîäíûõ ñèãíàëîâ
double InputX[9]; // òåêóùèé âõîäíîé âåêòîð (íåíîðìàëèçîâàííûé)
double Normà;// íîðìà òåêóùåãî âõîäíîãî âåêòîðà
double U_First[9];// çíà÷åíèÿ íà âûõîäå ñëîÿ ñêðûòûõ (âõîäíûõ) íåéðîíîâ
double U_Second;// çíà÷åíèå íà âûõîäå âûõîäíîãî íåéðîíà (àðãóìåíò ñèãìîèäàëüíîé ôóíêöèè)
double SigmaCurr;
double Differ;
//+----------------------------------------------
// íîðìàëèçîâàííàÿ ñëó÷àéíàÿ ôóíêöèÿ íà èíòåðâàëå (0,MaxValue), åñëè àðãóìåíò ïðîïóùåí - íà (0,1)
//+----------------------------------------------
double NormalRand(double MaxValue=1.0)
{
double answer;
answer=MathRand()/32767.0;
answer=MaxValue*answer;
return(answer);
}
//+----------------------------------------------
// íîðìàëèçîâàííàÿ ñëó÷àéíàÿ ôóíêöèÿ
//+----------------------------------------------
//------------------------------------------
// îïðåäåëåíèå ñèãìîèäàëüíîé ôóíêöèè (òàíãåíñ ãèïðåáîëè÷åñêèé)
//------------------------------------------
double Sigma(double argument)
{
double help;
help=(MathExp(argument)-MathExp(-argument))/(MathExp(argument)+MathExp(-argument));
return(help);
}
//------------------------------------------
// îïðåäåëåíèå ñèãìîèäàëüíîé ôóíêöèè
//------------------------------------------
//------------------------------------------
// âû÷èñëåíèå àðãóìåíòà ñèãìîèäàëüíîé ôóíêöèè
//------------------------------------------
double GetArgument()
{
int j;
double answer=0.0;
for (j=0;j<9;j++)
{
answer=answer+OutputSecondWeights[j]*OutputFirstLay[j];
}
return(answer);
}
//------------------------------------------
// âû÷èñëåíèå àðãóìåíòà ñèãìîèäàëüíîé ôóíêöèè
//------------------------------------------
//------------------------------------------
// âû÷èñëåíèå âûõîäîâ íåéðîíîâ ïåðâîãî ñëîÿ
//------------------------------------------
void GetFirstLayOutput()
{
int j,t;
for (j=0;j<9;j++)
{
OutputFirstLay[j]=0.0;
for (t=0;t<9;t++)
{
OutputFirstLay[j]=OutputFirstLay[j]+FirstLayWeights[j,t]*InputX[t];
}
}
return(0);
}
//------------------------------------------
// âû÷èñëåíèå âûõîäîâ íåéðîíîâ ïåðâîãî ñëîÿ
//------------------------------------------
//------------------------------------------
// âû÷èñëåíèå ãðàäèåíòà âåñîâ íåéðîíîâ
//------------------------------------------
void SetGradientWeights()
{
int j,t;
for (j=0;j<9;j++)
{
//GradientFirstLay[j,t]=0.0;
for (t=0;t<9;t++)
{
GradientFirstLay[j,t]=OutputSecondWeights[j]*InputX[t]*deviation*Differ;
//GradientFirstLay[j,t]=GradientFirstLay[j,t]+OutputSecondWeights[j]*InputX[t];
}
//GradientFirstLay[j,t]=GradientFirstLay[j,t]*deviation*Differ;
GradientSecondLay[j]=OutputFirstLay[j]*deviation*Differ;
}
}
//------------------------------------------
// âû÷èñëåíèå ãðàäèåíòà âåñîâ íåéðîíîâ
//------------------------------------------
//------------------------------------------
// èçìåíåíèå âåñîâ íåéðîíîâ
//------------------------------------------
void ChangeWeigts()
{
int i,k;
for (i=0;i<9;i++)
{
for (k=0;k<9;k++)
{
FirstLayWeights[i,k]=FirstLayWeights[i,k]-StudyCoeff*GradientFirstLay[i,k];
}
OutputSecondWeights [i]=OutputSecondWeights [i]-StudyCoeff*GradientSecondLay[i];// èçìåíåíèå âåñîâ âûõîäíîãî íåéðîíà
}
}
//------------------------------------------
// èçìåíåíèå âåñîâ íåéðîíîâ
//------------------------------------------
//------------------------------------------
// âûâîä â Æóðíàë âåñîâ íåéðîíîâ
//------------------------------------------
void PrintWeights()
{
int i,k;
for (i=0;i<9;i++)
{
for (k=0;k<9;k++)
{
// Print("W1["+i+","+k+"]=",FirstLayWeights[i,k]);
}
// Print("W2["+i+"]=",OutputSecondWeights[i]);
}
}
//------------------------------------------
// âûâîä â Æóðíàë âåñîâ íåéðîíîâ
//------------------------------------------
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,NeuroExitBuffer);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,TeacherBuffer);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ErrorBuffer);
SetIndexLabel(0,"Answer");
SetIndexLabel(1,"Teacher");
SetIndexLabel(2,"Error");
ArrayInitialize(NeuroExitBuffer,0.0);
ArrayInitialize(TeacherBuffer,0.0);
ArrayInitialize(ErrorBuffer,0.0);
//------------------------------------------
// çàïîëíåíèå âûáîðêè âõîäíûõ ñèãíàëîâ
//------------------------------------------
int i,k,m,n,p; // m - èíäåêñ âåñîâ âûõîäíîãî íåéðîíà, p - ñ÷åò÷èê öèêëà îáó÷åíèÿ
for (i=BeginBar;i>=EndBar;i--)
{
Normà=0.0;
for (k=0;k<=8;k++)
{InputX[k]=Close[i]-Close[i+k+1];
Normà=Normà+InputX[k]*InputX[k];
}
Normà=MathSqrt(Normà);
for (k=0;k<=8;k++)
{
InputNormalX[BeginBar-i,k]=InputX[k]/Normà;
TeacherBuffer[BeginBar-i]=0.0;
if (iCustom(NULL,0,"Kaufman2",1,i)>0) TeacherBuffer[BeginBar-i]=1;
if (iCustom(NULL,0,"Kaufman2",2,i)>0) TeacherBuffer[BeginBar-i]=-1;
//TeacherBuffer[BeginBar-i]=iCustom(NULL,0,"KaufmanTrend",0,BeginBar);
}
}
//------------------------------------------
// çàïîëíåíèå âûáîðêè âõîäíûõ ñèãíàëîâ
//------------------------------------------
MathSrand(LocalTime());
//------------------------------------------
// çàïîëíåíèå âåñîâ íåéðîíîâ ñëó÷àéíûìè âåëè÷èíàìè
//------------------------------------------
for (i=0;i<9;i++)
{
for (k=0;k<9;k++)
{
FirstLayWeights[i,k]=NormalRand(MaxRandom);
// Print("FirstLayWeights["+i+","+k+"]=",FirstLayWeights[i,k]);
}
OutputSecondWeights[i]=NormalRand(MaxRandom);
}
//------------------------------------------
// çàïîëíåíèå âåñîâ íåéðîíîâ ñëó÷àéíûìè âåëè÷èíàìè
//------------------------------------------
PrintWeights();
// Print("Sigma=",Sigma(NormalRand()));
Print("Èíèöèàëèçàöèÿ çàêîí÷åíà");
//------------------------------------------
// îáó÷åíèå
//------------------------------------------
for (p=1;p<=StudyNumber;p++) // öèêë îáó÷åíèÿ - p-íîìåð îáó÷åíèÿ
{
for (n=0;n<100;n++) // ïðîõîä ïî âûáîðêå âõîäíûõ ñèãíàëîâ - n-íîìåð îáó÷àþùåãî ñèãíàëà
{
for (int z=0;z<9;z++) InputX[z]=InputNormalX[n,z]; // çàïîëíèì âõîäíîé âåêòîð èç ìàññèâà ïîäãîòîâëåííûõ íîðìàëèçîâàííûõ âåêòîðîâ
GetFirstLayOutput();
U_Second=GetArgument();
Print("p=",p," n=",n," U_Second=",U_Second);
PrintWeights();
SigmaCurr=Sigma(Betta*U_Second);
Differ=Betta*(1-SigmaCurr*SigmaCurr); // ïðîèçâîäíàÿ îò òàíãåíñà ãèïåðáîëè÷åñêîãî
deviation=TeacherBuffer[BeginBar-n]-SigmaCurr; //îøèáêà ïîëó÷åíà, òåïåðü íàäî ïîäêîððåêòèðîâàòü âåñà
SetGradientWeights();
ChangeWeigts();
}// ïðîõîä ïî âûáîðêå âõîäíûõ ñèãíàëîâ
}// öèêë îáó÷åíèÿ
//------------------------------------------
// îáó÷åíèå
//------------------------------------------
PrintWeights();
Comment("Îáó÷åíèå çàêîí÷åíî");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,m;
int counted_bars=IndicatorCounted();
if (counted_bars<0) return(-1);
int limit;
//limit=Bars-10-counted_bars;
limit=300;
for (i=limit;i>=0;i--)
{
if (iCustom(NULL,0,"Kaufman2",1,i)>0) TeacherBuffer[i]=1;
if (iCustom(NULL,0,"Kaufman2",2,i)>0) TeacherBuffer[i]=-1;
Normà=0.0;
for (m=0;m<9;m++)
{
InputX[m]=Close[i]-Close[i+m+1];
Normà=Normà+InputX[m]*InputX[m];
}
Normà=MathSqrt(Normà);
Print("Normà=",Normà);
for (m=0;m<9;m++)
{
InputX[m]=InputX[m]/Normà;
}
GetFirstLayOutput();
U_Second=GetArgument();
Print("U_Second=",U_Second);
NeuroExitBuffer[i]=Sigma(Betta*U_Second);
//Print("NeuroExitBuffer["+i+"]=",NeuroExitBuffer[i]);
ErrorBuffer[i]=TeacherBuffer[i]-NeuroExitBuffer[i];
}
//---- TODO: add your code here
//----
return(0);
}
//+------------------------------------------------------------------+
Comments