my first custom indicator crashes my terminal... please help

 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkGoldenrod
//---- indicator parameters
extern int MA_Period=20;
extern int ATR_Period=10;
//---- buffers
double FFRI[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,FFRI);
IndicatorShortName("FFRI");
SetIndexLabel(0,"FFRI");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=MA_Period) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=MA_Period;i++) FFRI[Bars-i]=0.0;
//----
i=Bars-MA_Period-1;
if(counted_bars>=MA_Period) i=Bars-counted_bars-1;
while(i>=0)
{
FFRI[i]=((Close[i])-iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i))/iATR(NULL,0,ATR_Period,i);
}
return(0);
}

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



this is my first attempt at programming anything, so please be patient with my ignorance.

the purpose of this indicator is to display a how many ATRs price is away from a MA...

when i attach the indicator to a chart my terminal just shuts down.

could someone please show me where the problem lies.

lol explain it to me like you are trying to teach a toddler MQL4 :)

Thanks in Advance

Landon

 
LanBar:
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkGoldenrod
//---- indicator parameters
extern int MA_Period=20;
extern int ATR_Period=10;
//---- buffers
double FFRI[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,FFRI);
IndicatorShortName("FFRI");
SetIndexLabel(0,"FFRI");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=MA_Period) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=MA_Period;i++) FFRI[Bars-i]=0.0; 
//----
i=Bars-MA_Period-1;
if(counted_bars>=MA_Period) i=Bars-counted_bars-1;
while(i>=0)
{
FFRI[i]=((Close[i])-iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i))/iATR(NULL,0,ATR_Period,i);
} 
return(0);
}

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



this is my first attempt at programming anything, so please be patient with my ignorance.

the purpose of this indicator is to display a how many ATRs price is away from a MA...

when i attach the indicator to a chart my terminal just shuts down.

could someone please show me where the problem lies.

lol explain it to me like you are trying to teach a toddler MQL4 :)

Thanks in Advance

Landon



while(i>=0)
{
FFRI[i]=((Close[i])-iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i))/iATR(NULL,0,ATR_Period,i);
}

Don't you forget i--in this loop ?

 
thanks Matutin, i added the end of the loop but my terminal still crashes, any ideas?
 
LanBar:
thanks Matutin, i added the end of the loop but my terminal still crashes, any ideas?


Put some // to determine which line cause the crash

May be this one too complex ? FFRI[i]=((Close[i])-iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i))/iATR(NULL,0,ATR_Period,i);

try :

double Ma01 = iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i));

double Atr01 = iATR(NULL,0,ATR_Period,i);

double Close01 = Close[i]) ;

FFRI[i]= ( ( Close01 - Ma01 ) / Atr01);

 
Matutin:


Put some // to determine which line cause the crash

May be this one too complex ? FFRI[i]=((Close[i])-iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i))/iATR(NULL,0,ATR_Period,i);

try :

double Ma01 = iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i));

double Atr01 = iATR(NULL,0,ATR_Period,i);

double Close01 = Close[i]) ;

FFRI[i]= ( ( Close01 - Ma01 ) / Atr01);



Hello Again Matutin,

i like the way you wrote that, it is a lot simpler. but unfortunately my MT4 still crashes . i have several EAs running and other custom indicators so i don't think its my machine. i have to tell ya i have read over several hundred posts, the MQL4 Book and Coder Guru's tutorial and i am still stumped....

 
#property copyright "LanBar"
#property link "landonbarnard@gmail.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkGoldenrod
//---- indicator parameters
extern int MA_Period=20;
extern int ATR_Period=10;
//---- buffers
double FFRI[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,FFRI);
IndicatorShortName("FFRI");
SetIndexLabel(0,"FFRI");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
//----
if(Bars<=MA_Period) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=MA_Period;i++) FFRI[Bars-i]=0.0;
//----
i=Bars-MA_Period-1;
if(counted_bars>=MA_Period) i=Bars-counted_bars-1;
while(i>=0)
{
double Ma01 = iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i);
double Atr01 = iATR(NULL,0,ATR_Period,i);
double Close01 = (Close[i]);
FFRI[i]= ( ( Close01 - Ma01 ) / Atr01);
}
i--;
return(0);
}
//+------------------------------------------------------------------+
 
