-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradingBot.ipynb.txt
2943 lines (2943 loc) · 679 KB
/
tradingBot.ipynb.txt
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Lb9q2_QZgdNk"
},
"source": [
"<a href=\"https://colab.research.google.com/github/AI4Finance-Foundation/FinRL-Tutorials/blob/master/2-Advance/FinRL_Ensemble_StockTrading_ICAIF_2020.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gXaoZs2lh1hi"
},
"source": [
"# Deep Reinforcement Learning for Stock Trading from Scratch: Multiple Stock Trading Using Ensemble Strategy\n",
"\n",
"Tutorials to use OpenAI DRL to trade multiple stocks using ensemble strategy in one Jupyter Notebook | Presented at ICAIF 2020\n",
"\n",
"* This notebook is the reimplementation of our paper: Deep Reinforcement Learning for Automated Stock Trading: An Ensemble Strategy, using FinRL.\n",
"* Check out medium blog for detailed explanations: https://medium.com/@ai4finance/deep-reinforcement-learning-for-automated-stock-trading-f1dad0126a02\n",
"* Please report any issues to our Github: https://github.com/AI4Finance-LLC/FinRL-Library/issues\n",
"* **Pytorch Version** \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lGunVt8oLCVS"
},
"source": [
"# Content"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HOzAKQ-SLGX6"
},
"source": [
"* [1. Problem Definition](#0)\n",
"* [2. Getting Started - Load Python packages](#1)\n",
" * [2.1. Install Packages](#1.1) \n",
" * [2.2. Check Additional Packages](#1.2)\n",
" * [2.3. Import Packages](#1.3)\n",
" * [2.4. Create Folders](#1.4)\n",
"* [3. Download Data](#2)\n",
"* [4. Preprocess Data](#3) \n",
" * [4.1. Technical Indicators](#3.1)\n",
" * [4.2. Perform Feature Engineering](#3.2)\n",
"* [5.Build Environment](#4) \n",
" * [5.1. Training & Trade Data Split](#4.1)\n",
" * [5.2. User-defined Environment](#4.2) \n",
" * [5.3. Initialize Environment](#4.3) \n",
"* [6.Implement DRL Algorithms](#5) \n",
"* [7.Backtesting Performance](#6) \n",
" * [7.1. BackTestStats](#6.1)\n",
" * [7.2. BackTestPlot](#6.2) \n",
" * [7.3. Baseline Stats](#6.3) \n",
" * [7.3. Compare to Stock Market Index](#6.4) "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sApkDlD9LIZv"
},
"source": [
"<a id='0'></a>\n",
"# Part 1. Problem Definition"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HjLD2TZSLKZ-"
},
"source": [
"This problem is to design an automated trading solution for single stock trading. We model the stock trading process as a Markov Decision Process (MDP). We then formulate our trading goal as a maximization problem.\n",
"\n",
"The algorithm is trained using Deep Reinforcement Learning (DRL) algorithms and the components of the reinforcement learning environment are:\n",
"\n",
"\n",
"* Action: The action space describes the allowed actions that the agent interacts with the\n",
"environment. Normally, a ∈ A includes three actions: a ∈ {−1, 0, 1}, where −1, 0, 1 represent\n",
"selling, holding, and buying one stock. Also, an action can be carried upon multiple shares. We use\n",
"an action space {−k, ..., −1, 0, 1, ..., k}, where k denotes the number of shares. For example, \"Buy\n",
"10 shares of AAPL\" or \"Sell 10 shares of AAPL\" are 10 or −10, respectively\n",
"\n",
"* Reward function: r(s, a, s′) is the incentive mechanism for an agent to learn a better action. The change of the portfolio value when action a is taken at state s and arriving at new state s', i.e., r(s, a, s′) = v′ − v, where v′ and v represent the portfolio\n",
"values at state s′ and s, respectively\n",
"\n",
"* State: The state space describes the observations that the agent receives from the environment. Just as a human trader needs to analyze various information before executing a trade, so\n",
"our trading agent observes many different features to better learn in an interactive environment.\n",
"\n",
"* Environment: Dow 30 consituents\n",
"\n",
"\n",
"The data of the single stock that we will be using for this case study is obtained from Yahoo Finance API. The data contains Open-High-Low-Close price and volume.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ffsre789LY08"
},
"source": [
"<a id='1'></a>\n",
"# Part 2. Getting Started- Load Python Packages"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Uy5_PTmOh1hj"
},
"source": [
"<a id='1.1'></a>\n",
"## 2.1. Install all the packages through FinRL library\n"
]
},
{
"cell_type": "code",
"source": [
"!pip install setuptools==64.0.2\n",
"!apt-get install swig\n",
"!pip install wrds\n",
"!pip install git+https://github.com/AI4Finance-LLC/FinRL-Library.git"
],
"metadata": {
"id": "SOJEXYKl_wyu",
"outputId": "ec8efb9e-7720-41f2-a551-d265996aa907",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Requirement already satisfied: setuptools==64.0.2 in /usr/local/lib/python3.8/dist-packages (64.0.2)\n",
"Reading package lists... Done\n",
"Building dependency tree \n",
"Reading state information... Done\n",
"swig is already the newest version (3.0.12-1).\n",
"The following package was automatically installed and is no longer required:\n",
" libnvidia-common-460\n",
"Use 'apt autoremove' to remove it.\n",
"0 upgraded, 0 newly installed, 0 to remove and 21 not upgraded.\n",
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Requirement already satisfied: wrds in /usr/local/lib/python3.8/dist-packages (3.1.2)\n",
"Requirement already satisfied: sqlalchemy in /usr/local/lib/python3.8/dist-packages (from wrds) (1.4.46)\n",
"Requirement already satisfied: pandas in /usr/local/lib/python3.8/dist-packages (from wrds) (1.3.5)\n",
"Requirement already satisfied: mock in /usr/local/lib/python3.8/dist-packages (from wrds) (5.0.1)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from wrds) (1.21.6)\n",
"Requirement already satisfied: psycopg2-binary in /usr/local/lib/python3.8/dist-packages (from wrds) (2.9.5)\n",
"Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.8/dist-packages (from pandas->wrds) (2022.7)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.8/dist-packages (from pandas->wrds) (2.8.2)\n",
"Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.8/dist-packages (from sqlalchemy->wrds) (2.0.1)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil>=2.7.3->pandas->wrds) (1.15.0)\n",
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Collecting git+https://github.com/AI4Finance-LLC/FinRL-Library.git\n",
" Cloning https://github.com/AI4Finance-LLC/FinRL-Library.git to /tmp/pip-req-build-61v64vhl\n",
" Running command git clone --filter=blob:none --quiet https://github.com/AI4Finance-LLC/FinRL-Library.git /tmp/pip-req-build-61v64vhl\n",
" Resolved https://github.com/AI4Finance-LLC/FinRL-Library.git to commit 7a3ff8136bf98a30a588ed587297476929f20cd9\n",
" Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
" Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
" Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
"Collecting elegantrl@ git+https://github.com/AI4Finance-Foundation/ElegantRL.git#egg=elegantrl\n",
" Cloning https://github.com/AI4Finance-Foundation/ElegantRL.git to /tmp/pip-install-ex_o5ddw/elegantrl_eb5dd5dc326a403db11c9d90e7058b2a\n",
" Running command git clone --filter=blob:none --quiet https://github.com/AI4Finance-Foundation/ElegantRL.git /tmp/pip-install-ex_o5ddw/elegantrl_eb5dd5dc326a403db11c9d90e7058b2a\n",
" Resolved https://github.com/AI4Finance-Foundation/ElegantRL.git to commit 3a8520bee4e86b9f8683046ae7447b795e0d0849\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"Collecting pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2\n",
" Cloning https://github.com/quantopian/pyfolio.git to /tmp/pip-install-ex_o5ddw/pyfolio_c7d1b27160874fe78034d8a190c0a9ea\n",
" Running command git clone --filter=blob:none --quiet https://github.com/quantopian/pyfolio.git /tmp/pip-install-ex_o5ddw/pyfolio_c7d1b27160874fe78034d8a190c0a9ea\n",
" Resolved https://github.com/quantopian/pyfolio.git to commit 4b901f6d73aa02ceb6d04b7d83502e5c6f2e81aa\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"Requirement already satisfied: jqdatasdk in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.8.11)\n",
"Requirement already satisfied: gym>=0.17 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (0.21.0)\n",
"Requirement already satisfied: tensorboardX in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (2.5.1)\n",
"Requirement already satisfied: scikit-learn>=0.21.0 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.0.2)\n",
"Requirement already satisfied: exchange_calendars==3.6.3 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (3.6.3)\n",
"Requirement already satisfied: gputil in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.4.0)\n",
"Requirement already satisfied: ccxt>=1.66.32 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (2.6.1)\n",
"Requirement already satisfied: importlib-metadata==4.13.0 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (4.13.0)\n",
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (3.2.2)\n",
"Requirement already satisfied: stockstats>=0.4.0 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (0.5.1)\n",
"Requirement already satisfied: pandas>=1.1.5 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.3.5)\n",
"Requirement already satisfied: stable-baselines3<2.0.0,>=1.6.2 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.7.0)\n",
"Requirement already satisfied: numpy>=1.17.3 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.21.6)\n",
"Requirement already satisfied: ray[default,tune]==1.3.0 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (1.3.0)\n",
"Requirement already satisfied: alpaca_trade_api>=2.1.0 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (2.3.0)\n",
"Requirement already satisfied: yfinance in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (0.2.3)\n",
"Requirement already satisfied: lz4 in /usr/local/lib/python3.8/dist-packages (from finrl==0.3.5) (4.3.2)\n",
"Requirement already satisfied: korean-lunar-calendar in /usr/local/lib/python3.8/dist-packages (from exchange_calendars==3.6.3->finrl==0.3.5) (0.3.1)\n",
"Requirement already satisfied: toolz in /usr/local/lib/python3.8/dist-packages (from exchange_calendars==3.6.3->finrl==0.3.5) (0.12.0)\n",
"Requirement already satisfied: pyluach in /usr/local/lib/python3.8/dist-packages (from exchange_calendars==3.6.3->finrl==0.3.5) (2.0.2)\n",
"Requirement already satisfied: python-dateutil in /usr/local/lib/python3.8/dist-packages (from exchange_calendars==3.6.3->finrl==0.3.5) (2.8.2)\n",
"Requirement already satisfied: pytz in /usr/local/lib/python3.8/dist-packages (from exchange_calendars==3.6.3->finrl==0.3.5) (2022.7)\n",
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.8/dist-packages (from importlib-metadata==4.13.0->finrl==0.3.5) (3.11.0)\n",
"Requirement already satisfied: redis>=3.5.0 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (4.4.2)\n",
"Requirement already satisfied: colorama in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.4.6)\n",
"Requirement already satisfied: prometheus-client>=0.7.1 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.15.0)\n",
"Requirement already satisfied: msgpack<2.0.0,>=1.0.0 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (1.0.3)\n",
"Requirement already satisfied: opencensus in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.11.0)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (3.9.0)\n",
"Requirement already satisfied: jsonschema in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (4.3.3)\n",
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (3.8.1)\n",
"Requirement already satisfied: aiohttp-cors in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.7.0)\n",
"Requirement already satisfied: aioredis in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (2.0.1)\n",
"Requirement already satisfied: grpcio>=1.28.1 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (1.51.1)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (2.28.2)\n",
"Requirement already satisfied: pyyaml in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (6.0)\n",
"Requirement already satisfied: protobuf>=3.15.3 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (3.19.6)\n",
"Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (7.1.2)\n",
"Requirement already satisfied: gpustat in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (1.0.0)\n",
"Requirement already satisfied: py-spy>=0.2.0 in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.3.14)\n",
"Requirement already satisfied: tabulate in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.8.10)\n",
"Requirement already satisfied: colorful in /usr/local/lib/python3.8/dist-packages (from ray[default,tune]==1.3.0->finrl==0.3.5) (0.5.5)\n",
"Requirement already satisfied: websocket-client<2,>=0.56.0 in /usr/local/lib/python3.8/dist-packages (from alpaca_trade_api>=2.1.0->finrl==0.3.5) (1.4.2)\n",
"Requirement already satisfied: deprecation==2.1.0 in /usr/local/lib/python3.8/dist-packages (from alpaca_trade_api>=2.1.0->finrl==0.3.5) (2.1.0)\n",
"Requirement already satisfied: urllib3<2,>1.24 in /usr/local/lib/python3.8/dist-packages (from alpaca_trade_api>=2.1.0->finrl==0.3.5) (1.24.3)\n",
"Requirement already satisfied: websockets<11,>=9.0 in /usr/local/lib/python3.8/dist-packages (from alpaca_trade_api>=2.1.0->finrl==0.3.5) (10.4)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (6.0.4)\n",
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (22.2.0)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (1.8.2)\n",
"Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (2.1.1)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (1.3.1)\n",
"Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (4.0.2)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.8/dist-packages (from aiohttp->ray[default,tune]==1.3.0->finrl==0.3.5) (1.3.3)\n",
"Requirement already satisfied: packaging in /usr/local/lib/python3.8/dist-packages (from deprecation==2.1.0->alpaca_trade_api>=2.1.0->finrl==0.3.5) (21.3)\n",
"Requirement already satisfied: cryptography>=2.6.1 in /usr/local/lib/python3.8/dist-packages (from ccxt>=1.66.32->finrl==0.3.5) (39.0.0)\n",
"Requirement already satisfied: aiodns>=1.1.1 in /usr/local/lib/python3.8/dist-packages (from ccxt>=1.66.32->finrl==0.3.5) (3.0.0)\n",
"Requirement already satisfied: setuptools>=60.9.0 in /usr/local/lib/python3.8/dist-packages (from ccxt>=1.66.32->finrl==0.3.5) (64.0.2)\n",
"Requirement already satisfied: certifi>=2018.1.18 in /usr/local/lib/python3.8/dist-packages (from ccxt>=1.66.32->finrl==0.3.5) (2022.12.7)\n",
"Requirement already satisfied: cloudpickle>=1.2.0 in /usr/local/lib/python3.8/dist-packages (from gym>=0.17->finrl==0.3.5) (2.2.0)\n",
"Requirement already satisfied: scipy>=1.1.0 in /usr/local/lib/python3.8/dist-packages (from scikit-learn>=0.21.0->finrl==0.3.5) (1.7.3)\n",
"Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from scikit-learn>=0.21.0->finrl==0.3.5) (3.1.0)\n",
"Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.8/dist-packages (from scikit-learn>=0.21.0->finrl==0.3.5) (1.2.0)\n",
"Requirement already satisfied: torch>=1.11 in /usr/local/lib/python3.8/dist-packages (from stable-baselines3<2.0.0,>=1.6.2->finrl==0.3.5) (1.13.0+cu116)\n",
"Requirement already satisfied: six in /usr/local/lib/python3.8/dist-packages (from jqdatasdk->finrl==0.3.5) (1.15.0)\n",
"Requirement already satisfied: thriftpy2>=0.3.9 in /usr/local/lib/python3.8/dist-packages (from jqdatasdk->finrl==0.3.5) (0.4.16)\n",
"Requirement already satisfied: SQLAlchemy>=1.2.8 in /usr/local/lib/python3.8/dist-packages (from jqdatasdk->finrl==0.3.5) (1.4.46)\n",
"Requirement already satisfied: pymysql>=0.7.6 in /usr/local/lib/python3.8/dist-packages (from jqdatasdk->finrl==0.3.5) (1.0.2)\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->finrl==0.3.5) (3.0.9)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->finrl==0.3.5) (1.4.4)\n",
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib->finrl==0.3.5) (0.11.0)\n",
"Requirement already satisfied: ipython>=3.2.3 in /usr/local/lib/python3.8/dist-packages (from pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (7.9.0)\n",
"Requirement already satisfied: seaborn>=0.7.1 in /usr/local/lib/python3.8/dist-packages (from pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.11.2)\n",
"Requirement already satisfied: empyrical>=0.5.0 in /usr/local/lib/python3.8/dist-packages (from pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.5.5)\n",
"Requirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.8/dist-packages (from yfinance->finrl==0.3.5) (1.4.4)\n",
"Requirement already satisfied: html5lib>=1.1 in /usr/local/lib/python3.8/dist-packages (from yfinance->finrl==0.3.5) (1.1)\n",
"Requirement already satisfied: lxml>=4.9.1 in /usr/local/lib/python3.8/dist-packages (from yfinance->finrl==0.3.5) (4.9.2)\n",
"Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.8/dist-packages (from yfinance->finrl==0.3.5) (0.0.11)\n",
"Requirement already satisfied: beautifulsoup4>=4.11.1 in /usr/local/lib/python3.8/dist-packages (from yfinance->finrl==0.3.5) (4.11.1)\n",
"Requirement already satisfied: frozendict>=2.3.4 in /usr/local/lib/python3.8/dist-packages (from yfinance->finrl==0.3.5) (2.3.4)\n",
"Requirement already satisfied: pycares>=4.0.0 in /usr/local/lib/python3.8/dist-packages (from aiodns>=1.1.1->ccxt>=1.66.32->finrl==0.3.5) (4.3.0)\n",
"Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.8/dist-packages (from beautifulsoup4>=4.11.1->yfinance->finrl==0.3.5) (2.3.2.post1)\n",
"Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.8/dist-packages (from cryptography>=2.6.1->ccxt>=1.66.32->finrl==0.3.5) (1.15.1)\n",
"Requirement already satisfied: pandas-datareader>=0.2 in /usr/local/lib/python3.8/dist-packages (from empyrical>=0.5.0->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.9.0)\n",
"Requirement already satisfied: webencodings in /usr/local/lib/python3.8/dist-packages (from html5lib>=1.1->yfinance->finrl==0.3.5) (0.5.1)\n",
"Requirement already satisfied: jedi>=0.10 in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.18.2)\n",
"Requirement already satisfied: pygments in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (2.6.1)\n",
"Requirement already satisfied: backcall in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.2.0)\n",
"Requirement already satisfied: pickleshare in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.7.5)\n",
"Requirement already satisfied: traitlets>=4.2 in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (5.7.1)\n",
"Requirement already satisfied: pexpect in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (4.8.0)\n",
"Requirement already satisfied: decorator in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (4.4.2)\n",
"Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (2.0.10)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.8/dist-packages (from requests->ray[default,tune]==1.3.0->finrl==0.3.5) (2.10)\n",
"Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.8/dist-packages (from SQLAlchemy>=1.2.8->jqdatasdk->finrl==0.3.5) (2.0.1)\n",
"Requirement already satisfied: ply<4.0,>=3.4 in /usr/local/lib/python3.8/dist-packages (from thriftpy2>=0.3.9->jqdatasdk->finrl==0.3.5) (3.11)\n",
"Requirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from torch>=1.11->stable-baselines3<2.0.0,>=1.6.2->finrl==0.3.5) (4.4.0)\n",
"Requirement already satisfied: blessed>=1.17.1 in /usr/local/lib/python3.8/dist-packages (from gpustat->ray[default,tune]==1.3.0->finrl==0.3.5) (1.19.1)\n",
"Requirement already satisfied: psutil>=5.6.0 in /usr/local/lib/python3.8/dist-packages (from gpustat->ray[default,tune]==1.3.0->finrl==0.3.5) (5.9.4)\n",
"Requirement already satisfied: nvidia-ml-py<=11.495.46,>=11.450.129 in /usr/local/lib/python3.8/dist-packages (from gpustat->ray[default,tune]==1.3.0->finrl==0.3.5) (11.495.46)\n",
"Requirement already satisfied: box2d-py==2.3.5 in /usr/local/lib/python3.8/dist-packages (from gym>=0.17->finrl==0.3.5) (2.3.5)\n",
"Requirement already satisfied: pyglet>=1.4.0 in /usr/local/lib/python3.8/dist-packages (from gym>=0.17->finrl==0.3.5) (2.0.3)\n",
"Requirement already satisfied: importlib-resources>=1.4.0 in /usr/local/lib/python3.8/dist-packages (from jsonschema->ray[default,tune]==1.3.0->finrl==0.3.5) (5.10.2)\n",
"Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /usr/local/lib/python3.8/dist-packages (from jsonschema->ray[default,tune]==1.3.0->finrl==0.3.5) (0.19.3)\n",
"Requirement already satisfied: google-api-core<3.0.0,>=1.0.0 in /usr/local/lib/python3.8/dist-packages (from opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (2.11.0)\n",
"Requirement already satisfied: opencensus-context>=0.1.3 in /usr/local/lib/python3.8/dist-packages (from opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (0.1.3)\n",
"Requirement already satisfied: wcwidth>=0.1.4 in /usr/local/lib/python3.8/dist-packages (from blessed>=1.17.1->gpustat->ray[default,tune]==1.3.0->finrl==0.3.5) (0.2.5)\n",
"Requirement already satisfied: pycparser in /usr/local/lib/python3.8/dist-packages (from cffi>=1.12->cryptography>=2.6.1->ccxt>=1.66.32->finrl==0.3.5) (2.21)\n",
"Requirement already satisfied: google-auth<3.0dev,>=2.14.1 in /usr/local/lib/python3.8/dist-packages (from google-api-core<3.0.0,>=1.0.0->opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (2.15.0)\n",
"Requirement already satisfied: googleapis-common-protos<2.0dev,>=1.56.2 in /usr/local/lib/python3.8/dist-packages (from google-api-core<3.0.0,>=1.0.0->opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (1.57.1)\n",
"Requirement already satisfied: parso<0.9.0,>=0.8.0 in /usr/local/lib/python3.8/dist-packages (from jedi>=0.10->ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.8.3)\n",
"Requirement already satisfied: ptyprocess>=0.5 in /usr/local/lib/python3.8/dist-packages (from pexpect->ipython>=3.2.3->pyfolio@ git+https://github.com/quantopian/pyfolio.git#egg=pyfolio-0.9.2->finrl==0.3.5) (0.7.0)\n",
"Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from google-auth<3.0dev,>=2.14.1->google-api-core<3.0.0,>=1.0.0->opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (5.2.0)\n",
"Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.8/dist-packages (from google-auth<3.0dev,>=2.14.1->google-api-core<3.0.0,>=1.0.0->opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (4.9)\n",
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.8/dist-packages (from google-auth<3.0dev,>=2.14.1->google-api-core<3.0.0,>=1.0.0->opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (0.2.8)\n",
"Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.8/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3.0dev,>=2.14.1->google-api-core<3.0.0,>=1.0.0->opencensus->ray[default,tune]==1.3.0->finrl==0.3.5) (0.4.8)\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "osBHhVysOEzi"
},
"source": [
"\n",
"<a id='1.2'></a>\n",
"## 2.2. Check if the additional packages needed are present, if not install them. \n",
"* Yahoo Finance API\n",
"* pandas\n",
"* numpy\n",
"* matplotlib\n",
"* stockstats\n",
"* OpenAI gym\n",
"* stable-baselines\n",
"* tensorflow\n",
"* pyfolio"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nGv01K8Sh1hn"
},
"source": [
"<a id='1.3'></a>\n",
"## 2.3. Import Packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "EeMK7Uentj1V"
},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "lPqeTTwoh1hn"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib\n",
"import matplotlib.pyplot as plt\n",
"# matplotlib.use('Agg')\n",
"import datetime\n",
"\n",
"%matplotlib inline\n",
"from finrl.config_tickers import DOW_30_TICKER\n",
"from finrl.meta.preprocessor.yahoodownloader import YahooDownloader\n",
"from finrl.meta.preprocessor.preprocessors import FeatureEngineer, data_split\n",
"from finrl.meta.env_stock_trading.env_stocktrading import StockTradingEnv\n",
"from finrl.agents.stablebaselines3.models import DRLAgent,DRLEnsembleAgent\n",
"from finrl.plot import backtest_stats, backtest_plot, get_daily_return, get_baseline\n",
"\n",
"from pprint import pprint\n",
"\n",
"import sys\n",
"sys.path.append(\"../FinRL-Library\")\n",
"\n",
"import itertools"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T2owTj985RW4"
},
"source": [
"<a id='1.4'></a>\n",
"## 2.4. Create Folders"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "w9A8CN5R5PuZ"
},
"outputs": [],
"source": [
"import os\n",
"from finrl.main import check_and_make_directories\n",
"from finrl.config import (\n",
" DATA_SAVE_DIR,\n",
" TRAINED_MODEL_DIR,\n",
" TENSORBOARD_LOG_DIR,\n",
" RESULTS_DIR,\n",
" INDICATORS,\n",
" TRAIN_START_DATE,\n",
" TRAIN_END_DATE,\n",
" TEST_START_DATE,\n",
" TEST_END_DATE,\n",
" TRADE_START_DATE,\n",
" TRADE_END_DATE,\n",
")\n",
"\n",
"check_and_make_directories([DATA_SAVE_DIR, TRAINED_MODEL_DIR, TENSORBOARD_LOG_DIR, RESULTS_DIR])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "A289rQWMh1hq"
},
"source": [
"<a id='2'></a>\n",
"# Part 3. Download Data\n",
"Yahoo Finance is a website that provides stock data, financial news, financial reports, etc. All the data provided by Yahoo Finance is free.\n",
"* FinRL uses a class **YahooDownloader** to fetch data from Yahoo Finance API\n",
"* Call Limit: Using the Public API (without authentication), you are limited to 2,000 requests per hour per IP (or up to a total of 48,000 requests a day).\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NPeQ7iS-LoMm"
},
"source": [
"\n",
"\n",
"-----\n",
"class YahooDownloader:\n",
" Provides methods for retrieving daily stock data from\n",
" Yahoo Finance API\n",
"\n",
" Attributes\n",
" ----------\n",
" start_date : str\n",
" start date of the data (modified from config.py)\n",
" end_date : str\n",
" end date of the data (modified from config.py)\n",
" ticker_list : list\n",
" a list of stock tickers (modified from config.py)\n",
"\n",
" Methods\n",
" -------\n",
" fetch_data()\n",
" Fetches data from yahoo API\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JzqRRTOX6aFu",
"outputId": "a927dc8b-61c2-45bc-9d59-48cf0f655b97"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['AXP', 'AMGN', 'AAPL', 'BA', 'CAT', 'CSCO', 'CVX', 'GS', 'HD', 'HON', 'IBM', 'INTC', 'JNJ', 'KO', 'JPM', 'MCD', 'MMM', 'MRK', 'MSFT', 'NKE', 'PG', 'TRV', 'UNH', 'CRM', 'VZ', 'V', 'WBA', 'WMT', 'DIS', 'DOW']\n"
]
}
],
"source": [
"print(DOW_30_TICKER)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yCKm4om-s9kE",
"outputId": "deeef56c-fbba-42b7-ec25-3264de649245"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"[*********************100%***********************] 1 of 1 completed\n",
"Shape of DataFrame: (96942, 8)\n"
]
}
],
"source": [
"TRAIN_START_DATE = '2009-04-01'\n",
"TRAIN_END_DATE = '2021-01-01'\n",
"TEST_START_DATE = '2021-01-01'\n",
"TEST_END_DATE = '2022-06-01'\n",
"\n",
"df = YahooDownloader(start_date = TRAIN_START_DATE,\n",
" end_date = TEST_END_DATE,\n",
" ticker_list = DOW_30_TICKER).fetch_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "GiRuFOTOtj1Y",
"outputId": "dea34e9f-c452-4327-bfce-878c9dc5a9ee"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" date open high low close volume tic \\\n",
"0 2009-04-01 3.717500 3.892857 3.710357 3.308904 589372000 AAPL \n",
"1 2009-04-01 48.779999 48.930000 47.099998 36.228397 10850100 AMGN \n",
"2 2009-04-01 13.340000 14.640000 13.080000 11.732111 27701800 AXP \n",
"3 2009-04-01 34.520000 35.599998 34.209999 26.850748 9288800 BA \n",
"4 2009-04-01 27.500000 29.520000 27.440001 19.820395 15308300 CAT \n",
"\n",
" day \n",
"0 2 \n",
"1 2 \n",
"2 2 \n",
"3 2 \n",
"4 2 "
],
"text/html": [
"\n",
" <div id=\"df-a9343f51-d849-4d7d-a57d-6b37d2e5fc60\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" <th>volume</th>\n",
" <th>tic</th>\n",
" <th>day</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>2009-04-01</td>\n",
" <td>3.717500</td>\n",
" <td>3.892857</td>\n",
" <td>3.710357</td>\n",
" <td>3.308904</td>\n",
" <td>589372000</td>\n",
" <td>AAPL</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2009-04-01</td>\n",
" <td>48.779999</td>\n",
" <td>48.930000</td>\n",
" <td>47.099998</td>\n",
" <td>36.228397</td>\n",
" <td>10850100</td>\n",
" <td>AMGN</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2009-04-01</td>\n",
" <td>13.340000</td>\n",
" <td>14.640000</td>\n",
" <td>13.080000</td>\n",
" <td>11.732111</td>\n",
" <td>27701800</td>\n",
" <td>AXP</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>2009-04-01</td>\n",
" <td>34.520000</td>\n",
" <td>35.599998</td>\n",
" <td>34.209999</td>\n",
" <td>26.850748</td>\n",
" <td>9288800</td>\n",
" <td>BA</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2009-04-01</td>\n",
" <td>27.500000</td>\n",
" <td>29.520000</td>\n",
" <td>27.440001</td>\n",
" <td>19.820395</td>\n",
" <td>15308300</td>\n",
" <td>CAT</td>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-a9343f51-d849-4d7d-a57d-6b37d2e5fc60')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-a9343f51-d849-4d7d-a57d-6b37d2e5fc60 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-a9343f51-d849-4d7d-a57d-6b37d2e5fc60');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 8
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "DSw4ZEzVtj1Z",
"outputId": "08340d7a-f995-446d-84c1-0054e7aa9500"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" date open high low close volume \\\n",
"96937 2022-05-31 503.619995 504.109985 495.660004 491.949829 4003100 \n",
"96938 2022-05-31 210.380005 214.350006 209.110001 211.322525 9586400 \n",
"96939 2022-05-31 51.259998 51.560001 50.849998 49.042248 25016600 \n",
"96940 2022-05-31 43.480000 44.270000 43.049999 42.811329 8192000 \n",
"96941 2022-05-31 127.459999 129.899994 127.419998 127.591217 12304100 \n",
"\n",
" tic day \n",
"96937 UNH 1 \n",
"96938 V 1 \n",
"96939 VZ 1 \n",
"96940 WBA 1 \n",
"96941 WMT 1 "
],
"text/html": [
"\n",
" <div id=\"df-b5932b4c-b233-454e-ac1a-d1475e35b408\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>date</th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" <th>volume</th>\n",
" <th>tic</th>\n",
" <th>day</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>96937</th>\n",
" <td>2022-05-31</td>\n",
" <td>503.619995</td>\n",
" <td>504.109985</td>\n",
" <td>495.660004</td>\n",
" <td>491.949829</td>\n",
" <td>4003100</td>\n",
" <td>UNH</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96938</th>\n",
" <td>2022-05-31</td>\n",
" <td>210.380005</td>\n",
" <td>214.350006</td>\n",
" <td>209.110001</td>\n",
" <td>211.322525</td>\n",
" <td>9586400</td>\n",
" <td>V</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96939</th>\n",
" <td>2022-05-31</td>\n",
" <td>51.259998</td>\n",
" <td>51.560001</td>\n",
" <td>50.849998</td>\n",
" <td>49.042248</td>\n",
" <td>25016600</td>\n",
" <td>VZ</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96940</th>\n",
" <td>2022-05-31</td>\n",
" <td>43.480000</td>\n",
" <td>44.270000</td>\n",
" <td>43.049999</td>\n",
" <td>42.811329</td>\n",
" <td>8192000</td>\n",
" <td>WBA</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>96941</th>\n",
" <td>2022-05-31</td>\n",
" <td>127.459999</td>\n",
" <td>129.899994</td>\n",
" <td>127.419998</td>\n",
" <td>127.591217</td>\n",
" <td>12304100</td>\n",
" <td>WMT</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b5932b4c-b233-454e-ac1a-d1475e35b408')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-b5932b4c-b233-454e-ac1a-d1475e35b408 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-b5932b4c-b233-454e-ac1a-d1475e35b408');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 9
}
],
"source": [
"df.tail()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CV3HrZHLh1hy",
"outputId": "0988b9fe-9ab4-4d34-881d-f29b10e65c81"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(96942, 8)"
]
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "4hYkeaPiICHS",
"outputId": "c20f2da5-3224-4e6e-96a2-7b72b8dce6d8"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" date open high low close volume tic \\\n",
"0 2009-04-01 3.717500 3.892857 3.710357 3.308904 589372000 AAPL \n",
"1 2009-04-01 48.779999 48.930000 47.099998 36.228397 10850100 AMGN \n",
"2 2009-04-01 13.340000 14.640000 13.080000 11.732111 27701800 AXP \n",
"3 2009-04-01 34.520000 35.599998 34.209999 26.850748 9288800 BA \n",
"4 2009-04-01 27.500000 29.520000 27.440001 19.820395 15308300 CAT \n",
"\n",