Skip to content

Commit 49911e1

Browse files
committed
some better examples
1 parent cec6fba commit 49911e1

4 files changed

Lines changed: 119 additions & 10 deletions

File tree

examples/README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,44 @@
66
- the eth private key will only be used in the Py SDK to sign a message
77
- the eth private key is not required in order to trade on the platform
88
- the eth private key is not passed to the binary
9-
- copy the output of the script and post it into `create_cancel_order.py`
9+
- save the output of the script as you'll need to use it on all scripts
1010
- the output should look like
1111
```
1212
BASE_URL = 'https://testnet.zklighter.elliot.ai'
1313
API_KEY_PRIVATE_KEY = '0xea5d2eca5be67eca056752eaf27b173518b8a5550117c09d2b58c7ea7d306cc4426f913ccf27ab19'
1414
ACCOUNT_INDEX = 595
1515
API_KEY_INDEX = 1
1616
```
17-
- start trading using
18-
- `create_cancel_order.py` has an example which created an order on testnet & cancels it
19-
- you'll need to set up both your account index, api key index & API Key private key
17+
## Start trading on testnet
18+
For each example, you'll need to setup the base url / api_key_private_key / account_index and api_key index using the output of the `setup.py`
19+
20+
- `create_modify_cancel_order.py`
21+
- creates an ask (sell) order for 0.1 ETH @ $4050
22+
- modified the order and increases the size to 0.11 ETH and increases the price to $4100
23+
- cancels the order
24+
- Note: all of these operations use the client order index of the order. You can use the order from the exchange as well
25+
26+
- `create_grouped_ioc_with_attached_sl_tp.py`
27+
- creates an ask (sell) IoC order for 0.1 ETH
28+
- along w/ the order, it sets up a Stop Loss (SL) and a Take Profit (TP) order for the whole size of the order
29+
- the size of the SL/TP will be equal to the executed size of the order
30+
- the SL/TP orders are canceled when the sign of your position changes
31+
32+
- `create_position_tied_sl_tl.py`
33+
- creates a bid (buy) Stop Loss (SL) and a Take Profit (TP) to close your short position
34+
- the size of the orders will be for your whole position (because BaseAmount=0)
35+
- the orders will grow / shrink as you accumulate more position
36+
- the SL/TP orders are canceled when the sign of your position changes
37+
38+
### On SL/TP orders
39+
SL/TP orders need to be configured beyond just setting the trigger price. When the trigger price is set,
40+
the order will just be executed, like a normal order. This means that a market order, for example, might not have enough slippage! \
41+
Let's say that you have a 1 BTC long position, and the current price is $110'000. \
42+
You want to set up a take profit at $120'000
43+
- order should be an ask (sell) order, to close your position
44+
- the trigger price should be $120'000
45+
46+
What about the order type
2047

