Open-Source Binance AI Trading Bot: ML & Feature Engineering for Automated Crypto Signals
This article details an open‑source Binance trading bot that uses machine‑learning models and extensive feature‑engineering pipelines to generate and broadcast automated cryptocurrency buy‑sell signals via a Telegram channel, covering data acquisition, offline training, hyper‑parameter tuning, and live service deployment.
Intelligent Trading Bot
Core capabilities:
Custom Python functions define derived features, including technical indicators.
Offline batch processing trains machine‑learning models on historical data.
Prediction scores are analyzed to select optimal signal parameters.
A signal service periodically fetches new exchange data and generates buy/sell signals using the trained models.
A trader service executes actual orders based on generated signals.
Signal Channel
The signal service runs in the cloud and posts messages to the Telegram channel https://t.me/intelligent_trading_signals. Current configuration:
Exchange: Binance
Cryptocurrency: Bitcoin (BTC)
Analysis frequency: 1 minute
Score range: –1 to +1 (negative → likely decline, positive → likely rise)
Notification filter: send only when |score| > 0.20
Each 0.05 unit beyond the filter adds an up/down arrow symbol.
Example notifications:
₿ 24.518 📉📉📉 Score: -0.26
🟢 BUY: ₿ 24,033 Score: +0.34
Offline Model Training Workflow
If the environment is correctly configured, execute the following scripts in order (all accept -c config.json to specify a configuration file):
python -m scripts.download_binance -c config.json python -m scripts.merge -c config.json python -m scripts.features -c config.json python -m scripts.labels -c config.json python -m scripts.train -c config.json python -m scripts.signals -c config.json python -m scripts.train_signals -c config.jsonWhen no configuration file is supplied, scripts fall back to default parameters useful for testing but not intended for production. Use the provided sample configuration files (e.g., config-sample-v0.6.0.jsonc).
Download and Merge Source Data
data_sourcesdefines each source and a column_prefix to disambiguate identical column names.
Download latest historical data (Binance API by default): python -m scripts.download_binance -c config.json Merge multiple datasets into a single regular time‑grid: python -m scripts.merge -c config.json. This script handles additional sources (e.g., depth data, futures) and fills gaps.
Feature Generation
The scripts.features script computes derived features for all input records (non‑incremental mode; may take hours for complex configurations). It loads the merged data, applies the feature‑generation pipeline, and stores all derived features in an output file. Only a subset, defined in feature_sets, is used for training and prediction. Feature functions receive extra parameters (e.g., window size) from the configuration.
Supported feature generators: talib – TA‑lib technical analysis library. Example config:
{"columns": ["close"], "functions": ["SMA"], "windows": [5,10,15]} itbstats– Implements functions from tsfresh such as scipy_skew, scipy_kurtosis, lsbm, fmax, mean, std, area, slope. Example config:
{"columns": ["close"], "functions": ["skew","fmax"], "windows": [5,10,15]} itblib– Similar to talib but implemented in ITB. tsfresh – Generates functions from the tsfresh library.
Label Generation
The scripts.labels script adds columns that describe the prediction target (unknown in online mode). It loads features, computes label columns, and stores the results. Only a selected subset, defined in label_sets, is used for training.
Example label generators: highlow – Returns true if the future price exceeds a threshold. highlow2 – Computes future price rise/fall conditioned on no significant prior move. Sample config:
{"columns": ["close","high","low"], "function": "high", "thresholds": [1.0,1.5,2.0], "tolerance": 0.2, "horizon": 10080, "names": ["first_high_10","first_high_15","first_high_20"]} topbot2– Marks maxima/minima surrounded by opposite‑direction thresholds; parameters level and tolerances control required gaps. Sample config:
{"columns": "close", "function": "bot", "level": 0.02, "tolerances": [0.1,0.2], "names": ["bot2_1","bot2_2"]}Training Prediction Models
The scripts.train script trains multiple models for each (label, algorithm) pair using the specified input features and labels.
Hyper‑parameter tuning is not performed here; hyper‑parameters are assumed known and are described in model_store.py.
Trained model files are saved in the models folder with filenames (label_name, algorithm_name).
A prediction-metrics.txt file records prediction scores for all models.
Score Aggregation and Post‑Processing
Aggregates prediction scores from different algorithms for each label. Configuration resides in the score_aggregation section: buy_labels and sell_labels specify input scores to aggregate. window defines how many recent steps are used for rolling aggregation. combine determines how buy and sell scores are merged into a single output score.
Signal Generation
The aggregated score is fed to a rule engine described in trade_model, which decides whether to emit a BUY, SELL, or NO‑ACTION signal.
Training Signal Models
The scripts.train_signals script simulates trading with various buy‑sell signal parameters on historical data and selects the parameter set that yields the best performance.
Online Prediction Service
Start the service with: python -m service.server -c config.json The service assumes models were trained with the features specified in the configuration and uses the exchange credentials from the same configuration.
Signaler Service Loop (executed each minute)
Retrieve the latest data from the server and update the current data window (history length defined in the configuration).
Compute derived features on the updated window using the same definitions as in offline training.
Apply several pre‑trained models (gradient boosting, neural networks, linear regression) to predict future values for multiple target variables.
Aggregate the predictions to compute a final signal score (range –1 to +1). Positive scores indicate upward pressure; negative scores indicate downward pressure.
Send a notification containing the latest price, score, and optional arrow symbols.
Notes:
The final score should be considered together with other parameters and data sources before deciding on a trade.
Running the signaler requires the trained model files to be present in the MODELS folder.
Trader Service
The trader component can execute buy/sell orders based on signals but has not been thoroughly debugged for stability; it is currently a prototype integrated with the signaler.
Hyper‑Parameter (超参) Tuning
Two main challenges:
Selecting optimal hyper‑parameters for ML models, typically via grid search on the same data. This optimizes model scores, not necessarily trading performance; the scores are later used as intermediate features for performance optimization.
Interpreting the final aggregated score (e.g., +0.21) to decide on BUY, SELL, or HOLD. Additional scripts perform rolling‑prediction simulation ( python -m scripts.predict_rolling -c config.json) and train signal thresholds ( python -m scripts.train_signals -c config.json).
Configuration Parameters
Parameters are defined in two places: the App class in service.App.py (field config) and the command‑line -c config.json argument, which overrides values in App.config.
Key fields: data_folder – Location of data files used by offline scripts. symbol – Trading pair (e.g., BTCUSDT). labels, algorithms, train_features – Lists of label columns, algorithm names, and feature columns for training and prediction. buy_labels and sell_labels – Columns used by the signaler. trade_model – Parameters (mainly thresholds) for the signal rule engine. trader – Trading parameters (prototype, not fully tested). collector – Configuration for data‑collection services (synchronous polling or asynchronous streaming), not yet fully integrated.
For detailed examples, see the sample configuration files and comments in App.config.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
