Download MetaTrader 5

My indicator disappear anytime I change to new time-frame chart

To add comments, please log in or register
Looking for like-minded people? You will find lots of them on the Forum!
Harry
267
Harry 2014.05.23 00:16 

Hello,

 I start with an indicator (The FIRST PART). It works fine, when changing time frame.

Then I add the Second Part which is: If Close[i]-Close[i+1] is less than the median of the last 100 bar difference value, I will assign value 0 to all buffer.

Now the combined indicator works fine in 1 chart but when I change time frame, that indicator disappear. I have to reattach it to the chart.

 

I think the problem is caused by Array but I don't know how to fix it.

 

Can anyone help me with the error?

Thank you so much.

SCFX

//+------------------------------------------------------------------+
//|                                                          hf1.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              httakeprofit://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "httakeprofit://www.mql5.com"
#property version   "1.00"
#property strict

//--- indicator settings
//#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_width1 2  
#property indicator_width2 2 
#property indicator_width3 1 
#property indicator_width4 1 
#property indicator_width5 1 

#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Orange
#property indicator_color5 Orange

//--- input parameter
input int kperiod=30;
input int dperiod=9;
input int slow=9;
input int sample_roc_quartile=100;

//--- buffers
double buy[];
double sell[];
double stoploss[];
double takeprofit1[];
double takeprofit2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void) //*/*/*/*/*/
  {
//--- indicator buffers mapping
//--- 1 additional buffer used for counting.
   //IndicatorBuffers(6);
   IndicatorDigits(Digits);
//--- indicator line
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,buy);
   SetIndexArrow(0,241);
   
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(1,sell);
   SetIndexArrow(1,242);

   SetIndexStyle(2,DRAW_ARROW);
   SetIndexBuffer(2,stoploss);

   SetIndexStyle(3,DRAW_ARROW);
   SetIndexBuffer(3,takeprofit2); 
         
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexBuffer(4,takeprofit1);  
   
  
//--- name for DataWindow and indicator subwindow label

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
      int limit=rates_total-prev_calculated;
      
      double roc_s[];
      ArrayResize(roc_s,sample_roc_quartile+1);

//---

//------------------------------------------ 

for(int i=0; i<limit; i++)
{  

//FIRST PSART 

   if(iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i)>iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i)   &&   iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i+1)<iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i+1))   
      {  buy[i]=Close[i]; 
         stoploss[i]=Low[i]-0.005; 
         takeprofit1[i]=Close[i]+0.005;
         takeprofit2[i]=Close[i]+0.01;
         }
      
   if(iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i)<iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i)   &&   iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i+1)>iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i+1))   
      {  sell[i]=Close[i]; 
         stoploss[i]=High[i]; 
         takeprofit1[i]=Close[i]-0.0050;
         takeprofit2[i]=Close[i]-0.01;
         }   
         
//SECOND PART         
   for(int j=0;j<=sample_roc_quartile;j++)
   {
      roc_s[j]=Close[i+j]-Close[i+j+1];
      ArraySort(roc_s);
      
      if(   (Close[i]-Close[i+1])<roc_s[50]  )
         {  buy[i]=0; 
            stoploss[i]=0; 
            takeprofit1[i]=0;
            takeprofit2[i]=0;
         }
    }     
}
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
9603
Keith Watford 2014.05.23 01:02  

Possibly an array out of range error and the indicator is being removed

int limit=rates_total-prev_calculated;

 

for(int i=0; i<limit; i++)
iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i+1)<iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i+1))

You are possibly getting a message in the log about a global initialisation failure
 Check your logs

Harry
267
Harry 2014.05.23 01:27  

Thank you for your suggestion.

I check log, there is no error or warning notice.

I am not sure the (i+1) cause trouble since standing alone (or simply deleting Second part), this indicator work ok.

SCFX 

Ian Venner
2584
Ian Venner 2014.05.23 02:41  

There are errors I compiled your code and tested it:

2014.05.22 22:39:43.936 Custom indicator Test Indicator 3 XAUUSD,M1: removed
2014.05.22 22:39:43.905 Test Indicator 3 XAUUSD,M1: global initialization failed
2014.05.22 22:39:43.905 Test Indicator 3 XAUUSD,M5: uninit reason 3 //----------------------- change chart
2014.05.22 22:39:17.323 array out of range in 'Test Indicator 3.mq4' (117,32)
2014.05.22 22:39:15.591 Test Indicator 3 XAUUSD,M5: initialized
2014.05.22 22:39:09.570 Custom indicator Test Indicator 3 XAUUSD,M5: loaded successfully
 

Keith Watford
Moderator
9603
Keith Watford 2014.05.23 05:45  
scfx:

Thank you for your suggestion.

I check log, there is no error or warning notice.

I am not sure the (i+1) cause trouble since standing alone (or simply deleting Second part), this indicator work ok.

SCFX 


Then it is probably this

roc_s[j]=Close[i+j]-Close[i+j+1];

 You are trying to get the close price of bars that do not exist

ffoorr
1016
ffoorr 2014.05.23 08:22  

You should add a line when the indicator is started first : prev_calculated == 0,

something like that :

 int limit = -100;
   if( prev_calculated == 0 ) limit = 3000;// will calculate 3000 Bars
    else                      limit = rates_total-prev_calculated;
      

 no error on my computer either   ;-)

Harry
267
Harry 2014.05.23 19:50  

Hi ffoorr,

You solution is really good.

