From 19e9f13cd86bf8f5b55a1d750b32872d314b42b4 Mon Sep 17 00:00:00 2001 From: mihai Date: Thu, 3 Sep 2026 17:34:28 +0000 Subject: [PATCH 1/2] examples: add market SL TP and TWAP orders --- examples/README.md | 12 +++++++ .../orders/create_stop_loss_market_order.py | 27 ++++++++++++++++ .../orders/create_take_profit_market_order.py | 27 ++++++++++++++++ examples/orders/create_twap_market_order.py | 31 +++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 examples/orders/create_stop_loss_market_order.py create mode 100644 examples/orders/create_take_profit_market_order.py create mode 100644 examples/orders/create_twap_market_order.py diff --git a/examples/README.md b/examples/README.md index 6b6f5d8..643e029 100644 --- a/examples/README.md +++ b/examples/README.md @@ -24,6 +24,18 @@ - `create_market_order_eth_sell.py` - creates a market sell order for 0.1 ETH @ market price +- `create_stop_loss_market_order.py` + - creates a reduce-only market order that is triggered when ETH falls to the stop price + - configures the lowest acceptable execution price to limit slippage + +- `create_take_profit_market_order.py` + - creates a reduce-only market order that is triggered when ETH rises to the take-profit price + - configures the lowest acceptable execution price to limit slippage + +- `create_twap_market_order.py` + - creates a TWAP buy order that splits 0.21 ETH across market orders over 10 minutes + - configures the highest acceptable execution price for each market order + - `create_grouped_ioc_with_attached_sl_tp.py` - creates an ask (sell) IoC order for 0.1 ETH - along w/ the order, it sets up a Stop Loss (SL) and a Take Profit (TP) order for the whole size of the order diff --git a/examples/orders/create_stop_loss_market_order.py b/examples/orders/create_stop_loss_market_order.py new file mode 100644 index 0000000..f83638c --- /dev/null +++ b/examples/orders/create_stop_loss_market_order.py @@ -0,0 +1,27 @@ +import asyncio + +from examples.utils import default_example_setup + + +async def main(): + client, api_client, _ = default_example_setup() + + tx, tx_hash, err = await client.create_sl_order( + market_index=0, + client_order_index=0, + base_amount=1000, # 0.1 ETH + trigger_price=2500_00, # Trigger when ETH reaches $2500 + price=2425_00, # Lowest acceptable execution price + is_ask=True, + reduce_only=True, + ) + print(f"Create Stop Loss Order {tx=} {tx_hash=} {err=}") + if err is not None: + raise Exception(err) + + await client.close() + await api_client.close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/examples/orders/create_take_profit_market_order.py b/examples/orders/create_take_profit_market_order.py new file mode 100644 index 0000000..ba21640 --- /dev/null +++ b/examples/orders/create_take_profit_market_order.py @@ -0,0 +1,27 @@ +import asyncio + +from examples.utils import default_example_setup + + +async def main(): + client, api_client, _ = default_example_setup() + + tx, tx_hash, err = await client.create_tp_order( + market_index=0, + client_order_index=0, + base_amount=1000, # 0.1 ETH + trigger_price=3500_00, # Trigger when ETH reaches $3500 + price=3395_00, # Lowest acceptable execution price + is_ask=True, + reduce_only=True, + ) + print(f"Create Take Profit Order {tx=} {tx_hash=} {err=}") + if err is not None: + raise Exception(err) + + await client.close() + await api_client.close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/examples/orders/create_twap_market_order.py b/examples/orders/create_twap_market_order.py new file mode 100644 index 0000000..3d8cbfc --- /dev/null +++ b/examples/orders/create_twap_market_order.py @@ -0,0 +1,31 @@ +import asyncio +import time + +from examples.utils import default_example_setup + + +async def main(): + client, api_client, _ = default_example_setup() + + tx, tx_hash, err = await client.create_order( + market_index=0, + client_order_index=0, + base_amount=2100, # 0.21 ETH + price=4000_00, # Highest acceptable price for each market order + is_ask=False, + order_type=client.ORDER_TYPE_TWAP, + time_in_force=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME, + reduce_only=False, + trigger_price=0, + order_expiry=int(time.time() * 1000) + 10 * 60 * 1000, + ) + print(f"Create TWAP Order {tx=} {tx_hash=} {err=}") + if err is not None: + raise Exception(err) + + await client.close() + await api_client.close() + + +if __name__ == "__main__": + asyncio.run(main()) From 14a43db5e9cdb98bce82b90174641ffdb9855e0a Mon Sep 17 00:00:00 2001 From: mihaimarcu Date: Fri, 4 Sep 2026 13:04:09 +0300 Subject: [PATCH 2/2] changes to reflect the stop loss and take profit --- .../orders/create_stop_loss_market_order.py | 71 +++++++++++++++---- .../orders/create_take_profit_market_order.py | 71 +++++++++++++++---- 2 files changed, 112 insertions(+), 30 deletions(-) diff --git a/examples/orders/create_stop_loss_market_order.py b/examples/orders/create_stop_loss_market_order.py index f83638c..460fed8 100644 --- a/examples/orders/create_stop_loss_market_order.py +++ b/examples/orders/create_stop_loss_market_order.py @@ -1,26 +1,67 @@ import asyncio +import lighter + from examples.utils import default_example_setup +async def wait_for_long_position(api_client, account_index, market_index): + account_api = lighter.AccountApi(api_client) + + for _ in range(20): + response = await account_api.account( + by="index", + value=str(account_index), + active_only=True, + ) + if any( + position.market_id == market_index + and position.sign == 1 + and float(position.position) > 0 + for account in response.accounts + for position in account.positions + ): + return + await asyncio.sleep(0.5) + + raise RuntimeError("The entry order did not create a long position") + + async def main(): client, api_client, _ = default_example_setup() - tx, tx_hash, err = await client.create_sl_order( - market_index=0, - client_order_index=0, - base_amount=1000, # 0.1 ETH - trigger_price=2500_00, # Trigger when ETH reaches $2500 - price=2425_00, # Lowest acceptable execution price - is_ask=True, - reduce_only=True, - ) - print(f"Create Stop Loss Order {tx=} {tx_hash=} {err=}") - if err is not None: - raise Exception(err) - - await client.close() - await api_client.close() + try: + market_index = 0 + base_amount = 500 + + entry_tx, entry_response, err = await client.create_market_order( + market_index=market_index, + client_order_index=0, + base_amount=base_amount, + avg_execution_price=4000_00, + is_ask=False, + ) + print(f"Create Entry Order {entry_tx=} {entry_response=} {err=}") + if err is not None: + raise Exception(err) + + await wait_for_long_position(api_client, client.account_index, market_index) + + tx, tx_hash, err = await client.create_sl_order( + market_index=market_index, + client_order_index=1, + base_amount=base_amount, + trigger_price=2500_00, + price=2425_00, + is_ask=True, + reduce_only=True, + ) + print(f"Create Stop Loss Order {tx=} {tx_hash=} {err=}") + if err is not None: + raise Exception(err) + finally: + await client.close() + await api_client.close() if __name__ == "__main__": diff --git a/examples/orders/create_take_profit_market_order.py b/examples/orders/create_take_profit_market_order.py index ba21640..cbc3125 100644 --- a/examples/orders/create_take_profit_market_order.py +++ b/examples/orders/create_take_profit_market_order.py @@ -1,26 +1,67 @@ import asyncio +import lighter + from examples.utils import default_example_setup +async def wait_for_long_position(api_client, account_index, market_index): + account_api = lighter.AccountApi(api_client) + + for _ in range(20): + response = await account_api.account( + by="index", + value=str(account_index), + active_only=True, + ) + if any( + position.market_id == market_index + and position.sign == 1 + and float(position.position) > 0 + for account in response.accounts + for position in account.positions + ): + return + await asyncio.sleep(0.5) + + raise RuntimeError("The entry order did not create a long position") + + async def main(): client, api_client, _ = default_example_setup() - tx, tx_hash, err = await client.create_tp_order( - market_index=0, - client_order_index=0, - base_amount=1000, # 0.1 ETH - trigger_price=3500_00, # Trigger when ETH reaches $3500 - price=3395_00, # Lowest acceptable execution price - is_ask=True, - reduce_only=True, - ) - print(f"Create Take Profit Order {tx=} {tx_hash=} {err=}") - if err is not None: - raise Exception(err) - - await client.close() - await api_client.close() + try: + market_index = 0 + base_amount = 500 + + entry_tx, entry_response, err = await client.create_market_order( + market_index=market_index, + client_order_index=0, + base_amount=base_amount, + avg_execution_price=4000_00, + is_ask=False, + ) + print(f"Create Entry Order {entry_tx=} {entry_response=} {err=}") + if err is not None: + raise Exception(err) + + await wait_for_long_position(api_client, client.account_index, market_index) + + tx, tx_hash, err = await client.create_tp_order( + market_index=market_index, + client_order_index=1, + base_amount=base_amount, + trigger_price=3500_00, + price=3395_00, + is_ask=True, + reduce_only=True, + ) + print(f"Create Take Profit Order {tx=} {tx_hash=} {err=}") + if err is not None: + raise Exception(err) + finally: + await client.close() + await api_client.close() if __name__ == "__main__":