Timeseries variables in Expert Advisors

 

Hi,

I'm having a problem using series variables in an Expert Advisor. I can't seem to work it out. I know how to do this in an indicator, but things seem to be different in Expert Advisors.

If I do something simple like this

double myVal[];
 

int start()
{

   myVal[0] = 100;
   
   Print(Time[0] + " , ", myVal[1]);
   

   return(0);      
}

Then myVal[1] is always '0'.

How do I get the value of a variable one bar ago, in an Expert Advisor?

Thanks

 

Your array . . .

double myVal[];

has zero elements . . .

Do this . . .

double myVal[100];

or this . .

double myVal[];

ArrayResize(myVal, 100);

and read the documentation . . ArrayFunctions and the Book

 

Thank you. But it doesn't work - the reference to myVal[1] is still '0'.

Just to repeat - it all works fine in an indicator, but as soon as I use this in an Expert Advisor, I can't get anything except '0'.

 
bubblegum:

Thank you. But it doesn't work - the reference to myVal[1] is still '0'.

Just to repeat - it all works fine in an indicator, but as soon as I use this in an Expert Advisor, I can't get anything except '0'.

myVal[0] = 100;
   
   Print(Time[0] + " , ", myVal[1]);

You don't assign a value to myVal[1] . . so what else would it be other than Zero ?

What exactly are you trying to do ?

 

What I'm actually trying to do is calculate ADX in an Expert Advisor (not using the nonsense iADX function). I have a custom ADX calculation working in an Indicator, but I can't get it to work in an Expert Advisor.

This is the code I'm using in an indicator

//+------------------------------------------------------------------+
//|                                                 Wilder's DMI.mq4 |
//|                                                  coded by mladen |
//|                                                                  |
//| Directional movement index was developed by Welles Wilder        |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_separate_window
#property  indicator_buffers 5
#property indicator_minimum  0
#property indicator_color1   Lime
#property indicator_color2   Tomato
#property indicator_color3   DimGray
#property indicator_color4   Orange
#property indicator_color5   DimGray
#property indicator_width3   2 
#property indicator_width4   2 
#property indicator_style1   STYLE_DOT
#property indicator_style2   STYLE_DOT
#property indicator_style5   STYLE_DOT

//
//
//
//
//

extern int  DMI.Length =    14;
extern bool ShowADX    =  true;
extern bool ShowADXR   = false;
extern int  Level      =    20;

//
//
//
//
//

double DIp[];
double DIm[];
double ADX[];
double ADXR[];
double ADXLevel[];
double averageDIp[];
double averageDIm[];
double averageTR[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int init()
{
   IndicatorBuffers(8);
   SetIndexBuffer(0,DIp);      SetIndexLabel(0,"DI+");
   SetIndexBuffer(1,DIm);      SetIndexLabel(1,"DI-");
   SetIndexBuffer(2,ADX);      SetIndexLabel(2,"ADX");
   SetIndexBuffer(3,ADXR);     SetIndexLabel(3,"ADXR");
   SetIndexBuffer(4,ADXLevel); SetIndexLabel(4,NULL);
   SetIndexBuffer(5,averageDIp);
   SetIndexBuffer(6,averageDIm);
   SetIndexBuffer(7,averageTR);
      for (int i=0;i<8;i++) SetIndexEmptyValue(i,0.00);

   //
   //
   //
   //
   //

   IndicatorShortName("Wilder\'s DMI ("+DMI.Length+")");
   return(0);
}

int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           limit=MathMin(limit,Bars-2);

   //
   //
   //
   //
   //
   
   double sf = (DMI.Length-1.0)/DMI.Length;
      for (i=limit;i>=0;i--)
      {
         double currTR  = MathMax(High[i],Close[i+1])-MathMin(Low[i],Close[i+1]);
         double DeltaHi = High[i] - High[i+1];
              double DeltaLo = Low[i+1] - Low[i];
         double plusDM  = 0.00;
         double minusDM = 0.00;
         
            if ((DeltaHi > DeltaLo) && (DeltaHi > 0)) plusDM  = DeltaHi;
            if ((DeltaLo > DeltaHi) && (DeltaLo > 0)) minusDM = DeltaLo;      
         
         //
         //
         //
         //
         //
         
            averageDIp[i] = sf*averageDIp[i+1] + plusDM;
            averageDIm[i] = sf*averageDIm[i+1] + minusDM;
            averageTR[i]  = sf*averageTR[i+1]  + currTR;
            ADXLevel[i]   = Level;

         //
         //
         //
         //
         //
                  
            DIp[i] = 0.00;                   
            DIm[i] = 0.00;                   
            if (averageTR[i] > 0)
               {              
                  DIp[i] = 100.00 * averageDIp[i]/averageTR[i];
                  DIm[i] = 100.00 * averageDIm[i]/averageTR[i];
               }            

            if(ShowADX)
               {
                  double DX;
                  if((DIp[i] + DIm[i])>0) 
                       DX = 100*MathAbs(DIp[i] - DIm[i])/(DIp[i] + DIm[i]); 
                  else DX = 0.00;
                  ADX[i] = sf*ADX[i+1] + DX/DMI.Length; 
                  if(ShowADXR)
                         ADXR[i] = 0.5*(ADX[i] + ADX[i+DMI.Length]);
               }
      }   
   return(0);      
}

But I can't get it to calculate ADX correctly (it's always '0') in an Expert Advisor (obviously I've removed all the non-indicator stuff).

Thanks

 
bubblegum:

What I'm actually trying to do is calculate ADX in an Expert Advisor (not using the nonsense iADX function). I have a custom ADX calculation working in an Indicator, but I can't get it to work in an Expert Advisor.

This is the code I'm using in an indicator

But I can't get it to calculate ADX correctly (it's always '0') in an Expert Advisor (obviously I've removed all the non-indicator stuff).

Thanks

You are probably going to end up using the nonsense iADX function . . let me explain why . . .

You can't use Indicator buffers in an EA, you can't use indicator functions in an EA such as IndicatorCounted() so in your EA you have to code this stuff yourself . . . you have to handle the arrays and make them behave like buffers . . . there is a very good article that explains why and how to do this . . . but it's a little involved . . .

Transferring an Indicator Code into an Expert Advisor Code. Indicator Structure - MQL4 Articles

Alternatively . . why don't you just use iCustom to get the values you need from the Indicator that you already have ?

 

Thank you!

At least I know it's not just me going nuts. I'll have a look at the iCustom function.

 
bubblegum:

Thank you!

At least I know it's not just me going nuts. I'll have a look at the iCustom function.

Have a read of this: Detailed explanation of iCustom - MQL4 forum
 

Thank you very much for your help. It's all working fine now. Cheers. :)

Reason: