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
49 changes: 49 additions & 0 deletions examples/cancel_all_orders_single_market.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
from utils import default_example_setup


# Scope a cancel-all to a single market.
#
# cancel_all_market_index:
# NIL_MARKET_INDEX (255, default) - cancel resting orders across ALL markets
# 0..254 - cancel resting orders only in that perp market
#
# Note: a non-nil market index is only valid for an immediate cancel-all
# (TIME_IN_FORCE_IMMEDIATE_OR_CANCEL), not for a scheduled / dead-man's-switch one.
async def main():
client, api_client, _ = default_example_setup()
client.check_client()

market_index = 0

# cancel all of our resting orders, but only in market 0 (immediate uses timestamp_ms=0)
api_key_index, nonce = client.nonce_manager.next_nonce()
tx, tx_hash, err = await client.cancel_all_orders(
time_in_force=client.CANCEL_ALL_TIF_IMMEDIATE,
timestamp_ms=0,
cancel_all_market_index=market_index,
nonce=nonce,
api_key_index=api_key_index,
)
print(f"Cancel All (market {market_index}) {tx=} {tx_hash=} {err=}")
if err is not None:
raise Exception(err)

# for reference: omit cancel_all_market_index (defaults to all markets)
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
tx, tx_hash, err = await client.cancel_all_orders(
time_in_force=client.CANCEL_ALL_TIF_IMMEDIATE,
timestamp_ms=0,
nonce=nonce,
api_key_index=api_key_index,
)
print(f"Cancel All (all markets) {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())
78 changes: 78 additions & 0 deletions examples/self_trade_create_modify_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import asyncio
import time
from utils import default_example_setup


# Self-trade prevention (STP) on order creation / modification.
#
# self_trade_behavior_mode: what happens when your incoming order would match
# one of your own resting orders.
# EXPIRE_MAKER (0, default) - cancel your resting (maker) order
# EXPIRE_TAKER (1) - cancel the incoming (taker) order
# EXPIRE_BOTH (2) - cancel both
# REDUCE (3) - net the two against each other (no booked self-fill)
#
# self_trade_equality_mode: what counts as "yourself" for the check above.
# ACCOUNT_INDEX (0, default) - only the exact same account
# MASTER_ACCOUNT_INDEX (1) - any sub-account under the same master
#
# Notes:
# * Defaults (EXPIRE_MAKER + ACCOUNT_INDEX) reproduce the previous behavior and
# do not change the signed payload.
# * REDUCE is not allowed together with MASTER_ACCOUNT_INDEX.
# * Self-trade modes cannot be combined with integrator fees on the same tx.
async def main():
client, api_client, _ = default_example_setup()
market_index = 156

try:
client.check_client()

base_client_order_index = int(time.time() * 1000)
resting_bid_order_index = base_client_order_index
incoming_ask_order_index = base_client_order_index + 1

api_key_index, nonce = client.nonce_manager.next_nonce()
tx, tx_hash, err = await client.create_order(
market_index=market_index,
client_order_index=resting_bid_order_index,
base_amount=500,
price=95_200,
is_ask=False,
order_type=client.ORDER_TYPE_LIMIT,
time_in_force=client.ORDER_TIME_IN_FORCE_POST_ONLY,
reduce_only=False,
trigger_price=0,
nonce=nonce,
api_key_index=api_key_index,
)
print(f"Resting Bid {tx=} {tx_hash=} {err=}")
if err is not None:
raise Exception(err)

api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
tx, tx_hash, err = await client.create_order(
market_index=market_index,
client_order_index=incoming_ask_order_index,
base_amount=500,
price=95_200,
is_ask=True,
order_type=client.ORDER_TYPE_LIMIT,
time_in_force=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
reduce_only=False,
trigger_price=0,
self_trade_behavior_mode=client.SELF_TRADE_BEHAVIOR_EXPIRE_BOTH,
self_trade_equality_mode=client.SELF_TRADE_EQUALITY_MASTER_ACCOUNT_INDEX,
nonce=nonce,
api_key_index=api_key_index,
)
print(f"Self-Cross Create {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())
67 changes: 67 additions & 0 deletions examples/self_trade_grouped_orders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import asyncio
from lighter.signer_client import CreateOrderTxReq
from utils import default_example_setup


# Self-trade prevention applied to a grouped (OTOCO) order.
#
# For grouped orders the self-trade modes are specified once at the group level
# (a single value for the whole batch), matching the underlying signer.
async def main():
client, api_client, _ = default_example_setup()
client.check_client()

ioc_order = CreateOrderTxReq(
MarketIndex=0,
ClientOrderIndex=0,
BaseAmount=1000, # 0.1 ETH
Price=2500_00, # $2500
IsAsk=1, # sell
Type=client.ORDER_TYPE_LIMIT,
TimeInForce=client.ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL,
ReduceOnly=0,
TriggerPrice=0,
OrderExpiry=0,
)

take_profit_order = CreateOrderTxReq(
MarketIndex=0,
ClientOrderIndex=0,
BaseAmount=0,
Price=1550_00,
IsAsk=0,
Type=client.ORDER_TYPE_TAKE_PROFIT_LIMIT,
TimeInForce=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
ReduceOnly=1,
TriggerPrice=1500_00,
OrderExpiry=-1,
)

stop_loss_order = CreateOrderTxReq(
MarketIndex=0,
ClientOrderIndex=0,
BaseAmount=0,
Price=5050_00,
IsAsk=0,
Type=client.ORDER_TYPE_STOP_LOSS_LIMIT,
TimeInForce=client.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
ReduceOnly=1,
TriggerPrice=5000_00,
OrderExpiry=-1,
)

transaction = await client.create_grouped_orders(
grouping_type=client.GROUPING_TYPE_ONE_TRIGGERS_A_ONE_CANCELS_THE_OTHER,
orders=[ioc_order, take_profit_order, stop_loss_order],
self_trade_behavior_mode=client.SELF_TRADE_BEHAVIOR_EXPIRE_TAKER,
self_trade_equality_mode=client.SELF_TRADE_EQUALITY_ACCOUNT_INDEX,
)

print("Create Grouped Order Tx:", transaction)

await client.close()
await api_client.close()


if __name__ == "__main__":
asyncio.run(main())
Loading
Loading