Building a 6-Factor Quantitative Trading Model for Bitcoin Using Python

This tutorial walks you through building a true 6-factor Bitcoin trading strategy from scratch using Python. By leveraging classic technical indicators—EMA, MACD, RSI, KDJ, CCI, and OBV—we construct a signal classification model that outputs Buy, Sell, or Hold decisions.

Step 1: Load Bitcoin Data

btc_data = pd.read_csv("btc_data.csv", parse_dates=['Open time'])
btc_data.set_index('Open time', inplace=True)
btc_data.rename(columns=lambda x: x.lower(), inplace=True)

We load 15-minute interval BTC price data and set the timestamp as the index.

Step 2: Add Technical Indicators

Using the pandas_ta library, we calculate the following indicators:

  • EMA: 6, 18, and 60-period Exponential Moving Averages
  • MACD & Signal Line
  • RSI (14-period)
  • CCI (14-period Commodity Channel Index)
  • KDJ (Stochastic Oscillator)
  • OBV (On-Balance Volume)
import pandas_ta as ta

df['EMA_6'] = ta.ema(df['close'], length=6)
df['EMA_18'] = ta.ema(df['close'], length=18)
# ...
df['OBV'] = ta.obv(df['close'], df['volume'])
df.dropna(inplace=True)

Each factor represents a different dimension of market behavior—momentum, volume flow, or sentiment.

Step 3: Price Prediction Model

We use the technical indicators to predict the next period’s closing price:

X = df[["EMA_6", "EMA_18", ..., "OBV"]]
y = df['close'].shift(-1)

Train a regression model (e.g., linear regression):

from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(X_train, y_train)

You can include the predicted price (Predicted_Close) as an additional feature in the classification step.

Step 4: Signal Classification Model (Buy/Sell/Hold)

We define trading signals based on the following rule-based logic:

y = np.where(
    (EMA_6 > EMA_18) & (MACD > MACD_signal) & (RSI > 30) &
    (K > D) & (CCI > 100) & (OBV > OBV.shift(1)), 1,
    np.where(
        (EMA_6 < EMA_18) & (MACD < MACD_signal) & (RSI < 70) &
        (K < D) & (CCI < -100) & (OBV < OBV.shift(1)), -1, 0
    )
)

Then, train a classifier to predict these Buy/Sell/Hold labels:

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(max_depth=5)
model.fit(X_train, y_train)

Step 5: Visualizing Strategy Performance

Using Plotly, we visualize:

  • Actual vs. Predicted price
  • Buy signals (green triangle)
  • Sell signals (red inverted triangle)
  • Hold signals (yellow dots)
fig.add_trace(go.Scatter(..., name='Buy'))
fig.add_trace(go.Scatter(..., name='Sell'))
fig.add_trace(go.Scatter(..., name='Hold'))

Summary

  • ✅ Technical factors based on classic indicators: MACD, KDJ, OBV, etc.
  • 🔄 Labels generated using combined conditions (rule-based logic)
  • 🤖 Classification powered by machine learning (Random Forest)
  • 📈 Visual outputs help evaluate strategy performance and logic

Download & Connect

The complete code and dataset will be available on GitHub soon.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *