Rates for H4 Timeframe is not correctly pulled from MT5 using Python

 

Hi guys,

I have the following code, which retrieves the rates using python from MT5. But the information is not the same as the broker.

In this example I'm using Pepperstone. For timeframes like different than H2,H3,H4,H5,H6,H8 and H12 are correct.

Please see attached image with the difference. They are totally different to TradingView or the Broker data for 4H TIMEFRAME

mt5 = login('------------------','---------------------------','Pepperstone-MT5-Live01','C:/Program Files/Pepperstone MetaTrader 5/terminal64.exe')

# set time zone to UTC

timezone = pytz.timezone("America/Lima")

# create 'datetime' object in UTC time zone to avoid the implementation of a local time zone offset

utc_from = datetime(2024, 5, 1, tzinfo=timezone)

utc_to = datetime(2024, 5, 9, tzinfo=timezone)

rates_frame = mt5.copy_rates_range("XAUUSD", mt5.TIMEFRAME_H4, utc_from, utc_to)

rates_frame = pd.DataFrame(rates_frame)

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

rates_frame.index = rates_frame['time']

fplt.plot(rates_frame,

          type='candle',

          style='charles',

          title="XAUUSD 4H..",

          ylabel='Price ($)',

          ema=(30, 200),

          returnfig=True,

          volume=False)

fplt.show()


Thank you very much.


Marco.

Files:
Screenshot_1.png  205 kb
 
import MetaTrader5 as mt5
import pytz
import pandas as pd
from datetime import datetime
import mplfinance as fplt

# Initialize connection to MT5
if not mt5.initialize(login='------------------', server='Pepperstone-MT5-Live01', password='---------------------------', path='C:/Program Files/Pepperstone MetaTrader 5/terminal64.exe'):
    print("Initialize() failed, error code =", mt5.last_error())
    quit()

# Set time zone to UTC
timezone = pytz.utc

# Create 'datetime' object in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(2024, 5, 1, tzinfo=timezone)
utc_to = datetime(2024, 5, 9, tzinfo=timezone)

# Retrieve rates
rates = mt5.copy_rates_range("XAUUSD", mt5.TIMEFRAME_H4, utc_from, utc_to)

if rates is None:
    print("No rates retrieved, error code =", mt5.last_error())
else:
    rates_frame = pd.DataFrame(rates)
    rates_frame['time'] = pd.to_datetime(rates_frame['time'], unit='s')
    rates_frame.set_index('time', inplace=True)

    # Plotting
    fplt.plot(
        rates_frame,
        type='candle',
        style='charles',
        title="XAUUSD 4H",
        ylabel='Price ($)',
        ema=(30, 200),
        returnfig=True,
        volume=False
    )
    fplt.show()
 
Nguyen An Nguyen #:

Thank yoy Nguyen,

I noticed that the issue is in TradingView. Apparently the XAUUSD (Pepperstone broker selected chart) is totally different.

I selected the OANDA broker chart and it's as our the results in our python programs.

Thank you very much!


Marco.