Arbitrage Strategy¶
ArBot's arbitrage strategy engine is the core component that identifies and evaluates price differences between exchanges. This sophisticated system combines real-time data analysis, statistical methods, and risk management to find profitable trading opportunities.
How Arbitrage Works¶
Basic Concept¶
Traditional Arbitrage: Buy low on Exchange A, sell high on Exchange B, profit from the price difference.
Example:
Bitcoin Price:
Exchange A (Bybit): $43,250
Exchange B (Binance): $43,350
Opportunity: Buy on Bybit, sell on Binance
Gross Profit: $100 per Bitcoin
ArBot's Enhanced Approach¶
Real-Time Analysis: - Continuous price monitoring across all exchanges - Sub-second opportunity detection - Automatic fee and slippage calculations - Risk-adjusted profit estimation
Smart Filtering: - Trend-based opportunity selection - Premium detection and outlier filtering - Liquidity-based sizing - Market condition adaptation
Strategy Components¶
Price Data Collection¶
WebSocket Feeds:
# Real-time ticker structure
{
"symbol": "BTCUSDT",
"exchange": "binance",
"bid": 43250.50,
"ask": 43251.00,
"bid_size": 2.5,
"ask_size": 1.8,
"timestamp": 1640995200.123
}
Data Validation: - Price sanity checks (outlier detection) - Timestamp validation (freshness) - Size validation (minimum liquidity) - Cross-reference verification
Opportunity Detection¶
Spread Calculation:
def calculate_arbitrage(buy_exchange, sell_exchange):
# Get execution prices
buy_price = buy_exchange.ask # We buy at ask
sell_price = sell_exchange.bid # We sell at bid
# Calculate fees
buy_fee = buy_price * buy_exchange.taker_fee
sell_fee = sell_price * sell_exchange.taker_fee
# Account for slippage
slippage_cost = buy_price * slippage_tolerance
# Net profit calculation
gross_profit = sell_price - buy_price
total_costs = buy_fee + sell_fee + slippage_cost
net_profit = gross_profit - total_costs
return net_profit / buy_price # Profit percentage
Profitability Factors: 1. Price Spread: Difference between exchanges 2. Trading Fees: Maker/taker fees on both sides 3. Slippage: Expected price movement during execution 4. Market Impact: Effect of trade size on price
Signal Generation¶
Arbitrage Signal Structure:
@dataclass
class ArbitrageSignal:
symbol: str
buy_exchange: str
sell_exchange: str
buy_price: float
sell_price: float
profit: float
profit_percent: float
buy_size: float
sell_size: float
timestamp: float
confidence: float
Signal Quality Metrics: - Profit Percentage: Expected return - Confidence Score: Based on data quality and timing - Liquidity Score: Available size for execution - Risk Score: Market condition assessment
Advanced Features¶
Trend-Based Filtering¶
Strategy Enhancement: Traditional arbitrage doesn't consider market direction. ArBot's trend filtering only executes trades when market momentum is favorable.
Filter Modes:
Uptrend Buy Low¶
# Only trade during upward price trends
if trend == "↗":
# Buy from lower-priced exchange
# Market momentum helps secure profits
execute_arbitrage()
Downtrend Sell High¶
# Only trade during downward price trends
if trend == "↘":
# Sell to higher-priced exchange
# Downward momentum maintains spreads
execute_arbitrage()
Both Directions¶
# Trade in any trending market
if trend in ["↗", "↘"]:
# Avoid sideways/choppy markets
execute_arbitrage()
Premium Detection¶
Statistical Analysis: ArBot analyzes historical price patterns to identify exchange-specific premiums and filter out anomalous spreads.
Premium Calculation:
def detect_premium(symbol, exchange_pair, lookback_periods=100):
# Get historical spreads
historical_spreads = get_spread_history(symbol, exchange_pair, lookback_periods)
# Calculate baseline premium
baseline = median(historical_spreads)
std_dev = standard_deviation(historical_spreads)
# Current spread analysis
current_spread = get_current_spread(symbol, exchange_pair)
z_score = (current_spread - baseline) / std_dev
# Filter outliers (likely errors)
if abs(z_score) > outlier_threshold:
return "outlier" # Skip this opportunity
return baseline
Benefits: - Filters out data errors and manipulation - Identifies legitimate regional premiums - Improves signal quality - Reduces false positives
Dynamic Symbol Selection¶
Volume-Based Prioritization:
def select_dynamic_symbols(max_symbols=200):
# Get all available symbols
all_symbols = get_exchange_symbols()
# Filter by quote currency
filtered_symbols = filter_by_quote_currency(all_symbols)
# Sort by 24h volume
volume_sorted = sort_by_volume(filtered_symbols)
# Select top N symbols
return volume_sorted[:max_symbols]
Selection Criteria: 1. Trading Volume: Higher volume = better liquidity 2. Quote Currency: Focus on USDT, USDC, etc. 3. Spread History: Symbols with consistent arbitrage opportunities 4. Market Cap: Larger tokens tend to have better arbitrage
Risk-Adjusted Sizing¶
Position Sizing Logic:
def calculate_position_size(signal, available_balance):
# Base size from configuration
base_size = config.trade_amount_usd
# Adjust for confidence
confidence_multiplier = signal.confidence
# Adjust for liquidity
liquidity_limit = min(signal.buy_size, signal.sell_size) * 0.1
# Apply risk limits
max_position = config.max_position_size
# Calculate final size
adjusted_size = base_size * confidence_multiplier
position_size = min(adjusted_size, liquidity_limit, max_position)
return position_size
Strategy Configurations¶
Conservative Strategy¶
Low-Risk, High-Quality Opportunities:
{
"arbitrage": {
"min_profit_threshold": 0.01,
"max_position_size": 500.0,
"use_trend_filter": true,
"trend_filter_mode": "uptrend_buy_low",
"premium_detection": {
"enabled": true,
"outlier_threshold": 1.5
}
}
}
Characteristics: - Higher profit threshold (1.0%) - Smaller position sizes - Strict trend filtering - Conservative outlier detection
Aggressive Strategy¶
High-Frequency, Lower-Threshold Trading:
{
"arbitrage": {
"min_profit_threshold": 0.003,
"max_position_size": 2000.0,
"max_spread_age_seconds": 2.0,
"use_trend_filter": false,
"max_symbols": 300
}
}
Characteristics: - Lower profit threshold (0.3%) - Larger position sizes - Faster execution requirements - No trend filtering
Balanced Strategy¶
Moderate Risk-Reward Profile:
{
"arbitrage": {
"min_profit_threshold": 0.005,
"max_position_size": 1000.0,
"use_trend_filter": true,
"trend_filter_mode": "both",
"max_symbols": 200
}
}
Performance Metrics¶
Strategy Analytics¶
Key Performance Indicators:
{
"signals_generated": 1247,
"signals_executed": 1089,
"execution_rate": 87.3,
"avg_profit_percent": 0.0087,
"total_profit": 94.67,
"max_drawdown": 2.3,
"sharpe_ratio": 2.1,
"win_rate": 91.2
}
Signal Quality Metrics¶
Opportunity Analysis: - Signal Frequency: Opportunities per hour - Profit Distribution: Range of profit percentages - Exchange Pairs: Most profitable exchange combinations - Symbol Performance: Best performing trading pairs
Market Condition Analysis¶
Performance by Market State: - Trending Markets: Strategy performance during trends - Sideways Markets: Performance in low volatility - Volatile Markets: Performance during high volatility - Time of Day: Performance patterns by trading session
Advanced Optimizations¶
Machine Learning Integration¶
Predictive Analytics: - Price direction prediction - Spread duration forecasting - Market regime classification - Optimal execution timing
Feature Engineering:
features = {
"price_momentum": calculate_momentum(prices),
"volume_profile": analyze_volume_pattern(volumes),
"spread_history": get_spread_statistics(spreads),
"market_microstructure": analyze_order_book(orderbook)
}
Multi-Asset Arbitrage¶
Cross-Asset Opportunities: - BTC/ETH triangular arbitrage - Stablecoin arbitrage (USDT/USDC) - DeFi protocol arbitrage - Futures-spot arbitrage
Portfolio-Level Optimization¶
Risk Management: - Correlation analysis between positions - Sector exposure limits - Maximum position concentration - Dynamic hedging strategies
Backtesting and Validation¶
Historical Performance¶
Backtest Configuration:
{
"backtest": {
"start_date": "2024-01-01",
"end_date": "2024-12-31",
"initial_balance": 10000.0,
"strategy_params": {
"min_profit_threshold": 0.005,
"use_trend_filter": true
}
}
}
Validation Metrics: - Out-of-sample performance - Walk-forward analysis - Monte Carlo simulation - Stress testing scenarios
Strategy Optimization¶
Parameter Tuning: - Grid search optimization - Genetic algorithm optimization - Bayesian optimization - Ensemble methods
Implementation Best Practices¶
Development Guidelines¶
Code Quality: - Modular strategy components - Comprehensive testing - Performance optimization - Error handling
Risk Management: - Position size limits - Drawdown protection - Emergency stop mechanisms - Real-time monitoring
Deployment Considerations¶
Production Setup: - Low-latency infrastructure - Redundant data feeds - Monitoring and alerting - Backup systems
Scaling Strategies: - Horizontal scaling - Load balancing - Database optimization - Caching strategies
Troubleshooting¶
Common Issues¶
No Opportunities Found: - Check exchange connections - Verify profit thresholds - Review market conditions - Validate symbol selection
Low Profitability: - Analyze fee structures - Review slippage settings - Check execution timing - Optimize position sizing
High Error Rates: - Monitor network latency - Check API rate limits - Validate data quality - Review error handling
Performance Optimization¶
Latency Reduction: - Optimize network connections - Streamline data processing - Reduce computational overhead - Implement caching strategies
Accuracy Improvement: - Enhance data validation - Improve signal filtering - Optimize risk calculations - Refine execution logic
Strategy Evolution
Arbitrage strategies must continuously evolve as markets become more efficient. Regularly review and optimize your strategy parameters based on changing market conditions.
Market Impact
Large arbitrage trades can impact market prices and reduce profitability. Always consider your position size relative to market liquidity and adjust accordingly.
Created: July 12, 2025