This project is a grid bot trading application developed for Lighter.xyz using the Lighter Python SDK. The application implements a grid trading strategy that allows for automated trading based on predefined parameters.
- Grid Trading Strategy: Automatically places buy and sell orders at calculated price levels
- Real-time Market Data: Fetches order book data from Lighter.xyz
- Configurable Parameters: Easy configuration of grid size, order size, trading pair, etc.
- Logging: Comprehensive logging for monitoring and debugging
- Error Handling: Robust error handling and recovery mechanisms
lighter-grid-bot
├── src
│ ├── main.py # Entry point of the application
│ ├── bot # Contains bot-related logic
│ │ ├── __init__.py
│ │ ├── grid_bot.py # Implements the GridBot class
│ │ └── strategy.py # Defines trading strategies
│ ├── lighter # Contains Lighter SDK interactions
│ │ ├── __init__.py
│ │ ├── client.py # Client class for Lighter SDK
│ │ └── models.py # Data models for trading actions
│ ├── utils # Utility functions and configurations
│ │ ├── __init__.py
│ │ ├── config.py # Configuration handling
│ │ └── logger.py # Logging setup
│ └── tests # Unit tests for the application
│ ├── __init__.py
│ ├── test_bot.py # Tests for the GridBot class
│ └── test_client.py # Tests for the Client class
├── requirements.txt # Project dependencies
├── config.yaml # Configuration settings for the grid bot
├── .env.example # Example environment variables
├── .gitignore # Files to ignore in version control
└── README.md # Project documentation
git clone <repository-url>
cd lighter-grid-bot
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows use `.venv\\Scripts\\activate`
# Install dependencies
pip install -r requirements.txtEdit config.yaml with your trading parameters:
trading:
enabled: true
grid_size: 10 # Number of grid levels
take_profit: 0.02 # 2% take profit
stop_loss: 0.01 # 1% stop loss
api_key: "YOUR_ACTUAL_API_KEY"
api_secret: "YOUR_ACTUAL_API_SECRET"
trading_pair: "BTC/USD" # Trading pair
order_size: 0.01 # Order size in base currency
max_orders: 5 # Maximum active orders
logging:
level: "INFO"
file: "logs/trading.log"
database:
uri: "sqlite:///trading.db"- Create an account on Lighter.xyz
- Generate API keys from your account settings
- Copy
.env.exampleto.envand update with your credentials:cp .env.example .env
- Edit
.envwith your actual API credentials:PUBLIC_KEY=your_actual_public_key PRIVATE_KEY=your_actual_private_key KEY_ID=your_key_id BASE_URL=mainnet.zklighter.elliot.ai
The bot uses two configuration files:
-
.env- For sensitive API credentials (gitignored)PUBLIC_KEY: Your Lighter API public keyPRIVATE_KEY: Your Lighter API private keyKEY_ID: Your API key IDBASE_URL: Lighter API endpoint (mainnet.zklighter.elliot.ai)
-
config.yaml- For trading parametersmarket_id: Market to trade ongrid_levels: Number of grid levelsgrid_spacing: Price spacing between grid levelsorder_size: Size of each grid ordermin_price: Minimum price for gridmax_price: Maximum price for grid
Example config.yaml:
market_id: "ETH-USDC"
grid_levels: 10
grid_spacing: 50.0
order_size: 0.1
min_price: 2500.0
max_price: 3500.0Run the test script to verify everything works:
python test_bot.pypython src/main.pyThe grid bot works by:
- Calculating Grid Levels: Based on current market price and configured grid size
- Placing Orders: Buy orders below current price, sell orders above current price
- Managing Orders: Continuously monitoring and adjusting orders as market moves
- Profit Taking: Closing positions when profit targets are reached
- Current BTC price: $50,000
- Grid size: 10 levels
- Price step: 1% ($500)
- Buy orders at: $49,500, $49,000, $48,500, etc.
- Sell orders at: $50,500, $51,000, $51,500, etc.
| Parameter | Description | Default |
|---|---|---|
grid_size |
Number of grid levels | 10 |
take_profit |
Profit target percentage | 0.02 (2%) |
stop_loss |
Stop loss percentage | 0.01 (1%) |
trading_pair |
Trading pair (e.g., BTC/USD) | BTC/USD |
order_size |
Size of each order | 0.01 |
max_orders |
Maximum active orders | 5 |
- Start with small order sizes
- Use stop-loss to limit potential losses
- Monitor the bot regularly, especially during high volatility
- Test with paper trading/simulated environment first
- API Connection Issues: Check your API credentials and internet connection
- Order Placement Errors: Verify you have sufficient balance
- Market Data Errors: Check if the trading pair is available on Lighter
Check the log file for detailed information:
tail -f logs/trading.logContributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
This software is for educational purposes only. Use at your own risk. Cryptocurrency trading involves significant risk and may not be suitable for all investors. Always test with small amounts first and never invest more than you can afford to lose.
-
Setup Environment:
cp .env.example .env # Edit .env with your actual API credentials -
Configure Trading Parameters:
# Edit config.yaml with your trading strategy nano config.yaml -
Run the Bot:
python src/main.py
The bot will:
- Load configuration from
.envandconfig.yaml - Connect to Lighter API
- Place grid orders based on your parameters
- Monitor and manage orders automatically