pyquotex is a Python library designed to easily integrate with the Quotex API, enabling automated trading operations. Fully open-source and licensed under MIT, the library provides features like order execution, balance checking, real-time market data collection, and more. Perfect for traders and developers looking to build efficient and customized solutions.
To start using PyQuotex, you first need to initialize the client with your credentials:
from quotexapi.stable_api import Quotex
client = Quotex(
email="your_email@gmail.com",
password="your_password",
lang="en", # Default language (es=Spanish, en=English, pt=Portuguese)
)
# Optional: Enable debugging logs
client.debug_ws_enable = True
user_agent
: Customize the User-Agent (uses a predefined one by default)root_path
: Root directory for storing filesuser_data_dir
: Directory for user dataasset_default
: Default currency pair (e.g., “EURUSD”)period_default
: Default period in seconds (e.g., 60)The connection is performed asynchronously. Here are two ways to establish the connection:
import asyncio
async def connect():
check_connect, message = await client.connect()
if check_connect:
print("Connection successful!")
# Your code here
else:
print(f"Connection error: {message}")
# Execute
asyncio.run(connect())
async def connect_with_retries(attempts=5):
check_connect, message = await client.connect()
if not check_connect:
attempt = 0
while attempt <= attempts:
if not await client.check_connect():
check_connect, message = await client.connect()
if check_connect:
print("Reconnection successful!")
break
else:
attempt += 1
print(f"Retrying connection {attempt} of {attempts}")
await asyncio.sleep(5)
return check_connect, message
PyQuotex automatically handles sessions and saves data in a session.json
file. You can manually configure session data:
client.set_session(
user_agent="Mozilla/5.0...",
cookies="your_cookies", # Optional
ssid="your_ssid" # Optional
)
# Switch to practice account
client.set_account_mode("PRACTICE")
# Switch to real account
client.set_account_mode("REAL")
The system implements automatic reconnection when it detects disconnections. However, you can also handle it manually:
async def maintain_connection():
while True:
try:
if not await client.check_connect():
print("Disconnection detected, attempting to reconnect...")
check_connect, message = await client.connect()
if check_connect:
print("Reconnection successful")
else:
print(f"Reconnection error: {message}")
await asyncio.sleep(5)
await asyncio.sleep(1)
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(5)
It’s important to close the connection when you’re done:
client.close()
import asyncio
from quotexapi.stable_api import Quotex
async def complete_example():
# Initialization
client = Quotex(
email="your_email@gmail.com",
password="your_password",
lang="en"
)
try:
# Connection with retries
check_connect, message = await connect_with_retries()
if check_connect:
# Check balance
balance = await client.get_balance()
print(f"Current balance: {balance}")
# Perform operations...
except Exception as e:
print(f"Error: {e}")
finally:
client.close()
# Execute
asyncio.run(complete_example())
async/await
.