To tell you the truth, I hire someone to fix it last night. Yes he did it but the code is inefficient, please see below, as he add one more for(i).

I appreciate it if you could explain what it means with that prev_calculated ==0. I don't get it.

 

//+------------------------------------------------------------------+
//|                                                          hf1.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              httakeprofit://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "httakeprofit://www.mql5.com"
#property version   "1.00"
#property strict

//--- indicator settings
//#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_width1 2  
#property indicator_width2 2 
#property indicator_width3 1 
#property indicator_width4 1 
#property indicator_width5 1 

#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Orange
#property indicator_color5 Orange

//--- input parameter
input int kperiod=30;
input int dperiod=9;
input int slow=9;
input int sample_roc_quartile=100;

//--- buffers
double buy[];
double sell[];
double stoploss[];
double takeprofit1[];
double takeprofit2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void) //*/*/*/*/*/
  {
//--- indicator buffers mapping
//--- 1 additional buffer used for counting.
   //IndicatorBuffers(6);
   IndicatorDigits(Digits);
//--- indicator line
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,buy);
   SetIndexArrow(0,241);
   
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexBuffer(1,sell);
   SetIndexArrow(1,242);

   SetIndexStyle(2,DRAW_ARROW);
   SetIndexBuffer(2,stoploss);

   SetIndexStyle(3,DRAW_ARROW);
   SetIndexBuffer(3,takeprofit2); 
         
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexBuffer(4,takeprofit1);  
   
  
//--- name for DataWindow and indicator subwindow label

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
      int limit=rates_total-prev_calculated;
          if(limit<=0)limit =2;
     
      double roc_s[];
      ArrayResize(roc_s,sample_roc_quartile+1);

//---

//------------------------------------------ 

for(int i=1; i<limit; i++)
{  

//FIRST PSART 

   if(iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i)>iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i)   
   &&   iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i+1)<iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i+1))   
      {  buy[i]=Close[i]; 
         stoploss[i]=Low[i]-0.005; 
         takeprofit1[i]=Close[i]+0.005;
         takeprofit2[i]=Close[i]+0.01;
         }
      
   if(iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i)<iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i)   
   &&   iStochastic(NULL,0,kperiod,dperiod,slow,0,0,0,i+1)>iStochastic(NULL,0,kperiod,dperiod,slow,0,0,1,i+1))   
      {  sell[i]=Close[i]; 
         stoploss[i]=High[i]; 
         takeprofit1[i]=Close[i]-0.0050;
         takeprofit2[i]=Close[i]-0.01;
         }   
} 

    
//SECOND PART   

//--------------------------------------- HERE IS THE WHAT HE DID
int mid = (sample_roc_quartile +1)/2;
  
for(int i=1; i<limit&&(i+1)<rates_total; i++)
{    

//---------------------------------------------


  for(int j=0;j<=sample_roc_quartile&&(i+j+1)<rates_total;j++)
   {
      roc_s[j]=Close[i+j]-Close[i+j+1];
      ArraySort(roc_s);
   }   
      
      if( (Close[i]-Close[i+1])<roc_s[mid]  )
         {  buy[i]=0; 
            stoploss[i]=0; 
            takeprofit1[i]=0;
            takeprofit2[i]=0;
         }
    
 }    

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
douglas heng
20
douglas heng 2014.05.24 16:18  

Hi everyone...


I am having problem installing the IBFX MACD traditional indicator and other templates into my MT4.

Previous version, I would only need to copy and paste my files int the "indicator" folder located in the "expert" folder.

Current one has only "MQL4" folder which consist of a saperate "expert" and "indicator" folder.

I have tried pasting in both folders and restarting my platform but it just doesnt work

noting happen.

Can anyone out there please advise me on this issue? or perhaps any other ways for me to get the traditional MACD indicator with 2 EMA lines?

Thanks

Douglas
Ian Venner
2584
Ian Venner 2014.05.24 17:02  
douglas beng you need to stop putting your question on everyone elses topics when it is not related to their topic. START YOUR OWN TOPIC.
whroeder1
14470
whroeder1 2014.05.25 14:47  
douglas_heng: I am having problem installing the IBFX MACD traditional indicator and other templates into my MT4.
Don't Don't multiple (6) posts. Don't hijack other peoples post.
ffoorr
1016
ffoorr 2014.05.25 19:03  
scfx:

Hi ffoorr,

You solution is really good.

To tell you the truth, I hire someone to fix it last night. Yes he did it but the code is inefficient, please see below, as he add one more for(i).

I appreciate it if you could explain what it means with that prev_calculated ==0. I don't get it.

 


well scfx, I don't understand the second part of your indicator, so I can't tell.

When an indicator is started at the first time, (put on a chart) previous_calculation is egal to Zero by construction, so one can fix the starting point from which the indicator will start calculating . 

if( prev_calculated == 0)  limit  = 100;

or

if( prev_calculated < 50)  limit  = 500:

or say

if( prev_calculated < 500)  limit  = 3000: (all the same, the beginning of the chart is no used)

int limit = -100;
   if( prev_calculated == 0 ) limit = 3000;// will calculate 3000 Bars
    else                      limit = rates_total-prev_calculated;

at the first lauch of the indicator limit = 3000;

for(int i=1; i<3000; i++) // draw 3000 bars 

at the second lauch limit = 0; // ( because rates_total-prev_calculated = 0)  ;
for(int i=1; i<0; i++)    //   draw the last bars only, faster

very good idea this indi  ......

123
To add comments, please log in or register