This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Performance Metrics | |
y_pred_rf = rf.predict_proba(X_test)[:, 1] | |
y_pred = rf.predict(X_test) | |
fpr_rf, tpr_rf, _ = roc_curve(y_test, y_pred_rf) | |
print(classification_report(y_test, y_pred)) | |
print("Confusion Matrix") | |
print(confusion_matrix(y_test, y_pred)) | |
print('') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get features at event dates | |
X = data.loc[labels.index, :] | |
y = labels['bin'] | |
# Split data | |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False) | |
# Setting random forest parameters | |
n_estimator = 1000 | |
depth = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# creating dataframe of only bin labels | |
primary_forecast = pd.DataFrame(labels['bin']) | |
# setting predicted column to 1 | |
primary_forecast['pred'] = 1 | |
primary_forecast.columns = ['actual', 'pred'] | |
# Performance Metrics | |
actual = primary_forecast['actual'] | |
pred = primary_forecast['pred'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# determining our "bin" labels | |
labels = get_bins(triple_barrier_events, data['close']) | |
labels.side.value_counts() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
close = data['close'] | |
# determining daily volatility using the last 50 days | |
daily_vol = get_daily_vol(close=close, lookback=50) | |
# creating our event triggers using the CUSUM filter | |
cusum_events = get_t_events(close, threshold=daily_vol.mean()*0.1) | |
# adding vertical barriers with a half day expiration window | |
vertical_barriers = add_vertical_barrier(t_events=cusum_events, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# compute bands | |
window = 50 | |
data['avg'], data['upper'], data['lower'] = bbands(data['close'],window, no_of_stdev=1.5) | |
# compute sides | |
data['side'] = np.nan | |
long_signals = (data['close'] <= data['lower']) | |
short_signals = (data['close'] >= data['upper']) | |
data.loc[long_signals, 'side'] = 1 | |
data.loc[short_signals, 'side'] = -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#download the data into your google drive to use the following code | |
infp=PurePath('/content/drive/MyDrive/BTC_1m_2018_dollar.parq') | |
#import data and set timestamps as index | |
data = pd.read_parquet(infp) | |
data['timestamp']= pd.to_datetime((data['timestamp']).astype(float), unit='ms') | |
data = data.set_index('timestamp') | |
print(data.info()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def apply_pt_sl_on_t1(close, events, pt_sl, molecule): | |
""" | |
:param close: (series) close prices | |
:param events: (series) of indices that signify "events" | |
:param pt_sl: (array) element 0, indicates the profit taking level; | |
element 1 is stop loss level | |
:param molecule: (an array) a set of datetime index values for processing | |
:return: (dataframe) timestamps at which each barrier was touched | |
""" | |
# apply stop loss/profit taking, if it takes place before t1 (end of event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_events(close, t_events, pt_sl, target, min_ret, num_threads, | |
vertical_barrier_times=False, side=None): | |
""" | |
:param close: (series) Close prices | |
:param t_events: (series) of t_events. | |
These are timestamps that will seed every triple barrier. | |
:param pt_sl: (2 element array) element 0, indicates the profit taking level; | |
element 1 is stop loss level. | |
A non-negative float that sets the width of the two barriers. | |
A 0 value means that the respective horizontal barrier will be disabled. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_vertical_barrier(t_events, close, num_days=1): | |
""" | |
:param t_events: (series) series of events (symmetric CUSUM filter) | |
:param close: (series) close prices | |
:param num_days: (int) maximum number of days a trade can be active | |
:return: (series) timestamps of vertical barriers | |
""" | |
t1 = close.index.searchsorted(t_events + pd.Timedelta(days=num_days)) | |
t1 = t1[t1 < close.shape[0]] | |
t1 = pd.Series(close.index[t1], index=t_events[:t1.shape[0]]) # NaNs at end |
NewerOlder