Git Product home page Git Product logo

client-python's Introduction

Official Xena websocket client python library

For API documentation check out Help Center

Install

    pip install python-xena

Market Data example

	import asyncio

	from xena.websocket import XenaMDWebsocketClient
	import xena.proto.constants as constants

	loop = None

	async def main():
		global loop

		# create client instance and connect
		ws = XenaMDWebsocketClient(loop)
		await connect(ws)
		
		async def handle(ws, msg):
			print(msg)

		await ws.candles("BTC/USDT", handle, timeframe="1h", throttle_interval=250, throttle_unit=constants.ThrottleTimeUnit_Milliseconds)

		while True:
			await asyncio.sleep(20, loop=loop)


	if __name__ == "__main__":
		loop = asyncio.get_event_loop()
		loop.run_until_complete(main())

Trading Example

Register an account with Xena. Generate an API Key and assign relevant permissions.

	import asyncio
	import time

	from xena.websocket import XenaTradingWebsocketClient
	import xena.proto.constants as constants
	import xena.helpers as helpers

	loop = None

	def id(prefix):
		timestamp = int(time.time())
		return prefix + '-' + str(timestamp)


	async def connect(ws):
		while True:
			try:
				await ws.connect()
				break
			except Exception as e:
				print("exception {}".format(e))
				await asyncio.sleep(1)


	async def get_client():
		async def on_connection_close(ws, e):
			await connect(ws)

		api_key = ''
		api_secret = ''
		ws = XenaTradingWebsocketClient(api_key, api_secret, loop)
		ws.on_connection_close(on_connection_close)

		await connect(ws)
		return ws


	async def example_of_market_order():
		ws = await get_client()

		async def handle(ws, msg):
			print(msg)

		ws.listen_type(constants.MsgType_ExecutionReportMsgType, handle)
		await ws.market_order(8263200, id("market-order"), "BTC/USDT", constants.Side_Buy, "0.01")

		# or using helpers method
		cmd = helpers.market_order(8263200, id("market-order"), "BTC/USDT", constants.Side_Buy, "0.01")
		await ws.send_cmd(cmd)

		# looop
		while True:
			await asyncio.sleep(20, loop=loop)

	if __name__ == "__main__":
		loop = asyncio.get_event_loop()
		loop.run_until_complete(example_of_market_order())

For more examples check out "examples" folder

client-python's People

Contributors

xenaex avatar

Stargazers

 avatar Konstantin Vlasov avatar

Watchers

Konstantin Vlasov avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.