MQL5 / MetaTrader5 Terminal - Data Mismatch (using Python "copy_rates_range")

 

Good Day All,

I'm running Windows 8.1

I live in the EST TimeZone (US Eastern Time [ GMT-5 ])

I'm using Python 3.10.3 x64-bit and MetaTrader5

When I pull data using Windows Command Prompt in Python using the MQL5 Module using the below code:

(I've also tried adding the timezone to the datetime function: ' timezone = pytz.timezone("Etc/UTC")' ) - makes no difference

There is a discrepancy in the candlestick data: 

PYTHON (MQL5) METATRADER5 TERMINAL

2024.02.07 17:57

OPEN: 1.07669

HIGH: 1.07710

LOW: 1.07694

CLOSE: 1.07710

2024.02.07 17:57

OPEN: 1.07562 

HIGH: 1.07606

LOW: 1.07560

CLOSE: 1.07587 



import MetaTrader5 as mt
import pandas as pd
import pandas_ta as ta
import numpy as np
from datetime import datetime, timedelta

pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1500)
import pytz

mt.initialize()

login = ***
password = ***
server = 'MetaQuotes-Demo'

mt.login(login,password,server)

gmt = datetime.today() + timedelta(hours=7)
gmtd = datetime.today() - timedelta(minutes=480)

symbol = 'EURUSD'
timeframe = mt.TIMEFRAME_M1             
date_from = gmtd
date_to = datetime.now()
prices = pd.DataFrame(mt.copy_rates_range(symbol,timeframe,date_from,date_to))

prices['time'] = pd.to_datetime(prices['time'], unit = 's')

prices['time'] = prices['time'] + pd.Timedelta(hours=2)

prices.drop(columns=['spread','real_volume'],inplace=True)      

prices.rename(columns={'tick_volume': 'volume'}, inplace=True)

prices = prices.set_index('time')

prices.ta.rsi(length = 10, append = True)

prices.ta.ema(length = 6, append = True)

prices.ta.ema(length = 18, append = True)

prices.ta.ema(length = 50, append = True)

prices.ta.sma(length = 200, append = True)
Files:
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Please search 🔍 before you post. Your question has already been discussed multiple times on the forum.

The data feed supplies the OHLC and tick data quotes in accordance with the broker's time-zone, not the local time-zone.


Forum on trading, automated trading systems and testing trading strategies

Is there a way to know the Broker's Timezone using the Python API for MT5?

Fernando Carreiro, 2023.10.20 21:22

I was under the impression that the Python API offered functions equivalent to MQL5's TimeCurrent and TimeTradeServer, but it seems it does not.

I'm not a Python programmer, so I don't know if there are other workarounds to the issue, but the only thing I can think of at the moment, is to wait for an incomming tick, and then calculate the time difference, using the tick's timestamp and comparing it to the current UTC time.

Forum on trading, automated trading systems and testing trading strategies

Is there a way to know the Broker's Timezone using the Python API for MT5?

Fernando Carreiro, 2023.10.20 21:26

Another option is to monitor the "time" from symbol_info and when it changes, compre it to the UTC for calculating the difference.

This is somewhat similar to the tick timestamp example in the previous post.

Forum on trading, automated trading systems and testing trading strategies

Is there a way to know the Broker's Timezone using the Python API for MT5?

Alain Verleyen, 2023.10.21 03:14

You know your local time.

You know your local timezone, so GMT shift, it's easy to get it with Python.

When you get live ticks data, you get the "live" server time, like Fernando suggested.

From these information it's trivial to calculate the broker server time shift related to GMT. All of that can be done by code, there is no need to hardcode anything.

 
I have edited your post and redacted the account number and password. That is private information, even if it is just a demo account.
 

@Fernando Carreiro - Thank You for the support; and apologies for overseeing those things.

This isn't just a timezone issue (as I mentioned even after adjusting the time and matching the timezone "UTC" or "GMT+2")

There is a mismatch is all tick data.

When I search for the combination of values (open,high,low,close) it doesn't exists in the data.

 
Sioux Gougui #: This isn't just a timezone issue (as I mentioned even after adjusting the time and matching the timezone "UTC" or "GMT+2"). There is a mismatch is all tick data. When I search for the combination of values (open,high,low,close) it doesn't exists in the data.

Firstly, I am not a Python programmer, so I may be misinterpreting your code, but ...

... you are not supposed to be "adjusting" the time ...

prices['time'] = prices['time'] + pd.Timedelta(hours=2)

The date and time reported in the quotes data is the one supplied by the broker and the same one shown in the MetaTrader terminal's charts and Data Window.

So, don't try to adjust it!

 
Fernando Carreiro #:

Firstly, I am not a Python programmer, so I may be misinterpreting your code, but ...

... you are not supposed to be "adjusting" the time ...

The date and time reported in the quotes data is the one supplied by the broker and the same one shown in the MetaTrader terminal's charts and Data Window.

So, don't try to adjust it!

I understand what you're saying - the issue is not a timezone issue though.

Even if I don't adjust the timezone; the data doesn't match.

The tick open,high,low,close data is completely different. The combinations don't exists at any point during the day.

 
Sioux Gougui #:I understand what you're saying - the issue is not a timezone issue though. Even if I don't adjust the timezone; the data doesn't match. The tick open,high,low,close data is completely different. The combinations don't exists at any point during the day.

My apologies, but I have some doubts in accepting your word for it, because you ARE in fact doing time-zone adjustments.

Please present new code for something that carries out no time adjustments at all, and no indicators (just plain rates data). Present its output and comparison with the date presented on the chart in MetaTrader.

For example, return all the H1 OHLC data for the day 2024.02.07 and then compare it to the H1 data on the chart. I am suggesting H1, just to make it more manageable that M1 data (24 vs 1440 bars).

Reason: