Download MetaTrader 5

iClose function not timeshifting properly

To add comments, please log in or register
the_photon
7
the_photon  
Sirs,

In connection with another problem, I am trying to write a piece of MQL4 code in an EA that gets the closing values of the last eight hours and loads them into an array.
After one hour, the code reloads the array.  However, printing out the array values indicates that the values in the array are still the old values from last time.

The following code executes once per hour, at 5 minutes past the hour:

class Agent:
         double stackArray[8];
        // pseudocode, insert default Agent constructor here


        // comment:  this function is called every hour, at 5 minutes past the hour.
        void my_function()
       {
                this.stackArray[0] = iClose("EURUSD",PERIOD_H1,1);
                this.stackArray[1] = iClose("EURUSD",PERIOD_H1,2);
                this.stackArray[2] = iClose("EURUSD",PERIOD_H1,3);

                // pseudocode . . . and so on and so fourth

                this.stackArray[7] = iClose("EURUSD",PERIOD_H1,8);
        }

        void printArray()
        {
                 // pseudocode: this function prints out the array values.
         }

int onInit()
{
         // pseudocode: declare Agent my_Agent()

         // pseudocode . . . while loop that executes indefinitely
         {
                // pseudocode:  if it is currently 5 minutes past the hour.
                         my_Agent.my_function();
                         my_Agent.printArray();
          }
}


So, every hour at 5 minutes past the hour, my_Agent reads the most recent 8 bars at the one-hour level, loads them into stackArray, and then prints out the values.

After one hour, the EA re-loads the array (which I is what I want).  But the output from the printArray function indicates that it is still the same data in the array from the previous hour.

For example, at 17:05, the values in the stackArray were:
                1.06505   (value at index 0)
                1.06401   (value at index 1)
                ... etc
                1.06205   (value at index 6)
                1.06211   (value at index 7)

The problem comes when at 18:05, the values in the stack array are still the same, even though the array was re-loaded.

Any help on this problem would be greatly appreciated.
Fernando Carreiro
2947
Fernando Carreiro  
the_photon:
Sirs,

In connection with another problem, I am trying to write a piece of MQL4 code in an EA that gets the closing values of the last eight hours and loads them into an array.
After one hour, the code reloads the array.  However, printing out the array values indicates that the values in the array are still the old values from last time.

The following code executes once per hour, at 5 minutes past the hour:

So, every hour at 5 minutes past the hour, my_Agent reads the most recent 8 bars at the one-hour level, loads them into stackArray, and then prints out the values.

After one hour, the EA re-loads the array (which I is what I want).  But the output from the printArray function indicates that it is still the same data in the array from the previous hour.

For example, at 17:05, the values in the stackArray were:
                1.06505   (value at index 0)
                1.06401   (value at index 1)
                ... etc
                1.06205   (value at index 6)
                1.06211   (value at index 7)

The problem comes when at 18:05, the values in the stack array are still the same, even though the array was re-loaded.

Any help on this problem would be greatly appreciated.
You can't do that from the OnInit() Event Handler as that only runs once when the code is iniialised (when the EA is attached to the chart, for example). There is also no garantee that there will be data available, or even a connection when that Init event occurs.

You must update data from the OnTick() Event handler and since you are using muti-time-frame, you must check for 4066 and 4073 error conditions.

Please use the "SRC" button on the posting toolbar to add your source code, Don't just add it as plain text.

Also, consider providing real code and not pseudo code for proper evaluation of problems.
To add comments, please log in or register