|
| 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()) |
0 commit comments