Ejemplo n.º 1
0
def per_change(symbol, delta):
    """Returns the percentage change in the price of asset in the given 
    time-delta from the latest price in the terminal."""

    mt5.symbol_select(symbol)

    # latest tick data.
    now_tick = mt5.symbol_info_tick(symbol)
    now_ask = now_tick.ask  # latest ask price.
    now_time = now_tick.time  # latest time of tick

    # setting past-time adjusting with delta.
    past_time = dt.datetime.fromtimestamp(now_time - delta)

    # Getting past tick
    past_tick = mt5.copy_ticks_from(symbol, past_time, 1, mt5.COPY_TICKS_INFO)
    past_ask = past_tick[0][2]  # getting past ask.

    # calculating percentage change.
    per_chng = ((now_ask - past_ask) / past_ask) * 100

    if symbol[3:6] == 'USD':
        return per_chng
    else:
        return -(per_chng)
Ejemplo n.º 2
0
 def getTicks(self, time, size):
     t = toXm(time)
     ticks = mt5.copy_ticks_from(self.stock, t, size, mt5.COPY_TICKS_ALL) 
     data = []
     for tick in ticks:
         t = tick.time
         bid = tick.bid
         ask = tick.ask
         data.append([t, bid, ask])
     return data
Ejemplo n.º 3
0
    def scrapeTicks(self, timeframe, from_jst, size=100000):
        # タイムゾーンをUTCに設定する
        timezone = pytz.timezone("Etc/UTC")
        # create 'datetime' objects in UTC time zone to avoid the implementation of a local time zone offset
        
        if isXmSummerTime(from_jst):
            delta = deltaHour(6)
        else:
            delta = deltaHour(7)
        utc_from = datetime(from_jst.year, from_jst.month, from_jst.day, from_jst.hour, from_jst.minute, 0, tzinfo=timezone) - delta
        d = mt5.copy_ticks_from(self.stock, setting.timeframeConstant(timeframe) , utc_from, size, mt5.COPY_TICKS_ALL) 
        data = self.convert2Array(d)

        #df = pd.DataFrame(data = out, columns=['Time', 'Bid', 'Ask'])
        #df.to_csv(market + '_' + str(year) +'-' + str(month) + '-' + str(day) + '.csv', index=False)
        return data
Ejemplo n.º 4
0
def test():
    # connect to MetaTrader 5
    if not mt5.initialize():
        print("initialize() failed")
        mt5.shutdown()
    print('Version:', mt5.version())
    #dji = mt5.copy_rates_range('US30Cash', mt5.TIMEFRAME_M30, Now() - DeltaDay(2), Now())
    #print(dji)
 
    # request 1000 ticks from EURAUD
    euraud_ticks = mt5.copy_ticks_from("US30Cash", datetime(2020,4,17,23), 1000, mt5.COPY_TICKS_ALL)
    # request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
    audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(2020,1,27,13), datetime(2020,1,28,13), mt5.COPY_TICKS_ALL)
 
    # get bars from different symbols in a number of ways
    eurusd_rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1, datetime(2020,1,28,13), 1000)
    eurgbp_rates = mt5.copy_rates_from_pos("EURGBP", mt5.TIMEFRAME_M1, 0, 1000)
    eurcad_rates = mt5.copy_rates_range("EURCAD", mt5.TIMEFRAME_M1, datetime(2020,1,27,13), datetime(2020,1,28,13))
    #print(eurusd_rates)
    # shut down connection to MetaTrader 5
    mt5.shutdown()
    return
Ejemplo n.º 5
0
 def acquireTicks(self, from_jst, size=100000):
     utc_from = TimeUtility.jst2seasonalAwaretime(from_jst)
     d = mt5.copy_ticks_from(self.stock, utc_from, size, mt5.COPY_TICKS_ALL)
     data = self.convert2ArrayTick(d)
     return data
Ejemplo n.º 6
0
pd.set_option('display.max_columns', 500)  # number of columns to be displayed
pd.set_option('display.width', 1500)  # max table width to display
# 导入用于处理时区的pytz模块
import pytz

# 建立与MetaTrader 5程序端的连接
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)
# request 100 000 EURUSD ticks starting from 10.01.2019 in UTC time zone
ticks = mt5.copy_ticks_from("EURUSD", utc_from, 100000, mt5.COPY_TICKS_ALL)
print("Ticks received:", len(ticks))

# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()

# display data on each tick on a new line
print("Display obtained ticks 'as is'")
count = 0
for tick in ticks:
    count += 1
    print(tick)
    if count >= 10:
        break

# create DataFrame out of the obtained data
Ejemplo n.º 7
0
    my_symbols_etf_rates[symbol] = df_symbol_rates

