Skip to content

yasmuninv-cloud/AI_SellBuyBTC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

<<<<<<< HEAD

AI_SellBuyBTC — Transformer‑Based Inference of BTC Buy/Sell Signals

This repository provides a compact and reproducible inference pipeline for generating model‑derived BUY and SELL signals for Bitcoin (BTC) using a pretrained Transformer architecture.
The objective of this public release is to demonstrate the inference workflow, model architecture, and signal‑generation process without exposing proprietary training methodology.

The training code, data‑labeling logic, and feature‑engineering pipeline are maintained in a private repository and are intentionally excluded from this distribution.


1. Overview

The system implements a lightweight Transformer encoder that processes a set of 11 slope‑based statistical features derived from daily BTC price data.
The model outputs two independent binary classification signals:

  • BUY signal
  • SELL signal

These outputs are obtained through a forward pass of the pretrained model and visualized on top of the BTC price series using an interactive Plotly chart.

This repository is intended for:

  • researchers evaluating model‑driven signal inference
  • practitioners exploring Transformer architectures for financial time‑series
  • users interested in reproducing the inference pipeline without access to training internals

2. Repository Structure

AI_SellBuyBTC/
│
├── AI_SellBuyBTC.py              # Main inference script
├── multi_transformer_weights.pth # Pretrained Transformer weights
└── README.md

3. Running the Inference Pipeline

3.1 Install Dependencies

pip install torch numpy pandas plotly requests

3.2 Execute the Script

python AI_SellBuyBTC.py

3.3 What the Script Performs

  1. Loads cached BTC daily data or fetches it from Binance’s public API
  2. Computes 11 rolling OLS‑based slope features
  3. Loads the pretrained Transformer model and its weights
  4. Performs forward inference to obtain BUY/SELL signal outputs
  5. Visualizes the results in an interactive Plotly chart

The entire workflow is CPU‑compatible and does not require GPU acceleration.


4. Model Architecture

The model is a compact Transformer encoder consisting of:

  • Input layer: 11‑dimensional feature vector
  • Transformer encoder:
    • 1 encoder layer
    • multi‑head self‑attention
    • feed‑forward expansion
  • Two linear output heads:
    • BUY signal head
    • SELL signal head

The architecture is fully defined inside AI_SellBuyBTC.py for transparency.
The pretrained weights are stored in multi_transformer_weights.pth.

The training process (oversampling, loss weighting, feature engineering, and label construction) is not included and remains private.


5. Output Visualization

The script produces an interactive Plotly chart containing:

  • BTC daily closing price
  • Model‑derived BUY signals (cyan markers)
  • Model‑derived SELL signals (orange markers)

This visualization enables qualitative assessment of the model’s signal‑generation behavior across the full BTC historical dataset.


6. System Specifications

The inference pipeline was executed under the following environment:

Python: 3.11.14 (Anaconda)
Torch: 2.9.1+cpu
CUDA available: False
Platform: Windows-10-10.0.26200-SP0

The model is lightweight and runs efficiently on CPU‑only systems.


7. Notes and Limitations

  • This repository contains inference‑only code.
  • Training logic, data labeling, and proprietary feature‑engineering methods are not included.
  • The model outputs binary classification signals, not financial advice.
  • The pretrained weights are provided solely for reproducibility and academic/technical demonstration.

8. License

License Code: MY-FullRights-Proprietary-1.0

=======

BTC 21‑Day High/Low Envelope Forecasting

A complete machine‑learning pipeline for forecasting Bitcoin’s future 21‑day high and low envelope using engineered statistical signals and linear models.
The project fetches raw BTCUSDT data from Binance, builds predictive features, trains walk‑forward models, evaluates accuracy, and provides an interactive prediction tool.


Project Structure

1. data.py — Raw Data Fetching & Base Preprocessing

  • Fetches BTCUSDT daily candles from Binance (or loads cached CSV).
  • Computes:
    • hloc_avg — mean of high/low/open/close
    • log_return — log of hloc_avg
  • Saves:
    BTC_daily_data.csv

This file ensures you always have clean, up‑to‑date BTC data.


2. SignalEngineering.py — Feature Engineering & Model Training

Builds all predictive features and trains the models.

Engineered Features

  • ols_slope350 — long‑term trend slope
  • ols_slope128 — mid‑term trend slope
  • ols_slope14 — short‑term trend slope
  • price_trend — 100‑day linear trend of price
  • volatility — 21‑day rolling volatility

