Possible bug in the Python API

 

Reposting as the previous thread got deleted.


Hi all,


Pre build 2530, I had no issues with retrieving large chunks of tick data. Post build 2530, I was not always able to connect to the terminal when requesting large chunks of tick data. However, I am still able to connect to the terminal when requesting small chunks of tick data. I have tried everything from reinstalling MT5 to reinstalling my python environment. I have contacted service desk but service desk is not interested in entertaining my queries hence, I am posting here as there does not seem to be a proper channel available for the feedback of bugs. Is there anyone else experiencing the same issue?


Regards,

Somebody


Edit:

Before the previous thread got deleted, a user (Nicolishen I believe. I can't remember his forum name clearly) has kindly pointed out that this bug could be attributed to a memory overflow and has been around since the previous builds and has recommended that tick data be sequentially retrieved in small chunks.
 

What up, nuts. 


One issue is when you use the 32 bit python interpreter. If you install the 64bit interpreter then you can use more available memory. Still though you can chunk the calls to the terminal in order to reduce its memory footprint to free up more memory for your python script. 

import datetime as dt

import numpy as np
import pymt5adapter as mta


def chunk_time(from_: dt.datetime, to_: dt.datetime, **delta_kwargs):
    chunk_delta = dt.timedelta(**delta_kwargs)
    split_delta = dt.timedelta(milliseconds=1)
    time1 = from_
    while True:
        time2 = min((time1 + chunk_delta), to_)
        yield time1, time2
        time1 = time2 + split_delta
        if time2 >= to_:
            break


def copy_ticks_range(symbol, from_, to_, flags=mta.COPY_TICKS.ALL, **delta_kwargs):
    delta_kwargs = delta_kwargs or {'days': 90}
    ticks = np.hstack([mta.copy_ticks_range(symbol, f, t, flags)
                       for f, t in chunk_time(from_, to_, **delta_kwargs)])
    return ticks


if __name__ == '__main__':
    with mta.connected(raise_on_errors=True):
        to_ = dt.datetime.now(tz=dt.timezone.utc)
        from_ = to_ - dt.timedelta(weeks=52 * 3)
        ticks = copy_ticks_range('EURUSD', from_, to_, mta.COPY_TICKS.ALL, days=90)
        print(dt.datetime.utcfromtimestamp(ticks[0][0]))
        print(dt.datetime.utcfromtimestamp(ticks[-1][0]))
        print(f'{len(ticks):,}')
 

Hi Nico,


Thanks for the advice and help so far. I've been using 64 bit python interpreter in my projects so far and the issues only started appearing post build 2530. Your advice on chunking the calls do indeed mitigate the issue described beforehand but I suspect that it may not be a memory issue because my IDE did not show any signs of excessive memory usage. It was unable to even establish the connection required for the retrieval of data and hence it led me to believe that there was something poorly/incorrectly coded within the MT5 python API.