Python SDK
The Mudrex Python SDK provides a simple interface to interact with the Mudrex Futures API. It handles authentication, request execution, and trading workflows, allowing developers to integrate and start trading with minimal setup.
Installation
pip install mudrexOptional source installation:
git clone https://github.com/mudrex/mudrex-python-sdk.git
cd mudrex-python-sdk
pip install .Requirements
- Python 3.9 or higher
Resources
Quickstart
Iintialize client
from mudrex import TradeClient
client = TradeClient(api_secret="your_api_secret")Use environment variable (optional, recommended)
You can configure your API secret using an environment variable. This is optional, but recommended for better security.
export MUDREX_API_SECRET="your_api_secret"from mudrex import TradeClient
client = TradeClient()The client automatically authenticates during initialization.
Handle authentication errors
from mudrex import TradeClient, MudrexAPIError
try:
client = TradeClient(api_secret="your_api_secret")
print("Authentication successful")
except MudrexAPIError as e:
print(e)Example failure:
from mudrex import TradeClient, MudrexAPIError
try:
client = TradeClient(api_secret="wrong_secret")
except MudrexAPIError as e:
print(e)Place your first order
from mudrex import TradeClient
client = TradeClient(api_secret="your_api_secret")
resp = client.place_order(
"BTCUSDT",
leverage=10,
quantity="0.001",
order_type="LONG",
trigger_type="MARKET",
)
print(resp.order_id)Core Operations
Set leverage
client.set_leverage("BTCUSDT", leverage=10)Get leverage
resp = client.get_leverage("BTCUSDT")
print(resp["leverage"])
print(resp.leverage)
print(resp.margin_type)Fetch positions
positions = client.get_positions(limit=20)
for position in positions:
print(position)Cancel order
client.cancel_order(order_id)Error Handling
The SDK exposes two primary error types:
| Error Type | Description |
|---|---|
MudrexAPIError | API-level errors (invalid auth, bad request, etc.) |
MudrexRequestError | Network or request failures |
Example
from mudrex import TradeClient, MudrexAPIError, MudrexRequestError
client = TradeClient(api_secret="your_api_secret")
try:
client.place_order(
"BTCUSDT",
leverage=10,
quantity="0.001",
order_type="LONG",
trigger_type="MARKET",
)
except MudrexAPIError as e:
print(f"API error [{e.code}]: {e.message}")
except MudrexRequestError as e:
print(f"Network error: {e.message}")Notes
- Authentication is handled automatically during client initialization.
- All trading operations are symbol-based (e.g.,
BTCUSDT). - Quantity is passed as a string as per SDK requirements.
- Order types follow SDK conventions such as
LONGandSHORT.
Troubleshooting
Authentication issues
- Verify API secret.
- Ensure correct environment variable if used.
Order failures
- Check symbol format (e.g.,
BTCUSDT). - Validate leverage and quantity values.
Network errors
- Retry request or check connectivity.
Updated 8 days ago
