-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathTDQN.py
More file actions
executable file
·1080 lines (840 loc) · 46.2 KB
/
TDQN.py
File metadata and controls
executable file
·1080 lines (840 loc) · 46.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
"""
Goal: Implementing a custom enhanced version of the DQN algorithm specialized
to algorithmic trading.
Authors: Thibaut Théate and Damien Ernst
Institution: University of Liège
"""
###############################################################################
################################### Imports ###################################
###############################################################################
import math
import random
import copy
import datetime
import numpy as np
from collections import deque
from tqdm import tqdm
from matplotlib import pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
from tradingPerformance import PerformanceEstimator
from dataAugmentation import DataAugmentation
from tradingEnv import TradingEnv
###############################################################################
################################ Global variables #############################
###############################################################################
# Default parameters related to the DQN algorithm
gamma = 0.4
learningRate = 0.0001
targetNetworkUpdate = 1000
learningUpdatePeriod = 1
# Default parameters related to the Experience Replay mechanism
capacity = 100000
batchSize = 32
experiencesRequired = 1000
# Default parameters related to the Deep Neural Network
numberOfNeurons = 512
dropout = 0.2
# Default parameters related to the Epsilon-Greedy exploration technique
epsilonStart = 1.0
epsilonEnd = 0.01
epsilonDecay = 10000
# Default parameters regarding the sticky actions RL generalization technique
alpha = 0.1
# Default parameters related to preprocessing
filterOrder = 5
# Default paramters related to the clipping of both the gradient and the RL rewards
gradientClipping = 1
rewardClipping = 1
# Default parameter related to the L2 Regularization
L2Factor = 0.000001
# Default paramter related to the hardware acceleration (CUDA)
GPUNumber = 0
###############################################################################
############################### Class ReplayMemory ############################
###############################################################################
class ReplayMemory:
"""
GOAL: Implementing the replay memory required for the Experience Replay
mechanism of the DQN Reinforcement Learning algorithm.
VARIABLES: - memory: Data structure storing the experiences.
METHODS: - __init__: Initialization of the memory data structure.
- push: Insert a new experience into the replay memory.
- sample: Sample a batch of experiences from the replay memory.
- __len__: Return the length of the replay memory.
- reset: Reset the replay memory.
"""
def __init__(self, capacity=capacity):
"""
GOAL: Initializating the replay memory data structure.
INPUTS: - capacity: Capacity of the data structure, specifying the
maximum number of experiences to be stored
simultaneously.
OUTPUTS: /
"""
self.memory = deque(maxlen=capacity)
def push(self, state, action, reward, nextState, done):
"""
GOAL: Insert a new experience into the replay memory. An experience
is composed of a state, an action, a reward, a next state and
a termination signal.
INPUTS: - state: RL state of the experience to be stored.
- action: RL action of the experience to be stored.
- reward: RL reward of the experience to be stored.
- nextState: RL next state of the experience to be stored.
- done: RL termination signal of the experience to be stored.
OUTPUTS: /
"""
self.memory.append((state, action, reward, nextState, done))
def sample(self, batchSize):
"""
GOAL: Sample a batch of experiences from the replay memory.
INPUTS: - batchSize: Size of the batch to sample.
OUTPUTS: - state: RL states of the experience batch sampled.
- action: RL actions of the experience batch sampled.
- reward: RL rewards of the experience batch sampled.
- nextState: RL next states of the experience batch sampled.
- done: RL termination signals of the experience batch sampled.
"""
state, action, reward, nextState, done = zip(*random.sample(self.memory, batchSize))
return state, action, reward, nextState, done
def __len__(self):
"""
GOAL: Return the capicity of the replay memory, which is the maximum number of
experiences which can be simultaneously stored in the replay memory.
INPUTS: /
OUTPUTS: - length: Capacity of the replay memory.
"""
return len(self.memory)
def reset(self):
"""
GOAL: Reset (empty) the replay memory.
INPUTS: /
OUTPUTS: /
"""
self.memory = deque(maxlen=capacity)
###############################################################################
################################### Class DQN #################################
###############################################################################
class DQN(nn.Module):
"""
GOAL: Implementing the Deep Neural Network of the DQN Reinforcement
Learning algorithm.
VARIABLES: - fc1: Fully Connected layer number 1.
- fc2: Fully Connected layer number 2.
- fc3: Fully Connected layer number 3.
- fc4: Fully Connected layer number 4.
- fc5: Fully Connected layer number 5.
- dropout1: Dropout layer number 1.
- dropout2: Dropout layer number 2.
- dropout3: Dropout layer number 3.
- dropout4: Dropout layer number 4.
- bn1: Batch normalization layer number 1.
- bn2: Batch normalization layer number 2.
- bn3: Batch normalization layer number 3.
- bn4: Batch normalization layer number 4.
METHODS: - __init__: Initialization of the Deep Neural Network.
- forward: Forward pass of the Deep Neural Network.
"""
def __init__(self, numberOfInputs, numberOfOutputs, numberOfNeurons=numberOfNeurons, dropout=dropout):
"""
GOAL: Defining and initializing the Deep Neural Network of the
DQN Reinforcement Learning algorithm.
INPUTS: - numberOfInputs: Number of inputs of the Deep Neural Network.
- numberOfOutputs: Number of outputs of the Deep Neural Network.
- numberOfNeurons: Number of neurons per layer in the Deep Neural Network.
- dropout: Droupout probability value (handling of overfitting).
OUTPUTS: /
"""
# Call the constructor of the parent class (Pytorch torch.nn.Module)
super(DQN, self).__init__()
# Definition of some Fully Connected layers
self.fc1 = nn.Linear(numberOfInputs, numberOfNeurons)
self.fc2 = nn.Linear(numberOfNeurons, numberOfNeurons)
self.fc3 = nn.Linear(numberOfNeurons, numberOfNeurons)
self.fc4 = nn.Linear(numberOfNeurons, numberOfNeurons)
self.fc5 = nn.Linear(numberOfNeurons, numberOfOutputs)
# Definition of some Batch Normalization layers
self.bn1 = nn.BatchNorm1d(numberOfNeurons)
self.bn2 = nn.BatchNorm1d(numberOfNeurons)
self.bn3 = nn.BatchNorm1d(numberOfNeurons)
self.bn4 = nn.BatchNorm1d(numberOfNeurons)
# Definition of some Dropout layers.
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.dropout3 = nn.Dropout(dropout)
self.dropout4 = nn.Dropout(dropout)
# Xavier initialization for the entire neural network
torch.nn.init.xavier_uniform_(self.fc1.weight)
torch.nn.init.xavier_uniform_(self.fc2.weight)
torch.nn.init.xavier_uniform_(self.fc3.weight)
torch.nn.init.xavier_uniform_(self.fc4.weight)
torch.nn.init.xavier_uniform_(self.fc5.weight)
def forward(self, input):
"""
GOAL: Implementing the forward pass of the Deep Neural Network.
INPUTS: - input: Input of the Deep Neural Network.
OUTPUTS: - output: Output of the Deep Neural Network.
"""
x = self.dropout1(F.leaky_relu(self.bn1(self.fc1(input))))
x = self.dropout2(F.leaky_relu(self.bn2(self.fc2(x))))
x = self.dropout3(F.leaky_relu(self.bn3(self.fc3(x))))
x = self.dropout4(F.leaky_relu(self.bn4(self.fc4(x))))
output = self.fc5(x)
return output
###############################################################################
################################ Class TDQN ###################################
###############################################################################
class TDQN:
"""
GOAL: Implementing an intelligent trading agent based on the DQN
Reinforcement Learning algorithm.
VARIABLES: - device: Hardware specification (CPU or GPU).
- gamma: Discount factor of the DQN algorithm.
- learningRate: Learning rate of the ADAM optimizer.
- capacity: Capacity of the experience replay memory.
- batchSize: Size of the batch to sample from the replay memory.
- targetNetworkUpdate: Frequency at which the target neural
network is updated.
- observationSpace: Size of the RL observation space.
- actionSpace: Size of the RL action space.
- policyNetwork: Deep Neural Network representing the RL policy.
- targetNetwork: Deep Neural Network representing a target
for the policy Deep Neural Network.
- optimizer: Deep Neural Network optimizer (ADAM).
- replayMemory: Experience replay memory.
- epsilonValue: Value of the Epsilon, from the
Epsilon-Greedy exploration technique.
- iterations: Counter of the number of iterations.
METHODS: - __init__: Initialization of the RL trading agent, by setting up
many variables and parameters.
- getNormalizationCoefficients: Retrieve the coefficients required
for the normalization of input data.
- processState: Process the RL state received.
- processReward: Clipping of the RL reward received.
- updateTargetNetwork: Update the target network, by transfering
the policy network parameters.
- chooseAction: Choose a valid action based on the current state
observed, according to the RL policy learned.
- chooseActionEpsilonGreedy: Choose a valid action based on the
current state observed, according to
the RL policy learned, following the
Epsilon Greedy exploration mechanism.
- learn: Sample a batch of experiences and learn from that info.
- training: Train the trading DQN agent by interacting with its
trading environment.
- testing: Test the DQN agent trading policy on a new trading environment.
- plotExpectedPerformance: Plot the expected performance of the intelligent
DRL trading agent.
- saveModel: Save the RL policy model.
- loadModel: Load the RL policy model.
- plotTraining: Plot the training results (score evolution, etc.).
- plotEpsilonAnnealing: Plot the annealing behaviour of the Epsilon
(Epsilon-Greedy exploration technique).
"""
def __init__(self, observationSpace, actionSpace, numberOfNeurons=numberOfNeurons, dropout=dropout,
gamma=gamma, learningRate=learningRate, targetNetworkUpdate=targetNetworkUpdate,
epsilonStart=epsilonStart, epsilonEnd=epsilonEnd, epsilonDecay=epsilonDecay,
capacity=capacity, batchSize=batchSize):
"""
GOAL: Initializing the RL agent based on the DQN Reinforcement Learning
algorithm, by setting up the DQN algorithm parameters as well as
the DQN Deep Neural Network.
INPUTS: - observationSpace: Size of the RL observation space.
- actionSpace: Size of the RL action space.
- numberOfNeurons: Number of neurons per layer in the Deep Neural Network.
- dropout: Droupout probability value (handling of overfitting).
- gamma: Discount factor of the DQN algorithm.
- learningRate: Learning rate of the ADAM optimizer.
- targetNetworkUpdate: Update frequency of the target network.
- epsilonStart: Initial (maximum) value of Epsilon, from the
Epsilon-Greedy exploration technique.
- epsilonEnd: Final (minimum) value of Epsilon, from the
Epsilon-Greedy exploration technique.
- epsilonDecay: Decay factor (exponential) of Epsilon, from the
Epsilon-Greedy exploration technique.
- capacity: Capacity of the Experience Replay memory.
- batchSize: Size of the batch to sample from the replay memory.
OUTPUTS: /
"""
# Initialise the random function with a new random seed
random.seed(0)
# Check availability of CUDA for the hardware (CPU or GPU)
self.device = torch.device('cuda:'+str(GPUNumber) if torch.cuda.is_available() else 'cpu')
# Set the general parameters of the DQN algorithm
self.gamma = gamma
self.learningRate = learningRate
self.targetNetworkUpdate = targetNetworkUpdate
# Set the Experience Replay mechnism
self.capacity = capacity
self.batchSize = batchSize
self.replayMemory = ReplayMemory(capacity)
# Set both the observation and action spaces
self.observationSpace = observationSpace
self.actionSpace = actionSpace
# Set the two Deep Neural Networks of the DQN algorithm (policy and target)
self.policyNetwork = DQN(observationSpace, actionSpace, numberOfNeurons, dropout).to(self.device)
self.targetNetwork = DQN(observationSpace, actionSpace, numberOfNeurons, dropout).to(self.device)
self.targetNetwork.load_state_dict(self.policyNetwork.state_dict())
self.policyNetwork.eval()
self.targetNetwork.eval()
# Set the Deep Learning optimizer
self.optimizer = optim.Adam(self.policyNetwork.parameters(), lr=learningRate, weight_decay=L2Factor)
# Set the Epsilon-Greedy exploration technique
self.epsilonValue = lambda iteration: epsilonEnd + (epsilonStart - epsilonEnd) * math.exp(-1 * iteration / epsilonDecay)
# Initialization of the iterations counter
self.iterations = 0
# Initialization of the tensorboard writer
self.writer = SummaryWriter('runs/' + datetime.datetime.now().strftime("%d/%m/%Y-%H:%M:%S"))
def getNormalizationCoefficients(self, tradingEnv):
"""
GOAL: Retrieve the coefficients required for the normalization
of input data.
INPUTS: - tradingEnv: RL trading environement to process.
OUTPUTS: - coefficients: Normalization coefficients.
"""
# Retrieve the available trading data
tradingData = tradingEnv.data
closePrices = tradingData['Close'].tolist()
lowPrices = tradingData['Low'].tolist()
highPrices = tradingData['High'].tolist()
volumes = tradingData['Volume'].tolist()
# Retrieve the coefficients required for the normalization
coefficients = []
margin = 1
# 1. Close price => returns (absolute) => maximum value (absolute)
returns = [abs((closePrices[i]-closePrices[i-1])/closePrices[i-1]) for i in range(1, len(closePrices))]
coeffs = (0, np.max(returns)*margin)
coefficients.append(coeffs)
# 2. Low/High prices => Delta prices => maximum value
deltaPrice = [abs(highPrices[i]-lowPrices[i]) for i in range(len(lowPrices))]
coeffs = (0, np.max(deltaPrice)*margin)
coefficients.append(coeffs)
# 3. Close/Low/High prices => Close price position => no normalization required
coeffs = (0, 1)
coefficients.append(coeffs)
# 4. Volumes => minimum and maximum values
coeffs = (np.min(volumes)/margin, np.max(volumes)*margin)
coefficients.append(coeffs)
return coefficients
def processState(self, state, coefficients):
"""
GOAL: Process the RL state returned by the environment
(appropriate format and normalization).
INPUTS: - state: RL state returned by the environment.
OUTPUTS: - state: Processed RL state.
"""
# Normalization of the RL state
closePrices = [state[0][i] for i in range(len(state[0]))]
lowPrices = [state[1][i] for i in range(len(state[1]))]
highPrices = [state[2][i] for i in range(len(state[2]))]
volumes = [state[3][i] for i in range(len(state[3]))]
# 1. Close price => returns => MinMax normalization
returns = [(closePrices[i]-closePrices[i-1])/closePrices[i-1] for i in range(1, len(closePrices))]
if coefficients[0][0] != coefficients[0][1]:
state[0] = [((x - coefficients[0][0])/(coefficients[0][1] - coefficients[0][0])) for x in returns]
else:
state[0] = [0 for x in returns]
# 2. Low/High prices => Delta prices => MinMax normalization
deltaPrice = [abs(highPrices[i]-lowPrices[i]) for i in range(1, len(lowPrices))]
if coefficients[1][0] != coefficients[1][1]:
state[1] = [((x - coefficients[1][0])/(coefficients[1][1] - coefficients[1][0])) for x in deltaPrice]
else:
state[1] = [0 for x in deltaPrice]
# 3. Close/Low/High prices => Close price position => No normalization required
closePricePosition = []
for i in range(1, len(closePrices)):
deltaPrice = abs(highPrices[i]-lowPrices[i])
if deltaPrice != 0:
item = abs(closePrices[i]-lowPrices[i])/deltaPrice
else:
item = 0.5
closePricePosition.append(item)
if coefficients[2][0] != coefficients[2][1]:
state[2] = [((x - coefficients[2][0])/(coefficients[2][1] - coefficients[2][0])) for x in closePricePosition]
else:
state[2] = [0.5 for x in closePricePosition]
# 4. Volumes => MinMax normalization
volumes = [volumes[i] for i in range(1, len(volumes))]
if coefficients[3][0] != coefficients[3][1]:
state[3] = [((x - coefficients[3][0])/(coefficients[3][1] - coefficients[3][0])) for x in volumes]
else:
state[3] = [0 for x in volumes]
# Process the state structure to obtain the appropriate format
state = [item for sublist in state for item in sublist]
return state
def processReward(self, reward):
"""
GOAL: Process the RL reward returned by the environment by clipping
its value. Such technique has been shown to improve the stability
the DQN algorithm.
INPUTS: - reward: RL reward returned by the environment.
OUTPUTS: - reward: Process RL reward.
"""
return np.clip(reward, -rewardClipping, rewardClipping)
def updateTargetNetwork(self):
"""
GOAL: Taking into account the update frequency (parameter), update the
target Deep Neural Network by copying the policy Deep Neural Network
parameters (weights, bias, etc.).
INPUTS: /
OUTPUTS: /
"""
# Check if an update is required (update frequency)
if(self.iterations % targetNetworkUpdate == 0):
# Transfer the DNN parameters (policy network -> target network)
self.targetNetwork.load_state_dict(self.policyNetwork.state_dict())
def chooseAction(self, state):
"""
GOAL: Choose a valid RL action from the action space according to the
RL policy as well as the current RL state observed.
INPUTS: - state: RL state returned by the environment.
OUTPUTS: - action: RL action chosen from the action space.
- Q: State-action value function associated.
- QValues: Array of all the Qvalues outputted by the
Deep Neural Network.
"""
# Choose the best action based on the RL policy
with torch.no_grad():
tensorState = torch.tensor(state, dtype=torch.float, device=self.device).unsqueeze(0)
QValues = self.policyNetwork(tensorState).squeeze(0)
Q, action = QValues.max(0)
action = action.item()
Q = Q.item()
QValues = QValues.cpu().numpy()
return action, Q, QValues
def chooseActionEpsilonGreedy(self, state, previousAction):
"""
GOAL: Choose a valid RL action from the action space according to the
RL policy as well as the current RL state observed, following the
Epsilon Greedy exploration mechanism.
INPUTS: - state: RL state returned by the environment.
- previousAction: Previous RL action executed by the agent.
OUTPUTS: - action: RL action chosen from the action space.
- Q: State-action value function associated.
- QValues: Array of all the Qvalues outputted by the
Deep Neural Network.
"""
# EXPLOITATION -> RL policy
if(random.random() > self.epsilonValue(self.iterations)):
# Sticky action (RL generalization mechanism)
if(random.random() > alpha):
action, Q, QValues = self.chooseAction(state)
else:
action = previousAction
Q = 0
QValues = [0, 0]
# EXPLORATION -> Random
else:
action = random.randrange(self.actionSpace)
Q = 0
QValues = [0, 0]
# Increment the iterations counter (for Epsilon Greedy)
self.iterations += 1
return action, Q, QValues
def learning(self, batchSize=batchSize):
"""
GOAL: Sample a batch of past experiences and learn from it
by updating the Reinforcement Learning policy.
INPUTS: batchSize: Size of the batch to sample from the replay memory.
OUTPUTS: /
"""
# Check that the replay memory is filled enough
if (len(self.replayMemory) >= batchSize):
# Set the Deep Neural Network in training mode
self.policyNetwork.train()
# Sample a batch of experiences from the replay memory
state, action, reward, nextState, done = self.replayMemory.sample(batchSize)
# Initialization of Pytorch tensors for the RL experience elements
state = torch.tensor(state, dtype=torch.float, device=self.device)
action = torch.tensor(action, dtype=torch.long, device=self.device)
reward = torch.tensor(reward, dtype=torch.float, device=self.device)
nextState = torch.tensor(nextState, dtype=torch.float, device=self.device)
done = torch.tensor(done, dtype=torch.float, device=self.device)
# Compute the current Q values returned by the policy network
currentQValues = self.policyNetwork(state).gather(1, action.unsqueeze(1)).squeeze(1)
# Compute the next Q values returned by the target network
with torch.no_grad():
nextActions = torch.max(self.policyNetwork(nextState), 1)[1]
nextQValues = self.targetNetwork(nextState).gather(1, nextActions.unsqueeze(1)).squeeze(1)
expectedQValues = reward + gamma * nextQValues * (1 - done)
# Compute the Huber loss
loss = F.smooth_l1_loss(currentQValues, expectedQValues)
# Computation of the gradients
self.optimizer.zero_grad()
loss.backward()
# Gradient Clipping
torch.nn.utils.clip_grad_norm_(self.policyNetwork.parameters(), gradientClipping)
# Perform the Deep Neural Network optimization
self.optimizer.step()
# If required, update the target deep neural network (update frequency)
self.updateTargetNetwork()
# Set back the Deep Neural Network in evaluation mode
self.policyNetwork.eval()
def training(self, trainingEnv, trainingParameters=[],
verbose=False, rendering=False, plotTraining=False, showPerformance=False):
"""
GOAL: Train the RL trading agent by interacting with its trading environment.
INPUTS: - trainingEnv: Training RL environment (known).
- trainingParameters: Additional parameters associated
with the training phase (e.g. the number
of episodes).
- verbose: Enable the printing of a training feedback.
- rendering: Enable the training environment rendering.
- plotTraining: Enable the plotting of the training results.
- showPerformance: Enable the printing of a table summarizing
the trading strategy performance.
OUTPUTS: - trainingEnv: Training RL environment.
"""
"""
# Compute and plot the expected performance of the trading policy
trainingEnv = self.plotExpectedPerformance(trainingEnv, trainingParameters, iterations=50)
return trainingEnv
"""
# Apply data augmentation techniques to improve the training set
dataAugmentation = DataAugmentation()
trainingEnvList = dataAugmentation.generate(trainingEnv)
# Initialization of some variables tracking the training and testing performances
if plotTraining:
# Training performance
performanceTrain = []
score = np.zeros((len(trainingEnvList), trainingParameters[0]))
# Testing performance
marketSymbol = trainingEnv.marketSymbol
startingDate = trainingEnv.endingDate
endingDate = '2020-1-1'
money = trainingEnv.data['Money'][0]
stateLength = trainingEnv.stateLength
transactionCosts = trainingEnv.transactionCosts
testingEnv = TradingEnv(marketSymbol, startingDate, endingDate, money, stateLength, transactionCosts)
performanceTest = []
try:
# If required, print the training progression
if verbose:
print("Training progression (hardware selected => " + str(self.device) + "):")
# Training phase for the number of episodes specified as parameter
for episode in tqdm(range(trainingParameters[0]), disable=not(verbose)):
# For each episode, train on the entire set of training environments
for i in range(len(trainingEnvList)):
# Set the initial RL variables
coefficients = self.getNormalizationCoefficients(trainingEnvList[i])
trainingEnvList[i].reset()
startingPoint = random.randrange(len(trainingEnvList[i].data.index))
trainingEnvList[i].setStartingPoint(startingPoint)
state = self.processState(trainingEnvList[i].state, coefficients)
previousAction = 0
done = 0
stepsCounter = 0
# Set the performance tracking veriables
if plotTraining:
totalReward = 0
# Interact with the training environment until termination
while done == 0:
# Choose an action according to the RL policy and the current RL state
action, _, _ = self.chooseActionEpsilonGreedy(state, previousAction)
# Interact with the environment with the chosen action
nextState, reward, done, info = trainingEnvList[i].step(action)
# Process the RL variables retrieved and insert this new experience into the Experience Replay memory
reward = self.processReward(reward)
nextState = self.processState(nextState, coefficients)
self.replayMemory.push(state, action, reward, nextState, done)
# Trick for better exploration
otherAction = int(not bool(action))
otherReward = self.processReward(info['Reward'])
otherNextState = self.processState(info['State'], coefficients)
otherDone = info['Done']
self.replayMemory.push(state, otherAction, otherReward, otherNextState, otherDone)
# Execute the DQN learning procedure
stepsCounter += 1
if stepsCounter == learningUpdatePeriod:
self.learning()
stepsCounter = 0
# Update the RL state
state = nextState
previousAction = action
# Continuous tracking of the training performance
if plotTraining:
totalReward += reward
# Store the current training results
if plotTraining:
score[i][episode] = totalReward
# Compute the current performance on both the training and testing sets
if plotTraining:
# Training set performance
trainingEnv = self.testing(trainingEnv, trainingEnv)
analyser = PerformanceEstimator(trainingEnv.data)
performance = analyser.computeSharpeRatio()
performanceTrain.append(performance)
self.writer.add_scalar('Training performance (Sharpe Ratio)', performance, episode)
trainingEnv.reset()
# Testing set performance
testingEnv = self.testing(trainingEnv, testingEnv)
analyser = PerformanceEstimator(testingEnv.data)
performance = analyser.computeSharpeRatio()
performanceTest.append(performance)
self.writer.add_scalar('Testing performance (Sharpe Ratio)', performance, episode)
testingEnv.reset()
except KeyboardInterrupt:
print()
print("WARNING: Training prematurely interrupted...")
print()
self.policyNetwork.eval()
# Assess the algorithm performance on the training trading environment
trainingEnv = self.testing(trainingEnv, trainingEnv)
# If required, show the rendering of the trading environment
if rendering:
trainingEnv.render()
# If required, plot the training results
if plotTraining:
fig = plt.figure()
ax = fig.add_subplot(111, ylabel='Performance (Sharpe Ratio)', xlabel='Episode')
ax.plot(performanceTrain)
ax.plot(performanceTest)
ax.legend(["Training", "Testing"])
plt.savefig(''.join(['Figures/', str(marketSymbol), '_TrainingTestingPerformance', '.png']))
#plt.show()
for i in range(len(trainingEnvList)):
self.plotTraining(score[i][:episode], marketSymbol)
# If required, print the strategy performance in a table
if showPerformance:
analyser = PerformanceEstimator(trainingEnv.data)
analyser.displayPerformance('TDQN')
# Closing of the tensorboard writer
self.writer.close()
return trainingEnv
def testing(self, trainingEnv, testingEnv, rendering=False, showPerformance=False):
"""
GOAL: Test the RL agent trading policy on a new trading environment
in order to assess the trading strategy performance.
INPUTS: - trainingEnv: Training RL environment (known).
- testingEnv: Unknown trading RL environment.
- rendering: Enable the trading environment rendering.
- showPerformance: Enable the printing of a table summarizing
the trading strategy performance.
OUTPUTS: - testingEnv: Trading environment backtested.
"""
# Apply data augmentation techniques to process the testing set
dataAugmentation = DataAugmentation()
testingEnvSmoothed = dataAugmentation.lowPassFilter(testingEnv, filterOrder)
trainingEnv = dataAugmentation.lowPassFilter(trainingEnv, filterOrder)
# Initialization of some RL variables
coefficients = self.getNormalizationCoefficients(trainingEnv)
state = self.processState(testingEnvSmoothed.reset(), coefficients)
testingEnv.reset()
QValues0 = []
QValues1 = []
done = 0
# Interact with the environment until the episode termination
while done == 0:
# Choose an action according to the RL policy and the current RL state
action, _, QValues = self.chooseAction(state)
# Interact with the environment with the chosen action
nextState, _, done, _ = testingEnvSmoothed.step(action)
testingEnv.step(action)
# Update the new state
state = self.processState(nextState, coefficients)
# Storing of the Q values
QValues0.append(QValues[0])
QValues1.append(QValues[1])
# If required, show the rendering of the trading environment
if rendering:
testingEnv.render()
self.plotQValues(QValues0, QValues1, testingEnv.marketSymbol)
# If required, print the strategy performance in a table
if showPerformance:
analyser = PerformanceEstimator(testingEnv.data)
analyser.displayPerformance('TDQN')
return testingEnv
def plotTraining(self, score, marketSymbol):
"""
GOAL: Plot the training phase results
(score, sum of rewards).
INPUTS: - score: Array of total episode rewards.
- marketSymbol: Stock market trading symbol.
OUTPUTS: /
"""
fig = plt.figure()
ax1 = fig.add_subplot(111, ylabel='Total reward collected', xlabel='Episode')
ax1.plot(score)
plt.savefig(''.join(['Figures/', str(marketSymbol), 'TrainingResults', '.png']))
#plt.show()
def plotQValues(self, QValues0, QValues1, marketSymbol):
"""
Plot sequentially the Q values related to both actions.
:param: - QValues0: Array of Q values linked to action 0.
- QValues1: Array of Q values linked to action 1.
- marketSymbol: Stock market trading symbol.
:return: /
"""
fig = plt.figure()
ax1 = fig.add_subplot(111, ylabel='Q values', xlabel='Time')
ax1.plot(QValues0)
ax1.plot(QValues1)
ax1.legend(['Short', 'Long'])
plt.savefig(''.join(['Figures/', str(marketSymbol), '_QValues', '.png']))
#plt.show()
def plotExpectedPerformance(self, trainingEnv, trainingParameters=[], iterations=10):
"""
GOAL: Plot the expected performance of the intelligent DRL trading agent.
INPUTS: - trainingEnv: Training RL environment (known).
- trainingParameters: Additional parameters associated
with the training phase (e.g. the number
of episodes).
- iterations: Number of training/testing iterations to compute
the expected performance.
OUTPUTS: - trainingEnv: Training RL environment.
"""
# Preprocessing of the training set
dataAugmentation = DataAugmentation()
trainingEnvList = dataAugmentation.generate(trainingEnv)
# Save the initial Deep Neural Network weights
initialWeights = copy.deepcopy(self.policyNetwork.state_dict())
# Initialization of some variables tracking both training and testing performances
performanceTrain = np.zeros((trainingParameters[0], iterations))
performanceTest = np.zeros((trainingParameters[0], iterations))
# Initialization of the testing trading environment
marketSymbol = trainingEnv.marketSymbol
startingDate = trainingEnv.endingDate
endingDate = '2020-1-1'
money = trainingEnv.data['Money'][0]
stateLength = trainingEnv.stateLength
transactionCosts = trainingEnv.transactionCosts
testingEnv = TradingEnv(marketSymbol, startingDate, endingDate, money, stateLength, transactionCosts)
# Print the hardware selected for the training of the Deep Neural Network (either CPU or GPU)
print("Hardware selected for training: " + str(self.device))
try:
# Apply the training/testing procedure for the number of iterations specified
for iteration in range(iterations):
# Print the progression
print(''.join(["Expected performance evaluation progression: ", str(iteration+1), "/", str(iterations)]))
# Training phase for the number of episodes specified as parameter
for episode in tqdm(range(trainingParameters[0])):
# For each episode, train on the entire set of training environments
for i in range(len(trainingEnvList)):
# Set the initial RL variables
coefficients = self.getNormalizationCoefficients(trainingEnvList[i])
trainingEnvList[i].reset()
startingPoint = random.randrange(len(trainingEnvList[i].data.index))
trainingEnvList[i].setStartingPoint(startingPoint)
state = self.processState(trainingEnvList[i].state, coefficients)
previousAction = 0
done = 0
stepsCounter = 0
# Interact with the training environment until termination
while done == 0:
# Choose an action according to the RL policy and the current RL state
action, _, _ = self.chooseActionEpsilonGreedy(state, previousAction)
# Interact with the environment with the chosen action
nextState, reward, done, info = trainingEnvList[i].step(action)
# Process the RL variables retrieved and insert this new experience into the Experience Replay memory
reward = self.processReward(reward)
nextState = self.processState(nextState, coefficients)
self.replayMemory.push(state, action, reward, nextState, done)
# Trick for better exploration
otherAction = int(not bool(action))
otherReward = self.processReward(info['Reward'])
otherDone = info['Done']
otherNextState = self.processState(info['State'], coefficients)
self.replayMemory.push(state, otherAction, otherReward, otherNextState, otherDone)
# Execute the DQN learning procedure
stepsCounter += 1
if stepsCounter == learningUpdatePeriod:
self.learning()
stepsCounter = 0
# Update the RL state
state = nextState
previousAction = action
# Compute both training and testing current performances
trainingEnv = self.testing(trainingEnv, trainingEnv)
analyser = PerformanceEstimator(trainingEnv.data)
performanceTrain[episode][iteration] = analyser.computeSharpeRatio()
self.writer.add_scalar('Training performance (Sharpe Ratio)', performanceTrain[episode][iteration], episode)
testingEnv = self.testing(trainingEnv, testingEnv)
analyser = PerformanceEstimator(testingEnv.data)
performanceTest[episode][iteration] = analyser.computeSharpeRatio()
self.writer.add_scalar('Testing performance (Sharpe Ratio)', performanceTest[episode][iteration], episode)
# Restore the initial state of the intelligent RL agent
if iteration < (iterations-1):
trainingEnv.reset()
testingEnv.reset()
self.policyNetwork.load_state_dict(initialWeights)
self.targetNetwork.load_state_dict(initialWeights)
self.optimizer = optim.Adam(self.policyNetwork.parameters(), lr=learningRate, weight_decay=L2Factor)
self.replayMemory.reset()
self.iterations = 0
stepsCounter = 0
iteration += 1
except KeyboardInterrupt:
print()
print("WARNING: Expected performance evaluation prematurely interrupted...")
print()
self.policyNetwork.eval()
# Compute the expected performance of the intelligent DRL trading agent
expectedPerformanceTrain = []
expectedPerformanceTest = []