133 lines
4.2 KiB
Python
133 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Quick test to verify TradingAgents is working.
|
||
This uses the actual API (not assumptions).
|
||
"""
|
||
|
||
from decimal import Decimal
|
||
|
||
print("="*70)
|
||
print("TRADINGAGENTS QUICK TEST")
|
||
print("="*70)
|
||
|
||
# Test 1: Security
|
||
print("\n1. Testing Security Validators...")
|
||
from tradingagents.security import validate_ticker, validate_date
|
||
|
||
ticker = validate_ticker("AAPL")
|
||
date = validate_date("2024-01-15")
|
||
print(f" ✓ Validated ticker: {ticker}")
|
||
print(f" ✓ Validated date: {date}")
|
||
|
||
try:
|
||
validate_ticker("../etc/passwd")
|
||
print(" ✗ FAIL: Should reject path traversal")
|
||
except ValueError:
|
||
print(" ✓ Path traversal rejected")
|
||
|
||
# Test 2: Portfolio
|
||
print("\n2. Testing Portfolio Management...")
|
||
from tradingagents.portfolio import Portfolio, MarketOrder
|
||
|
||
# Create portfolio
|
||
portfolio = Portfolio(
|
||
initial_capital=Decimal('100000'),
|
||
commission_rate=Decimal('0.001')
|
||
)
|
||
print(f" ✓ Portfolio created: ${portfolio.cash:,.2f}")
|
||
|
||
# Buy AAPL
|
||
order = MarketOrder('AAPL', Decimal('100'))
|
||
trade = portfolio.execute_order(order, Decimal('150.00'))
|
||
print(f" ✓ Bought {trade['quantity']} AAPL @ ${trade['price']}")
|
||
|
||
# Check position
|
||
position = portfolio.get_position('AAPL')
|
||
print(f" ✓ Position: {position.quantity} shares @ ${position.avg_cost_basis:.2f}")
|
||
|
||
# Calculate value with new price
|
||
new_prices = {'AAPL': Decimal('155.00')}
|
||
total_value = portfolio.total_value(new_prices)
|
||
pnl = portfolio.unrealized_pnl(new_prices)
|
||
print(f" ✓ Portfolio value: ${total_value:,.2f}")
|
||
print(f" ✓ Unrealized P&L: ${pnl:,.2f}")
|
||
|
||
# Test 3: Performance Metrics
|
||
print("\n3. Testing Performance Analytics...")
|
||
# Make another trade to generate metrics
|
||
order2 = MarketOrder('MSFT', Decimal('50'))
|
||
trade2 = portfolio.execute_order(order2, Decimal('300.00'))
|
||
print(f" ✓ Bought {trade2['quantity']} MSFT @ ${trade2['price']}")
|
||
|
||
# Get metrics
|
||
metrics = portfolio.get_performance_metrics(prices={'AAPL': Decimal('155'), 'MSFT': Decimal('310')})
|
||
print(f" ✓ Total return: {metrics.total_return:.2%}")
|
||
print(f" ✓ Number of trades: {metrics.total_trades}")
|
||
|
||
# Test 4: Save/Load
|
||
print("\n4. Testing Persistence...")
|
||
portfolio.save('test_portfolio.json')
|
||
print(" ✓ Portfolio saved")
|
||
|
||
loaded = Portfolio.load('test_portfolio.json')
|
||
print(f" ✓ Portfolio loaded: ${loaded.cash:,.2f}")
|
||
print(f" ✓ Positions: {len(loaded.get_all_positions())}")
|
||
|
||
# Test 5: Orders
|
||
print("\n5. Testing Order Types...")
|
||
from tradingagents.portfolio import LimitOrder, StopLossOrder
|
||
|
||
limit = LimitOrder('GOOGL', Decimal('10'), limit_price=Decimal('140.00'))
|
||
print(f" ✓ Limit order created: {limit.ticker} @ ${limit.limit_price}")
|
||
|
||
stop = StopLossOrder('AAPL', Decimal('100'), stop_price=Decimal('145.00'))
|
||
print(f" ✓ Stop-loss order created: {stop.ticker} @ ${stop.stop_price}")
|
||
|
||
# Test 6: Backtesting (basic check - won't run full backtest)
|
||
print("\n6. Testing Backtesting Framework...")
|
||
try:
|
||
from tradingagents.backtest import Backtester, BacktestConfig, BuyAndHoldStrategy
|
||
|
||
config = BacktestConfig(
|
||
initial_capital=Decimal('100000'),
|
||
start_date='2024-01-01',
|
||
end_date='2024-01-31'
|
||
)
|
||
print(f" ✓ Backtest config created")
|
||
|
||
strategy = BuyAndHoldStrategy()
|
||
print(f" ✓ Strategy created: {strategy.name}")
|
||
|
||
backtester = Backtester(config)
|
||
print(f" ✓ Backtester initialized")
|
||
|
||
print(" ℹ Full backtest requires network (skipping)")
|
||
|
||
except ImportError as e:
|
||
print(f" ⚠ Backtest import issue: {e}")
|
||
|
||
# Test 7: Integration
|
||
print("\n7. Testing Integration Layer...")
|
||
try:
|
||
from tradingagents.portfolio.integration import execute_agent_decision
|
||
print(" ✓ Integration module available")
|
||
except ImportError as e:
|
||
print(f" ⚠ Integration import: {e}")
|
||
|
||
# Summary
|
||
print("\n" + "="*70)
|
||
print("SUMMARY")
|
||
print("="*70)
|
||
print("✓ Security validators working")
|
||
print("✓ Portfolio management working")
|
||
print("✓ Performance analytics working")
|
||
print("✓ Persistence working")
|
||
print("✓ Order types working")
|
||
print("✓ Backtesting framework available")
|
||
print("\n🎉 TradingAgents is ready to use!")
|
||
print("\nNext steps:")
|
||
print(" 1. Run examples: python examples/portfolio_example.py")
|
||
print(" 2. Run tests: pytest tests/portfolio/ -v")
|
||
print(" 3. Read docs: tradingagents/portfolio/README.md")
|
||
print("="*70)
|