fann4mt_proto_prediction_RSI-RVI-STOCH

Author: Julien Loutre
Indicators Used
Relative strength indexRelative Vigor indexStochastic oscillator
Miscellaneous
Implements a curve of type %1
0 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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---