forked from x10xchange/python_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_client_example.py
More file actions
66 lines (53 loc) · 2.1 KB
/
simple_client_example.py
File metadata and controls
66 lines (53 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import asyncio
from decimal import Decimal
from x10.perpetual.accounts import StarkPerpetualAccount
from x10.perpetual.configuration import MAINNET_CONFIG
from x10.perpetual.orderbook import OrderBook
from x10.perpetual.orders import OrderSide, TimeInForce
from x10.perpetual.simple_client.simple_trading_client import BlockingTradingClient
async def setup_and_run():
api_key = "<api>"
public_key = "<public>"
private_key = "<private>"
vault = 100001
stark_account = StarkPerpetualAccount(
vault=vault,
private_key=private_key,
public_key=public_key,
api_key=api_key,
)
client = await BlockingTradingClient.create(endpoint_config=MAINNET_CONFIG, account=stark_account)
market = (await client.get_markets())["EDEN-USD"]
best_ask_condition = asyncio.Condition()
slippage = Decimal("0.0005")
async def best_ask_initialised(best_ask):
async with best_ask_condition:
best_ask_condition.notify_all()
orderbook = await OrderBook.create(
MAINNET_CONFIG,
market_name=market.name,
start=True,
best_ask_change_callback=best_ask_initialised,
best_bid_change_callback=None,
)
async with best_ask_condition:
await best_ask_condition.wait()
best_ask_price = orderbook.best_ask()
if best_ask_price is None:
raise ValueError("Best ask price is None after initialization")
order_price = market.trading_config.round_price(best_ask_price.price * (1 + slippage))
print(f"Best ask price: {best_ask_price}")
print(f"Placing market order on {market.name} for {market.trading_config.min_order_size} at {order_price}")
placed_order = await client.create_and_place_order(
amount_of_synthetic=market.trading_config.min_order_size * Decimal("10"),
price=order_price,
market_name=market.name,
side=OrderSide.BUY,
post_only=False,
time_in_force=TimeInForce.IOC,
)
print(f"Placed order result: {placed_order}")
await client.close()
await orderbook.close()
if __name__ == "__main__":
asyncio.run(main=setup_and_run())