# get rates - time, open, high, low, close, tick_volume, spread and real_volume columns - range based
my_symbols_etf_rates = {}
for symbol in my_symbols_etf:
    symbol_rates = mt5.copy_rates_range(symbol, mt5.TIMEFRAME_D1,
                                        datetime(2020, 5, 18),
                                        datetime(2020, 5, 22, 23))
    df_symbol_rates = pd.DataFrame(symbol_rates)
    df_symbol_rates['time'] = pd.to_datetime(df_symbol_rates['time'], unit='s')
    my_symbols_etf_rates[symbol] = df_symbol_rates

# get ticks from - what are TICKS and how this data can be used?
my_symbols_etf_ticks = {}
for symbol in my_symbols_etf:
    symbol_ticks = mt5.copy_ticks_from(symbol, datetime(2020, 5, 18, 0), 100,
                                       mt5.COPY_TICKS_TRADE)
    df_symbol_ticks = pd.DataFrame(symbol_ticks)
    df_symbol_ticks['time'] = pd.to_datetime(df_symbol_ticks['time'], unit='s')
    my_symbols_etf_ticks[symbol] = df_symbol_ticks

# copy ticks range function
timezone = pytz.timezone("Etc/UTC")
my_symbols_etf_ticks = {}
for symbol in my_symbols_etf:
    symbol_ticks = mt5.copy_ticks_range(
        symbol, datetime(2020, 7, 14, 0, tzinfo=timezone),
        datetime(2020, 7, 17, 23, tzinfo=timezone), mt5.COPY_TICKS_ALL)
    df_symbol_ticks = pd.DataFrame(symbol_ticks)
    df_symbol_ticks['time'] = pd.to_datetime(df_symbol_ticks['time'], unit='s')
    my_symbols_etf_ticks[symbol] = df_symbol_ticks
Ejemplo n.º 8
0
Archivo: kkt.py Proyecto: xbobekf/MT5
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import MetaTrader5 as mt5

# connect to MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()

# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())

# request 1000 ticks from EURAUD
euraud_ticks = mt5.copy_ticks_from("ETHLTC.", datetime(2020, 1, 28, 13), 1000,
                                   mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
audusd_ticks = mt5.copy_ticks_range("BCHETH.", datetime(2020, 1, 27, 13),
                                    datetime(2020, 1, 28, 13),
                                    mt5.COPY_TICKS_ALL)

# get bars from different symbols in a number of ways
eurusd_rates = mt5.copy_rates_from("ETHLTC.", mt5.TIMEFRAME_M1,
                                   datetime(2020, 1, 28, 13), 1000)
eurgbp_rates = mt5.copy_rates_from_pos("BCHETH.", mt5.TIMEFRAME_M1, 0, 1000)
eurcad_rates = mt5.copy_rates_range("ETHEUR.", mt5.TIMEFRAME_M1,
                                    datetime(2020, 1, 27, 13),
                                    datetime(2020, 1, 28, 13))

# shut down connection to MetaTrader 5
mt5.shutdown()
Ejemplo n.º 9
0
rates7de = collections.deque(array) 
rates25de = collections.deque(array) 
rates99de = collections.deque(array) 

# connect to MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()

while(True):
    signal = ''
    currentTime = datetime.now()
    rates7 = mt5.copy_rates_from(pair, timeframe, currentTime, 7)
    rates25 = mt5.copy_rates_from(pair, timeframe, currentTime, 25)
    rates99 = mt5.copy_rates_from(pair, timeframe, currentTime, 99)
    ticks = mt5.copy_ticks_from(pair, currentTime, 1, mt5.COPY_TICKS_ALL)
    
    calculated7 = 0
    for pos in rates7:
        calculated7+=pos[4]
    rates7de.appendleft(calculated7/7)
    rates7de.pop()
    print ('7:', rates7de)

    calculated25 = 0
    for pos in rates25:
        calculated25+=pos[4]
    rates25de.appendleft(calculated25/25)
    rates25de.pop()
    print ('25:', rates25de) 
Ejemplo n.º 10
0
#needs to use metatrader5 for chart data and datetime for the current time
from datetime import datetime
import MetaTrader5 as mt5
import sys
import time

# Initializing MT5 connection 
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()

print(mt5.terminal_info())
print(mt5.version())

gbpusd_ticks = mt5.copy_ticks_from("GBPUSD", datetime.now(), 10000, mt5.COPY_TICKS_ALL)

print('gbpusd_ticks(', len(gbpusd_ticks), ')')
for val in gbpusd_ticks[:5000]: print(val)




      
Ejemplo n.º 11
0
register_matplotlib_converters()
import MetaTrader5 as mt5
import pytz

# connect to MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()

# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())

# request 1000 ticks from EURAUD
euraud_ticks = mt5.copy_ticks_from("EURAUD", datetime(2020, 1, 28, 13), 1000,
                                   mt5.COPY_TICKS_ALL)
eurusd_ticks = mt5.copy_ticks_from("EURUSD", datetime(2020, 1, 28, 13), 1000,
                                   mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(2020, 1, 27, 13),
                                    datetime(2020, 1, 28, 13),
                                    mt5.COPY_TICKS_ALL)

# get bars from different symbols in a number of ways
eurusd_rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1,
                                   datetime(2020, 1, 28, 13), 1000)
eurgbp_rates = mt5.copy_rates_from_pos("EURGBP", mt5.TIMEFRAME_M1, 0, 1000)
eurcad_rates = mt5.copy_rates_range("EURCAD", mt5.TIMEFRAME_M1,
                                    datetime(2020, 1, 27, 13),
                                    datetime(2020, 1, 28, 13))
Ejemplo n.º 12
0
    startTime2 = (string_datetime +
                  datetime.timedelta(minutes=-15)).strftime("%Y-%m-%d %H:%M")
    return startTime2


# 建立与MetaTrader 5程序端的连接
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(2022, 3, 8, tzinfo=timezone)
# request 100 000 EURUSD ticks starting from 10.01.2019 in UTC time zone
ticks = mt5.copy_ticks_from("NI225", utc_from, 10000, mt5.COPY_TICKS_ALL)
print("Ticks received:", len(ticks))

# shut down connection to the MetaTrader 5 terminal
mt5.shutdown()

# display data on each tick on a new line
# print("Display obtained ticks 'as is'")
# count = 0
# for tick in ticks:
#     count += 1
#     print(tick)
#     if count >= 10:
#         break

# create DataFrame out of the obtained data
Ejemplo n.º 13
0
import MetaTrader5 as mt5
import datetime as dt
import pandas as pd

mt5.initialize()

now_tic = mt5.symbol_info_tick('EURUSD')

delta = dt.timedelta(hours=5).seconds  # 5 hr Delta in seconds.

past_time = int(now_tic.time - delta)

past_tic = mt5.copy_ticks_from('EURUSD', dt.datetime.fromtimestamp(past_time),
                               1, mt5.COPY_TICKS_INFO)
past_ask = past_tic[0][2]
# past_tic['time'] = pd.to_datetime(past_tic['time'], unit='s')
# # print(past_tic)

per_change = ((now_tic.ask - past_ask) / past_ask) * 100
print(per_change)
register_matplotlib_converters()
import MetaTrader5 as mt5

# connect to MetaTrader 5
if not mt5.initialize():
    print("initialize() failed")
    mt5.shutdown()

# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())

# request ticks from GBPUSD
gbpusd_ticks = mt5.copy_ticks_from("GBPUSD", datetime(2021, 1, 18, 13), 1,
                                   mt5.COPY_TICKS_ALL)
# request ticks from GBPJPY within 2020.01.20 13:00 - 2020.03.18 13:00
gbpusdticks = mt5.copy_ticks_range("GBPUSD", datetime(2021, 1, 20, 13),
                                   datetime(now), mt5.COPY_TICKS_ALL)

# get bars from different symbols in 3 different ways
gbpusd_rates = mt5.copy_rates_from("GBPUSD", mt5.TIMEFRAME_M15,
                                   datetime(2020, 1, 20, 13), 1)
gbpusd_rates = mt5.copy_rates_from_pos("GBPUSD", mt5.TIMEFRAME_M15, 0, 1)
gbpusd_rates = mt5.copy_rates_range("GBPUSD", mt5.TIMEFRAME_M15,
                                    datetime(2020, 1, 20, 13),
                                    datetime(2020, 3, 18, 13))

# shut down connection to MetaTrader 5
mt5.shutdown()
Ejemplo n.º 15
0
 def get_ticks(self, symbol, start_date, qtd):
     ticks = mt5.copy_ticks_from(symbol, start_date, qtd, COPY_TICKS_ALL)
     return ticks
Ejemplo n.º 16
0
    def __del__(self):
        self.sock.close()


# Connecting to Trade Account, Throwing an error code if failed
authorized = mt5.login(accout_number, password="******")
if authorized:
    print(mt5.account_info())  # Trading Account Data
else:
    print("Failed to connect to trade account, error code=", mt5.last_error)

print(mt5.terminal_info())
print(mt5.version())

now = datetime.now(
)  # Now specifically for the purpose of getting the data and placing the trades right now

gbpusd_ticks = mt5.copy_ticks_from("GBPUSD", now, 1, mt5.COPY_TICKS_ALL)
eurusd_ticks = mt5.copy_ticks_from("EURUSD", now, 1, mt5.COPY_TICKS_ALL)
audusd_ticks = mt5.copy_ticks_from("AUDUSD", now, 1, mt5.COPY_TICKS_ALL)
usdjpy_ticks = mt5.copy_ticks_from("USDJPY", now, 1, mt5.COPY_TICKS_ALL)

