Download MetaTrader 5

iCustom returns 2147483647

To add comments, please log in or register
Chee Chua
661
Chee Chua  


#property indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Silver


//--- input_1 parameters
extern bool      Export=false;

//--- buffers
double ExtMapBuffer1[];

int iHandle = -1;
int iErrorCode;

// begin Encog main config
                        string EXPORT_FILENAME = "mt4.csv";
                        int _neuronCount = 103;
                        int _layerCount = 3;
                        int _contextTargetOffset[] = {0,0,0};
                        int _contextTargetSize[] = {0,0,0};
                        bool _hasContext = false;
                        int _inputCount = 40;
                        int _layerContextCount[] = {0,0,0};
                        int _layerCounts[] = {1,61,41};
                        int _layerFeedCounts[] = {1,60,40};
                        int _layerIndex[] = {0,1,62};
                        double _layerOutput[] = {0,0,1};
                        double _layerSums[] = {0,0,0};
                        int _outputCount = 1;
                        int _weightIndex[] = {0,61,2521};
                        double _weights[] = {0.2938570914,-0.069337041,0.3257095745};
                        int _activation[] = {1,1,0};
                        double _p[] = {1,1,1};
// end Encog main config


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  IndicatorBuffers(1);
  SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
  
   IndicatorShortName("Encog Generated Indicator" );
   SetIndexLabel(0,"Line1");
  
      if( Export )
      {
         iHandle = FileOpen(EXPORT_FILENAME,FILE_CSV|FILE_WRITE,',');
         if(iHandle < 1)
         {
            iErrorCode = GetLastError();
            Print("Error updating file: ",iErrorCode);
            return(false);
         }
      
FileWrite(iHandle,"time","close","stoch_k","stoch_d","williams_r","prediction");
      }
      else
      {
         iHandle = -1;
      }

   return(0);
  }

void ActivationTANH(double& x[], int start, int size)
{
   for (int i = start; i < start + size; i++)
   {
      x[i] = 2.0 / (1.0 + MathExp(-2.0 * x[i])) - 1.0;
   }
}
                
void ActivationSigmoid(double& x[], int start, int size)
{
   for (int i = start; i < start + size; i++)
   {
      x[i] = 1.0/(1.0 + MathExp(-1*x[i]));
   }
}
                
void ActivationElliottSymmetric(double& x[], int start, int size)
{
   for (int i = start; i < start + size; i++)
   {
      double s = _p[0];
      x[i] = (x[i] * s) / (1 + MathAbs(x[i] * s));
   }
}
                
void ActivationElliott(double& x[], int start, int size)
{
   for (int i = start; i < start + size; i++)
   {
      double s = _p[0];
      x[i] = ((x[i]*s)/2)/(1 + MathAbs(x[i]*s)) + 0.5;
   }
}

