NLD_BPChart

Author: 2019 © NELODI.com
0 Views
0 Downloads
0 Favorites
NLD_BPChart
ÿþ//+-------------------------------------------+

//|                           NLD_BPChart.mq5 |

//|                     http://www.nelodi.com |

//|             Copyright (c) 2019 NELODI.com |

//+-------------------------------------------+

#property copyright   "2019 © NELODI.com"

#property link        "http://www.nelodi.com"

#property description "NELODI - BackProp Chart"



#property strict



#include "NLD_BackProp.mqh"



// Set OUTPUT_NEURONS, HIDDEN_NEURONS and INPUT_NEURONS in the "NLD_BackProp.mqh" file:

// * OUTPUT_NEURONS sets the number of Open prices, needed from History data (default=200)

// * INPUT_NEURONS sets the number of Close prices, projected into the Future (default=10)

// * HIDDEN_NEURONS is the number of Neurons in the Hidden Layer (default=100)



#define iForward OUTPUT_NEURONS

#define iBackward INPUT_NEURONS



// Extend the current OHLC prices by "iShift" bars,

// to make them stand out from the rest (better visibility)

#define iShift 3



//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_plots   3



#property indicator_type1   DRAW_FILLING

#property indicator_type2   DRAW_FILLING

#property indicator_type3   DRAW_FILLING



#property indicator_color1  C'128,0,0',C'128,0,0'

#property indicator_color2  C'0,128,0',C'0,128,0'

#property indicator_color3  C'64,0,0',C'0,64,0'



#property indicator_label1  "H"

#property indicator_label2  "L"

#property indicator_label3  "OC"



//--- indicator buffers

double ExtHighBuffer[];

double ExtTopBuffer[];

double ExtBotBuffer[];

double ExtLowBuffer[];

double ExtOpenBuffer[];

double ExtCloseBuffer[];



int lastPeriod=PERIOD_CURRENT;

bool trained=false;



//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtTopBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtBotBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(4,ExtOpenBuffer,INDICATOR_DATA);

   SetIndexBuffer(5,ExtCloseBuffer,INDICATOR_DATA);



   PlotIndexSetInteger(0,PLOT_SHIFT,iForward+iShift);

   PlotIndexSetInteger(1,PLOT_SHIFT,iForward+iShift);

   PlotIndexSetInteger(2,PLOT_SHIFT,iForward+iShift);



   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);



   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);



//--- Neural Network initialization

   trained=false;

   bpInit();



//--- initialization done

  }

//+------------------------------------------------------------------+

//| Calculation                                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   if(rates_total<iForward+iShift) return(0);



   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(spread,true);

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(tick_volume,true);



   ArraySetAsSeries(ExtHighBuffer,true);

   ArraySetAsSeries(ExtTopBuffer,true);

   ArraySetAsSeries(ExtBotBuffer,true);

   ArraySetAsSeries(ExtLowBuffer,true);

   ArraySetAsSeries(ExtOpenBuffer,true);

   ArraySetAsSeries(ExtCloseBuffer,true);



   int ps=PeriodSeconds();



   double o_h=EMPTY_VALUE,o_l=EMPTY_VALUE,o_o=EMPTY_VALUE,o_c=EMPTY_VALUE;



   bool done=true;



   int nowPeriod=Period();



   double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);

   double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);

   double scaleIn=MathPow(10,_Digits-1)/INPUT_NEURONS;

   double scaleOut=MathPow(10,_Digits-1)/OUTPUT_NEURONS;



   for(int i=iForward+iShift,k=0; i<rates_total && !IsStopped(); i++,k++)

     {

      o_h=ExtHighBuffer[i];  ExtHighBuffer[i]=high[k];

      o_l=ExtLowBuffer[i];   ExtLowBuffer[i]=low[k];

      o_o=ExtOpenBuffer[i];  ExtOpenBuffer[i]=open[k];

      o_c=ExtCloseBuffer[i]; ExtCloseBuffer[i]=close[k];



      ExtTopBuffer[i]=close[k];

      ExtBotBuffer[i]=close[k];



      // No re-training or re-painting

      if(nowPeriod==lastPeriod &&

         o_h==ExtHighBuffer[i] &&

         o_l==ExtLowBuffer[i] &&

         o_o==ExtOpenBuffer[i] &&

         o_c==ExtCloseBuffer[i])

         break;



      // Neural Network training using History data (completed candles) ...

      if((k>0) && (k<rates_total-iBackward-iForward))

        {

         double op=open[k+iForward]; // Baseline (zero) = current bar Open price

         for(int j=0; j<iBackward; j++) FInputLayer[j]=(open[k+iForward+j]-op)*scaleIn;

         for(int j=0; j<iForward; j++)  FTargetLayer[j]=(close[k+iForward-j]-op)*scaleOut;

         bpTrain();

         trained=true;

        }

     }



   // Set shifted candles to last OHLC prices ...

   for(int j=0; j<iForward+iShift; j++)

     {

      ExtHighBuffer[j]=high[0];

      ExtLowBuffer[j]=low[0];

      ExtOpenBuffer[j]=open[0];

      ExtCloseBuffer[j]=close[0];

      ExtTopBuffer[j]=close[0];

      ExtBotBuffer[j]=close[0];

     }



   if(trained)

     {

      // Calculate Neural Network outputs for most recent inputs ...

      double cx=0,op=open[0];

      for(int j=0; j<iBackward; j++) FInputLayer[j]=(open[j]-op)*scaleIn;

      bpApply();

      

      for(int j=0; j<iForward; j++)

        {

         // Get the Close price from Neural Network ...

         cx=FOutputLayer[iForward-j-1]/scaleOut+op;



         ExtOpenBuffer[j]=op;

         ExtHighBuffer[j]=MathMax(op,cx);

         ExtLowBuffer[j]=MathMin(op,cx);



         ExtCloseBuffer[j]=cx;

         ExtTopBuffer[j]=cx;

         ExtBotBuffer[j]=cx;

        }

     }



   lastPeriod=nowPeriod;



//--- done

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments