# MQL5 CopyRates()

Independent and unofficial. Not affiliated with or endorsed by any broker or platform named here. Educational only: nothing here is advice, and nothing here recommends a broker or a trade.

This is the markdown version of https://openbrokerapi.com/platforms/metatrader-5/functions/mql5-copyrates, written from the same records as that page.

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.

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.

- Function: `CopyRates()`
- Interface: MQL5
- Group: Bars and ticks
- Status: verified. Source: [mql5.com](https://www.mql5.com/en/docs/series/copyrates), checked 2026-10-12.

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. It is in no OpenAPI file on this site and the request builder does not list it.

Note: 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.

```mql5
int CopyRates(
  string symbol_name,
  ENUM_TIMEFRAMES timeframe,
  int start_pos,
  int count,
  MqlRates rates_array[]
)
```

```mql5
int CopyRates(
  string symbol_name,
  ENUM_TIMEFRAMES timeframe,
  datetime start_time,
  int count,
  MqlRates rates_array[]
)
```

```mql5
int CopyRates(
  string symbol_name,
  ENUM_TIMEFRAMES timeframe,
  datetime start_time,
  datetime stop_time,
  MqlRates rates_array[]
)
```

## Arguments

- `symbol_name` (type string, required): The symbol's name.
- `timeframe` (type ENUM_TIMEFRAMES, required): The bar size, as a value of the ENUM_TIMEFRAMES enumeration.
- `start_pos` (type int, required): 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.
- `count` (type int, required): In the first and second forms: how many bars to copy.
- `rates_array` (type MqlRates[], required): 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_time` (type datetime, required): In the second and third forms: the time of the first bar to copy.
- `stop_time` (type datetime, required): 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.

```mql5
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);
  }
```

## Related MQL5 functions: Bars and ticks

- [CopyTicks()](https://openbrokerapi.com/platforms/metatrader-5/functions/mql5-copyticks): Copies ticks of a symbol into an array of MqlTick structures, from a time onwards or the most recent ones. The oldest tick copied is at index 0.
- [CopyTicksRange()](https://openbrokerapi.com/platforms/metatrader-5/functions/mql5-copyticksrange): Copies the ticks of a symbol between two times into an array of MqlTick structures. The oldest tick copied is at index 0.

## More on MetaTrader 5

- [All MetaTrader 5 functions](https://openbrokerapi.com/platforms/metatrader-5/functions): 78 documented here, grouped by what they do.
- [Historical candles: the call at each API](https://openbrokerapi.com/endpoints/candles): The same job at every broker and platform API recorded here, HTTP or not.
- [MetaTrader 5 API quickstart](https://openbrokerapi.com/platforms/metatrader-5/quickstart)
- [All MetaTrader 5 error codes](https://openbrokerapi.com/errors/mt5)
- [MetaTrader 5 API page](https://openbrokerapi.com/platforms/metatrader-5): The full record: access, authentication, limits and the source for each.

## Licence and how to cite

The records behind this page are all rights reserved. This file is the page itself, at its own address, in another format. It is not part of the CC BY 4.0 downloads listed at https://openbrokerapi.com/data. Quote from it with a link to the page.

Cite as: MQL5 CopyRates(). Broker API Hub, https://openbrokerapi.com/platforms/metatrader-5/functions/mql5-copyrates, newest check 2026-10-12.

Where you rely on a single figure, cite the source and the checked date printed beside it as well. Where this page and a broker's own documentation differ, the documentation wins.
