Skip to content

asterforum/asterforum-ai-trader

Repository files navigation

DeepSeek AI Trading Bot

A 24/7 crypto trading bot powered by DeepSeek AI that makes autonomous trading decisions every 3 minutes. The bot enforces strict risk management, position exclusivity (no hedging), and always uses OCO (stop-loss + take-profit) orders.

Features

  • AI-Powered Decisions: Uses DeepSeek AI to analyze market data and make trading decisions
  • 24/7 Operation: Runs continuously with 3-minute decision cycles
  • Risk Management: Enforces strict risk limits (2% max account risk, 0.5% per trade)
  • Position Exclusivity: Never holds both long and short positions on the same symbol
  • OCO Orders: All positions include stop-loss and take-profit orders
  • Flip Logic: Automatically closes opposite positions before opening new ones
  • Comprehensive State: Analyzes prices, indicators, funding, OI, and account data

Architecture

src/
├── config/
│   ├── config.js          # Configuration management
│   └── prompts.js         # AI system prompt and tool definitions
├── broker/
│   └── broker.js          # Exchange interface with position exclusivity
├── ai/
│   └── deepseek.js        # DeepSeek AI integration
├── data/
│   └── stateGenerator.js  # Market data and state payload generation
├── trading/
│   └── tradingLoop.js     # Main trading loop with cron scheduling
├── utils/
│   └── logger.js          # Logging utility
├── index.js               # Main application entry point
└── test.js                # Test suite

Quick Start

  1. Install Dependencies

    npm install
  2. Configure Environment

    cp env.example .env
    # Edit .env with your API keys and settings
  3. Set Up Environment Variables

    # Required
    DEEPSEEK_API_KEY=your_deepseek_api_key_here
    
    # Optional (with defaults)
    DEEPSEEK_BASE_URL=https://api.deepseek.com
    ACCOUNT_VALUE_USD=10000
    MAX_ACCOUNT_RISK_PCT=2.0
    PER_TRADE_RISK_PCT=0.5
    TRADING_ENABLED=false
  4. Test the Setup

    npm test
  5. Run the Bot

    npm start

Configuration

Environment Variables

Variable Description Default Required
DEEPSEEK_API_KEY DeepSeek AI API key - Yes
DEEPSEEK_BASE_URL DeepSeek API base URL https://api.deepseek.com No
ACCOUNT_VALUE_USD Account value in USD 10000 No
MAX_ACCOUNT_RISK_PCT Max account risk percentage 2.0 No
PER_TRADE_RISK_PCT Max risk per trade percentage 0.5 No
TRADING_ENABLED Enable actual trading false No
LOG_LEVEL Logging level info No

Risk Management

  • Account Risk: Maximum 2% of account value at risk across all positions
  • Per Trade Risk: Maximum 0.5% of account value per individual trade
  • Position Exclusivity: Only one direction per symbol (no hedging)
  • OCO Orders: All positions must have stop-loss and take-profit orders

AI Decision Making

The bot uses DeepSeek AI with a comprehensive system prompt that includes:

Trading Rules

  • Long Signals: Price > EMA20 AND (MACD momentum up OR RSI(7) crosses up from <30)
  • Short Signals: Price < EMA20 AND (MACD momentum down OR RSI(7) crosses down from >70)
  • Trend Filter: If 4h trend conflicts with intraday bias, halve position size

Position Management

  • Exclusivity: Never hold both long and short on the same symbol
  • Flip Logic: Close existing position before opening opposite direction
  • Invalidation: Exit positions when invalidation conditions are met

Risk Controls

  • Size Limits: Respect maximum risk per trade and account
  • Funding Filter: Avoid entries when funding is against the trade
  • Liquidity Filter: Consider open interest changes

State Payload

Every 3 minutes, the bot generates a comprehensive state payload including:

  • Account Info: Value, balance, PnL, position count
  • Current Positions: Symbol, side, size, entry price, PnL, SL/TP
  • Market Data: Prices, technical indicators (EMA, RSI, MACD), funding rates
  • Risk Metrics: Total risk, account risk percentage, utilization
  • Market Microstructure: Open interest, volume, volatility

Tool Functions

The AI can execute these functions:

  • get_positions() - Retrieve current positions
  • place_order() - Place new order with OCO
  • close_position() - Close existing position
  • modify_orders() - Adjust stop-loss/take-profit
  • flip_position() - Close and open opposite position atomically
  • log_note() - Log decision rationale
  • set_risk() - Update risk parameters

Development

Project Structure

  • Mock Broker: Currently uses mock implementation for testing
  • Real Exchange Integration: Replace src/broker/broker.js with actual exchange API
  • Market Data: Replace mock data in src/data/stateGenerator.js with real market feeds

Testing

# Run all tests
npm test

# Test specific components
node src/test.js

Adding New Exchanges

  1. Implement exchange-specific broker in src/broker/
  2. Update configuration in src/config/config.js
  3. Add exchange-specific market data in src/data/stateGenerator.js

Safety Features

  • Trading Disabled by Default: Set TRADING_ENABLED=true to enable
  • Position Exclusivity: Enforced both in AI prompt and broker code
  • Risk Limits: Hard-coded limits prevent excessive risk
  • Error Handling: Continues operation despite individual cycle failures
  • Graceful Shutdown: Handles SIGINT/SIGTERM signals

Monitoring

The bot logs all activities including:

  • Trading cycle execution
  • AI decisions and tool calls
  • Order placements and modifications
  • Position changes
  • Error conditions

License

MIT License - see LICENSE file for details.

Disclaimer

This software is for educational purposes only. Trading cryptocurrencies involves substantial risk of loss. Use at your own risk.

About

Algorithmic trading module for AsterForum with real-time execution.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors