Skip to content

Commit fd4ee25

Browse files
examples: add market stop-loss, take-profit, and TWAP orders (#169)
* examples: add market SL TP and TWAP orders * changes to reflect the stop loss and take profit
1 parent ebc5066 commit fd4ee25

4 files changed

Lines changed: 179 additions & 0 deletions

File tree

‎examples/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
- `create_market_order_eth_sell.py`
2525
- creates a market sell order for 0.1 ETH @ market price
2626

27+
- `create_stop_loss_market_order.py`
28+
- creates a reduce-only market order that is triggered when ETH falls to the stop price
29+
- configures the lowest acceptable execution price to limit slippage
30+
31+
- `create_take_profit_market_order.py`
32+
- creates a reduce-only market order that is triggered when ETH rises to the take-profit price
33+
- configures the lowest acceptable execution price to limit slippage
34+
35+
- `create_twap_market_order.py`
36+
- creates a TWAP buy order that splits 0.21 ETH across market orders over 10 minutes
37+
- configures the highest acceptable execution price for each market order
38+
2739
- `create_grouped_ioc_with_attached_sl_tp.py`
2840
- creates an ask (sell) IoC order for 0.1 ETH
2941
- along w/ the order, it sets up a Stop Loss (SL) and a Take Profit (TP) order for the whole size of the order
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
3+
import lighter
4+
5+
from examples.utils import default_example_setup
6+
7+
8+
async def wait_for_long_position(api_client, account_index, market_index):
9+
account_api = lighter.AccountApi(api_client)
10+
11+
for _ in range(20):
12+
response = await account_api.account(
13+
by="index",
14+
value=str(account_index),
15+
active_only=True,
16+
)
17+
if any(
18+
position.market_id == market_index
19+
and position.sign == 1
20+
and float(position.position) > 0
21+
for account in response.accounts
22+
for position in account.positions
23+
):
24+
return
25+
await asyncio.sleep(0.5)
26+
27+
raise RuntimeError("The entry order did not create a long position")
28+
29+
30+
async def main():
31+
client, api_client, _ = default_example_setup()
32+
33+
try:
34+
market_index = 0
35+
base_amount = 500
36+
37+
entry_tx, entry_response, err = await client.create_market_order(
38+
market_index=market_index,
39+
client_order_index=0,
40+
base_amount=base_amount,
41+
avg_execution_price=4000_00,
42+
is_ask=False,
43+
)
44+
print(f"Create Entry Order {entry_tx=} {entry_response=} {err=}")
45+
if err is not None:
46+
raise Exception(err)
47+
48+
await wait_for_long_position(api_client, client.account_index, market_index)
49+
50+
tx, tx_hash, err = await client.create_sl_order(
51+
market_index=market_index,
52+
client_order_index=1,
53+
base_amount=base_amount,
54+
trigger_price=2500_00,
55+
price=2425_00,
56+
is_ask=True,
57+
reduce_only=True,
58+
)
59+
print(f"Create Stop Loss Order {tx=} {tx_hash=} {err=}")
60+
if err is not None:
61+
raise Exception(err)
62+
finally:
63+
await client.close()
64+
await api_client.close()
65+
66+
67+
if __name__ == "__main__":
68+
asyncio.run(main())
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import asyncio
2+
3+
import lighter
4+
5+
from examples.utils import default_example_setup
6+
7+
8+
async def wait_for_long_position(api_client, account_index, market_index):
9+
account_api = lighter.AccountApi(api_client)
10+
11+
for _ in range(20):
12+
response = await account_api.account(
13+
by="index",
14+
value=str(account_index),
15+
active_only=True,
16+
)
17+
if any(
18+
position.market_id == market_index
19+
and position.sign == 1
20+
and float(position.position) > 0
21+
for account in response.accounts
22+
for position in account.positions
23+
):
24+
return
25+
await asyncio.sleep(0.5)
26+
27+
raise RuntimeError("The entry order did not create a long position")
28+
29+
30+
async def main():
31+
client, api_client, _ = default_example_setup()
32+
33+
try:
34+
market_index = 0
35+
base_amount = 500
36+
37+
entry_tx, entry_response, err = await client.create_market_order(
38+
market_index=market_index,
39+
client_order_index=0,
40+
base_amount=base_amount,
41+
avg_execution_price=4000_00,
42+
is_ask=False,
43+
)
44+
print(f"Create Entry Order {entry_tx=} {entry_response=} {err=}")
45+
if err is not None:
46+
raise Exception(err)
47+
48+
await wait_for_long_position(api_client, client.account_index, market_index)
49+
50+
tx, tx_hash, err = await client.create_tp_order(
51+
market_index=market_index,
52+
client_order_index=1,
53+
base_amount=base_amount,
54+
trigger_price=3500_00,
55+
price=3395_00,
56+
is_ask=True,
57+
reduce_only=True,
58+
)
59+
print(f"Create Take Profit Order {tx=} {tx_hash=} {err=}")
60+
if err is not None:
61+
raise Exception(err)
62+
finally:
63+
await client.close()
64+
await api_client.close()
65+
66+
67+
if __name__ == "__main__":
68+
asyncio.run(main())
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import asyncio
2+
import time
3+
4+
from examples.utils import default_example_setup
5+
6+
7+
async def main():
8+
client, api_client, _ = default_example_setup()
9+
10+
tx, tx_hash, err = await client.create_order(
11+
market_index=0,
12+
client_order_index=0,
13+
base_amount=2100, # 0.21 ETH
14+
price=4000_00, # Highest acceptable price for each market order
15+
is_ask=False,
16+
order_type=client.ORDER_TYPE_TWAP,
17+
time_in_force=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
18+
reduce_only=False,
19+
trigger_price=0,
20+
order_expiry=int(time.time() * 1000) + 10 * 60 * 1000,
21+
)
22+
print(f"Create TWAP Order {tx=} {tx_hash=} {err=}")
23+
if err is not None:
24+
raise Exception(err)
25+
26+
await client.close()
27+
await api_client.close()
28+
29+
30+
if __name__ == "__main__":
31+
asyncio.run(main())

0 commit comments

Comments
 (0)