2148
## Setup steps for mainnet
2249
- deposit money on Lighter to create an account first
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import asyncio
2+
import lighter
3+
import logging
4+
5+
from lighter.signer_client import CreateOrderTxReq
6+
7+
logging.basicConfig(level=logging.DEBUG)
8+
9+
# The API_KEY_PRIVATE_KEY provided belongs to a dummy account registered on Testnet.
10+
# It was generated using the setup_system.py script, and serves as an example.
11+
BASE_URL = "https://testnet.zklighter.elliot.ai"
12+
API_KEY_PRIVATE_KEY = "0xed636277f3753b6c0275f7a28c2678a7f3a95655e09deaebec15179b50c5da7f903152e50f594f7b"
13+
ACCOUNT_INDEX = 65
14+
API_KEY_INDEX = 3
15+
16+
17+
async def main():
18+
client = lighter.SignerClient(
19+
url=BASE_URL,
20+
private_key=API_KEY_PRIVATE_KEY,
21+
account_index=ACCOUNT_INDEX,
22+
api_key_index=API_KEY_INDEX,
23+
)
24+
25+
ioc_order = CreateOrderTxReq(
26+
MarketIndex=0,
27+
ClientOrderIndex=0,
28+
BaseAmount=1000, # 0.1 ETH
29+
Price=300000, # $3000
30+
IsAsk=1, # sell
31+
Type=lighter.SignerClient.ORDER_TYPE_LIMIT,
32+
TimeInForce=lighter.SignerClient.ORDER_TIME_IN_FORCE_IMMEDIATE_OR_CANCEL,
33+
ReduceOnly=0,
34+
TriggerPrice=0,
35+
OrderExpiry=0,
36+
)
37+
38+
# Create a One-Cancels-the-Other grouped order with a take-profit and a stop-loss order
39+
take_profit_order = CreateOrderTxReq(
40+
MarketIndex=0,
41+
ClientOrderIndex=0,
42+
BaseAmount=0,
43+
Price=300000,
44+
IsAsk=0,
45+
Type=lighter.SignerClient.ORDER_TYPE_TAKE_PROFIT_LIMIT,
46+
TimeInForce=lighter.SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
47+
ReduceOnly=1,
48+
TriggerPrice=300000,
49+
OrderExpiry=-1,
50+
)
51+
52+
stop_loss_order = CreateOrderTxReq(
53+
MarketIndex=0,
54+
ClientOrderIndex=0,
55+
BaseAmount=0,
56+
Price=500000,
57+
IsAsk=0,
58+
Type=lighter.SignerClient.ORDER_TYPE_STOP_LOSS_LIMIT,
59+
TimeInForce=lighter.SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
60+
ReduceOnly=1,
61+
TriggerPrice=500000,
62+
OrderExpiry=-1,
63+
)
64+
65+
transaction = await client.create_grouped_orders(
66+
grouping_type=lighter.SignerClient.GROUPING_TYPE_ONE_TRIGGERS_A_ONE_CANCELS_THE_OTHER,
67+
orders=[ioc_order, take_profit_order, stop_loss_order],
68+
)
69+
70+
print("Create Grouped Order Tx:", transaction)
71+
await client.close()
72+
73+
if __name__ == "__main__":
74+
asyncio.run(main())
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ async def main():
3636
tx, tx_hash, err = await client.create_order(
3737
market_index=0,
3838
client_order_index=123,
39-
base_amount=100000,
40-
price=405000,
39+
base_amount=1000, # 0.1 ETH
40+
price=405000, # $4050
4141
is_ask=True,
4242
order_type=lighter.SignerClient.ORDER_TYPE_LIMIT,
4343
time_in_force=lighter.SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
@@ -48,8 +48,15 @@ async def main():
4848
if err is not None:
4949
raise Exception(err)
5050

51-
auth, err = client.create_auth_token_with_expiry(lighter.SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY)
52-
print(f"{auth=}")
51+
# create order
52+
tx, tx_hash, err = await client.modify_order(
53+
market_index=0,
54+
order_index=123,
55+
base_amount=1100, # 0.11 ETH
56+
price=410000, # $4100
57+
trigger_price=0,
58+
)
59+
print(f"Modify Order {tx=} {tx_hash=} {err=}")
5360
if err is not None:
5461
raise Exception(err)
5562

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ACCOUNT_INDEX = 65
1414
API_KEY_INDEX = 3
1515

16+
1617
async def main():
1718
client = lighter.SignerClient(
1819
url=BASE_URL,
@@ -25,7 +26,7 @@ async def main():
2526
take_profit_order = CreateOrderTxReq(
2627
MarketIndex=0,
2728
ClientOrderIndex=0,
28-
BaseAmount=1000,
29+
BaseAmount=0,
2930
Price=300000,
3031
IsAsk=0,
3132
Type=lighter.SignerClient.ORDER_TYPE_TAKE_PROFIT_LIMIT,
@@ -38,7 +39,7 @@ async def main():
3839
stop_loss_order = CreateOrderTxReq(
3940
MarketIndex=0,
4041
ClientOrderIndex=0,
41-
BaseAmount=1000,
42+
BaseAmount=0,
4243
Price=500000,
4344
IsAsk=0,
4445
Type=lighter.SignerClient.ORDER_TYPE_STOP_LOSS_LIMIT,

0 commit comments

Comments
 (0)