Skip to main content

The xStation5 API Python library

Project description

xStation5 API Python Library

Test xapi-python

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 xStation5 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:

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:
        # Create a new xAPI object and connect to the xStation5 platform
        x = await xapi.connect(**credentials)

    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())

Once you have connected to the platform, you can use the xAPI object to retrieve market data and execute trades.

Here is an example of how to subscribe to market data 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():
    while True:
        try:
            x = await xapi.connect(**credentials)

            # 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 ...")
            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
from xapi import TradeCmd, TradeType, TradeStatus

# 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:
        x = await xapi.connect(**credentials)

        # 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())

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": "demo",
    "safe": true
}

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

python3 examples/get-balance.py

Unit Tests

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

python3 -m unittest discover tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xapi-python-0.0.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

xapi_python-0.0.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file xapi-python-0.0.1.tar.gz.

File metadata

  • Download URL: xapi-python-0.0.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for xapi-python-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2ebfe800edf99d16a848f333fb5ecc51524036478bd09cc1dfed80d0d1a3a291
MD5 58d7d0cbca8df86eeba370626327c8fd
BLAKE2b-256 8380f6ff1d4084c0a2fcc666a96d5f5895baa7e9c3cdd2dab498cd03a1bb655e

See more details on using hashes here.

File details

Details for the file xapi_python-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: xapi_python-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for xapi_python-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 97aa511bfc9f41a295357cf40d1f7d2d3432e019d129482bbb8e9859cee94319
MD5 02c9a119b45ab33b89b93853ae9be71a
BLAKE2b-256 b668a78a16cefcc22b43e622afd33f2ec3ee84660d24e98595ac641465e3f1b6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page