Created
September 16, 2025 11:55
-
-
Save miohtama/fb3a942d84d658d92a50ccc67cfcf1d9 to your computer and use it in GitHub Desktop.
GMX Python API example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# github.com/tradingstrategy-ai/web3-ethereum-defi | |
from eth_defi.gmx.create_increase_order import IncreaseOrder | |
from eth_defi.gmx.trading import GMXTrading | |
from eth_defi.provider.multi_provider import create_multi_provider_web3 | |
from eth_defi.gmx.config import GMXConfig | |
from eth_defi.hotwallet import HotWallet | |
from eth_account import Account | |
import os | |
# Setup connection to Arbitrum | |
json_rpc_url = os.environ.get("JSON_RPC_ARBITRUM") | |
if not json_rpc_url: | |
json_rpc_url = input("Please enter your Arbitrum JSON-RPC URL: ") | |
web3 = create_multi_provider_web3(json_rpc_url) | |
config = GMXConfig(web3) | |
print(f"Connected to chain {web3.eth.chain_id}") | |
# initialise the trading manager | |
trading_manager = GMXTrading(config) | |
market_symbol = "ETH" | |
collateral_symbol = "USDC" | |
# Create a long position with USDC as collateral | |
# This will return a transaction object. Which then need to be signed in order to get exectued | |
increase_order_tx = trading_manager.open_position( | |
market_symbol=market_symbol, | |
collateral_symbol=collateral_symbol, | |
start_token_symbol=collateral_symbol, | |
is_long=True, | |
size_delta_usd=100, | |
leverage=2, | |
slippage_percent=0.003, | |
debug_mode=False, | |
execution_buffer=2.2, | |
) | |
private_key = os.environ.get("PRIVATE_KEY") | |
account = Account.from_key(private_key) | |
hot_wallet = HotWallet(account) | |
hot_wallet.sync_nonce(web3) | |
# This will sign the transaction | |
signed_tx = hot_wallet.sign_transaction(increase_order_tx) | |
# And finally send the transaction | |
tx_hash = web3.eth.send_transaction(signed_tx) | |
print("Trade broadcasted: {tx_hash}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment