A dark, deep forest in the middle of nowhere. Maybe Siberia? Full of bears, wolves and randomness. Each tree has its own lifecycle and has different insects chewing them branches. What does that have to do with the risk management? We will get to it.
To start, I would like to ask a question- how do we calculate our expected return for next trade? If we have been trading for a while, we can use a formula ET = (Avg Win * Win Rate) – (Avg Loss * Loss Rate) which works really fine! We will get, for example, a value of 150. That means that if we make a trade, based on our historical performance, we can expect to make 150 dollars (or whatever currency you use) from it. We can use this formula to evaluate strategies, ourselves. The drawback? It is unconditional. This Expected Trade can tell us how much we can make on average, but it will not say how much we can make AND add a confidence interval. What if market is very volatile versus a calm market? Does our expected trade change? Well… probably? But how do we know?
The best would be if we knew the probability of our next trade being successful based on some market or maybe our own trading returns, ideas, etc. Then by knowing our average win (which we could also calculate conditionally? But not on this article) we can determine for sure if we want to make the next trade.
Let’s make an example. I am trading Bollinger Bands, meaning I want a big range to happen. Naturally, during calm markets, for example- Asian hours, my strategy will likely not win. But can we just use the value of VIX to determine that? Or maybe ATR? What about a weekday? US10Y yield? Imagine being able to plug a bunch of variables and then having a probability of this Bollinger Bands strategy making a positive trade. Well, we can construct such algorithm using Random Forest Machine Learning algorithm. For the basic outline, see Fig. 1

Now, to be fairly honest I would probably suck at explaining how the Random Forest works, but also, it’s a little over the scope of this article. I would highly advice to check this video:
Decision Tree Classification Clearly Explained! – YouTube which explains clearly what the Decision Trees are and then watch this: Random Forest Algorithm Clearly Explained! – YouTube. by the end of this combined 20min lesson you will understand clearly the underlying principle behind the algorithm.
So, how does it work?
STEP 1. Prepare the data: Let’s first note that we only care about our trade being profitable or not, hence ignoring how much we were right (or wrong). For that, we convert our profitable trades to 1 and losing trades to 0. The idea being that our algorithm generates alpha, which is simply predicts market going up or down. Profit or loss that goes afterwards is a subject to risk management, not alpha itself.


STEP 2. Add features: Now, the goal is to add anything that could have contributed to the result of our strategy. Market volatility? Add VIX. Strong Dollar? Add USD Index, and you can add as many features as you want. Just make sure to avoid forward looking data. See Fig. 2

Thanks to Google and thousands of programmers, we have Python libraries to use, such as sklearn. They make it very easy to train and use the models. See the code below:
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
data = pd.read_csv("C:/Users/Admin/Downloads/Untitled spreadsheet - main (3).csv")
# Column names = features
X = data[['vix', 'usd', 'us10y']]
# We try to predict this
y = data['iswin']
# Scale the features (best to have data ranging from 0 to 1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
# The model. Hard work is done by the SKLearn
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train)
# We input some random values but remember to scale them using the same scaler
new_data = pd.DataFrame({'vix': [30], 'usd': [95], 'us10y': [1.8]})
new_data_scaled = scaler.transform(new_data)
y_probabilities = rf_model.predict_proba(new_data_scaled)
predicted_iswin = rf_model.predict(new_data_scaled)
predicted_class_probabilities = y_probabilities[0]
confidence_interval = np.percentile(predicted_class_probabilities, [97.5, 2.5])
print(f'Predicted iswin: {predicted_iswin[0]}')
print(f'Predicted Class Probabilities: [0 1] {predicted_class_probabilities}')
print(f'95% Confidence Interval: {confidence_interval}')
y_pred = rf_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Model Accuracy: {accuracy:.2f}')
feature_importances = rf_model.feature_importances_
plt.figure(figsize=(10, 6))
plt.bar(X.columns, feature_importances)
plt.xlabel('Columns')
plt.ylabel('Feature Importance')
plt.title('Significance of Each Column')
plt.show()
That’s it! Model outputs several values.
- Model Accuracy: 57% – Why so low? Well, for this strategy, the features might be not very relevant. We need to add more features and then filter them. This is done by displaying feature_importances. See below:
If we have only three features, we can just drop the least significant one- in this case the US 10-year yield or we should add more features. But if we have a ton of features, one way to go is to remove every one below the average. Or leave the 10% of the most important ones?
- 95% Confidence interval: 70% chance that if VIX is 15; USD is 95 and US10Y is 1.8, next trade of ours will be 0- a negative one.
- Predicted Class Probabilities – it gives a 71% chance to get a negative trade.
So, what is the conclusion? The conclusion is – don’t make the trade! Add some filters to the strategy! Now if we are trading manually with no algorithms, we can use this analysis to develop filters of when we are doing badly, when we should not trade! Maybe we are really bad at calm markers? Then we can add a bunch of indicators as features and discover new things about ourselves, so we know when not to trade.
Fighter pilots are trained to not listen to our body but see the dashboard of the plane. Sometimes our senses betray us! We think we are good at something, but in reality, that’s simply not the case- a random forest can help us discover our strengths and weaknesses. And thus… We grow.
Leave a Reply