-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_ubuntu.sh
More file actions
411 lines (333 loc) Β· 9.1 KB
/
setup_ubuntu.sh
File metadata and controls
411 lines (333 loc) Β· 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/bash
# π§ Crypto Trading Bot - Ubuntu Setup Script
# This script automates the setup process for Ubuntu Linux
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running on Ubuntu
check_ubuntu() {
if [[ ! -f /etc/os-release ]] || ! grep -q "Ubuntu" /etc/os-release; then
log_error "This script is designed for Ubuntu Linux"
exit 1
fi
local version=$(lsb_release -rs)
log_info "Detected Ubuntu $version"
if [[ $(echo "$version >= 20.04" | bc -l) -eq 0 ]]; then
log_warning "Ubuntu 20.04 or newer is recommended"
fi
}
# Update system packages
update_system() {
log_info "Updating system packages..."
sudo apt update && sudo apt upgrade -y
log_success "System packages updated"
}
# Install system dependencies
install_system_deps() {
log_info "Installing system dependencies..."
sudo apt install -y \
build-essential \
curl \
wget \
git \
vim \
htop \
unzip \
software-properties-common \
apt-transport-https \
ca-certificates \
gnupg \
lsb-release \
bc
log_success "System dependencies installed"
}
# Install Python 3.11+
install_python() {
log_info "Installing Python 3.11+..."
# Add deadsnakes PPA for newer Python versions
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install -y \
python3.11 \
python3.11-pip \
python3.11-venv \
python3.11-dev \
python3-pip \
python3-venv \
python3-dev
# Set python3.11 as default python3 if available
if command -v python3.11 &> /dev/null; then
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
log_success "Python 3.11 installed and set as default"
else
log_warning "Python 3.11 not available, using system Python3"
fi
python3 --version
}
# Install Node.js and npm
install_nodejs() {
log_info "Installing Node.js and npm..."
# Install Node.js 18.x
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# Verify installation
node --version
npm --version
log_success "Node.js and npm installed"
}
# Create project structure
setup_project() {
log_info "Setting up project structure..."
# Create directories
mkdir -p config logs state
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Upgrade pip
pip install --upgrade pip
log_success "Project structure created"
}
# Install Python dependencies
install_python_deps() {
log_info "Installing Python dependencies..."
# Activate virtual environment
source venv/bin/activate
# Install core dependencies
pip install \
cryptography \
pydantic \
python-dotenv \
pyyaml \
aiohttp \
websockets \
fastapi \
uvicorn
# Install data analysis libraries
pip install pandas numpy scipy || log_warning "Some data analysis libraries failed to install"
# Install optional dependencies
pip install python-binance structlog || log_warning "Some optional dependencies failed to install"
# Install testing dependencies
pip install pytest pytest-asyncio pytest-mock
# Install development tools
pip install black flake8 mypy
log_success "Python dependencies installed"
}
# Install Node.js dependencies for dashboard
install_dashboard_deps() {
log_info "Installing dashboard dependencies..."
if [[ -d "dashboard" ]]; then
cd dashboard
npm install
cd ..
log_success "Dashboard dependencies installed"
else
log_warning "Dashboard directory not found, skipping dashboard setup"
fi
}
# Create configuration files
create_config() {
log_info "Creating configuration files..."
# Create .env file
cat > .env << 'EOF'
# API Configuration
BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_api_secret_here
TESTNET=true
# Bot Configuration
TRADING_ENABLED=false
DRY_RUN=true
LOG_LEVEL=INFO
# Database Configuration (optional)
DATABASE_URL=sqlite:///trading_bot.db
# Notification Configuration
NOTIFICATIONS_ENABLED=true
CONSOLE_NOTIFICATIONS=true
EOF
# Create bot config
cat > config/bot_config.yaml << 'EOF'
# Trading Bot Configuration
symbols:
- BTCUSDT
- ETHUSDT
- ADAUSDT
strategies:
liquidity:
enabled: true
weight: 1.0
min_confidence: 0.6
momentum:
enabled: true
weight: 1.2
min_confidence: 0.7
chart_patterns:
enabled: false
weight: 0.8
min_confidence: 0.8
risk_management:
max_position_size: 0.02
daily_loss_limit: 0.05
max_drawdown: 0.15
stop_loss_pct: 0.02
take_profit_pct: 0.04
logging:
level: INFO
log_dir: logs
console_logging: true
EOF
# Set proper permissions
chmod 600 .env
chmod 600 config/bot_config.yaml
log_success "Configuration files created"
}
# Create systemd service (optional)
create_service() {
log_info "Creating systemd service..."
local service_file="/etc/systemd/system/crypto-trading-bot.service"
local current_dir=$(pwd)
local current_user=$(whoami)
sudo tee $service_file > /dev/null << EOF
[Unit]
Description=Crypto Trading Bot
After=network.target
[Service]
Type=simple
User=$current_user
WorkingDirectory=$current_dir
Environment=PATH=$current_dir/venv/bin
ExecStart=$current_dir/venv/bin/python -m crypto_trading_bot.main
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
log_success "Systemd service created (not enabled by default)"
log_info "To enable: sudo systemctl enable crypto-trading-bot"
log_info "To start: sudo systemctl start crypto-trading-bot"
}
# Run tests
run_tests() {
log_info "Running tests..."
source venv/bin/activate
# Run basic test
if python3 test_basic.py; then
log_success "Basic tests passed"
else
log_warning "Some basic tests failed"
fi
# Run component tests
if python3 run_tests.py --component strategies; then
log_success "Strategy tests passed"
else
log_warning "Some strategy tests failed"
fi
}
# Create startup scripts
create_scripts() {
log_info "Creating startup scripts..."
# Create start script
cat > start_bot.sh << 'EOF'
#!/bin/bash
echo "π Starting Crypto Trading Bot..."
# Activate virtual environment
source venv/bin/activate
# Start the trading bot
python3 -m crypto_trading_bot.main
EOF
# Create API server start script
cat > start_api.sh << 'EOF'
#!/bin/bash
echo "π Starting API Server..."
# Activate virtual environment
source venv/bin/activate
# Install FastAPI if not installed
pip install fastapi uvicorn
# Start the API server
python3 api_server.py
EOF
# Create dashboard start script
cat > start_dashboard.sh << 'EOF'
#!/bin/bash
echo "π Starting Dashboard..."
# Navigate to dashboard directory
cd dashboard
# Install dependencies if needed
npm install
# Start the dashboard
npm start
EOF
# Make scripts executable
chmod +x start_bot.sh start_api.sh start_dashboard.sh
log_success "Startup scripts created"
}
# Display final instructions
show_instructions() {
log_success "π Setup completed successfully!"
echo
echo "π Next Steps:"
echo "1. Configure your API keys in .env file:"
echo " nano .env"
echo
echo "2. Test the installation:"
echo " source venv/bin/activate"
echo " python3 test_basic.py"
echo
echo "3. Start the API server:"
echo " ./start_api.sh"
echo " # API will be available at http://localhost:8000"
echo
echo "4. Start the dashboard (in a new terminal):"
echo " ./start_dashboard.sh"
echo " # Dashboard will be available at http://localhost:3000"
echo
echo "5. Start the trading bot (in a new terminal):"
echo " ./start_bot.sh"
echo
echo "π Documentation:"
echo " - API docs: http://localhost:8000/docs"
echo " - Setup guide: UBUNTU_SETUP_GUIDE.md"
echo
echo "β οΈ Important:"
echo " - Always test with TESTNET=true first"
echo " - Set DRY_RUN=true for paper trading"
echo " - Configure your Binance API keys before live trading"
echo
}
# Main execution
main() {
echo "π§ Crypto Trading Bot - Ubuntu Setup"
echo "===================================="
echo
check_ubuntu
update_system
install_system_deps
install_python
install_nodejs
setup_project
install_python_deps
install_dashboard_deps
create_config
create_service
create_scripts
run_tests
show_instructions
}
# Run main function
main "$@"