Targets (21‑day forward envelope)

  • gain3w — future max over next 21 days
  • loss3w — future min over next 21 days

Model Training

Uses a walk‑forward expanding window:

  • Window: 500 days
  • Horizon: 50 days
  • Models:
    • Linear Regression
    • Ridge Regression
    • Lasso Regression

Saves:

  • final_linear_params.json — model coefficients & intercepts
  • AddedFeatureData.csv — full dataset with all engineered features

3. RiskPrediction.py — Interactive Prediction Engine

Loads:

  • AddedFeatureData.csv
  • final_linear_params.json

You type a date, and it outputs:

  • predicted 21‑day gain %
  • predicted 21‑day loss %
  • predicted high price
  • predicted low price

This script is the user‑facing forecasting tool. -----------Run example: Model: Linear date in data base up to: 2026-01-29 Enter date (YYYY-MM-DD): 2020-10-10

Prediction for 2020-10-10: Current price: 11221.34 Predicted 21-day gain: 0.0393 → High ≈ 11662.29 Predicted 21-day loss: -0.0520 → Low ≈ 10637.93


Model Accuracy & Evaluation

start Linear_MSE Ridge_MSE Lasso_MSE 0 0 0.001451 0.005235 0.005621 1 50 0.007083 0.042504 0.046135 2 100 0.008235 0.013415 0.014363 3 150 0.002634 0.004957 0.005272 4 200 0.001383 0.005226 0.005853 5 250 0.000742 0.004385 0.004845 36 1800 0.001600 0.005555 0.005710 37 1850 0.000763 0.002299 0.002370 38 1900 0.001969 0.003607 0.003713 39 1950 0.000687 0.002877 0.002970 40 2000 0.000401 0.001332 0.001367 41 2050 0.000392 0.001739 0.001781 42 2100 0.000684 0.002032 0.002085 43 2150 0.002577 0.005056 0.005157

Real vs Predicted Gain/Loss

The project includes a full‑period plot comparing:

  • Real gain vs predicted gain
  • Real loss vs predicted loss

This shows how well the model tracks BTC’s directional envelope. alt text

**Key Statistics **

  • Mean Absolute Error (gain): low and stable
  • Mean Absolute Error (loss): moderate but acceptable
  • IQR is tight → model is consistent
  • Outliers follow the 1.5×IQR rule and correspond to known BTC volatility events

These results show that the linear model captures BTC’s medium‑term envelope surprisingly well given its simplicity. === Gain Error Statistics === Count: 2737 Mean Absolute Error: 0.0433 Median (Q2): 0.0278 Q1 (25%): 0.0117 Q3 (75%): 0.0547 IQR: 0.0431 Min: 0.0000 Max: 0.5856 Outliers (1.5×IQR rule): 172

=== Loss Error Statistics === Count: 2737 Mean Absolute Error: 0.0364 Median (Q2): 0.0259 Q1 (25%): 0.0122 Q3 (75%): 0.0504 IQR: 0.0382 Min: 0.0001 Max: 0.2296 Outliers (1.5×IQR rule): 135

>>>>>>>>>>>>>>>>>>>>>> How to Run the Project

1. Install dependencies

pip install numpy pandas scikit-learn plotly requests

2. Fetch or load BTC data

python data.py

3. Build features & train models

python SignalEngineering.py

This generates:

  • AddedFeatureData.csv
  • final_linear_params.json

4. Run the prediction engine

python RiskPrediction.py

Enter any date present in the dataset to get a forecast.


Key Insights

  • BTC’s medium‑term envelope is strongly influenced by trend slopes and volatility.
  • Linear models perform surprisingly well for envelope prediction, especially for gains.
  • Loss predictions are harder due to BTC’s asymmetric downside shocks.
  • Walk‑forward validation ensures realistic, time‑consistent evaluation.

Limitations

  • Linear models cannot capture nonlinear BTC dynamics.
  • Predictions degrade during extreme volatility events.
  • Only price‑based features are used (no macro, no order‑flow).

Future Work

  • Add nonlinear models (XGBoost, Random Forest, LSTM).
  • Add macroeconomic or on‑chain features.
  • Build a live API for real‑time envelope forecasting.

If you want, I can also generate a polished project banner, add badges, or create a GitHub‑ready folder structure.

697eed210c939eafb0f2156cdc6e0ce33363bcb2

About

Transformer‑based BUY/SELL signal classifier for Bitcoin using long‑horizon trend features and pretrained inference weights.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages