Convert tick data to tick bars/candlesticks using python-MT5, and pandas

Viewed 424

I am trying to convert data obtained using a metatrader module for python found on the official mql5 website. I am trying to use tick data rather than importing candlestick data. Tick bars or candlesticks sample a set amount of ticks rather than a set amount of time in order to calculate ohlc. For example, 100 ticks creates a candle instead of 1 minute. Using the functions to copy ticks from metatrader5

copy_ticks_from

or

copy_ticks_range

results in a dataframe called copy_ ticks_from or copy_ticks_range but the data output is the same format.

time bid ask last volume time_msc flags volume_real

Ive watched videos and searched and searched, and will continue to but any help is greatly appreciated.

an example of code input and out can be found at https://www.mql5.com/en/docs/integration/python_metatrader5/mt5copyticksfrom_py

edit426221500

I was inspired by this article https://towardsdatascience.com/advanced-candlesticks-for-machine-learning-i-tick-bars-a8b93728b4c5

I think I am understanding a but more after this read through. I believe i need to use similar code to get desired output. Im working on converting my dataframe to a numpy array. After I will modify the code found in the reference above to be

Something like

def generate_tickbars(ticks, frequency=1000):
times = ticks[:,0]
time = ticks[:,1]
prices = ticks[:,2,3]

not sure about volume or the preceding lines but I think im on the right track or this may at least be one way of doing it.

researching from https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_numpy.html and then going to try and get the conversion working.

Edit426221640

using

ticks_frame.to_numpy(dtype=None, copy=True,)

I get a numpy array as an output.

array([[Timestamp('2020-01-10 01:05:00'), 1552.91, 1553.16, ...,
    1578618300331, 134, 0.0],
   [Timestamp('2020-01-10 01:05:00'), 1552.83, 1553.32, ...,
    1578618300634, 134, 0.0],
   [Timestamp('2020-01-10 01:05:01'), 1552.87, 1553.32, ...,
    1578618301834, 130, 0.0],

...,etc I am now stuck at the code referenced in the link above from the previous edit.

# expects a numpy array with trades
# each trade is composed of: [time, price, quantity]
def generate_tickbars(ticks, frequency=1000):
times = ticks[:,0]
prices = ticks[:,1]
volumes = ticks[:,2]
res = np.zeros(shape=(len(range(frequency, len(prices), frequency)), 6))
it = 0
for i in range(frequency, len(prices), frequency):
    res[it][0] = times[i-1]                        # time
    res[it][1] = prices[i-frequency]               # open
    res[it][2] = np.max(prices[i-frequency:i])     # high
    res[it][3] = np.min(prices[i-frequency:i])     # low
    res[it][4] = prices[i-1]                       # close
    res[it][5] = np.sum(volumes[i-frequency:i])    # volume
    it += 1
return res

How do make this work for my data? Is there a simpler way to accomplish this?

Edit426221745

I believe i have resampled the data correctly using different approach.

def bar(xs, y): return np.int64(xs / y) * y
ticks_frame.groupby(bar(np.arange(len(ticks_frame)), 
1000)).agg({'bid': 'ohlc', 'volume': 'sum'})

Now onto plotting bars or candlesticks.

Edit426222250

Still stuck at the point of last edit. Although i can use bid for ohlc and group ticks and view that way it seems my issue is i need to reshape the dataframe or create a new dataframe from ticks_frame that uses bid to calculate ohlc values. Any and all help is greatly appreciated.

0 Answers
Related