Skip to main content

xStation5 API Python Library

Test xapi-python PyPi Downloads Codecov

The xStation5 API Python library provides a simple and easy-to-use API for interacting with the xStation5 trading platform. With this library, you can connect to the xStation5 platform, retrieve market data, and execute trades.

This library may be used for BFB Capital and XTB xStation5 accounts.

API documentation: http://developers.xstore.pro/documentation

Disclaimer

This xStation5 API Python library is not affiliated with, endorsed by, or in any way officially connected to the xStation5 trading platform or its parent company. The library is provided as-is and is not guaranteed to be suitable for any particular purpose. The use of this library is at your own risk, and the author(s) of this library will not be liable for any damages arising from the use or misuse of this library. Please refer to the license file for more information.

Installation

You can install xAPI using pip. Simply run the following command:

pip install xapi-python

Usage

To use xAPI, you will need to have an active account with the xStation5 trading platform. Once you have an account, you can use the xAPI library to connect to the platform and begin trading.

Here is an example of how to use the xAPI library to connect to the xStation5 platform and subscribe to market data:

import asyncio
import xapi

# Replace these values with your own credentials
CREDENTIALS = {
    "accountId": "<your_client_id>",
    "password": "<your_password>",
    "host": "ws.xtb.com",
    "type": "real",
    "safe": True
}

async def main():
    while True:
        try:
            async with await xapi.connect(**CREDENTIALS) as x:
                # Subscribe for the current price of BITCOIN and ETHEREUM
                await x.stream.getTickPrices("BITCOIN")
                await x.stream.getTickPrices("ETHEREUM")

                # Listen for coming price ticks
                async for message in x.stream.listen():
                    print(message['data'])

        except xapi.LoginFailed as e:
            print(f"Log in failed: {e}")
            return

        except xapi.ConnectionClosed as e:
            print(f"Connection closed: {e}, reconnecting ...")
            await asyncio.sleep(1)
            continue

if __name__ == "__main__":
    try:
        asyncio.run(main())

    except KeyboardInterrupt:
        pass

And here is an example of how to execute a trade using the xAPI library:

import asyncio
import xapi

# Replace these values with your own credentials
CREDENTIALS = {
    "accountId": "<your_client_id>",
    "password": "<your_password>",
    "host": "ws.xtb.com",
    "type": "real",
    "safe": False
}

async def main():
    try:
        async with await xapi.connect(**CREDENTIALS) as x:
            # Open a new trade for BITCOIN
            response = await x.socket.tradeTransaction(
                symbol="BITCOIN",
                cmd=TradeCmd.BUY_LIMIT,
                type=TradeType.OPEN,
                price=10.00,
                volume=1
            )

            if response['status'] == True:
                print("Transaction sent to market")
            else:
                print("Failed to trade a transaction", response)

    except xapi.LoginFailed as e:
        print(f"Log in failed: {e}")

    except xapi.ConnectionClosed as e:
        print(f"Connection closed: {e}")

if __name__ == "__main__":
    asyncio.run(main())

Command Line Interface (CLI)

The xAPI library provides a command line interface (CLI) for interacting with the xStation5 API. You can run the xAPI commands without leaving the terminal. Just use the xapi command.

xAPI in action

Examples

To run the examples for the xAPI library, you will need to have an account with the xStation5 trading platform.

Before running the examples, you should create a file called credentials.json in the project directory. This file should contain your account credentials, like this:

credentials.json

{
    "accountId": "<your_client_id>",
    "password": "<your_password>",
    "host": "ws.xtb.com",
    "type": "real",
    "safe": false
}

Once you have created the credentials.json file, you can run an example using the following command:

python3 examples/get-margin-level.py

Unit Tests

This will run all of the unit tests in the tests directory:

git clone https://github.com/pawelkn/xapi-python.git
cd xapi-python
python3 -m unittest discover tests

Buy Me A Coffee! ☕

If you find the project beneficial and would like to support me, please consider showing your appreciation by buying me a coffee on Buy Me A Coffee

Metadata

Release files for xapi-python 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xapi-python 0.2.1
File Size Uploaded
xapi_python-0.2.1.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xapi-python 0.2.1
File Interpreter ABI Platform
xapi_python-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 31.4 kB

Release files / xapi_python-0.2.1.tar.gz

Download URL xapi_python-0.2.1.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
377fbe51299d3181ef3161b0cd7e1d64d7972657c07e4c1405e0dce10a738599
BLAKE2b-256 checksum
How to use checksums
4eb072625ff6b9ef13d267b22fb080ea61d1d6398d8fc7b98009172ad868d08c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release files / xapi_python-0.2.1-py3-none-any.whl

Download URL xapi_python-0.2.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
783e682b7994c63614607adc7504b9f9b10307db12cb78241e5f70f5b545cc05
BLAKE2b-256 checksum
How to use checksums
3515001abfaa2d6e9d75481075bce96aaa83faf2982ed221625741ee71f3bc73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page