void ComputeLayer(int currentLayer)
{
   int x,y;
   int inputIndex = _layerIndex[currentLayer];
   int outputIndex = _layerIndex[currentLayer - 1];
   int inputSize = _layerCounts[currentLayer];
   int outputSize = _layerFeedCounts[currentLayer - 1];

   int index = _weightIndex[currentLayer - 1];

   int limitX = outputIndex + outputSize;
   int limitY = inputIndex + inputSize;

   // weight values
   for (x = outputIndex; x < limitX; x++)
   {
      double sum = 0;
      for (y = inputIndex; y < limitY; y++)
      {
         sum += _weights[index] *_layerOutput[y];
         index++;  
      }
      
      _layerOutput[x] = sum;
      _layerSums[x] = sum;
   }
      

   switch(_activation[currentLayer - 1] )
   {
      case 0: // linear
         break;
      case 1:
         ActivationTANH(_layerOutput, outputIndex, outputSize);
                        break;
      case 2:
         ActivationSigmoid(_layerOutput, outputIndex, outputSize);
         break;
      case 3:
         ActivationElliottSymmetric(_layerOutput, outputIndex, outputSize);
         break;
      case 4:
         ActivationElliott(_layerOutput, outputIndex, outputSize);
                break;
   }
                        
   // update context values
   int offset = _contextTargetOffset[currentLayer];

   for (x = 0; x < _contextTargetSize[currentLayer]; x++)
   {
      _layerOutput[offset + x] = _layerOutput[outputIndex + x];
   }
}

                
void Compute(double &input_1[], double &output[])
{
   int i,x;
   int sourceIndex = _neuronCount
      - _layerCounts[_layerCount - 1];

   ArrayCopy(_layerOutput,input_1,sourceIndex,0,_inputCount);
                        
   for(i = _layerCount - 1; i > 0; i--)
        {
          ComputeLayer(i);
   }

   // update context values
        int offset = _contextTargetOffset[0];

   for(x = 0; x < _contextTargetSize[0]; x++)
        {
          _layerOutput[offset + x] = _layerOutput[x];
   }

   ArrayCopy(output,_layerOutput,0,0,_outputCount);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
      if( iHandle>0 )
      {
         FileClose(iHandle);
      }
  
//----
   return(0);
  }
  
  string PadInt(int num, int digits)
{
   string result = num;
   while( StringLen(result)<digits )
   {
      result="0" + result;
   }
   return (result);
}

void WriteExportLine(int pos)
{
   datetime dt = Time[pos];
   string when =
      PadInt(TimeYear(dt),4) +
      PadInt(TimeMonth(dt),2) +
      PadInt(TimeDay(dt),2) +
      PadInt(TimeHour(dt),2) +
      PadInt(TimeMinute(dt),2) +
      PadInt(TimeSeconds(dt),2);
                        FileWrite(iHandle, when,
                        Close[pos],
                        iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos),
                        iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos),
                        iWPR(Symbol(), Period(), 21, 0)
                        );
}

double Norm(double x,double normalizedHigh, double normalizedLow, double dataHigh, double dataLow)
{
        return (((x - dataLow)
                / (dataHigh - dataLow))
                * (normalizedHigh - normalizedLow) + normalizedLow);
}
                
double DeNorm(double x,double normalizedHigh, double normalizedLow, double dataHigh, double dataLow) {
        return (((dataLow - dataHigh) * x - normalizedHigh
                * dataLow + dataHigh * normalizedLow)
                / (normalizedLow - normalizedHigh));
}
  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int countedBars = IndicatorCounted();
   //---- check for possible errors
   if (countedBars<0) return(-1);
   //---- last counted bar will be recounted
   if (countedBars>0) countedBars--;
  
   int pos=Bars-countedBars-1;
  
   static datetime Close_Time;
