1
from datetime import datetime

import MetaTrader5 as mt5

import requests

import datetime as dt  

import numpy as np

import json

from statistics import mean

import pandas as pd

import pytz

import os

import time

import math



#Im living in Singapore and the timing on MT5 is in a different timezone. I managed to find the code below to sync it to UTC

u = datetime.utcnow()

utc_from = u.replace(tzinfo=pytz.utc)

currency_pair = {"EURUSD": 2, "GBPUSD": 2, "USDJPY": 2, "USDCAD": 2, "USDCHF": 2, "NZDUSD": 2, "AUDUSD": 2, "AUDNZD": 2, "AUDCAD": 2}

for currency in currency_pair:

   rates = mt5.copy_rates_from(currency, mt5.TIMEFRAME_M30, utc_from, 500)


   rates_frame = pd.DataFrame(rates)
   rates_frame['close_time'] = rates['time']


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

Hi All

I'm trying to get the historic price as well as the latest pricing, however, the last pricing that I got is different from what is showing on tradingview.

May I know what am I missing?

          time           open     high    low   close   tick_volume spread  real_volume close_time
499 2020-07-06 13:00:00 0.94365 0.94386 0.94247 0.94258 1217          25     0           1594040400

asset is AUDCAD

the pricing on tradingview is 0.945 instead of my result of 0.94258.

there is a difference of 0.945 - 0.942 = 0.003

Andy Quek
  • 51
  • 5
  • Can you add some more context, such as what library/API do you use for trading information and a small example of the problem. – mabergerx Jul 06 '20 at 13:21
  • Hi mabergerx, apologies for the lack of info. I learn python off youtube and not sure if i understand what is library/API. are those stuff that are imported at the top api/library? – Andy Quek Jul 06 '20 at 13:30
  • I see that you added your imports and some example input/output, so now I think we can help you! Thanks – mabergerx Jul 06 '20 at 13:44

1 Answers1

1

Unfortunately, I can't easily reproduce the error due to platform mismatch of the MetaTrader5 package requirements and my system, however, I suspect that it is a timezone issue. You could try to set your whole interpreter to be in the UTC timezone for a run of this script by doing:

os.environ["TZ"] = "UTC"

before you import datetime. Then, the interpeter will think you are in the UTC timezone through the environment variable. Then you can just use any datetime object or functionality within the UTC zone directly, without the need to convert stuff and therefor have a chance to run into some convertion mismatch.

Alternatively, you can try using pytz as suggested in their example for this function:

from datetime import datetime
import MetaTrader5 as mt5
# display data on the MetaTrader 5 package
print("MetaTrader5 package author: ",mt5.__author__)
print("MetaTrader5 package version: ",mt5.__version__)
 
# import the 'pandas' module for displaying data obtained in the tabular form
import pandas as pd
pd.set_option('display.max_columns', 500) # number of columns to be displayed
pd.set_option('display.width', 1500)      # max table width to display
# import pytz module for working with time zone
import pytz
 
# establish connection to MetaTrader 5 terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()
 
# set time zone to UTC
timezone = pytz.timezone("Etc/UTC")
# create 'datetime' object in UTC time zone to avoid the implementation of a local time zone offset
utc_from = datetime(2020, 1, 10, tzinfo=timezone)
# get 10 EURUSD H4 bars starting from 01.10.2020 in UTC time zone
rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_H4, utc_from, 10)
 
# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()
# display each element of obtained data in a new line
print("Display obtained data 'as is'")
for rate in rates:
    print(rate)
 
# create DataFrame out of the obtained data
rates_frame = pd.DataFrame(rates)
# convert time in seconds into the datetime format
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
mabergerx
  • 1,216
  • 7
  • 19
  • Hi mabergerx, thanks for the help. i managed to solved it. the problem is even though i change it to UTC the time is still slower by 3 hours. I just add to manually add in 3 hours and its returning the right pricing already. – Andy Quek Jul 07 '20 at 12:05