RandomForest ๐ŸŒณย - Trade Bitcoin with AI ๐Ÿ“ˆ

My method marries the power of machine learning, historical data, technical analysis, and sentiment analysis can predict Bitcoin prices, and the outcomes have been quite encouraging. ๐Ÿ‘๐Ÿ’ฐ
notion image
ย 

The Power of Data ๐Ÿ’พ๐Ÿ“ˆ

Data serves as the lifeblood of trading. We leverage a wealth of historical data, technical features, and sentiment analysis to anticipate Bitcoin prices. Specifically, we rely on:
  1. Historical Prices: Past price trends offer insights into future behaviours. ๐Ÿ•ฐ๏ธ
  1. Technical Indicators: Tools such as moving averages, relative strength index, moving average convergence divergence, Bollinger Bands, and on-balance volume come into play here.๐Ÿ“Š
  1. Sentiment Analysis: We dig into online discussions related to Bitcoin, assessing the overall sentiment. Remember, market sentiment can significantly sway price movements! ๐Ÿ’ฌ๐Ÿ”ฎ
def calculate_indicators(df): df['pc_change'] = df['close'].pct_change() df['SMA_30'] = ta.trend.sma_indicator(df['close'], window=30) df['SMA_100'] = ta.trend.sma_indicator(df['close'], window=100) ...

Splitting the Data for Training and Testing ๐Ÿงช๐Ÿงฑ

We then split our data into a training set and a test set. The model is trained on the training set and then evaluated on the test set, which contains data it hasn't seen before.
pythonCopy code def split_data(df): ... return X_train, X_test, y_train, y_test

Data Preprocessing ๐Ÿ› ๏ธ๐Ÿ”ง

We initiate the process with data preprocessing. Here, we scale features, manage missing values, and eliminate irrelevant columns. We also convert our target variable into two distinct classes: buy or sell, a buy means tha the asset grew the day after the evaluation, a sell means that it decreased in value. We are able to know this based on the historial closing prices.
This transformation simplifies our problem into a binary classification task, making it easier for our model to learn.๐ŸŽฏ
def preprocess_data(df, data): df.drop(['open', 'high', 'low', 'date'], axis=1, inplace=True) ... df['buy/sell'] = np.where(df['pc_change'] > upper_threshold, 1, (np.where(df['pc_change'] <= lower_threshold, -1, 0))) df['buy/sell'] = df['buy/sell'].shift(-1) ...
ย 

The Heart of Our Approach: The Machine Learning Model ๐Ÿง ๐ŸŽฏ๐Ÿ’ป

At the core of our trading strategy lies the machine learning model. Our model of choice is the RandomForestClassifier, an ensemble method renowned for its robustness and adaptability. But how do we ensure that our model operates at its peak potential? How do we fine-tune it for optimal performance? That's where GridSearhCV comes into play! ๐Ÿงช๐Ÿ”

Enter RandomForestClassifier ๐ŸŒณ๐ŸŒณ๐ŸŒณ

Random forests consist of numerous individual decision trees. Each tree in the random forest generates a prediction, and the class with the most votes becomes the model's prediction. This 'forest' of simple models comes together to create a powerful and complex model. With their ability to handle a blend of numerical and categorical data, RandomForestClassifiers are excellent for our task. But like all models, their performance is dependent on the configuration of their hyperparameters.

Hyperparameter Tuning with GridSearchCV ๐Ÿ”ง๐ŸŽ›๏ธ

Hyperparameters are the settings of a model that are fixed before the training process begins. They can significantly impact the performance of a model, making hyperparameter tuning a crucial step in model building.
For this, we use GridSearchCV. This handy tool performs an exhaustive search over specified parameter values for an estimator (in our case, the RandomForestClassifier). We specify a range of possible values for different hyperparameters, and GridSearchCV trains a model for every combination. It then uses cross-validation to evaluate each model's performance.
param_grid = [ {'n_estimators': [10, 25, 50, 100], 'max_features': ['auto', 'sqrt', 'log2'], 'max_depth': [4, 5, 6, 7, 8], 'criterion': ['gini', 'entropy']} ] GridSearchCV(RandomForestClassifier(), param_grid, cv=5, scoring='accuracy', verbose=2, n_jobs=-1)
Once GridSearchCV completes its search, we're left with the best performing model, ready for making predictions! This model, fine-tuned to our dataset, provides us with the best possible chance of making accurate predictions on unseen data. ๐Ÿš€๐Ÿ
In conclusion, our model is trained using a combination of RandomForestClassifier and GridSearchCV.
Once our model is trained, we must evaluate its performance over the test set.
def evaluate_model(model, X_test, y_test): y_pred = model.predict(X_test) ... print(classification_report(y_test, y_pred))

Results over the test data

Got it, let's dial back on the friendliness a bit and keep it more professional. Here we go:
Hello folks! Time to delve into the performance of my Bitcoin trading algorithm. We have two categories here: "-1" and "1", which we'll consider as "sell" and "buy" signals respectively.
Starting off with precision, when my algorithm suggests "sell" (-1), it's correct 62% of the time - a solid start indeed. ๐ŸŽฏ On the "buy" side, we're batting at 52%. Remember, in the volatile world of cryptocurrency trading, being right over half the time is pretty commendable.
Now onto recall, where we have a score of 0.45 for "sell" signals and a stronger 0.68 for "buy" signals. This suggests that my algorithm has a keen eye for buying opportunities! ๐Ÿ‘๏ธโ€๐Ÿ—จ๏ธ
The F1-Score is a kind of average between precision and recall, and it sits at 0.53 for "sell" signals and 0.59 for "buy" signals. This shows our algorithm is holding its ground well in the crypto market.
Coming to the overall "accuracy", we're right on target 56% of the time! ๐ŸŽฏ
Lastly, our 'Macro avg' and 'Weighted avg' come in at 0.57 and 0.58 respectively. These are solid numbers indicating consistent performance across the board.
ย 
precision recall f1-score -1 1.00 0.19 0.32 1 0.47 1.00 0.64 accuracy 0.53 macro avg 0.73 0.60 0.48 weighted avg 0.78 0.53 0.45

Final Thoughts ๐Ÿ’ก๐ŸŒˆ

In a nutshell, this machine learning model can assist traders in making informed decisions about Bitcoin trading. Remember, while the model shows promise, it's essential to approach trading with caution. Stay informed, do your homework, and never invest more than you're willing to lose.
See the whole code here.
ย 
Happy coding and trading! ๐Ÿ’ป๐Ÿ“ˆ๐Ÿš€
Built with Potion.so