Help - How to program nested indicator for an EA?

 
Hi all.
I want to define a variable which is dependent of the same variable one period ago, but I have absolutely no idea how to do that. I already tried it with an array and a loop, but somehow it doesn`t work :(.
So I wanted to ask, if somebody could help me. The code is as follows:

      
      adEMA1[i]=alpha1*admacd_vol*Close[i]+(1-alpha1*admacd_vol)*adEMA1[(i-1)];
      adEMA2[i]=alpha2*admacd_vol*Close[i]+(1-alpha1*admacd_vol)*adEMA2[(i-1)];  
      adjMACD[i]=adEMA1[i]-adEMA2[i];
Explanation: The today adEMA consists of alpha1/2(constant) multiplied admacd_vol(constant) and by the closing price of today + (......) multiplied by the adEMA1 of the last period.
I hope it's clear what I want to do;) Is there a good possibility to calculate in such a scenario the first value of adEMA? Take adEMA1[(i-1)] at the beginning as a constant?
 
Thank you in advance for your help!

Best regards,
paci

 

"I want to define a variable which is dependent of the same variable one period ago, but I have absolutely no idea how to do that. I already tried it with an array and a loop, but somehow it doesn`t work "

"It doesn't work" doesn't give much to go on.

Show your code.

 

Hi phy.

I tried it with the following code:


   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
 
   for(int i=0; i<limit; i++)      
   {
      adEMA1[i]=alpha1*admacd_vol*Close[i]+(1-alpha1*admacd_vol)*adEMA1[(i-1)];
      adEMA2[i]=alpha2*admacd_vol*Close[i]+(1-alpha1*admacd_vol)*adEMA2[(i-1)];  
      adjMACD[i]=adEMA1[i]-adEMA2[i];
   }
   
Print(adjMACD[i]);
But I just receive the value 0, which in my opnion can't be correct, because adEMA1(i) and adEMA2(i) without the lagging adEMA(i-1) & adEMA2(i-1) results in a "normal" value.
This is the only way I think it could work, that's why I asked in the forum.

Still hope for any help.

Best regards
paci


 

Show more code.

 

"without the lagging adEMA(i-1) & adEMA2(i-1) "

Those would be adEMA(i+1) & adEMA2(i+1)

if you are talking about bars that came before the bar specified by "i"

You probably want to run the loop from limit to 0, not from 0 to limit...

 
phy:

"without the lagging adEMA(i-1) & adEMA2(i-1) "

Those would be adEMA(i+1) & adEMA2(i+1)

if you are talking about bars that came before the bar specified by "i"

You probably want to run the loop from limit to 0, not from 0 to limit...

Hi phy.

Thanks for your help!!


this is the whole code for this function:

int adMACD()
{
double adEMA1[];
double adEMA2[];
double adjMACD[];
int EMA1n=12;                                                                       //Periodenanzahl für den ersten EMA
int EMA2m=26;                                                                       //Periodenanzahl für den zweiten EMA
double alpha1=(2/(EMA1n+1));                                                        //Alpha-Parameter 1
double alpha2=(2/(EMA2m+1));                                                        //Alpha-Parameter 2
double admacd_stdev10=iStdDev(NULL,0,10,1,MODE_EMA,PRICE_CLOSE,0);
double admacd_stdev50=iStdDev(NULL,0,50,1,MODE_EMA,PRICE_CLOSE,0);
double admacd_stdev10_1=iStdDev(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1);                //Um 1 Periode adjustiert
double admacd_stdev50_1=iStdDev(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,1);                //Um 1 Periode adjustiert
int limit;
 
if(adMACD==false)
{
double admacd_vol=(admacd_stdev10/admacd_stdev50);    
double admacd_vol_1=(admacd_stdev10_1/admacd_stdev50_1);                            //Um 1 Periode adjustiert
 
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
 
   for(int i=0; i<limit; i++)      
   {
      adEMA1[i]=alpha1*admacd_vol*Close[i]+(1-alpha1*admacd_vol)*adEMA1[(i-1)];
      adEMA2[i]=alpha2*admacd_vol*Close[i]+(1-alpha1*admacd_vol)*adEMA2[(i-1)];  
      adjMACD[i]=adEMA1[i]-adEMA2[i];
   }
   
Print(adjMACD[i]);

By every tick, I would like to have the new value of adjMACD(1).Therefore I have to calculate the value of adEMA1 and adEMA2 every tick. The problem is the variable adEMA1(i-1) and adEMA2(i-1), which should have the value of adEMA of the last period (eg. the last day). I thought I could program this with a loop, but with this method I don't get an accurate value for adjMACD (always 0). That`s why I think it's wrong.

(And sorry for my programming style, I am very new to it and try to learn it)


Best regards & thanks for your help

paci

 

You did not specify a size for your arrays. Without that definition, the results can be unpredicatable.

example:

double adEMA1[];

could be

double adEMA1[100];

if you need to store no more than 100 values

Reason: