cp11_15_TradingStrategies(dataframe.plot.scatter)2

https://blog.csdn.net/Linli522362242/article/details/102314389

Deep Neural Networks

Deep neural networks (DNNs) try to emulate the functioning of the human brain. They are in general composed of an input layer (the features), an output layer (the labels), and a number of hidden layers. The presence of hidden layers is what makes a neural network deep. It allows it to learn more complex relationships and to perform better on a number of problem types. When applying DNNs one generally speaks of deep learning instead of machine learning. For an introduction to this field, refer to Géron (2017) or Gibson and Patterson (2017).

DNNs with scikit-learn

This section applies the MLPClassifier algorithm from scikit-learn, as introduced in “Deep neural networks”. First, it is trained and tested on the whole data set, using the digitized features. The algorithm achieves exceptional performance in-sample (see Figure 15-17), which illustrates the power of DNNs for this type of problem. It also hints at strong overfitting, since the performance indeed seems unrealistically good:

from sklearn.neural_network import MLPClassifier

model = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes= 2*[250], random_state=1)

%time model.fit(data[cols_bin], data['direction'])

data['pos_dnn_sk'] = model.predict(data[cols_bin])

data['strat_dnn_sk'] = data['pos_dnn_sk'] * data['returns']

data[['returns', 'strat_dnn_sk']].sum().apply(np.exp)

data[['returns', 'strat_dnn_sk']].cumsum().apply(np.exp).plot(figsize=(10,6),
                                                               title='Performance of EUR/USD and DNN-based trading strategy (scikit-learn, in-sample)'
                                                              )

To avoid overfitting of the DNN model, a randomized train-test split is applied next. The algorithm again outperforms the passive benchmark investment and achieves a positive absolute performance (Figure 15-18). However, the results seem more realistic now:

DNNs with TensorFlow

TensorFlow has become a popular package for deep learning. It is developed and supported by Google Inc. and applied there to a great variety of machine learning problems. Zedah and Ramsundar (2018) cover TensorFlow for deep learning in depth

As with scikit-learn, the application of the DNNClassifier algorithm from TensorFlow to derive an algorithmic trading strategy is straightforward given the background from “Deep neural networks”. The training and test data is the same as before. First, the training of the model. In-sample, the algorithm outperforms the passive benchmark investment and shows a considerable absolute return (see Figure 15-19), again hinting at overfitting

train, test = train_test_split(data, test_size=0.5, random_state=100)

train = train.copy().sort_index()

test = test.copy().sort_index()

#Increases the number of hidden layers and hidden units.
model = MLPClassifier(solver='lbfgs', alpha=1e-5, max_iter=500, hidden_layer_sizes=3*[500], random_state=1)

%time model.fit(train[cols_bin], train['direction'])

test['pos_dnn_sk'] = model.predict(test[cols_bin])

test['strat_dnn_sk'] = test['pos_dnn_sk'] * test['returns']

test[['returns', 'strat_dnn_sk']].sum().apply(np.exp)

test[['returns', 'strat_dnn_sk']].cumsum().apply(np.exp).plot(figsize=(10,6),
                                                              title='Performance of EUR/USD and DNN-based trading strategy (scikit-learn, randomized train-test split)'
                                                             )

DNNs with TensorFlow

TensorFlow has become a popular package for deep learning. It is developed and supported by Google Inc. and applied there to a great variety of machine learning problems. Zedah and Ramsundar (2018) cover TensorFlow for deep learning in depth

As with scikit-learn, the application of the DNNClassifier algorithm from TensorFlow to derive an algorithmic trading strategy is straightforward given the background from “Deep neural networks”. The training and test data is the same as before. First, the training of the model. In-sample, the algorithm outperforms the passive benchmark investment and shows a considerable absolute return (see Figure 15-19), again hinting at overfitting

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)

fc =[tf.contrib.layers.real_valued_column('lags', dimension=lags)]

model = tf.contrib.learn.DNNClassifier(hidden_units=3*[500],
                                       n_classes=len(bins)+1,
                                       feature_columns=fc
                                      )

def input_fn():
    fc = {'lags': tf.constant(data[cols_bin].values)}
    la = tf.constant(data['direction'].apply(lambda x: 0 if x<0 else 1).values,
                     shape=[data['direction'].size,1]
                    )
    return fc, la

%time model.fit(input_fn=input_fn, steps=250)

model.evaluate(input_fn=input_fn, steps=1)

pred = np.array(list(model.predict(input_fn = input_fn)))
pred[:50]

data['pos_dnn_tf'] = np.where(pred>0,1,-1)

data['strat_dnn_tf'] = data['pos_dnn_tf'] * data['returns']

data[['returns', 'strat_dnn_tf']].sum().apply(np.exp)

data[['returns', 'strat_dnn_tf']].cumsum().apply(np.exp).plot(figsize=(10,6),
                                                             title='Performance of EUR/USD and DNN-based trading strategy (TensorFlow, in-sample)'
                                                             )

The following code again implements a randomized train-test split to get a more realistic view of the performance of the DNN-based algorithmic trading strategy. The performance is, as expected, worse out-of-sample (see Figure 15-20). In addition, given the specific parameterization the TensorFlow DNNClassifier underperforms the scikit-learn MLPClassifier algorithm by quite few percentage points:

model = tf.contrib.learn.DNNClassifier(hidden_units=3*[500],
                                       n_classes=len(bins)+1,
                                       feature_columns=fc
                                      )

data=train

%time model.fit(input_fn=input_fn, steps=2500)

data=test

model.evaluateate(input_fn=input_fn, steps=1)

pred = np.array(list(model.predict(input_fn=input_fn)))

test['pos_dnn_tf'] = np.where(pred>0,1,-1)

test['strat_dnn_tf'] = test['pos_dnn_tf']*test['returns']

test[['returns','strat_dnn_sk', 'strat_dnn_tf']].sum().apply(np.exp)

test[['returns','strat_dnn_sk','strat_dnn_tf']].cumsum().apply(np.exp).plot(figsize=(10,6),
                                                                           titile='Performance of EUR/USD and DNN-based trading strategy (TensorFlow, randomized train-test split)')

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值