How do i get some values from a bar at the end of each bar?

 

I want to make a script that gets the value for 50EMA, 100EMA and the current price for each bar.(e.g. M30)

So i have made this code to pick up the values:

double x55EMA = iMA(NULL, NULL,55,0,MODE_EMA,PRICE_CLOSE,0);
double x100EMA = iMA(NULL, NULL,100,0,MODE_EMA,PRICE_CLOSE,0);
double Price = Bid;

But i don't know how to get into some kind a loop so i get the values for each bar.

Can anyone help me with that?

Thank you in advance.

 

The values of iMA() function should be stored in an Array. An Array is like a Table of Contents. If you want to know about the Contents, you have to reference the page#. In your example, you are asking for page 0 i.e,0);. The 0 bar is the current bar which have Not closed yet.

 

If you want the value of the last closed bar then use,1);. If you're trying to loop through bars and get values then use something like this.

int i; /* Bars= the total number of bars on chart. */
for(int i=1; i<=Bars; i++)
{
   Alert(Close[i-1]);
   double x55EMA = iMA(NULL, 0,55,0,MODE_EMA,PRICE_CLOSE, i );
   Alert(x55EMA);
}
 

Thanks ubzen,

Let me get this a try .

 

Ohh isn't this wrong?

int i; /* Bars= the total number of bars on chart. */
for(int i=1; i<=Bars; i++)

should be either:

int i; /* Bars= the total number of bars on chart. */
for(i=1; i<=Bars; i++)

OR

/* Bars= the total number of bars on chart. */
for(int i=1; i<=Bars; i++)

 
Yea you're correct. i already defined by int i; no need to define it again. :)
 

I works fine.

Thanks!

I have another problem but have to create a new question.

 
How can I modify that to apply high-open to each element in array and select index with max difference?
 
Mike Smith:
How can I modify that to apply high-open to each element in array and select index with max difference?

Do not double/triple post.

I have deleted your duplicate posts.

 
Mike Smith:
How can I modify that to apply high-open to each element in array and select index with max difference?

I assume that means high minus open?

This may give you some ideas.

Not tested.

  int barsToCheck = 10;
  int firstBarIndex=1;
  
  double array[];
  ArrayResize(array,barsToCheck);
  
  for(int x=0;x<barsToCheck;x++)
    array[x]=High[x+firstBarIndex]-Open[x+firstBarIndex];
  
  int highestIndex=ArrayMaximum(array);
  
  Print ("Index with highest value=",highestIndex,". Chart bar index=",highestIndex+firstBarIndex,". Value is ",DoubleToString(array[highestIndex],Digits));
Reason: