Package: xtbclient
XTBClientX-Trade Brokers (XTB) trading platform client, supporting both asynchronous and synchronous clients with typed classes.A python library supporting both asyncio and "normal" clients for X-Trade Brokers (XTB) trading using websockets.It will use eitherwebsocketsif using theasync XTB clientorwebsocket-clientif using thesync XTB clientSource codeAvailable ongithub.comInstallingTo install the client(s) just runpip install xtb-client.
Or, better yet, using poetry runpoetry add xtb-client.Getting startedBoth clients support context manager notation  -withorasync withstatement.Async clientIf you call the API too quicky the server may disconnect you so I've added somesleepcalls to simulate waiting a bit.importasyncioimportdatetimeimportloggingimportsysfromXTBClient.clientimportXTBAsyncClientfromXTBClient.models.modelsimportConnectionMode,Period,Transaction,TradeOperation,TradeTypefromXTBClient.models.requestsimportChartLastInfoRecord,ChartRangeRecordlogging.basicConfig(level=logging.DEBUG,stream=sys.stdout)asyncdefasync_client_test(user,password):logger=logging.getLogger("async client")asyncwithXTBAsyncClient(user,password,mode=ConnectionMode.DEMO)asclient:start=datetime.datetime.now()future=start.replace(year=start.year+1)start=start.replace(year=start.year-1)transaction=Transaction(cmd=TradeOperation.Buy,custom_comment="Testing custom comment",expiration=future,offset=0,order=0,price=1.12,sl=0.0,symbol="EURUSD",tp=0.0,type=TradeType.Open,volume=1)id=awaitclient.trade_transaction(transaction)logger.info(f"Initiated transaction:{id}")status=awaitclient.transaction_status(id)logger.info(f"Trade transaction status:{status}")chart_info=awaitclient.get_chart_last_request(ChartLastInfoRecord(Period.PERIOD_M5,datetime.datetime.now(),"EURPLN"))logger.info(f"Last chart info:{chart_info}")chart_info=awaitclient.get_chart_range_request(ChartRangeRecord(Period.PERIOD_M5,datetime.datetime.utcnow(),datetime.datetime.now(),"EURPLN",ticks=5))logger.info(f"Chart range info:{chart_info}")trades=awaitclient.get_trades(False)logger.info(f"All trades:{trades}")trades=awaitclient.get_trades_history(start=start,end=datetime.datetime.now())logger.info(f"All trades history:{trades}")awaitasyncio.sleep(1)eurpln=awaitclient.get_symbol("EURPLN")logger.info(f"EURO -> PLN:{eurpln}")current_user=awaitclient.get_current_user_data()logger.info(f"Current user:{current_user}")calendars=awaitclient.get_calendar()logger.info(f"All calendars:{calendars}")awaitasyncio.sleep(1)symbols=awaitclient.get_all_symbols()logger.info(f"All symbols:{symbols}")asyncio.get_event_loop().run_until_complete(async_client_test("user","password"))Sync (normal) clientIf you call the API too quicky the server may disconnect you so I've added somesleepcalls to simulate waiting a bit.importdatetimeimportloggingimportsysimporttimefromXTBClient.models.modelsimportConnectionMode,Period,Transaction,TradeOperation,TradeTypefromXTBClient.models.requestsimportChartLastInfoRecord,ChartRangeRecordfromXTBClient.client.xtbimportXTBSyncClientlogging.basicConfig(level=logging.DEBUG,stream=sys.stdout)defsync_client_test(user,password):logger=logging.getLogger("sync client")withXTBSyncClient(user,password,mode=ConnectionMode.DEMO,proxy=None)asclient:start=datetime.datetime.now()future=start.replace(year=start.year+1)start=start.replace(year=start.year-1)transaction=Transaction(cmd=TradeOperation.Buy,custom_comment="Testing custom comment",expiration=future,offset=0,order=0,price=1.12,sl=0.0,symbol="EURUSD",tp=0.0,type=TradeType.Open,volume=1)id=client.trade_transaction(transaction)logger.info(f"Initiated transaction:{id}")status=client.transaction_status(id)logger.info(f"Trade transaction status:{status}")chart_info=client.get_chart_last_request(ChartLastInfoRecord(Period.PERIOD_M5,datetime.datetime.now(),"EURPLN"))logger.info(f"Last chart info:{chart_info}")chart_info=client.get_chart_range_request(ChartRangeRecord(Period.PERIOD_M5,datetime.datetime.utcnow(),datetime.datetime.now(),"EURPLN",ticks=5))logger.info(f"Chart range info:{chart_info}")trades=client.get_trades(False)logger.info(f"All trades:{trades}")trades=client.get_trades_history(start=start,end=datetime.datetime.now())logger.info(f"All trades history:{trades}")time.sleep(1)eurpln=client.get_symbol("EURPLN")logger.info(f"EURO -> PLN:{eurpln}")current_user=client.get_current_user_data()logger.info(f"Current user:{current_user}")calendars=client.get_calendar()logger.info(f"All calendars:{calendars}")time.sleep(1)symbols=client.get_all_symbols()logger.info(f"All symbols:{symbols}")sync_client_test("user","password")API and usageSince this is Python you can browse through the code to find out what methods are available and how to use them.
All methods and classes should have typing hints so you can tell what each method expects as parameters.All return types from the XTB API is typed, there's no dictionaries with unknown key/value pairs or anything similar.Working on the codeThis project uses poetry for dependency management as well as for it's publishing functionality.
To get started all you need to do is:open a command promptclone the repositorybrowse to the newly cloned repository in the already opened command promptrunpoetry installWork in progressStreaming operations likegetCandlesand others are missingAdd missingnormalAPI methodsMore unit tests could be addedBetter documentationAdded library to PyPi repository