MQL5 CopyRates()
Copies historical bars of a symbol and timeframe into an array of MqlRates structures, by start position and count, by start time and count, or between two times.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
This is an MQL5 function, not an HTTP endpoint. It has no method, no path and no address to send a request to: it runs inside the MetaTrader 5 terminal, in a program written in MQL5.
The page shows three forms of the call, which differ in how the range is given. An argument marked required here is required in the forms that have it.
Signature
The 3 forms of the call on the vendor's page, with the vendor's comments taken out.
int CopyRates( string symbol_name, ENUM_TIMEFRAMES timeframe, int start_pos, int count, MqlRates rates_array[] )
int CopyRates( string symbol_name, ENUM_TIMEFRAMES timeframe, datetime start_time, int count, MqlRates rates_array[] )
int CopyRates( string symbol_name, ENUM_TIMEFRAMES timeframe, datetime start_time, datetime stop_time, MqlRates rates_array[] )
Arguments
| Name | Type | Description |
|---|---|---|
| symbol_namerequired | string | The symbol's name. |
| timeframerequired | ENUM_TIMEFRAMES | The bar size, as a value of the ENUM_TIMEFRAMES enumeration. |
| start_posrequired | int | In the first form: the position of the first bar to copy. The page says positions count from the present into the past, so 0 is the current bar. |
| countrequired | int | In the first and second forms: how many bars to copy. |
| rates_arrayrequired | MqlRates[] | The array of MqlRates that receives the bars. The page recommends a dynamic array where the amount of data is not known in advance. |
| start_timerequired | datetime | In the second and third forms: the time of the first bar to copy. |
| stop_timerequired | datetime | In the third form: the time of the last bar to copy. |
Return value
- Type
- int
- What comes back
- The number of bars copied, or -1 on an error. The page says it is also -1 when the whole of the requested range lies outside the data the server holds, or beyond the terminal's maximum bars in a chart.
Sample
Written by this site from the signature and arguments recorded on this page. It has not been run against a terminal, a server or an account, and the values in it are placeholders. It holds no credential and needs none: the program runs inside a terminal that is already logged in to an account. Nothing is entered on this site.
void OnStart()
{
MqlRates rates[];
ArraySetAsSeries(rates,true);
int copied=CopyRates(_Symbol,Period(),0,24,rates);
if(copied<0)
{
PrintFormat("CopyRates error %d",GetLastError());
return;
}
for(int i=0; i<copied; i++)
PrintFormat("%s open %.5f close %.5f",TimeToString(rates[i].time),rates[i].open,rates[i].close);
}