Trying to capture the current closed price - page 2

 
 ArrayTotal =0.0;
 
   for(int x=0; x<32; x++)
    {
      ArrayTotal+=CloseArray[x];
    }

So what exactly is the purpose?

You run a for loop, 32 in which you add together all close array[x] values to ArrayTotal.

Then you divide ArrayTotal to give average.

You print the result, then, on next tick reset ArrayTotal, and run the calculation again.

Also you are always calling CloseArray[0] in this line.

Print("Buy Last Close = ",NormalizeDouble(CloseArray[0],_Digits));

CopyClose

The function gets into close_array the history data of bar close prices for the selected symbol-period pair in the specified quantity. It should be noted that elements ordering is from present to past, i.e., starting position of 0 means the current bar.

we already noted that bar 0 is always an open bar if you want the close price of bar 0 you will have to wait until it closes, then that close value will be shifted to bar [1] so this is the bar that will give you the most actual closing value.

Print("Buy Last Close = ",NormalizeDouble(CloseArray[1],_Digits));

so you either read from CloseArray[1] or you start copying at 1 so 0 reads bar 1.

CloseHandle=CopyClose("EURUSD", 1, 1, 32, CloseArray);



 

I changed my CopyBuffer command(Thanks) but the values are still static and never changing. Why is it not updating?  The average and the close should change on every tick. Correct?

2015.07.25 18:45:00.852    2015.06.03 09:17:08   Buy Last Close = 1.09843
2015.07.25 18:45:00.852    2015.06.03 09:17:08   Buy Array Average = 1.09858
2015.07.25 18:45:00.852    2015.06.03 09:17:08   --
2015.07.25 18:45:00.842    2015.06.03 09:17:07   --
2015.07.25 18:45:00.842    2015.06.03 09:17:07   Buy Last Close = 1.09843
2015.07.25 18:45:00.842    2015.06.03 09:17:07   Buy Array Average = 1.09858
2015.07.25 18:45:00.842    2015.06.03 09:17:07   --
2015.07.25 18:45:00.842    2015.06.03 09:17:07   Buy Last Close = 1.09843
2015.07.25 18:45:00.842    2015.06.03 09:17:07   Buy Array Average = 1.09858
2015.07.25 18:45:00.842    2015.06.03 09:17:07   --
2015.07.25 18:45:00.842    2015.06.03 09:17:07   --
2015.07.25 18:45:00.842    2015.06.03 09:17:07   Buy Last Close = 1.09843
2015.07.25 18:45:00.842    2015.06.03 09:17:07   Buy Array Average = 1.09858

int OnInit()
  {
 
   CloseHandle=CopyClose("EURUSD", 1, 1, 32, CloseArray);
   if(CloseHandle==INVALID_HANDLE)
      {
       Print(" Was not possible to recieve handle of Close");
       return(INIT_FAILED);
      }     
}



 double ArrayTotal;
 double ArrayAverage;

void OnTick()
{ 
 ArrayTotal =0.0;
 
   for(int x=0; x<32; x++)
    {
      ArrayTotal+=CloseArray[x];
    }
    
    ArrayAverage=ArrayTotal/32;

 double AskBuy = ArrayAverage - .00051;
  double AskSale = ArrayAverage + .00051;
  

 Print("-- ");
 Print("Buy Array Average = ",NormalizeDouble(ArrayAverage,_Digits));
 Print("Buy Last Close = ",NormalizeDouble(CloseArray[0],_Digits));
 Print("-- ");

}
 

Did you set the direction?


Changing the Indexing Direction

Function ArraySetAsSeries() allows changing the method of accessing elements of a dynamic array; the physical order of data storing in the computer memory is not changed at that. This function simply changes the method of addressing array elements, so when copying one array to another using function ArrayCopy(), the contents of the recipient array will not depend on the indexing direction in the source array.

Direction of indexing cannot be changed for statically distributed arrays. Even if an array was passed as a parameter to a function, attempts to change the indexing direction inside this function will bring no effect.

For indicator buffers, like for usual arrays, indexing direction can also be set as backward (like in timeseries), i.e. reference to the zero position in the indicator buffer will mean reference to the last value on the corresponding indicator buffer and this will correspond to the value of the indicator on the latest bar. Still, the physical location of indicator bars will be unchanged.

Receiving Price Data in Indicators

Each custom indicator must necessarily contain the OnCalculate() function, to which price data required for calculating values in indicator buffers are passed. Indexing direction in these passed arrays can be found out using function ArrayGetAsSeries().

Arrays passed to the function reflect price data, i.e. these arrays have the sign of a timeseries and function ArrayIsSeries() will return true when checking these arrays. However, in any case indexing direction should be checked only by function ArrayGetAsSeries().

In order not to be dependent on default values, ArraySetAsSeries() should be unconditionally called for the arrays you are going to work with, and set the required direction.

Receiving Price Data and Indicator Values

Default indexing direction of all arrays in Expert Advisors, indicators and scripts is left-to-right. If necessary, in any mql5 program you can request timeseries values on any symbol and timeframe, as well as values of indicators calculated on any symbol and timeframe.

Use functions Copy...() for these purposes:

  • CopyBuffer – copy values of an indicator buffer to an array of double type;
  • CopyRates – copy price history to an array of structures MqlRates;
  • CopyTime – copy Time values to an array of datetime type;
  • CopyOpen – copy Open values to an array of double type;
  • CopyHigh – copy High values to an array of double type;
  • CopyLow – copy Low values to an array of double type;
  • CopyClose – copy Close values to an array of double type;
  • CopyTickVolume – copy tick volumes to an array of long type;
  • CopyRealVolume – copy equity volumes to a long type array;
  • CopySpread – copy the spread history to an array of int type;

 

All these functions work in a similar way. Let's consider the data obtaining mechanism on the example of CopyBuffer(). It is implied that the indexing direction of requested data is that of timeseries, and the position with index 0 (zero) stores data of the current yet uncompleted bar. In order to get access to  these data we need to copy the necessary volume of data into the recipient array, e.g. into array buffer.

copyBuffer

 
Yes I set the direction using ArraySetAsSeries values are still static.  But even if the direction was not set the average should change on every tick because to the constant update of values correct?
 
flwilliams87:
Yes I set the direction using ArraySetAsSeries values are still static.  But even if the direction was not set the average should change on every tick because to the constant update of values correct?

Your CopyClose() statement should be in OnTick() not in OnInit().

CopyClose() returns the number of copied elements or -1, not an handle. You have to check the returned value.

Your are copying from index 1, so the values will change every new bar only, not every tick.

 
flwilliams87:
OK, Got my array that provides my the most current price close but when I run it I get the same values. I have the copybuffer in my OnInit Section.  And I get the same values for AskBuy and AskSale.  What am I doing wrong?

if(buy) priceClose= priceBID;

else priceClose= priceAsk; 

If you refer to the chart, always  priceClose= priceBID 

Reason: