-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathtradingSimulator.py
More file actions
executable file
·694 lines (583 loc) · 31.3 KB
/
tradingSimulator.py
File metadata and controls
executable file
·694 lines (583 loc) · 31.3 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
# coding=utf-8
"""
Goal: Implement a trading simulator to simulate and compare trading strategies.
Authors: Thibaut Théate and Damien Ernst
Institution: University of Liège
"""
###############################################################################
################################### Imports ###################################
###############################################################################
import os
import sys
import importlib
import pickle
import itertools
import numpy as np
import pandas as pd
from tabulate import tabulate
from tqdm import tqdm
from matplotlib import pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
from tradingEnv import TradingEnv
from tradingPerformance import PerformanceEstimator
from timeSeriesAnalyser import TimeSeriesAnalyser
from TDQN import TDQN
###############################################################################
################################ Global variables #############################
###############################################################################
# Variables defining the default trading horizon
startingDate = '2012-1-1'
endingDate = '2020-1-1'
splitingDate = '2018-1-1'
# Variables defining the default observation and state spaces
stateLength = 30
observationSpace = 1 + (stateLength-1)*4
actionSpace = 2
# Variables setting up the default transaction costs
percentageCosts = [0, 0.1, 0.2]
transactionCosts = percentageCosts[1]/100
# Variables specifying the default capital at the disposal of the trader
money = 100000
# Variables specifying the default general training parameters
bounds = [1, 30]
step = 1
numberOfEpisodes = 50
# Dictionary listing the fictive stocks supported
fictives = {
'Linear Upward' : 'LINEARUP',
'Linear Downward' : 'LINEARDOWN',
'Sinusoidal' : 'SINUSOIDAL',
'Triangle' : 'TRIANGLE',
}
# Dictionary listing the 30 stocks considered as testbench
stocks = {
'Dow Jones' : 'DIA',
'S&P 500' : 'SPY',
'NASDAQ 100' : 'QQQ',
'FTSE 100' : 'EZU',
'Nikkei 225' : 'EWJ',
'Google' : 'GOOGL',
'Apple' : 'AAPL',
'Facebook' : 'FB',
'Amazon' : 'AMZN',
'Microsoft' : 'MSFT',
'Twitter' : 'TWTR',
'Nokia' : 'NOK',
'Philips' : 'PHIA.AS',
'Siemens' : 'SIE.DE',
'Baidu' : 'BIDU',
'Alibaba' : 'BABA',
'Tencent' : '0700.HK',
'Sony' : '6758.T',
'JPMorgan Chase' : 'JPM',
'HSBC' : 'HSBC',
'CCB' : '0939.HK',
'ExxonMobil' : 'XOM',
'Shell' : 'RDSA.AS',
'PetroChina' : 'PTR',
'Tesla' : 'TSLA',
'Volkswagen' : 'VOW3.DE',
'Toyota' : '7203.T',
'Coca Cola' : 'KO',
'AB InBev' : 'ABI.BR',
'Kirin' : '2503.T'
}
# Dictionary listing the 5 trading indices considered as testbench
indices = {
'Dow Jones' : 'DIA',
'S&P 500' : 'SPY',
'NASDAQ 100' : 'QQQ',
'FTSE 100' : 'EZU',
'Nikkei 225' : 'EWJ'
}
# Dictionary listing the 25 company stocks considered as testbench
companies = {
'Google' : 'GOOGL',
'Apple' : 'AAPL',
'Facebook' : 'FB',
'Amazon' : 'AMZN',
'Microsoft' : 'MSFT',
'Twitter' : 'TWTR',
'Nokia' : 'NOK',
'Philips' : 'PHIA.AS',
'Siemens' : 'SIE.DE',
'Baidu' : 'BIDU',
'Alibaba' : 'BABA',
'Tencent' : '0700.HK',
'Sony' : '6758.T',
'JPMorgan Chase' : 'JPM',
'HSBC' : 'HSBC',
'CCB' : '0939.HK',
'ExxonMobil' : 'XOM',
'Shell' : 'RDSA.AS',
'PetroChina' : 'PTR',
'Tesla' : 'TSLA',
'Volkswagen' : 'VOW3.DE',
'Toyota' : '7203.T',
'Coca Cola' : 'KO',
'AB InBev' : 'ABI.BR',
'Kirin' : '2503.T'
}
# Dictionary listing the classical trading strategies supported
strategies = {
'Buy and Hold' : 'BuyAndHold',
'Sell and Hold' : 'SellAndHold',
'Trend Following Moving Averages' : 'MovingAveragesTF',
'Mean Reversion Moving Averages' : 'MovingAveragesMR'
}
# Dictionary listing the AI trading strategies supported
strategiesAI = {
'TDQN' : 'TDQN'
}
###############################################################################
########################### Class TradingSimulator ############################
###############################################################################
class TradingSimulator:
"""
GOAL: Accurately simulating multiple trading strategies on different stocks
to analyze and compare their performance.
VARIABLES: /
METHODS: - displayTestbench: Display consecutively all the stocks
included in the testbench.
- analyseTimeSeries: Perform a detailled analysis of the stock
market price time series.
- plotEntireTrading: Plot the entire trading activity, with both
the training and testing phases rendered on
the same graph.
- simulateNewStrategy: Simulate a new trading strategy on a
a certain stock of the testbench.
- simulateExistingStrategy: Simulate an already existing
trading strategy on a certain
stock of the testbench.
- evaluateStrategy: Evaluate a trading strategy on the
entire testbench.
- evaluateStock: Compare different trading strategies
on a certain stock of the testbench.
"""
def displayTestbench(self, startingDate=startingDate, endingDate=endingDate):
"""
GOAL: Display consecutively all the stocks included in the
testbench (trading indices and companies).
INPUTS: - startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
OUTPUTS: /
"""
# Display the stocks included in the testbench (trading indices)
for _, stock in indices.items():
env = TradingEnv(stock, startingDate, endingDate, 0)
env.render()
# Display the stocks included in the testbench (companies)
for _, stock in companies.items():
env = TradingEnv(stock, startingDate, endingDate, 0)
env.render()
def analyseTimeSeries(self, stockName, startingDate=startingDate, endingDate=endingDate, splitingDate=splitingDate):
"""
GOAL: Perform a detailled analysis of the stock market
price time series.
INPUTS: - stockName: Name of the stock (in the testbench).
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- splitingDate: Spliting date between the training dataset
and the testing dataset.
OUTPUTS: /
"""
# Retrieve the trading stock information
if(stockName in fictives):
stock = fictives[stockName]
elif(stockName in indices):
stock = indices[stockName]
elif(stockName in companies):
stock = companies[stockName]
# Error message if the stock specified is not valid or not supported
else:
print("The stock specified is not valid, only the following stocks are supported:")
for stock in fictives:
print("".join(['- ', stock]))
for stock in indices:
print("".join(['- ', stock]))
for stock in companies:
print("".join(['- ', stock]))
raise SystemError("Please check the stock specified.")
# TRAINING DATA
print("\n\n\nAnalysis of the TRAINING phase time series")
print("------------------------------------------\n")
trainingEnv = TradingEnv(stock, startingDate, splitingDate, 0)
timeSeries = trainingEnv.data['Close']
analyser = TimeSeriesAnalyser(timeSeries)
analyser.timeSeriesDecomposition()
analyser.stationarityAnalysis()
analyser.cyclicityAnalysis()
# TESTING DATA
print("\n\n\nAnalysis of the TESTING phase time series")
print("------------------------------------------\n")
testingEnv = TradingEnv(stock, splitingDate, endingDate, 0)
timeSeries = testingEnv.data['Close']
analyser = TimeSeriesAnalyser(timeSeries)
analyser.timeSeriesDecomposition()
analyser.stationarityAnalysis()
analyser.cyclicityAnalysis()
# ENTIRE TRADING DATA
print("\n\n\nAnalysis of the entire time series (both training and testing phases)")
print("---------------------------------------------------------------------\n")
tradingEnv = TradingEnv(stock, startingDate, endingDate, 0)
timeSeries = tradingEnv.data['Close']
analyser = TimeSeriesAnalyser(timeSeries)
analyser.timeSeriesDecomposition()
analyser.stationarityAnalysis()
analyser.cyclicityAnalysis()
def plotEntireTrading(self, trainingEnv, testingEnv):
"""
GOAL: Plot the entire trading activity, with both the training
and testing phases rendered on the same graph for
comparison purposes.
INPUTS: - trainingEnv: Trading environment for training.
- testingEnv: Trading environment for testing.
OUTPUTS: /
"""
# Artificial trick to assert the continuity of the Money curve
ratio = trainingEnv.data['Money'][-1]/testingEnv.data['Money'][0]
testingEnv.data['Money'] = ratio * testingEnv.data['Money']
# Concatenation of the training and testing trading dataframes
dataframes = [trainingEnv.data, testingEnv.data]
data = pd.concat(dataframes)
# Set the Matplotlib figure and subplots
fig = plt.figure(figsize=(10, 8))
ax1 = fig.add_subplot(211, ylabel='Price', xlabel='Time')
ax2 = fig.add_subplot(212, ylabel='Capital', xlabel='Time', sharex=ax1)
# Plot the first graph -> Evolution of the stock market price
trainingEnv.data['Close'].plot(ax=ax1, color='blue', lw=2)
testingEnv.data['Close'].plot(ax=ax1, color='blue', lw=2, label='_nolegend_')
ax1.plot(data.loc[data['Action'] == 1.0].index,
data['Close'][data['Action'] == 1.0],
'^', markersize=5, color='green')
ax1.plot(data.loc[data['Action'] == -1.0].index,
data['Close'][data['Action'] == -1.0],
'v', markersize=5, color='red')
# Plot the second graph -> Evolution of the trading capital
trainingEnv.data['Money'].plot(ax=ax2, color='blue', lw=2)
testingEnv.data['Money'].plot(ax=ax2, color='blue', lw=2, label='_nolegend_')
ax2.plot(data.loc[data['Action'] == 1.0].index,
data['Money'][data['Action'] == 1.0],
'^', markersize=5, color='green')
ax2.plot(data.loc[data['Action'] == -1.0].index,
data['Money'][data['Action'] == -1.0],
'v', markersize=5, color='red')
# Plot the vertical line seperating the training and testing datasets
ax1.axvline(pd.Timestamp(splitingDate), color='black', linewidth=2.0)
ax2.axvline(pd.Timestamp(splitingDate), color='black', linewidth=2.0)
# Generation of the two legends and plotting
ax1.legend(["Price", "Long", "Short", "Train/Test separation"])
ax2.legend(["Capital", "Long", "Short", "Train/Test separation"])
plt.savefig(''.join(['Figures/', str(trainingEnv.marketSymbol), '_TrainingTestingRendering', '.png']))
#plt.show()
def simulateNewStrategy(self, strategyName, stockName,
startingDate=startingDate, endingDate=endingDate, splitingDate=splitingDate,
observationSpace=observationSpace, actionSpace=actionSpace,
money=money, stateLength=stateLength, transactionCosts=transactionCosts,
bounds=bounds, step=step, numberOfEpisodes=numberOfEpisodes,
verbose=True, plotTraining=True, rendering=True, showPerformance=True,
saveStrategy=False):
"""
GOAL: Simulate a new trading strategy on a certain stock included in the
testbench, with both learning and testing phases.
INPUTS: - strategyName: Name of the trading strategy.
- stockName: Name of the stock (in the testbench).
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- splitingDate: Spliting date between the training dataset
and the testing dataset.
- observationSpace: Size of the RL observation space.
- actionSpace: Size of the RL action space.
- money: Initial capital at the disposal of the agent.
- stateLength: Length of the trading agent state.
- transactionCosts: Additional costs incurred while trading
(e.g. 0.01 <=> 1% of transaction costs).
- bounds: Bounds of the parameter search space (training).
- step: Step of the parameter search space (training).
- numberOfEpisodes: Number of epsiodes of the RL training phase.
- verbose: Enable the printing of a simulation feedback.
- plotTraining: Enable the plotting of the training results.
- rendering: Enable the rendering of the trading environment.
- showPerformance: Enable the printing of a table summarizing
the trading strategy performance.
- saveStrategy: Enable the saving of the trading strategy.
OUTPUTS: - tradingStrategy: Trading strategy simulated.
- trainingEnv: Trading environment related to the training phase.
- testingEnv: Trading environment related to the testing phase.
"""
# 1. INITIALIZATION PHASE
# Retrieve the trading strategy information
if(strategyName in strategies):
strategy = strategies[strategyName]
trainingParameters = [bounds, step]
ai = False
elif(strategyName in strategiesAI):
strategy = strategiesAI[strategyName]
trainingParameters = [numberOfEpisodes]
ai = True
# Error message if the strategy specified is not valid or not supported
else:
print("The strategy specified is not valid, only the following strategies are supported:")
for strategy in strategies:
print("".join(['- ', strategy]))
for strategy in strategiesAI:
print("".join(['- ', strategy]))
raise SystemError("Please check the trading strategy specified.")
# Retrieve the trading stock information
if(stockName in fictives):
stock = fictives[stockName]
elif(stockName in indices):
stock = indices[stockName]
elif(stockName in companies):
stock = companies[stockName]
# Error message if the stock specified is not valid or not supported
else:
print("The stock specified is not valid, only the following stocks are supported:")
for stock in fictives:
print("".join(['- ', stock]))
for stock in indices:
print("".join(['- ', stock]))
for stock in companies:
print("".join(['- ', stock]))
raise SystemError("Please check the stock specified.")
# 2. TRAINING PHASE
# Initialize the trading environment associated with the training phase
trainingEnv = TradingEnv(stock, startingDate, splitingDate, money, stateLength, transactionCosts)
# Instanciate the strategy classes
if ai:
strategyModule = importlib.import_module(str(strategy))
className = getattr(strategyModule, strategy)
tradingStrategy = className(observationSpace, actionSpace)
else:
strategyModule = importlib.import_module('classicalStrategy')
className = getattr(strategyModule, strategy)
tradingStrategy = className()
# Training of the trading strategy
trainingEnv = tradingStrategy.training(trainingEnv, trainingParameters=trainingParameters,
verbose=verbose, rendering=rendering,
plotTraining=plotTraining, showPerformance=showPerformance)
# 3. TESTING PHASE
# Initialize the trading environment associated with the testing phase
testingEnv = TradingEnv(stock, splitingDate, endingDate, money, stateLength, transactionCosts)
# Testing of the trading strategy
testingEnv = tradingStrategy.testing(trainingEnv, testingEnv, rendering=rendering, showPerformance=showPerformance)
# Show the entire unified rendering of the training and testing phases
if rendering:
self.plotEntireTrading(trainingEnv, testingEnv)
# 4. TERMINATION PHASE
# If required, save the trading strategy with Pickle
if(saveStrategy):
fileName = "".join(["Strategies/", strategy, "_", stock, "_", startingDate, "_", splitingDate])
if ai:
tradingStrategy.saveModel(fileName)
else:
fileHandler = open(fileName, 'wb')
pickle.dump(tradingStrategy, fileHandler)
# Return of the trading strategy simulated and of the trading environments backtested
return tradingStrategy, trainingEnv, testingEnv
def simulateExistingStrategy(self, strategyName, stockName,
startingDate=startingDate, endingDate=endingDate, splitingDate=splitingDate,
observationSpace=observationSpace, actionSpace=actionSpace,
money=money, stateLength=stateLength, transactionCosts=transactionCosts,
rendering=True, showPerformance=True):
"""
GOAL: Simulate an already existing trading strategy on a certain
stock of the testbench, the strategy being loaded from the
strategy dataset. There is no training phase, only a testing
phase.
INPUTS: - strategyName: Name of the trading strategy.
- stockName: Name of the stock (in the testbench).
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- splitingDate: Spliting date between the training dataset
and the testing dataset.
- observationSpace: Size of the RL observation space.
- actionSpace: Size of the RL action space.
- money: Initial capital at the disposal of the agent.
- stateLength: Length of the trading agent state.
- transactionCosts: Additional costs incurred while trading
(e.g. 0.01 <=> 1% of transaction costs).
- rendering: Enable the rendering of the trading environment.
- showPerformance: Enable the printing of a table summarizing
the trading strategy performance.
OUTPUTS: - tradingStrategy: Trading strategy simulated.
- trainingEnv: Trading environment related to the training phase.
- testingEnv: Trading environment related to the testing phase.
"""
# 1. INITIALIZATION PHASE
# Retrieve the trading strategy information
if(strategyName in strategies):
strategy = strategies[strategyName]
ai = False
elif(strategyName in strategiesAI):
strategy = strategiesAI[strategyName]
ai = True
# Error message if the strategy specified is not valid or not supported
else:
print("The strategy specified is not valid, only the following strategies are supported:")
for strategy in strategies:
print("".join(['- ', strategy]))
for strategy in strategiesAI:
print("".join(['- ', strategy]))
raise SystemError("Please check the trading strategy specified.")
# Retrieve the trading stock information
if(stockName in fictives):
stock = fictives[stockName]
elif(stockName in indices):
stock = indices[stockName]
elif(stockName in companies):
stock = companies[stockName]
# Error message if the stock specified is not valid or not supported
else:
print("The stock specified is not valid, only the following stocks are supported:")
for stock in fictives:
print("".join(['- ', stock]))
for stock in indices:
print("".join(['- ', stock]))
for stock in companies:
print("".join(['- ', stock]))
raise SystemError("Please check the stock specified.")
# 2. LOADING PHASE
# Check that the strategy to load exists in the strategy dataset
fileName = "".join(["Strategies/", strategy, "_", stock, "_", startingDate, "_", splitingDate])
exists = os.path.isfile(fileName)
# If affirmative, load the trading strategy
if exists:
if ai:
strategyModule = importlib.import_module(strategy)
className = getattr(strategyModule, strategy)
tradingStrategy = className(observationSpace, actionSpace)
tradingStrategy.loadModel(fileName)
else:
fileHandler = open(fileName, 'rb')
tradingStrategy = pickle.load(fileHandler)
else:
raise SystemError("The trading strategy specified does not exist, please provide a valid one.")
# 3. TESTING PHASE
# Initialize the trading environments associated with the testing phase
trainingEnv = TradingEnv(stock, startingDate, splitingDate, money, stateLength, transactionCosts)
testingEnv = TradingEnv(stock, splitingDate, endingDate, money, stateLength, transactionCosts)
# Testing of the trading strategy
trainingEnv = tradingStrategy.testing(trainingEnv, trainingEnv, rendering=rendering, showPerformance=showPerformance)
testingEnv = tradingStrategy.testing(trainingEnv, testingEnv, rendering=rendering, showPerformance=showPerformance)
# Show the entire unified rendering of the training and testing phases
if rendering:
self.plotEntireTrading(trainingEnv, testingEnv)
return tradingStrategy, trainingEnv, testingEnv
def evaluateStrategy(self, strategyName,
startingDate=startingDate, endingDate=endingDate, splitingDate=splitingDate,
observationSpace=observationSpace, actionSpace=actionSpace,
money=money, stateLength=stateLength, transactionCosts=transactionCosts,
bounds=bounds, step=step, numberOfEpisodes=numberOfEpisodes,
verbose=False, plotTraining=False, rendering=False, showPerformance=False,
saveStrategy=False):
"""
GOAL: Evaluate the performance of a trading strategy on the entire
testbench of stocks designed.
INPUTS: - strategyName: Name of the trading strategy.
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- splitingDate: Spliting date between the training dataset
and the testing dataset.
- observationSpace: Size of the RL observation space.
- actionSpace: Size of the RL action space.
- money: Initial capital at the disposal of the agent.
- stateLength: Length of the trading agent state.
- transactionCosts: Additional costs incurred while trading
(e.g. 0.01 <=> 1% of transaction costs).
- bounds: Bounds of the parameter search space (training).
- step: Step of the parameter search space (training).
- numberOfEpisodes: Number of epsiodes of the RL training phase.
- verbose: Enable the printing of simulation feedbacks.
- plotTraining: Enable the plotting of the training results.
- rendering: Enable the rendering of the trading environment.
- showPerformance: Enable the printing of a table summarizing
the trading strategy performance.
- saveStrategy: Enable the saving of the trading strategy.
OUTPUTS: - performanceTable: Table summarizing the performance of
a trading strategy.
"""
# Initialization of some variables
performanceTable = [["Profit & Loss (P&L)"], ["Annualized Return"], ["Annualized Volatility"], ["Sharpe Ratio"], ["Sortino Ratio"], ["Maximum DrawDown"], ["Maximum DrawDown Duration"], ["Profitability"], ["Ratio Average Profit/Loss"], ["Skewness"]]
headers = ["Performance Indicator"]
# Loop through each stock included in the testbench (progress bar)
print("Trading strategy evaluation progression:")
#for stock in tqdm(itertools.chain(indices, companies)):
for stock in tqdm(stocks):
# Simulation of the trading strategy on the current stock
try:
# Simulate an already existing trading strategy on the current stock
_, _, testingEnv = self.simulateExistingStrategy(strategyName, stock, startingDate, endingDate, splitingDate, observationSpace, actionSpace, money, stateLength, transactionCosts, rendering, showPerformance)
except SystemError:
# Simulate a new trading strategy on the current stock
_, _, testingEnv = self.simulateNewStrategy(strategyName, stock, startingDate, endingDate, splitingDate, observationSpace, actionSpace, money, stateLength, transactionCosts, bounds, step, numberOfEpisodes, verbose, plotTraining, rendering, showPerformance, saveStrategy)
# Retrieve the trading performance associated with the trading strategy
analyser = PerformanceEstimator(testingEnv.data)
performance = analyser.computePerformance()
# Get the required format for the display of the performance table
headers.append(stock)
for i in range(len(performanceTable)):
performanceTable[i].append(performance[i][1])
# Display the performance table computed
tabulation = tabulate(performanceTable, headers, tablefmt="fancy_grid", stralign="center")
print(tabulation)
# Computation of the average Sharpe Ratio (default performance indicator)
sharpeRatio = np.mean([float(item) for item in performanceTable[3][1:]])
print("Average Sharpe Ratio: " + "{0:.3f}".format(sharpeRatio))
return performanceTable
def evaluateStock(self, stockName,
startingDate=startingDate, endingDate=endingDate, splitingDate=splitingDate,
observationSpace=observationSpace, actionSpace=actionSpace,
money=money, stateLength=stateLength, transactionCosts=transactionCosts,
bounds=bounds, step=step, numberOfEpisodes=numberOfEpisodes,
verbose=False, plotTraining=False, rendering=False, showPerformance=False,
saveStrategy=False):
"""
GOAL: Simulate and compare the performance achieved by all the supported
trading strategies on a certain stock of the testbench.
INPUTS: - stockName: Name of the stock (in the testbench).
- startingDate: Beginning of the trading horizon.
- endingDate: Ending of the trading horizon.
- splitingDate: Spliting date between the training dataset
and the testing dataset.
- money: Initial capital at the disposal of the agent.
- stateLength: Length of the trading agent state.
- transactionCosts: Additional costs incurred while trading
(e.g. 0.01 <=> 1% of transaction costs).
- bounds: Bounds of the parameter search space (training).
- step: Step of the parameter search space (training).
- numberOfEpisodes: Number of epsiodes of the RL training phase.
- verbose: Enable the printing of a simulation feedback.
- plotTraining: Enable the plotting of the training results.
- rendering: Enable the rendering of the trading environment.
- showPerformance: Enable the printing of a table summarizing
the trading strategy performance.
- saveStrategy: Enable the saving of the trading strategy.
OUTPUTS: - performanceTable: Table summarizing the performance of
a trading strategy.
"""
# Initialization of some variables
performanceTable = [["Profit & Loss (P&L)"], ["Annualized Return"], ["Annualized Volatility"], ["Sharpe Ratio"], ["Sortino Ratio"], ["Maximum DrawDown"], ["Maximum DrawDown Duration"], ["Profitability"], ["Ratio Average Profit/Loss"], ["Skewness"]]
headers = ["Performance Indicator"]
# Loop through all the trading strategies supported (progress bar)
print("Trading strategies evaluation progression:")
for strategy in tqdm(itertools.chain(strategies, strategiesAI)):
# Simulation of the current trading strategy on the stock
try:
# Simulate an already existing trading strategy on the stock
_, _, testingEnv = self.simulateExistingStrategy(strategy, stockName, startingDate, endingDate, splitingDate, observationSpace, actionSpace, money, stateLength, transactionCosts, rendering, showPerformance)
except SystemError:
# Simulate a new trading strategy on the stock
_, _, testingEnv = self.simulateNewStrategy(strategy, stockName, startingDate, endingDate, splitingDate, observationSpace, actionSpace, money, stateLength, transactionCosts, bounds, step, numberOfEpisodes, verbose, plotTraining, rendering, showPerformance, saveStrategy)
# Retrieve the trading performance associated with the trading strategy
analyser = PerformanceEstimator(testingEnv.data)
performance = analyser.computePerformance()
# Get the required format for the display of the performance table
headers.append(strategy)
for i in range(len(performanceTable)):
performanceTable[i].append(performance[i][1])
# Display the performance table
tabulation = tabulate(performanceTable, headers, tablefmt="fancy_grid", stralign="center")
print(tabulation)
return performanceTable