MT5 Quote History

easy direct connection to any MT4 and MT5 server

MT5 Quote History

MT5 protocol have just 2 type of requests to get quote history:

  1. DownloadQuoteHistoryToday(RequestQuoteHistoryToday) that returns OHLC data for last 24 hours.
  2. DownloadQuoteHistoryMonth(RequestQuoteHistoryMonth) that returns OHLC data for specified month.

Internally MT5 has just one timeframe - M1. If you need to convert from M1 to other please use api.ConvertToTimeframe function.

If you need to get history for several month please use this example.

private void QuoteHistory()
{
    MT5API api = new MT5API(101240050, "Ava112358", "3.10.134.148", 443);
    api.Connect();
    var bars = GetQuoteHistory(api, "EURUSD", "2022-01-01T00:00:00", "2022-03-01T00:00:00", 240);
    foreach (var item in bars)
        Console.WriteLine(item.Time);
    Console.WriteLine("Press any key to quit...");
    Console.ReadKey();
}

public List<Bar> GetQuoteHistory(MT5API api, string symbol = "EURUSD", string from = "2022-01-01T00:00:00", 
    string to = "2022-03-01T00:00:00", int timeFrame = 240)
{
    DateTime start = DateTime.Parse(from);
    DateTime end = DateTime.Parse(to);
    var res = new SortedDictionary<DateTime, Bar>();
    while (end > start)
    {
        var bars = api.DownloadQuoteHistoryMonth(symbol, end.Year, end.Month, end.Day, timeFrame);
        foreach (var item in bars)
            if (item.Time >= start && item.Time <= end)
                if(!res.ContainsKey(item.Time))
                    res.Add(item.Time, item);
        end = end.AddMonths(-1);
    }
    return res.Values.ToList();
}

Leave a Reply