start = input(
    "What do you want to do? \n quotes = if you want to get quotes, type it here \n trade = to place a trade. "
)
if start == "quotes":
    print(gbpusd_ticks, eurusd_ticks, audusd_ticks, usdjpy_ticks)

if start == "trade":
    print()
Ejemplo n.º 17
0
# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())
print('Year, Month, Day, Hour, Minute, WeekDay ', now.year, now.month, now.day, now.hour, now.minute, now.weekday())
# Weekend End Enable / Disable Code
# if now.weekday() > 4:
#     mt5.shutdown()
#     print("Week End No Rates")
#     exit(0)
# end

december_month = 12
# request 1000 ticks from EURAUD
euraud_ticks = mt5.copy_ticks_from("EURAUD", datetime(now.year, now.month, now.day, now.hour), 1000, mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
# if now.day > 1:
#     audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(now.year, now.month, now.day-1, now.hour), datetime(now.year, now.month, now.day, now.hour), mt5.COPY_TICKS_ALL)

if now.weekday() == 0 and now.day == 1 and now.month > 1:
    audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(now.year, now.month-1, now.day - 3, now.hour),
                                        datetime(now.year, now.month, now.day, now.hour), mt5.COPY_TICKS_ALL)
elif now.weekday() > 0 and now.day == 1 and now.month > 1:
    audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(now.year, now.month - 1, now.day - 1, now.hour),
                                            datetime(now.year, now.month, now.day, now.hour), mt5.COPY_TICKS_ALL)
elif now.weekday() == 0 and now.day == 1 and now.month == 1:
    audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(now.year-1, december_month, now.day - 3, now.hour),
                                        datetime(now.year, now.month, now.day, now.hour), mt5.COPY_TICKS_ALL)
elif now.weekday() > 0 and now.day == 1 and now.month == 1:
    audusd_ticks = mt5.copy_ticks_range("AUDUSD", datetime(now.year-1, december_month, now.day - 1, now.hour),
Ejemplo n.º 18
0
if not mt5.initialize():
    mt5.initialize()

  
 
# request connection status and parameters
print(mt5.terminal_info())
# get data on MetaTrader 5 version
print(mt5.version())
 
start_date=datetime(2021,1,27,10)
end_date=datetime(2021,1,27,11)

# request 1000 ticks from EURAUD
#datetime(year,month,day,hour)
euraud_ticks = mt5.copy_ticks_from("EURAUD", start_date, 1000, mt5.COPY_TICKS_ALL)
# request ticks from AUDUSD within 2019.04.01 13:00 - 2019.04.02 13:00
audusd_ticks = mt5.copy_ticks_range("AUDUSD", start_date, end_date, mt5.COPY_TICKS_ALL)
 
# get bars from different symbols in a number of ways
eurusd_rates = mt5.copy_rates_from("EURUSD", mt5.TIMEFRAME_M1,start_date, 1000)
eurgbp_rates = mt5.copy_rates_from_pos("EURGBP", mt5.TIMEFRAME_M1, 0, 1000)
eurcad_rates = mt5.copy_rates_range("EURCAD", mt5.TIMEFRAME_M1, start_date, end_date)
 
# shut down connection to MetaTrader 5
mt5.shutdown()
 
#DATA
print('euraud_ticks(', len(euraud_ticks), ')')
for val in euraud_ticks[:10]: print(val)
 
Ejemplo n.º 19
0
#____________Symbol info____________#
# mt5.symbol_select('EURTRY', False) #puts/removes the entered symbol in the marketwatch

eurinfo = mt5.symbol_info('EURUSD')  #gives the information of the asset
print(eurinfo)
print('=' * 90, '\n')

eurtick = mt5.symbol_info_tick('EURUSD')  #gives the latest tick info
print('tick info:', eurtick)
print('=' * 90, '\n')

#_______________retriving OHLC data______________#
eurbar = mt5.copy_rates_from('EURUSD', mt5.TIMEFRAME_H1, tiem, 1)
print('bars: ', eurbar, '\n')
# for i in eurbar:
#     print(i)

eurbar2 = mt5.copy_rates_range('EURUSD', mt5.TIMEFRAME_H1, tiem, tiem2)
print('bars range: ', eurbar2, '\n')

eurbar3 = mt5.copy_rates_from_pos('EURUSD', mt5.TIMEFRAME_H1, 0, 3)
print('bars from pos:', eurbar3, '\n')
print('=' * 90, '\n')

#__________________retriving ticks____________________#
eurtic = mt5.copy_ticks_from('EURUSD', tiem, 3, mt5.COPY_TICKS_INFO)
print('EURUSD Tic', eurtic, '\n')

eurtic2 = mt5.copy_ticks_range('EURUSD', tiem, tiem2, mt5.TIMEFRAME_H1)
print('EURUSD Tic range', eurtic2, '\n')