Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions examples/orders/create_stop_loss_market_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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()

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__":
asyncio.run(main())
68 changes: 68 additions & 0 deletions examples/orders/create_take_profit_market_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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()

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__":
asyncio.run(main())
31 changes: 31 additions & 0 deletions examples/orders/create_twap_market_order.py
Original file line number Diff line number Diff line change
@@ -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())
Loading