This is not tested. Try: 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkGoldenrod

extern int MA_Period=20;
extern int ATR_Period=10;

double FFRI[];

int init()
{
SetIndexBuffer(0,FFRI);
SetIndexStyle(0,DRAW_LINE);
IndicatorShortName("FFRI");
SetIndexLabel(0,"FFRI");
return(0);
}

int start()
{
 int i,limit,counted_bars=IndicatorCounted();
 if(counted_bars<0) return(0);
 if(counted_bars>0) counted_bars--;
 limit=Bars-1-counted_bars;

   for(i=limit;i>=0;i--) 
      FFRI[i]=((Close[i])-iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i))/iATR(NULL,0,ATR_Period,i);

 return(0);
}
good luck. ~ cameo.
 
cameofx:
This is not tested. Try: good luck. ~ cameo.


Hey Cameo thanks for the assistance!

my terminal did not crash this time :)

now i am not getting any results in the indicator window except on the last bar.

hey cameo can you explain to me the difference in your code verse mine please?

 
//+------------------------------------------------------------------+
//| FFRI.mq4 |
//| LanBar |
//| landonbarnard@gmail.com |
//+------------------------------------------------------------------+
#property copyright "LanBar"
#property link "landonbarnard@gmail.com"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DarkGoldenrod
//---- indicator parameters
extern int MA_Period=20;
extern int ATR_Period=10;
//---- buffers
double FFRI[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexBuffer(0,FFRI);
SetIndexStyle(0,DRAW_LINE);
IndicatorShortName("FFRI");
SetIndexLabel(0,"FFRI");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,limit,counted_bars=IndicatorCounted();
if(counted_bars<0) return(0);
if(counted_bars>0) counted_bars--;
limit=Bars-1-counted_bars;

for(i=limit;i>=0;i--)
double Ma01=iMA(NULL,0,MA_Period,0,MODE_SMA,PRICE_CLOSE,i);
double Atr01=iATR(NULL,0,ATR_Period,i);
double Close01=(Close[i]);
FFRI[i]=((Close01-Ma01)/Atr01);


return(0);

}

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


here is the code that i have so far it dose not display any lines in the indicator window.... X-(

 
LanBar:

now i am not getting any results in the indicator window except on the last bar. 

 You're welcome. Is that good or bad... Did it redraw ok? I don't have MT handy. 

hey cameo can you explain to me the difference in your code verse mine please? 

For starters you have a while loop inside a for loop without any need of it. The more concise the code the better or else you'll risk introducing error on a thicker haystack, so to speak. You need to figure out loop to have solid coding. Play around with these (put in start() as a script): 

for(int i = 10; i>=0; i--) Alert("i =", i,"; Close[i] = ", Close[i]); 
// now change it to incrementing i++; and try using i>0 instead of i>=0;
while(int i > 5) { Alert("i =", i,"; Close[i] = ", Close[i]); i--; if(i==0) break; } 
// now change it to incrementing; and try it with the if statement ommited; 
// also try changing the constant (5, 10 above) to extern variables & change it on attaching script to chart

And use the standard counted_bars, limit routine for your start(). You may also want to look at other loop such as switch & experiment with it. See reference on it. Bottom line is you need to know what you're doing to your variables at all time. Start with a working part & make a copy when you introduce new addition or alteration. 

good luck. ~ cameo 

 
LanBar:   

here is the code that i have so far it dose not display any lines in the indicator window.... X-(

Try replacing the line inside the for loop with Close[i] only. Or iMa(....) only. That should draw a line. And start building from that. See if you can plot (High[i]+Low[i])/2. And please use the SRC button to paste your code. Good coding Landon. And let me know how it goes.