fann4mt_proto_prediction_RSI-RVI-STOCH

Download script
Author: Julien Loutre

This tool utilizes an Artificial Neural Network (ANN) to analyze financial market trends by identifying patterns within technical indicators. Instead of relying on traditional static rules, it attempts to "learn" market behavior by processing historical data through a digital architecture modeled after human biological neurons.

Core Logic and Data Processing

The script functions by analyzing three primary technical indicators:

  • RSI (Relative Strength Index)
  • RVI ( Relative Vigor Index)
  • Stochastic Oscillator

To ensure consistency, the script standardizes these different values into a uniform scale of 0 to 100. This allows the neural network to interpret all three inputs as a unified set of data points.

The Learning Process (Training)

When the script is first initialized, it undergoes a "training" phase. During this period, it examines 1,000 historical snapshots of the market. For every snapshot, it looks at the values of the three indicators and compares them against a specific outcome: whether the market moved significantly in a certain direction (a "pip" target) within the subsequent five bars.

  • Signal Generation: The script defines a "success" as a significant price move upward or downward.
  • Optimization: It adjusts its internal connections to minimize errors, aiming for a high level of accuracy in predicting these moves based on the indicator combinations it observes.
  • Persistence: Once the network is trained, it saves its "knowledge" to a local file so that it does not need to repeat the learning process every time you restart the platform.

Live Application and Prediction

Once training is complete, the script shifts into live mode. It continuously feeds current market data (the latest RSI, RVI, and Stochastic values) into the trained neural network.

The network then processes this information to produce a "Predicted Value." This value is displayed on a separate window as a line graph. These predictions represent the network's estimation of market momentum based on the patterns it learned during its initial training phase. By observing these predicted values relative to specific levels (such as the 20 and 80 marks), traders can visualize the software's interpretation of potential market direction.

Indicators Used
Relative strength indexRelative Vigor indexStochastic oscillator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
fann4mt_proto_prediction_RSI-RVI-STOCH
#property copyright "Julien Loutre"
#property link      "http://fann4mt.thetradingtheory.com"

/*
Please read the tutorial and documentation on the fann4mt website:
http://fann4mt.thetradingtheory.com
*/


#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  LightBlue
#property  indicator_width1 1
#property  indicator_maximum 100
#property  indicator_minimum 0
#property  indicator_level1 20
#property  indicator_level2 80

#include <fann4mt_simple.mqh>

double buffer[]; // display buffer

double outputArray[][1];

string annFilename = "c:\\nn\\proto_RSI_RVI_STO.net";


int deinit() {
   nn_destroy_all();
}


int init() {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,buffer);
   SetIndexLabel(0,"Predicted values");
   SetIndexShift(0,0);
   
   int i;
   
   nn_setDebugChannel("msg");
   nn_debug("msg","","***************************************************");
   
   // All our data, input and output, are going to be in the [0;100] range
   nn_set_dataRange(0,100);
   
   // try to load the ANN from a file, or create it if the file is not found
   // 3 inputs, 2 hidden layers of 12 and 8 neurons, and one output.
   int annID = nn_loadOrCreateNetwork(annFilename,3,12,8,1);
   
   // train only if the ANN has just been created.
   if (nn_isLoaded(annID) == 0) {
      // Creating the training set
      double trainingData[][4];
      for (i=0;i<=1000;i++) {
         createData(trainingData,i,true);
      }
      nn_scaleDataRange(trainingData,4); // Scale the dataset from our data range ([0;100]) to the ANN working range ([0;1])
      
      nn_set_max_MSE(0.005);
      nn_set_epoch(1000);
      nn_debug("msg","","Starting the training");
      nn_train(annID, trainingData);
   }
   
   // Creating the test dataset (not including the desired output this time of course)
   double liveData[][3];
   for (i=0;i<=600;i++) {
      createData(liveData,i,false);
   }
   nn_scaleDataRange(liveData,3); // scale the data on the [0;1] range
   
   nn_debug("msg","","Starting the test");
   nn_computeScaledBatch(annID, liveData, outputArray); // Compute the dataset, and de-scale the output values back to our range [0;100]
   
   nn_saveNetwork(annID, annFilename); // Save the ANN to use for next time
   
   return(0);
}

int start() {
   int i;
   
   // display the computed results
   for (i=0;i<array_size_m(outputArray,1);i++) {
      buffer[i] = outputArray[i][0];
      //buffer[i] = threshold(outputArray[i][0],3,97)*100; // <- you can also apply a threshold on the value to make it easier to read
   }
   
   return(0);
}

// add a new set into a dataset
void createData(double& dataset[][], int p, bool includeOutput=false) {
   int pips = 100;
   int i=p;
   double buffer[];
   if (includeOutput) {
      i=p+5;
   }
   double rsi = iRSI(NULL,0,14,PRICE_OPEN,i);
   double rvi = iRVI(NULL, 0, 14,MODE_MAIN,i);
   double sto = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,i);
   
   rsi = cap(rsi,0,100); // keep the value between 0 and 100
   rvi = nn_map(rvi,-100,100,0,100); // convert the [-100;100] range to [0;100]
   sto = cap(sto,0,100); // keep the value between 0 and 100
   
   cArray_init(buffer);
   
   cArray_push(buffer,rsi);
   cArray_push(buffer,rvi);
   cArray_push(buffer,sto);
   
   if (includeOutput) {
      double max=-20000;
      double min=20000;
      double tmpdiff;
      double signal = 50;
      // Search if in the next 5 bars the market will move more than "pips" pips
      for (int j=p;j<=i;j++) {
         tmpdiff = (Open[j]-Open[i])/Point; // in pips
         if (tmpdiff>max) max = tmpdiff;
         if (tmpdiff<min) min = tmpdiff;
      }
      if (max > pips && max>0-min) {
         signal = 100;
      }
      if (min < 0-pips && max<0-min) {
         signal = 0;
      }
      cArray_push(buffer,signal);
   }
   cArray_commit(dataset,buffer);
}

double cap(double value, int min, int max) {
   if (value>max) value=max;
   if (value<min) value=min;
   return (value);
}

int threshold(double value, int bottom, int top) {
   int tmp=0;
   if (value>top) tmp=1;
   if (value<bottom) tmp=-1;
   return (tmp);
}

Comments