//  ArraySetAsSeries(ExtMapBuffer1,false);
  
   // only do this on a new bar
   if ( Close_Time != Time[0])
   {
      Close_Time = Time[0];
      while(pos>1)
      {
                if( _inputCount>0 && Bars>=9 )
                {
                        double input_1[40];
                        double output[1];
                        input_1[0]=Norm(Close[pos+0],1.0,-1.0,1.15926,1.03555);
                        input_1[1]=Norm(Close[pos+1],1.0,-1.0,1.15926,1.03555);
                        input_1[2]=Norm(Close[pos+2],1.0,-1.0,1.15926,1.03555);
                        input_1[3]=Norm(Close[pos+3],1.0,-1.0,1.15926,1.03555);
                        input_1[4]=Norm(Close[pos+4],1.0,-1.0,1.15926,1.03555);
                        input_1[5]=Norm(Close[pos+5],1.0,-1.0,1.15926,1.03555);
                        input_1[6]=Norm(Close[pos+6],1.0,-1.0,1.15926,1.03555);
                        input_1[7]=Norm(Close[pos+7],1.0,-1.0,1.15926,1.03555);
                        input_1[8]=Norm(Close[pos+8],1.0,-1.0,1.15926,1.03555);
                        input_1[9]=Norm(Close[pos+9],1.0,-1.0,1.15926,1.03555);
                        input_1[10]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+0),1.0,-1.0,96.55705996,0.0);
                        input_1[11]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+1),1.0,-1.0,96.55705996,0.0);
                        input_1[12]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+2),1.0,-1.0,96.55705996,0.0);
                        input_1[13]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+3),1.0,-1.0,96.55705996,0.0);
                        input_1[14]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+4),1.0,-1.0,96.55705996,0.0);
                        input_1[15]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+5),1.0,-1.0,96.55705996,0.0);
                        input_1[16]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+6),1.0,-1.0,96.55705996,0.0);
                        input_1[17]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+7),1.0,-1.0,96.55705996,0.0);
                        input_1[18]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+8),1.0,-1.0,96.55705996,0.0);
                        input_1[19]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 0, pos+9),1.0,-1.0,96.55705996,0.0);
                        input_1[20]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+0),1.0,-1.0,93.56165112,0.0);
                        input_1[21]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+1),1.0,-1.0,93.56165112,0.0);
                        input_1[22]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+2),1.0,-1.0,93.56165112,0.0);
                        input_1[23]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+3),1.0,-1.0,93.56165112,0.0);
                        input_1[24]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+4),1.0,-1.0,93.56165112,0.0);
                        input_1[25]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+5),1.0,-1.0,93.56165112,0.0);
                        input_1[26]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+6),1.0,-1.0,93.56165112,0.0);
                        input_1[27]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+7),1.0,-1.0,93.56165112,0.0);
                        input_1[28]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+8),1.0,-1.0,93.56165112,0.0);
                        input_1[29]=Norm(iStochastic(Symbol(), Period(), 8, 5, 5, MODE_EMA, 0, 1, pos+9),1.0,-1.0,93.56165112,0.0);
                        input_1[30]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[31]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[32]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[33]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[34]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[35]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[36]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[37]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[38]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        input_1[39]=Norm(iWPR(Symbol(), Period(), 21, 0),1.0,-1.0,0.0,-11.39430285);
                        Compute(input_1,output);
                        ExtMapBuffer1[pos] = DeNorm(output[0],1.0,-1.0,430.0,-113.0);
                }
         if( Export )
         {
            WriteExportLine(pos);
         }
         pos--;
      }
   }
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+

What's wrong with this indicator?

Unable to get the value of iCustom.

Returning  2147483647 ????

Keith Watford
Moderator
8293
Keith Watford  
jon:What's wrong with this indicator?

Unable to get the value of iCustom.

Returning  2147483647 ????

That's equivalent to EMPTY_VALUE
Chee Chua
661
Chee Chua  
Keith Watford:
That's equivalent to EMPTY_VALUE

double EncogExample1 = iCustom(Symbol_1, 60, "EncogExample_Trained", false, 0, 0);

The indicator draws correctly. There is a value, but I could only get the EMPTY_VALUE.

What's wrong or missing in the indicator codes? 

 

Keith Watford
Moderator
8293
Keith Watford  

From your image it appears that the indicator does not draw a line on bar[0] and [1]

so

double EncogExample1 = iCustom(Symbol_1, 60, "EncogExample_Trained", false, 0, 0);


that could be why you are getting EMPTY_VALUE

Check values for earlier bars.

Also make sure that you are calling the correct buffer index.

DanielRpo
14
DanielRpo  

I was having the same problem on my EA. I solved it with a minimun bars before start the calculation of the EA.


void OnTick()

  { 

     if(Bars<100)

     {

      Print("Less than 100 bars"); //number of bars according the minimum bars of the indicator that you are using, the 100 is only for the example, you need to use a variable with the number of periods for the indicator

      return;  

     }

//..... Your logic calling the indicator

}

Eleni Anna Branou
Moderator
4846
Eleni Anna Branou  

Use the SRC button to post your code.


To add comments, please log in or register