Package: xapi-python
xStation5 API Python LibraryThe 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 forBFB CapitalandXTBxStation5 accounts.API documentation:http://developers.xstore.pro/documentationDisclaimerThis 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.InstallationYou can install xAPI using pip. Simply run the following command:pipinstallxapi-pythonUsageTo 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:importasyncioimportxapi# Replace these values with your own credentialsCREDENTIALS={"accountId":"<your_client_id>","password":"<your_password>","host":"ws.xtb.com","type":"real","safe":True}asyncdefmain():try:# Create a new xAPI object and connect to the xStation5 platformasyncwithawaitxapi.connect(**CREDENTIALS)asx:passexceptxapi.LoginFailedase:print(f"Log in failed:{e}")exceptxapi.ConnectionClosedase: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:importasyncioimportxapi# Replace these values with your own credentialsCREDENTIALS={"accountId":"<your_client_id>","password":"<your_password>","host":"ws.xtb.com","type":"real","safe":True}asyncdefmain():whileTrue:try:asyncwithawaitxapi.connect(**CREDENTIALS)asx:# Subscribe for the current price of BITCOIN and ETHEREUMawaitx.stream.getTickPrices("BITCOIN")awaitx.stream.getTickPrices("ETHEREUM")# Listen for coming price ticksasyncformessageinx.stream.listen():print(message['data'])exceptxapi.LoginFailedase:print(f"Log in failed:{e}")returnexceptxapi.ConnectionClosedase:print(f"Connection closed:{e}, reconnecting ...")awaitasyncio.sleep(1)continueif__name__=="__main__":try:asyncio.run(main())exceptKeyboardInterrupt:passAnd here is an example of how to execute a trade using the xAPI library:importasyncioimportxapifromxapiimportTradeCmd,TradeType,TradeStatus# Replace these values with your own credentialsCREDENTIALS={"accountId":"<your_client_id>","password":"<your_password>","host":"ws.xtb.com","type":"real","safe":False}asyncdefmain():try:asyncwithawaitxapi.connect(**CREDENTIALS)asx:# Open a new trade for BITCOINresponse=awaitx.socket.tradeTransaction(symbol="BITCOIN",cmd=TradeCmd.BUY_LIMIT,type=TradeType.OPEN,price=10.00,volume=1)ifresponse['status']==True:print("Transaction sent to market")else:print("Failed to trade a transaction",response)exceptxapi.LoginFailedase:print(f"Log in failed:{e}")exceptxapi.ConnectionClosedase:print(f"Connection closed:{e}")if__name__=="__main__":asyncio.run(main())ExamplesTo 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 calledcredentials.jsonin 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 thecredentials.jsonfile, you can run an example using the following command:python3examples/get-margin-level.pyUnit TestsThis will run all of the unit tests in the tests directory:gitclonehttps://github.com/pawelkn/xapi-python.gitcdxapi-python
python3-munittestdiscovertestsBuy 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 onBuy Me A Coffee