This project is a Go (Golang) application designed to interact with the Binance Spot API for automated trading. It leverages the following key libraries and concepts:
- Go: Main programming language used.
- Binance API: Spot trading endpoints for placing orders, checking balances, and retrieving price information.
- godotenv: Used for loading environment variables from a
.env
file. - HMAC-SHA256: Required for signing requests to Binance.
Note: This README and the associated code were generated with the assistance of AI.
-
Environment Variables
- The bot reads
BINANCE_API_KEY
andBINANCE_API_SECRET
from a.env
file. - If either key is missing or empty, the bot exits with an error.
- The bot reads
-
Initialization
- The
init()
function loads the.env
file and checks if the required environment variables exist. - If
.env
is missing or the variables are not defined, the bot logs a fatal error and terminates.
- The
-
Ping Check
- At startup, the bot sends a
ping
request to Binance (/api/v3/ping
). - If the ping fails, the bot immediately exits. Otherwise, it proceeds to start.
- At startup, the bot sends a
-
Trading Strategy
- Retrieves 24h volume data for symbols, checks balances, and gathers kline data.
- Uses simple RSI & MACD indicators to determine buy/sell conditions, plus stop-loss and take-profit.
-
Logging
- Various log messages indicate buy/sell events, stop-loss triggers, take-profit triggers, etc.
main.go
# Contains the main trading bot logic.env
# Environment variables (not committed to version control)go.mod
# (Optional) Go module file for dependencies
-
Install Go
- Download and install from https://go.dev/dl/.
-
Create a .env File
- In the project root, create
.env
:BINANCE_API_KEY=YOUR_API_KEY BINANCE_API_SECRET=YOUR_API_SECRET
- Replace with actual values.
- In the project root, create
-
Build & Run
go build -o binance_bot main.go ./binance_bot Or directly: go run main.go
-
Portfolio
Inmain.go
, edit: portfolio := map[string]float64{ "BTCUSDT": 80, "ETHUSDT": 40, } Adjust as needed. -
Intervals & Thresholds
autoTradePortfolio( portfolio, buyInterval, // e.g. 3600 seconds stopLossPct, // e.g. 0.05 = 5% takeProfitPct, // e.g. 0.1 = 10% rsiPeriod, buyRSIThreshold, sellRSIThreshold, ... )
- No Guarantee of Profit
- This code is for educational purposes only and not financial advice.
- Security
- Keep your
.env
private; never enable withdrawal on your API keys.
- AI Generated Code
- Portions of this project were generated by AI. Always review before production use.
Use or modify this code freely for personal or learning projects.
Happy Trading!