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


async def main():
client, api_client, _ = default_example_setup()

# Sell some ETH at $2500
# The size of the SL/TP orders will be equal to the size of the executed order

# set SL trigger price at 5000 and limit price at 5050
# set TP trigger price at 1500 and limit price at 1550
# Note: set the limit price to be higher than the SL/TP trigger price to ensure the order will be filled
# If the mark price of ETH reaches 1500, there might be no one willing to sell you ETH at 1500, so trying to buy at 1550 would increase the fill rate

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,
)

# Create a One-Cancels-the-Other grouped order with a take-profit and a stop-loss order
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],
integrator_account_index=6,
integrator_taker_fee=100,
integrator_maker_fee=100,
)

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

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


if __name__ == "__main__":
asyncio.run(main())
67 changes: 67 additions & 0 deletions examples/create_modify_cancel_skip_nonce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import asyncio
from utils import default_example_setup


async def main():
client, api_client, _ = default_example_setup()
client.check_client()

# Note: change this to 2048 to trade spot ETH. Make sure you have at least 0.1 ETH to trade spot.
market_index = 0

# create order
api_key_index, base_nonce = client.nonce_manager.next_nonce()
nonce_interval = 1000
tx, tx_hash, err = await client.create_order(
market_index=market_index,
client_order_index=123,
base_amount=1000, # 0.1 ETH
price=4050_00, # $4050
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,
skip_nonce=1,
nonce=base_nonce,
api_key_index=api_key_index,
)
print(f"Create Order {tx=} {tx_hash=} {err=}")
if err is not None:
raise Exception(err)

## modify order
# use the same API key so the TX goes after the create order TX
tx, tx_hash, err = await client.modify_order(
market_index=market_index,
order_index=123,
base_amount=1100, # 0.11 ETH
price=4100_00, # $4100
trigger_price=0,
skip_nonce=1,
nonce=base_nonce + nonce_interval,
api_key_index=api_key_index,
)
print(f"Modify Order {tx=} {tx_hash=} {err=}")
if err is not None:
raise Exception(err)

## cancel order
# use the same API key so the TX goes after the modify order TX
tx, tx_hash, err = await client.cancel_order(
market_index=market_index,
order_index=123,
skip_nonce=1,
nonce=base_nonce + 2 * nonce_interval,
api_key_index=api_key_index,
)
print(f"Cancel 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())
30 changes: 30 additions & 0 deletions examples/create_order_skip_nonce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import asyncio
from utils import default_example_setup


async def main():
client, api_client, _ = default_example_setup()
client.check_client()

# Note: change this to 2048 to trade spot ETH. Make sure you have at least 0.1 ETH to trade spot.
market_index = 0

tx, tx_hash, err = await client.create_market_order(
market_index=market_index,
client_order_index=0,
base_amount=1000, # 0.1 ETH
avg_execution_price=4000_00, # $4000 -- worst acceptable price for the order
is_ask=False,
skip_nonce=1,
nonce=2222,
)
print(f"Create 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
Loading