How to collect data with XTB API ?

To create trading strategy we need data, and in this article Iwill describe how to obtain the historical prices from XTB trading platform.

Slawomir Lisowski
3 min readJun 4, 2023
How to collect data with XTB API ?

So far we were focused on costs of trading. This may seem a bit strange to you that object of our interest were not charts, technical indicators and so on. In my opinion if we understand how limit costs of trading, this will let us focus only on choose most appropriate and efficient trading startegy. Now it’s time to obtain most important component of algorithmic trading, namely data. First you should create demo account on XTB website. For better organization of work, let me create config.py file with XTB credentials and path to catalog with spreads. You can find csv files with spreads at this link .

Now, task is to create python class, that get data from XTB servers and convert to more friendly format i.e. a pandas dataframe. I will try to explain code snippets step by step.

Ok first we define the class DataCollectorXtb and initial attributes. In attributes we can see two dicts period_dict and max_range. To understand why these dict are defined, let’s look at XTB API docs.

XTB API periods

As we see periods of historical bars are encoded in minutes, and we have acccess to limited list of frequencies. Of course possible is download historical data in one minute frequency and just resample it, to for example 10 minutes, but hereby is also some limitation. This limitation comes from period for which historical data are available.

XTB API periods limitations

To overcome this limitations we will use method check_max_range.

This method if it is necessity will change date range of data we are looking for, in order to meet requirements, that are described in API documentation. Next method will help us to convert response from XTB API to pandas dataframe.

Finally, let’s add to DataCollectorXtb class method that will equip it with possibility to add spreads to dataframe. Last method get_data, lastly let us create pandas dataframe.

When script is ready, last step is test it, and prepare dataframe.

In arguments, passed to the class we see that range of date is one year, and period is 5min. As we said before there is limitations of back period long, for which data can be collected. So we have info that max range for given period is one month from now, and this range is taken into account. Now we know how to collect data with XTB API, and we are ready to prepare and test trading strategies. But first we need some framework or script to achieve this, and implement some order of our trading activities.

More you can find on my github .

This article is not investment advice. The information contained on this website and the resources available for download through this website are for educational and informational purposes only.

--

--

Slawomir Lisowski

I'm passionate of data science and machine learning, especially in the field of finance and trading. Investing is risky. Invest responsibly