NeuroProba_v1

Download script
Author: Copyright � 2005, MetaQuotes Software Corp.
NeuroProba_v1

This script functions as an intelligent technical analysis tool that utilizes a "Neural Network" logic to interpret market trends. Instead of relying on simple static formulas, it attempts to "learn" from historical data to predict market direction more accurately.

Here is a breakdown of how the logic works in plain language:

1. The Learning Phase (Training)

When you first load the tool, it doesn't immediately start making predictions. Instead, it performs an internal "study" session based on settings you provide (like the number of cycles and the specific window of historical bars to look at).

  • Data Collection: It looks at past price changes over a specific period.
  • The Teacher: The script uses an external signal (specifically looking for a "Kaufman" trend indicator) as its "Teacher." This teacher provides the "correct answers"—telling the system whether the market went up or down in the past.
  • Trial and Error: The internal neural network takes the historical data, makes a guess at what the movement was, and compares that guess to the "Teacher's" correct answer.
  • Adjustment: If the guess is wrong, the script calculates the difference (the error) and automatically adjusts its internal "weights" (its logic parameters). It repeats this hundreds of times until it has refined its mathematical model to better mimic the successful trends found in the past.

2. The Prediction Phase (Live Execution)

Once the training is complete, the script moves into live mode. For every new candle that appears on your chart:

  • Normalization: It takes the most recent price movements and converts them into a standardized format. This ensures that large price swings don't overwhelm the logic compared to small ones; it focuses on the pattern of the move rather than just the raw magnitude.
  • Processing: The data passes through the "layers" of the neural network it refined during the training phase.
  • Output: It produces a smoothed value that represents its current "belief" about the market direction.

3. Visual Feedback

The script provides three distinct visual lines on your chart to help you see what is happening:

  • Teacher Signal: This shows you what the underlying trend indicator is currently saying.
  • Answer (Neuro Output): This is the neural network's interpretation of the market. Because it has been "trained," this line aims to provide a refined, processed version of the trend.
  • Error Buffer: This shows you exactly how far off the neural network is from the teacher's signal at any given moment. A smaller error generally indicates that the network is successfully capturing the trend logic.

Summary for the User

In short, this tool doesn't just follow a trend; it learns what a successful trend looks like by studying a specific window of history and then tries to replicate that success in real-time. It acts as a sophisticated "filter" that attempts to interpret market movement through a self-adjusting mathematical model.

Miscellaneous
Implements a curve of type %1
5 Views
0 Downloads
0 Favorites
NeuroProba_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                   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