The PriceManager
class handles real-time price updates and WebSocket connections.
from src.core.price_manager import PriceManager
price_manager = PriceManager()
start()
: Initializes WebSocket connections and starts price monitoringstop()
: Gracefully closes connections and stops monitoringregister_price_callback(callback)
: Registers a callback for price updatesregister_order_callback(callback)
: Registers a callback for order updatesget_current_price(symbol: str) -> float
: Returns current price for a symbol
The OrderManager
class handles order operations and position management.
from src.core.order_manager import OrderManager
order_manager = OrderManager()
start()
: Initializes order monitoringstop()
: Gracefully stops order monitoringhandle_price_update(symbol: str, price: float)
: Processes price updateshandle_order_update(order_data: dict)
: Processes order status updatesget_position_duration(order_id: str) -> timedelta
: Returns position age
The StateManager
class manages system state and persistence.
from src.core.state_manager import StateManager
state_manager = StateManager()
start()
: Initializes state monitoringstop()
: Gracefully stops state monitoringget_current_state() -> dict
: Returns current system statesave_state() -> bool
: Persists current state to database
from src.db.models import Order, TradePair, SystemState
- Fields: id, binance_order_id, symbol, side, price, quantity, status
- Relationships: related_orders, trade_pair
- Fields: id, buy_order_id, sell_order_id, status
- Relationships: buy_order, sell_order
- Fields: id, timestamp, state_data, status
from src.db.operations import (
create_order,
get_order_by_id,
update_order,
get_open_orders
)
create_order(data: dict) -> Order
: Creates new order recordget_order_by_id(order_id: str) -> Order
: Retrieves order by IDupdate_order(order_id: str, data: dict) -> bool
: Updates order recordget_open_orders() -> List[Order]
: Returns all open orders
from src.config.settings import (
BINANCE_API_KEY,
BINANCE_API_SECRET,
TRADING_SYMBOL,
MIN_PROFIT_PERCENTAGE,
MAX_SELL_VALUE_USDC
)
from src.config.logging_config import setup_logging
setup_logging()
from tools.manage_positions import (
list_positions,
view_position,
cancel_order
)
list_positions() -> List[dict]
: Lists all positionsview_position(order_id: str) -> dict
: Views specific positioncancel_order(order_id: str) -> bool
: Cancels an order
from src.utils.exceptions import (
OrderValidationError,
WebSocketError,
DatabaseError
)
from src.utils.recovery import (
recover_state,
reconcile_orders,
validate_system_state
)
from src.core.price_manager import PriceManager
from src.core.order_manager import OrderManager
from src.core.state_manager import StateManager
# Initialize components
price_manager = PriceManager()
order_manager = OrderManager()
state_manager = StateManager()
# Register callbacks
price_manager.register_price_callback(order_manager.handle_price_update)
price_manager.register_order_callback(order_manager.handle_order_update)
# Start components
price_manager.start()
order_manager.start()
state_manager.start()
from src.db.operations import create_order, get_order_by_id
# Create a new order
order_data = {
"symbol": "TRUMPUSDC",
"side": "BUY",
"price": 100.0,
"quantity": 1.0
}
order = create_order(order_data)
# Get order details
order = get_order_by_id(order.id)
from src.utils.exceptions import OrderValidationError
try:
# Attempt to create order
order = create_order(order_data)
except OrderValidationError as e:
# Handle validation error
logger.error(f"Order validation failed: {str(e)}")
except Exception as e:
# Handle unexpected error
logger.error(f"Unexpected error: {str(e)}")