Skip to content

Commit

Permalink
feat: ensured all async etc
Browse files Browse the repository at this point in the history
  • Loading branch information
8ball030 committed Jul 1, 2024
1 parent 064fcd7 commit 0d19e20
Show file tree
Hide file tree
Showing 10 changed files with 476 additions and 245 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ site/
.history

*trader
agent

node_modules
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ price = client.get_product(DEFAULT_SYMBOL)
print(price)
```

Fopr a demonstration of the async client please refer to the file in examples/async_client.py


## Installation
Expand Down
105 changes: 105 additions & 0 deletions examples/run_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""
Example of a client that connects to a server and sends a message.
"""

import asyncio
import os
from pprint import pprint

from hundred_x.async_client import AsyncHundredXClient
from hundred_x.enums import Environment, OrderSide, OrderType, TimeInForce


async def main():
key = os.getenv("HUNDRED_X_PRIVATE_KEY")
subaccount_id = 0

if not key:
raise ValueError("HUNDRED_X_PRIVATE_KEY environment variable is not set.")

client = AsyncHundredXClient(Environment.PROD, key, subaccount_id=subaccount_id)

print(f"Using Wallet: {client.public_key}")
print(f"Using Subaccount ID: {client.subaccount_id}")

# In order to hit the authenticated endpoints, we need to login.
await client.login()

# We check the initial balance.
print("Initial Balance")
response = await client.get_spot_balances()
pprint(response)

# We first get the symbol.
print("Symbol")
response = await client.get_symbol("btcperp")
pprint(response)

# We create an order.
print("Create Order")
response = await client.create_order(
subaccount_id=subaccount_id,
product_id=response['productId'], # This is the product_id for the symbol 'btcperp
quantity=0.001,
price=round(int(response['markPrice']) * 0.99 / 1e18), # This is the current price of the symbol 'btcperp'
side=OrderSide.BUY,
order_type=OrderType.LIMIT,
time_in_force=TimeInForce.GTC,
)

# We check the open orders.
print("Open Orders")
response = await client.get_open_orders("btcperp")
pprint(response)

# We cancel the order.
print("Cancel Order")
response = await client.cancel_order(order_id=response[0]["id"], product_id=response[0]["productId"])
pprint(response)

# We check the positions.
print("Positions")
response = await client.get_position()
pprint(response)

# We check if we can cancel and replace an order.
# We check the open orders.
print("Open Orders")
response = await client.get_open_orders("btcperp")
pprint(response)

# We create an order.
print("Create Order")

response = await client.create_order(
subaccount_id=subaccount_id,
product_id=response[0]["productId"],
quantity=0.001,
price=round(int(response[0]["price"]) * 0.99 / 1e18),
side=OrderSide.BUY,
order_type=OrderType.LIMIT,
time_in_force=TimeInForce.GTC,
)

# We check the open orders.
print("Open Orders")
response = await client.get_open_orders("btcperp")
pprint(response)

# We cancel and replace the order.
print("Cancel and Replace Order")
response = await client.cancel_and_replace_order(
order_id_to_cancel=response[0]["id"],
product_id=response[0]["productId"],
quantity=0.002,
price=round(int(response[0]["price"]) * 0.99 / 1e18),
side=OrderSide.BUY,
)
pprint(response)

# We cancel all orders.
response = await client.cancel_all_orders(subaccount_id=subaccount_id, product_id=1002)


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

0 comments on commit 0d19e20

Please sign in to comment.