What We’ll Build Together
In this tutorial, we’ll build a small Python trading script against the DerivaDEX testnet. By the end, you will have a script that:
- connects to your funded
main strategy
- prints a live strategy snapshot
- buys
0.1 ETHP at market
- places a resting limit buy order 10% below the current mark price
- cancels that resting order
We’ll use testnet so you can see one complete trade lifecycle without touching real funds.
Prerequisites
- Python
3.14
- a funded
main strategy on DerivaDEX testnet for the same wallet you will trade with
- a Sepolia RPC URL, which is the HTTPS endpoint your script uses to reach Ethereum
- the private key for that testnet wallet
If you still need to fund the strategy, work through Your First Trade on DerivaDEX first. That tutorial gets collateral into main, which this script uses as-is.
Step 1: Install the Python client
Open a terminal and create a fresh working directory:
You should be able to confirm the environment with:
You should see Python 3.14.x and the installed ddx-python package details.
Step 2: Save your testnet connection settings
Create a file called config.toml in that same directory:
Replace YOUR_INFURA_PROJECT_ID with your own Sepolia RPC project, or swap in another Sepolia RPC URL.
At this point, your working directory should contain:
Step 3: Connect to testnet and print your strategy
Now create rest_quickstart.py with this starting version:
This first version does only two things: it opens the DerivaDEX client, then asks for the main strategy that belongs to your wallet.
Use the private key for the same wallet that owns the funded testnet strategy, then run the script:
You should see output in this order:
Connected as trader ...
Strategy response
The JSON printed under Strategy response is your confirmation that the client is connected to the right wallet and can read the strategy you funded earlier.
From here on, each request follows the same path: your script builds the order locally, your wallet signs it, and the client sends only the encrypted request to DerivaDEX.
Step 4: Submit your first market order
Now we’ll teach the script to buy 0.1 ETHP at market.
First, add these imports and constants near the top of rest_quickstart.py:
Then add this block inside main(), directly after print_json("Strategy response", strategy_response):
client.signed.get_nonce() supplies a uniqueness salt for this signed intent. Replay safety comes from the signed clientTimestampMs and recvWindowMs fields; client.signed.place_order(...) overwrites the replay placeholders after it fetches operator time from /v2/time, signs the intent, and encrypts the request.
Run the script again:
You should now see a third label in the output:
Market order submitted
The JSON under that label is your confirmation that DerivaDEX accepted the signed market order request.
Note: Each time you run the script from here onward, the market-order block will submit another 0.1 ETHP market buy. That is fine on testnet, but if you want to keep working on the later steps without adding more position, comment out the market-order block before you run the script again.
Step 5: Place and cancel a resting limit order
Next, we’ll fetch the live mark price and use it to place a second buy order well below the market so it rests on the order book instead of filling immediately. The mark price is the fair price DerivaDEX uses for margin and risk checks.
First, update the intent import so it includes CancelOrderIntent:
Then add this block directly after the market-order block:
This version reads the current ETHP mark price, multiplies it by 0.9, and rounds to a whole dollar before placing the limit order. That keeps the order about 10% below the market, which usually leaves it resting on the order book instead of filling right away.
The five-second pause gives the limit order a moment to appear on the order book before the cancel request goes out. limit_order_intent.hash() gives the cancel request the exact order hash it needs to target that resting order.
Run the script one more time:
You should now see the full sequence:
Connected as trader ...
Strategy response
Market order submitted
Current ETHP mark price: ...
Limit order submitted
Cancel request submitted
Once you can produce that sequence reliably, you have a working baseline script for DerivaDEX’s API trading flow.
Step 7: Watch the trade land in testnet
If you keep the DerivaDEX testnet open in another tab while you run the script, you can usually watch the results land:
Positions shows the 0.1 ETHP market buy after it fills
Open Orders shows the resting limit buy briefly on the order book, then drops it after the cancel request lands
That gives you both views of the same workflow: terminal output from the script and trading state inside the exchange UI.
Lesson complete
You have now:
- connected the DerivaDEX Python client to testnet
- loaded strategy state for your trading wallet
- submitted a market order
- priced and placed a resting limit order from the live mark price
- canceled that resting order cleanly
You now have a working Python baseline for DerivaDEX API trading on testnet.Last modified on June 22, 2026