Using another curreny's data in EA

 

Hi everybody!


Is that possible to use the data of another currency in an expert advisor? For exaple I would like to create an expert advisor which I would use for the EURUSD, but I need data for the prediction from anothor currencys? Ergo I wouldn't use the Close[i] prices of EURUSD but for examle the USDJPY Close[i].



Thanks in advance,

Roley 

 

roley05: Is that possible to use the data of another currency in an expert advisor? For exaple I would like to create an expert advisor which I would use for the EURUSD, but I need data for the prediction from anothor currencys? Ergo I wouldn't use the Close[i] prices of EURUSD but for examle the USDJPY Close[i].

Yes, you can do that by using the iClose() function instead to get data from other Symbols or Time-frames. However, you have to properly handle 4066 and 4073 errors when there are delays in collecting that data.

Forum on trading, automated trading systems and testing trading strategies

Anyone know how to develop a multicurrency indicator ?

Fernando Carreiro, 2016.04.21 04:46

In a standard indicator, you would build the buffer arrays with data from the parameters sent via the OnCalulate( ... ) event function. But, for multi-currency and/or multi-time-frame, you will have to use one of two solutions:

  • Using the old method of the "iFunction" variations, such as iTime(), iVolume, iOpen, iClose, etc. but with a different symbol such as "EURUSD", "JPYUSD", etc. instead of the default _Symbol or Symbol().
  • Using the newer method of the first variant of ArrayCopyRates() function together with array pointers of MqlRates.  The "copied" arrays, will not actually take up any space and will just be pointers to the existing data of the various symbols and time-frames.

However, for this to work, one of two conditions have to exist for the multi-symbol and/or multi-time-frame to work:

  • Either the respective charts for the symbols and time-frames are already open, so that no error is generated,
  • or you will get an error 4066 (ERR_HISTORY_WILL_UPDATED) the first time you request the data, and you will have to code a sleep & retry loop, in order to wait for data to download and then request the data again.

My personal suggested solution, which I find as the most efficient as well as easiest way to handle the 4066 error, is the ArrayCopyRates() and MqlRates method.

There is more information about this the MQL4 documentation and help files.

PS! NB! When accessing built-in indicator functions, such as iMA(), iATR(), etc. for the various symbols and time-frames, remember to also implement the sleep and retry loops so as not to get the 4066 error either. Here is a quote from the MQL4 documentaion:

Forum on trading, automated trading systems and testing trading strategies

Anyone know how to develop a multicurrency indicator ?

Fernando Carreiro, 2016.04.24 19:57

I have re-created the "clean slate" conditions and coded a small script to test this in a continuous loop with no sleep function. I ran the script twice against a "EURUSD" chart, but had the code request data on "NZDCAD" which was never loaded before.

The results on the first run are as follows.

  • During the first 73607 iterations (that lasted approximately 516ms), the “ArrayCopyRates” always returned a count of -1, and generated an 4073 error (Error 4073 - No history data). The array size was always 0 during these iterations.
  • Finally on the 73608th iteration, it returned a count of 512 and the array size was set to 512 as well and the time for the 0th element had a valid time (and not 0) and no errors were generated.
I then did a second run of the Script, since the data was already loaded and these were the results:
  • During the first and only iteration, the “ArrayCopyRates” returned a count of 2048 but generated an 4066 error even though the return count was valid (Error 4066 - Requested history data is in updating state). The Array size was also 2048 and the time for the 0th element was valid (and not 0).
I have attached a ZIP file with the test script as well as the resulting log files.

A leave you to draw your own conclusions!

PS! My conclusion, is to combine all the tests before proceeding. (i.e. test the return code of  “ArrayCopyRates”, check the error codes, test the array size of the MqlRates array and (as a safe guard as WHRoeder suggests) also test the validity of the date of the array elements.

EDIT: Had I solely used the "pair[0].time == 0" test, it would of given an array index error (and caused execution to halt) during the first iterations of the first run when no data was available. Therefore, I stand by my statement, that one needs to check the return code and array size, but agree that the time test can offer a subsequent level of verification once the other conditions have been met.


iClose - Timeseries and Indicators Access - MQL4 Reference
iClose - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
Close price value for the bar of specified symbol with timeframe and shift. If local history is empty (not loaded), function returns 0. To check errors, one has to call the GetLastError() function.
 
On MT4: Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors.
          Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
 

Thanks for the comments!


Does this method work in backtesing, when the strategy tester is used?

 
Yes, but never assume indexes are the same, always use iBarShift.
 
roley: Does this method work in backtesing, when the strategy tester is used?

Yes and no!

You can only access bar data for other currencies, not tick data. So you cannot access current prices in the middle of a bar - only the fixed Open, High, Low, Close and Tick Volume of bars; but as @whroeder1 warned, you will have to use iBarShift() to synchronize the information.

Also, you cannot place multi-symbol orders. Only orders for the current symbol being tested.

 

Hi!


Thanks for the comment. What is not clear yet that how can I reindex the array to match all bars to the same time period.

For example if I count the two arrays of EURUSD and GBPUSD with the iBarShift function and the first one has 210 bars and the second one has 208 bars for the same period, how can I put togehter the appropriate bars?

 
roley: how can I put togehter the appropriate bars?

You already know the answer, iBarShift.

 
whroeder1:

You already know the answer, iBarShift.


more specifically???

 
roley: more specifically???

Specifically? For any index of the first pair, use iBarshift to get the index for the second one. How much more specific can I get?

 
roley: more specifically???

Please take the time to read the documentation on the subject, namely the iBarShift() function in this case.

If the function in question takes mainly the "time" as a parameter, then obviously you use the "time" of a specific bar in one symbol, to obtain the corresponding bar index of the other symbol.

iBarShift - Timeseries and Indicators Access - MQL4 Reference
iBarShift - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
Index of the bar which covers the specified time. If there is no bar for the specified time (history "gap"), the function will return -1 or the nearest bar index (depending on