Try using OnTimer.
Other than that, you can use CopyRates().
Try using OnTimer.
Other than that, you can use CopyRates().
Thanks for your answer.
Coul you please explain further your statement about OnInit? A link with more details would be great too. I did not know about it.
Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Thanks for the details.
Now the problem is how to us OnTimer only once to retrieve the data I need.
In fact, I want to do this operation only once when the EA starts running.
Any suggestion?
void OnTick() { static bool boolInitialise = true; if( boolInitialise ) { // Do something only once the first time OnTick is called boolInitialise = false; // Cancel future execution of this block of code in future calls to OnTick } }
It was my idea :)
Many thanks for your interest, Fernando.
I changed the code as you suggested:
void OnTick() { static bool boolInitialise = true; if( boolInitialise ) { string FILE_NAME = ChartSymbol() + "_Period_" + IntegerToString(Period()) + "_Windows_" + IntegerToString(Window) + ".txt"; int file_handle=FileOpen(FILE_NAME,FILE_WRITE); if(file_handle<0) { Print("Failed to open the file by the absolute path "); Print("Error code ",GetLastError()); } FileWriteString(file_handle, "#Open,Close,High,Low,Volume,Time" + "\n"); string candleValues; for(int i = 1; i < Window ; i++ ){ candleValues = StringFormat("%g,%g,%g,%g,%g,%s", iOpen(_Symbol,PERIOD_CURRENT,i), iClose(_Symbol,PERIOD_CURRENT,i), iHigh(_Symbol,PERIOD_CURRENT,i), iLow(_Symbol,PERIOD_CURRENT,i), iVolume(_Symbol,PERIOD_CURRENT,i), TimeToStr(iTime(_Symbol,PERIOD_CURRENT,i))); FileWriteString(file_handle,candleValues + "\n"); } FileClose(file_handle); boolInitialise = false; } }
I still obtain "holes" in the retrieved data.
For example, yesterday at 22:38 circa I asked for the past 10k candlestick values for M1 GBPUSD.
I have this "hole" approximately after 8.2k correct data:
1.41546,1.41565,1.41566,1.41545,17,2021.05.25 00:13 1.41561,1.41548,1.41564,1.41548,17,2021.05.25 00:12 1.41566,1.41564,1.41566,1.41555,28,2021.05.25 00:11 1.41556,1.41565,1.41573,1.41546,31,2021.05.25 00:10 1.41565,1.41557,1.41568,1.41553,21,2021.05.25 00:09 1.29275,1.2928,1.29283,1.29271,18,2020.10.29 20:57 1.29275,1.29276,1.29281,1.29269,26,2020.10.29 20:56 1.29274,1.29277,1.29282,1.29274,13,2020.10.29 20:55 1.29265,1.29275,1.29278,1.29265,17,2020.10.29 20:54 1.29256,1.29264,1.29269,1.29255,18,2020.10.29 20:53 1.29267,1.29255,1.29267,1.29255,19,2020.10.29 20:52 1.29261,1.29269,1.29271,1.29261,13,2020.10.29 20:51 1.29246,1.2926,1.29261,1.29245,14,2020.10.29 20:50
Retrieved data switch from
2021.05.25 00:09
to
2020.10.29 20:57
I do not get why.
edit: The problem for which I opened this discussion is solved. I will open another discussion to address the other problem. Thanks again.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
In the OnInit of my EA, I use this loop to retrive previous candlestick's open, close, high, low, volume and time values:
in which Window is an extern int i could set when I attach the EA.
I do not know why, it may happen that the EA misses some candlestick. This usually happens when "Window" is a great number (order of 5000).
When this happens, I have missing data in my output file.
I usually parse the output data with Pandas, and retrieve a list of missing dates in the format "yyyy.mm.dd hh:mi".
Is it possible to retrieve the values for a past candlestick using this date, instead of the index "i" as in the code I posted?
If it was not possible, do you have any hint on how to tru to retrieve these missing candlestick's values? I am thinking about calculating the offset between the present date and the date of the candlestick I want to retrieve... but I would prefer a simpler way.