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()
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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
Thank you very much.
Marco.