chore: 添加Stock-Prediction-Models项目文件
添加了Stock-Prediction-Models项目的多个文件,包括数据集、模型代码、README文档和CSS样式文件。这些文件用于股票预测模型的训练和展示,涵盖了LSTM、GRU等深度学习模型的应用。
No files matched your search
@@ -0,0 +1,3 @@
|
||||
*DS_Store
|
||||
*ipynb_checkpoints
|
||||
*__pycache__
|
||||
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,337 @@
|
||||
<p align="center">
|
||||
<a href="#readme">
|
||||
<img alt="logo" width="50%" src="output/evolution-strategy.png">
|
||||
</a>
|
||||
</p>
|
||||
<p align="center">
|
||||
<a href="https://github.com/huseinzol05/Stock-Prediction-Models/blob/master/LICENSE"><img alt="MIT License" src="https://img.shields.io/badge/License-Apache--License--2.0-yellow.svg"></a>
|
||||
<a href="#"><img src="https://img.shields.io/badge/deeplearning-30--models-success.svg"></a>
|
||||
<a href="#"><img src="https://img.shields.io/badge/agent-23--models-success.svg"></a>
|
||||
</p>
|
||||
|
||||
---
|
||||
|
||||
**Stock-Prediction-Models**, Gathers machine learning and deep learning models for Stock forecasting, included trading bots and simulations.
|
||||
|
||||
## Table of contents
|
||||
* [Models](#models)
|
||||
* [Agents](#agents)
|
||||
* [Realtime Agent](realtime-agent)
|
||||
* [Data Explorations](#data-explorations)
|
||||
* [Simulations](#simulations)
|
||||
* [Tensorflow-js](#tensorflow-js)
|
||||
* [Misc](#misc)
|
||||
* [Results](#results)
|
||||
* [Results Agent](#results-agent)
|
||||
* [Results signal prediction](#results-signal-prediction)
|
||||
* [Results analysis](#results-analysis)
|
||||
* [Results simulation](#results-simulation)
|
||||
|
||||
## Contents
|
||||
|
||||
### Models
|
||||
|
||||
#### [Deep-learning models](deep-learning)
|
||||
1. LSTM
|
||||
2. LSTM Bidirectional
|
||||
3. LSTM 2-Path
|
||||
4. GRU
|
||||
5. GRU Bidirectional
|
||||
6. GRU 2-Path
|
||||
7. Vanilla
|
||||
8. Vanilla Bidirectional
|
||||
9. Vanilla 2-Path
|
||||
10. LSTM Seq2seq
|
||||
11. LSTM Bidirectional Seq2seq
|
||||
12. LSTM Seq2seq VAE
|
||||
13. GRU Seq2seq
|
||||
14. GRU Bidirectional Seq2seq
|
||||
15. GRU Seq2seq VAE
|
||||
16. Attention-is-all-you-Need
|
||||
17. CNN-Seq2seq
|
||||
18. Dilated-CNN-Seq2seq
|
||||
|
||||
**Bonus**
|
||||
|
||||
1. How to use one of the model to forecast `t + N`, [how-to-forecast.ipynb](deep-learning/how-to-forecast.ipynb)
|
||||
2. Consensus, how to use sentiment data to forecast `t + N`, [sentiment-consensus.ipynb](deep-learning/sentiment-consensus.ipynb)
|
||||
|
||||
#### [Stacking models](stacking)
|
||||
1. Deep Feed-forward Auto-Encoder Neural Network to reduce dimension + Deep Recurrent Neural Network + ARIMA + Extreme Boosting Gradient Regressor
|
||||
2. Adaboost + Bagging + Extra Trees + Gradient Boosting + Random Forest + XGB
|
||||
|
||||
### [Agents](agent)
|
||||
|
||||
1. Turtle-trading agent
|
||||
2. Moving-average agent
|
||||
3. Signal rolling agent
|
||||
4. Policy-gradient agent
|
||||
5. Q-learning agent
|
||||
6. Evolution-strategy agent
|
||||
7. Double Q-learning agent
|
||||
8. Recurrent Q-learning agent
|
||||
9. Double Recurrent Q-learning agent
|
||||
10. Duel Q-learning agent
|
||||
11. Double Duel Q-learning agent
|
||||
12. Duel Recurrent Q-learning agent
|
||||
13. Double Duel Recurrent Q-learning agent
|
||||
14. Actor-critic agent
|
||||
15. Actor-critic Duel agent
|
||||
16. Actor-critic Recurrent agent
|
||||
17. Actor-critic Duel Recurrent agent
|
||||
18. Curiosity Q-learning agent
|
||||
19. Recurrent Curiosity Q-learning agent
|
||||
20. Duel Curiosity Q-learning agent
|
||||
21. Neuro-evolution agent
|
||||
22. Neuro-evolution with Novelty search agent
|
||||
23. ABCD strategy agent
|
||||
|
||||
### [Data Explorations](misc)
|
||||
|
||||
1. stock market study on TESLA stock, [tesla-study.ipynb](misc/tesla-study.ipynb)
|
||||
2. Outliers study using K-means, SVM, and Gaussian on TESLA stock, [outliers.ipynb](misc/outliers.ipynb)
|
||||
3. Overbought-Oversold study on TESLA stock, [overbought-oversold.ipynb](misc/overbought-oversold.ipynb)
|
||||
4. Which stock you need to buy? [which-stock.ipynb](misc/which-stock.ipynb)
|
||||
|
||||
### [Simulations](simulation)
|
||||
|
||||
1. Simple Monte Carlo, [monte-carlo-drift.ipynb](simulation/monte-carlo-drift.ipynb)
|
||||
2. Dynamic volatility Monte Carlo, [monte-carlo-dynamic-volatility.ipynb](simulation/monte-carlo-dynamic-volatility.ipynb)
|
||||
3. Drift Monte Carlo, [monte-carlo-drift.ipynb](simulation/monte-carlo-drift.ipynb)
|
||||
4. Multivariate Drift Monte Carlo BTC/USDT with Bitcurate sentiment, [multivariate-drift-monte-carlo.ipynb](simulation/multivariate-drift-monte-carlo.ipynb)
|
||||
5. Portfolio optimization, [portfolio-optimization.ipynb](simulation/portfolio-optimization.ipynb), inspired from https://pythonforfinance.net/2017/01/21/investment-portfolio-optimisation-with-python/
|
||||
|
||||
### [Tensorflow-js](stock-forecasting-js)
|
||||
|
||||
I code [LSTM Recurrent Neural Network](deep-learning/1.lstm.ipynb) and [Simple signal rolling agent](agent/simple-agent.ipynb) inside Tensorflow JS, you can try it here, [huseinhouse.com/stock-forecasting-js](https://huseinhouse.com/stock-forecasting-js/), you can download any historical CSV and upload dynamically.
|
||||
|
||||
### [Misc](misc)
|
||||
|
||||
1. fashion trending prediction with cross-validation, [fashion-forecasting.ipynb](misc/fashion-forecasting.ipynb)
|
||||
2. Bitcoin analysis with LSTM prediction, [bitcoin-analysis-lstm.ipynb](misc/bitcoin-analysis-lstm.ipynb)
|
||||
3. Kijang Emas Bank Negara, [kijang-emas-bank-negara.ipynb](misc/kijang-emas-bank-negara.ipynb)
|
||||
|
||||
## Results
|
||||
|
||||
### Results Agent
|
||||
|
||||
**This agent only able to buy or sell 1 unit per transaction.**
|
||||
|
||||
1. Turtle-trading agent, [turtle-agent.ipynb](agent/1.turtle-agent.ipynb)
|
||||
|
||||
<img src="output-agent/turtle-agent.png" width="70%" align="">
|
||||
|
||||
2. Moving-average agent, [moving-average-agent.ipynb](agent/2.moving-average-agent.ipynb)
|
||||
|
||||
<img src="output-agent/moving-average-agent.png" width="70%" align="">
|
||||
|
||||
3. Signal rolling agent, [signal-rolling-agent.ipynb](agent/3.signal-rolling-agent.ipynb)
|
||||
|
||||
<img src="output-agent/signal-rolling-agent.png" width="70%" align="">
|
||||
|
||||
4. Policy-gradient agent, [policy-gradient-agent.ipynb](agent/4.policy-gradient-agent.ipynb)
|
||||
|
||||
<img src="output-agent/policy-gradient-agent.png" width="70%" align="">
|
||||
|
||||
5. Q-learning agent, [q-learning-agent.ipynb](agent/5.q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/q-learning-agent.png" width="70%" align="">
|
||||
|
||||
6. Evolution-strategy agent, [evolution-strategy-agent.ipynb](agent/6.evolution-strategy-agent.ipynb)
|
||||
|
||||
<img src="output-agent/evolution-strategy-agent.png" width="70%" align="">
|
||||
|
||||
7. Double Q-learning agent, [double-q-learning-agent.ipynb](agent/7.double-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/double-q-learning.png" width="70%" align="">
|
||||
|
||||
8. Recurrent Q-learning agent, [recurrent-q-learning-agent.ipynb](agent/8.recurrent-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/recurrent-q-learning.png" width="70%" align="">
|
||||
|
||||
9. Double Recurrent Q-learning agent, [double-recurrent-q-learning-agent.ipynb](agent/9.double-recurrent-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/double-recurrent-q-learning.png" width="70%" align="">
|
||||
|
||||
10. Duel Q-learning agent, [duel-q-learning-agent.ipynb](agent/10.duel-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/double-q-learning.png" width="70%" align="">
|
||||
|
||||
11. Double Duel Q-learning agent, [double-duel-q-learning-agent.ipynb](agent/11.double-duel-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/double-duel-q-learning.png" width="70%" align="">
|
||||
|
||||
12. Duel Recurrent Q-learning agent, [duel-recurrent-q-learning-agent.ipynb](agent/12.duel-recurrent-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/duel-recurrent-q-learning.png" width="70%" align="">
|
||||
|
||||
13. Double Duel Recurrent Q-learning agent, [double-duel-recurrent-q-learning-agent.ipynb](agent/13.double-duel-recurrent-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/double-duel-recurrent-q-learning.png" width="70%" align="">
|
||||
|
||||
14. Actor-critic agent, [actor-critic-agent.ipynb](agent/14.actor-critic-agent.ipynb)
|
||||
|
||||
<img src="output-agent/actor-critic.png" width="70%" align="">
|
||||
|
||||
15. Actor-critic Duel agent, [actor-critic-duel-agent.ipynb](agent/14.actor-critic-duel-agent.ipynb)
|
||||
|
||||
<img src="output-agent/actor-critic-duel.png" width="70%" align="">
|
||||
|
||||
16. Actor-critic Recurrent agent, [actor-critic-recurrent-agent.ipynb](agent/16.actor-critic-recurrent-agent.ipynb)
|
||||
|
||||
<img src="output-agent/actor-critic-recurrent.png" width="70%" align="">
|
||||
|
||||
17. Actor-critic Duel Recurrent agent, [actor-critic-duel-recurrent-agent.ipynb](agent/17.actor-critic-duel-recurrent-agent.ipynb)
|
||||
|
||||
<img src="output-agent/actor-critic-duel-recurrent.png" width="70%" align="">
|
||||
|
||||
18. Curiosity Q-learning agent, [curiosity-q-learning-agent.ipynb](agent/18.curiosity-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/curiosity-q-learning.png" width="70%" align="">
|
||||
|
||||
19. Recurrent Curiosity Q-learning agent, [recurrent-curiosity-q-learning.ipynb](agent/19.recurrent-curiosity-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/recurrent-curiosity-q-learning.png" width="70%" align="">
|
||||
|
||||
20. Duel Curiosity Q-learning agent, [duel-curiosity-q-learning-agent.ipynb](agent/20.duel-curiosity-q-learning-agent.ipynb)
|
||||
|
||||
<img src="output-agent/duel-curiosity-q-learning.png" width="70%" align="">
|
||||
|
||||
21. Neuro-evolution agent, [neuro-evolution.ipynb](agent/21.neuro-evolution-agent.ipynb)
|
||||
|
||||
<img src="output-agent/neuro-evolution.png" width="70%" align="">
|
||||
|
||||
22. Neuro-evolution with Novelty search agent, [neuro-evolution-novelty-search.ipynb](agent/22.neuro-evolution-novelty-search-agent.ipynb)
|
||||
|
||||
<img src="output-agent/neuro-evolution-novelty-search.png" width="70%" align="">
|
||||
|
||||
23. ABCD strategy agent, [abcd-strategy.ipynb](agent/23.abcd-strategy-agent.ipynb)
|
||||
|
||||
<img src="output-agent/abcd-strategy.png" width="70%" align="">
|
||||
|
||||
### Results signal prediction
|
||||
|
||||
I will cut the dataset to train and test datasets,
|
||||
|
||||
1. Train dataset derived from starting timestamp until last 30 days
|
||||
2. Test dataset derived from last 30 days until end of the dataset
|
||||
|
||||
So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot.
|
||||
|
||||
1. LSTM, accuracy 95.693%, time taken for 1 epoch 01:09
|
||||
|
||||
<img src="output/lstm.png" width="70%" align="">
|
||||
|
||||
2. LSTM Bidirectional, accuracy 93.8%, time taken for 1 epoch 01:40
|
||||
|
||||
<img src="output/bidirectional-lstm.png" width="70%" align="">
|
||||
|
||||
3. LSTM 2-Path, accuracy 94.63%, time taken for 1 epoch 01:39
|
||||
|
||||
<img src="output/lstm-2path.png" width="70%" align="">
|
||||
|
||||
4. GRU, accuracy 94.63%, time taken for 1 epoch 02:10
|
||||
|
||||
<img src="output/gru.png" width="70%" align="">
|
||||
|
||||
5. GRU Bidirectional, accuracy 92.5673%, time taken for 1 epoch 01:40
|
||||
|
||||
<img src="output/bidirectional-gru.png" width="70%" align="">
|
||||
|
||||
6. GRU 2-Path, accuracy 93.2117%, time taken for 1 epoch 01:39
|
||||
|
||||
<img src="output/gru-2path.png" width="70%" align="">
|
||||
|
||||
7. Vanilla, accuracy 91.4686%, time taken for 1 epoch 00:52
|
||||
|
||||
<img src="output/vanilla.png" width="70%" align="">
|
||||
|
||||
8. Vanilla Bidirectional, accuracy 88.9927%, time taken for 1 epoch 01:06
|
||||
|
||||
<img src="output/bidirectional-vanilla.png" width="70%" align="">
|
||||
|
||||
9. Vanilla 2-Path, accuracy 91.5406%, time taken for 1 epoch 01:08
|
||||
|
||||
<img src="output/vanilla-2path.png" width="70%" align="">
|
||||
|
||||
10. LSTM Seq2seq, accuracy 94.9817%, time taken for 1 epoch 01:36
|
||||
|
||||
<img src="output/lstm-seq2seq.png" width="70%" align="">
|
||||
|
||||
11. LSTM Bidirectional Seq2seq, accuracy 94.517%, time taken for 1 epoch 02:30
|
||||
|
||||
<img src="output/bidirectional-lstm-seq2seq.png" width="70%" align="">
|
||||
|
||||
12. LSTM Seq2seq VAE, accuracy 95.4190%, time taken for 1 epoch 01:48
|
||||
|
||||
<img src="output/lstm-seq2seq-vae.png" width="70%" align="">
|
||||
|
||||
13. GRU Seq2seq, accuracy 90.8854%, time taken for 1 epoch 01:34
|
||||
|
||||
<img src="output/gru-seq2seq.png" width="70%" align="">
|
||||
|
||||
14. GRU Bidirectional Seq2seq, accuracy 67.9915%, time taken for 1 epoch 02:30
|
||||
|
||||
<img src="output/bidirectional-gru-seq2seq.png" width="70%" align="">
|
||||
|
||||
15. GRU Seq2seq VAE, accuracy 89.1321%, time taken for 1 epoch 01:48
|
||||
|
||||
<img src="output/gru-seq2seq-vae.png" width="70%" align="">
|
||||
|
||||
16. Attention-is-all-you-Need, accuracy 94.2482%, time taken for 1 epoch 01:41
|
||||
|
||||
<img src="output/attention-is-all-you-need.png" width="70%" align="">
|
||||
|
||||
17. CNN-Seq2seq, accuracy 90.74%, time taken for 1 epoch 00:43
|
||||
|
||||
<img src="output/cnn-seq2seq.png" width="70%" align="">
|
||||
|
||||
18. Dilated-CNN-Seq2seq, accuracy 95.86%, time taken for 1 epoch 00:14
|
||||
|
||||
<img src="output/dilated-cnn-seq2seq.png" width="70%" align="">
|
||||
|
||||
**Bonus**
|
||||
|
||||
1. How to forecast,
|
||||
|
||||
<img src="output/how-to-forecast.png" width="70%" align="">
|
||||
|
||||
2. Sentiment consensus,
|
||||
|
||||
<img src="output/sentiment-consensus.png" width="70%" align="">
|
||||
|
||||
### Results analysis
|
||||
|
||||
1. Outliers study using K-means, SVM, and Gaussian on TESLA stock
|
||||
|
||||
<img src="misc/outliers.png" width="70%" align="">
|
||||
|
||||
2. Overbought-Oversold study on TESLA stock
|
||||
|
||||
<img src="misc/overbought-oversold.png" width="70%" align="">
|
||||
|
||||
3. Which stock you need to buy?
|
||||
|
||||
<img src="misc/which-stock.png" width="40%" align="">
|
||||
|
||||
### Results simulation
|
||||
|
||||
1. Simple Monte Carlo
|
||||
|
||||
<img src="simulation/monte-carlo-simple.png" width="70%" align="">
|
||||
|
||||
2. Dynamic volatity Monte Carlo
|
||||
|
||||
<img src="simulation/monte-carlo-dynamic-volatility.png" width="70%" align="">
|
||||
|
||||
3. Drift Monte Carlo
|
||||
|
||||
<img src="simulation/monte-carlo-drift.png" width="70%" align="">
|
||||
|
||||
4. Multivariate Drift Monte Carlo BTC/USDT with Bitcurate sentiment
|
||||
|
||||
<img src="simulation/multivariate-drift-monte-carlo.png" width="70%" align="">
|
||||
|
||||
5. Portfolio optimization
|
||||
|
||||
<img src="simulation/portfolio-optimization.png" width="40%" align="">
|
||||
@@ -0,0 +1,892 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>signal</th>\n",
|
||||
" <th>trend</th>\n",
|
||||
" <th>RollingMax</th>\n",
|
||||
" <th>RollingMin</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>5</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>785.309998</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>6</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>762.559998</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>754.020020</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>8</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>736.080017</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>9</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>758.489990</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>10</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>764.479980</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>11</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>771.229980</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>12</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>760.539978</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>13</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>769.200012</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>768.270020</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>15</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>760.989990</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>16</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>761.679993</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>17</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>768.239990</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>18</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>770.840027</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>19</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>758.039978</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>20</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>747.919983</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>21</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>750.500000</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>22</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>762.520020</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>23</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>759.109985</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>24</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>771.190002</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>25</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>776.419983</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>26</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>789.289978</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>736.080017</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>27</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>789.270020</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>736.080017</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>28</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>796.099976</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>736.080017</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>29</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>797.070007</td>\n",
|
||||
" <td>796.099976</td>\n",
|
||||
" <td>736.080017</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>...</th>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>222</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>932.450012</td>\n",
|
||||
" <td>939.330017</td>\n",
|
||||
" <td>906.659973</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>223</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>928.530029</td>\n",
|
||||
" <td>939.330017</td>\n",
|
||||
" <td>906.659973</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>224</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>920.969971</td>\n",
|
||||
" <td>939.330017</td>\n",
|
||||
" <td>906.659973</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>225</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>924.859985</td>\n",
|
||||
" <td>939.330017</td>\n",
|
||||
" <td>906.659973</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>226</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>944.489990</td>\n",
|
||||
" <td>939.330017</td>\n",
|
||||
" <td>906.659973</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>227</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>949.500000</td>\n",
|
||||
" <td>944.489990</td>\n",
|
||||
" <td>913.809998</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>228</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>959.109985</td>\n",
|
||||
" <td>949.500000</td>\n",
|
||||
" <td>913.809998</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>229</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>953.270020</td>\n",
|
||||
" <td>959.109985</td>\n",
|
||||
" <td>913.809998</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>230</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>957.789978</td>\n",
|
||||
" <td>959.109985</td>\n",
|
||||
" <td>913.809998</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>231</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>951.679993</td>\n",
|
||||
" <td>959.109985</td>\n",
|
||||
" <td>913.809998</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>232</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>969.960022</td>\n",
|
||||
" <td>959.109985</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>233</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>978.890015</td>\n",
|
||||
" <td>969.960022</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>234</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>977.000000</td>\n",
|
||||
" <td>978.890015</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>235</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>972.599976</td>\n",
|
||||
" <td>978.890015</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>236</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>989.250000</td>\n",
|
||||
" <td>978.890015</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>237</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>987.830017</td>\n",
|
||||
" <td>989.250000</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>238</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>989.679993</td>\n",
|
||||
" <td>989.250000</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>239</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>992.000000</td>\n",
|
||||
" <td>989.679993</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>240</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>992.179993</td>\n",
|
||||
" <td>992.000000</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>241</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>992.179993</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>242</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>984.450012</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>243</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>988.200012</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>244</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>968.450012</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>245</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>970.539978</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>915.000000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>246</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>973.330017</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>920.969971</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>247</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>972.559998</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>920.969971</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>248</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>1019.270020</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>920.969971</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>249</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>1017.109985</td>\n",
|
||||
" <td>1019.270020</td>\n",
|
||||
" <td>920.969971</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>250</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>1016.640015</td>\n",
|
||||
" <td>1019.270020</td>\n",
|
||||
" <td>920.969971</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>251</th>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" <td>1025.500000</td>\n",
|
||||
" <td>1019.270020</td>\n",
|
||||
" <td>924.859985</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>252 rows × 4 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" signal trend RollingMax RollingMin\n",
|
||||
"0 0.0 768.700012 NaN NaN\n",
|
||||
"1 0.0 762.130005 NaN NaN\n",
|
||||
"2 0.0 762.020020 NaN NaN\n",
|
||||
"3 0.0 782.520020 NaN NaN\n",
|
||||
"4 0.0 790.510010 NaN NaN\n",
|
||||
"5 0.0 785.309998 NaN NaN\n",
|
||||
"6 0.0 762.559998 NaN NaN\n",
|
||||
"7 0.0 754.020020 NaN NaN\n",
|
||||
"8 0.0 736.080017 NaN NaN\n",
|
||||
"9 0.0 758.489990 NaN NaN\n",
|
||||
"10 0.0 764.479980 NaN NaN\n",
|
||||
"11 0.0 771.229980 NaN NaN\n",
|
||||
"12 0.0 760.539978 NaN NaN\n",
|
||||
"13 0.0 769.200012 NaN NaN\n",
|
||||
"14 0.0 768.270020 NaN NaN\n",
|
||||
"15 0.0 760.989990 NaN NaN\n",
|
||||
"16 0.0 761.679993 NaN NaN\n",
|
||||
"17 0.0 768.239990 NaN NaN\n",
|
||||
"18 0.0 770.840027 NaN NaN\n",
|
||||
"19 0.0 758.039978 NaN NaN\n",
|
||||
"20 0.0 747.919983 NaN NaN\n",
|
||||
"21 0.0 750.500000 NaN NaN\n",
|
||||
"22 0.0 762.520020 NaN NaN\n",
|
||||
"23 0.0 759.109985 NaN NaN\n",
|
||||
"24 0.0 771.190002 NaN NaN\n",
|
||||
"25 0.0 776.419983 NaN NaN\n",
|
||||
"26 0.0 789.289978 790.510010 736.080017\n",
|
||||
"27 0.0 789.270020 790.510010 736.080017\n",
|
||||
"28 -1.0 796.099976 790.510010 736.080017\n",
|
||||
"29 -1.0 797.070007 796.099976 736.080017\n",
|
||||
".. ... ... ... ...\n",
|
||||
"222 0.0 932.450012 939.330017 906.659973\n",
|
||||
"223 0.0 928.530029 939.330017 906.659973\n",
|
||||
"224 0.0 920.969971 939.330017 906.659973\n",
|
||||
"225 0.0 924.859985 939.330017 906.659973\n",
|
||||
"226 -1.0 944.489990 939.330017 906.659973\n",
|
||||
"227 -1.0 949.500000 944.489990 913.809998\n",
|
||||
"228 -1.0 959.109985 949.500000 913.809998\n",
|
||||
"229 0.0 953.270020 959.109985 913.809998\n",
|
||||
"230 0.0 957.789978 959.109985 913.809998\n",
|
||||
"231 0.0 951.679993 959.109985 913.809998\n",
|
||||
"232 -1.0 969.960022 959.109985 915.000000\n",
|
||||
"233 -1.0 978.890015 969.960022 915.000000\n",
|
||||
"234 0.0 977.000000 978.890015 915.000000\n",
|
||||
"235 0.0 972.599976 978.890015 915.000000\n",
|
||||
"236 -1.0 989.250000 978.890015 915.000000\n",
|
||||
"237 0.0 987.830017 989.250000 915.000000\n",
|
||||
"238 -1.0 989.679993 989.250000 915.000000\n",
|
||||
"239 -1.0 992.000000 989.679993 915.000000\n",
|
||||
"240 -1.0 992.179993 992.000000 915.000000\n",
|
||||
"241 -1.0 992.809998 992.179993 915.000000\n",
|
||||
"242 0.0 984.450012 992.809998 915.000000\n",
|
||||
"243 0.0 988.200012 992.809998 915.000000\n",
|
||||
"244 0.0 968.450012 992.809998 915.000000\n",
|
||||
"245 0.0 970.539978 992.809998 915.000000\n",
|
||||
"246 0.0 973.330017 992.809998 920.969971\n",
|
||||
"247 0.0 972.559998 992.809998 920.969971\n",
|
||||
"248 -1.0 1019.270020 992.809998 920.969971\n",
|
||||
"249 0.0 1017.109985 1019.270020 920.969971\n",
|
||||
"250 0.0 1016.640015 1019.270020 920.969971\n",
|
||||
"251 -1.0 1025.500000 1019.270020 924.859985\n",
|
||||
"\n",
|
||||
"[252 rows x 4 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"count = int(np.ceil(len(df) * 0.1))\n",
|
||||
"signals = pd.DataFrame(index=df.index)\n",
|
||||
"signals['signal'] = 0.0\n",
|
||||
"signals['trend'] = df['Close']\n",
|
||||
"signals['RollingMax'] = (signals.trend.shift(1).rolling(count).max())\n",
|
||||
"signals['RollingMin'] = (signals.trend.shift(1).rolling(count).min())\n",
|
||||
"signals.loc[signals['RollingMax'] < signals.trend, 'signal'] = -1\n",
|
||||
"signals.loc[signals['RollingMin'] > signals.trend, 'signal'] = 1\n",
|
||||
"signals"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def buy_stock(\n",
|
||||
" real_movement,\n",
|
||||
" signal,\n",
|
||||
" initial_money = 10000,\n",
|
||||
" max_buy = 1,\n",
|
||||
" max_sell = 1,\n",
|
||||
"):\n",
|
||||
" \"\"\"\n",
|
||||
" real_movement = actual movement in the real world\n",
|
||||
" delay = how much interval you want to delay to change our decision from buy to sell, vice versa\n",
|
||||
" initial_state = 1 is buy, 0 is sell\n",
|
||||
" initial_money = 1000, ignore what kind of currency\n",
|
||||
" max_buy = max quantity for share to buy\n",
|
||||
" max_sell = max quantity for share to sell\n",
|
||||
" \"\"\"\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" current_inventory = 0\n",
|
||||
"\n",
|
||||
" def buy(i, initial_money, current_inventory):\n",
|
||||
" shares = initial_money // real_movement[i]\n",
|
||||
" if shares < 1:\n",
|
||||
" print(\n",
|
||||
" 'day %d: total balances %f, not enough money to buy a unit price %f'\n",
|
||||
" % (i, initial_money, real_movement[i])\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" if shares > max_buy:\n",
|
||||
" buy_units = max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = shares\n",
|
||||
" initial_money -= buy_units * real_movement[i]\n",
|
||||
" current_inventory += buy_units\n",
|
||||
" print(\n",
|
||||
" 'day %d: buy %d units at price %f, total balance %f'\n",
|
||||
" % (i, buy_units, buy_units * real_movement[i], initial_money)\n",
|
||||
" )\n",
|
||||
" states_buy.append(0)\n",
|
||||
" return initial_money, current_inventory\n",
|
||||
"\n",
|
||||
" for i in range(real_movement.shape[0] - int(0.025 * len(df))):\n",
|
||||
" state = signal[i]\n",
|
||||
" if state == 1:\n",
|
||||
" initial_money, current_inventory = buy(\n",
|
||||
" i, initial_money, current_inventory\n",
|
||||
" )\n",
|
||||
" states_buy.append(i)\n",
|
||||
" elif state == -1:\n",
|
||||
" if current_inventory == 0:\n",
|
||||
" print('day %d: cannot sell anything, inventory 0' % (i))\n",
|
||||
" else:\n",
|
||||
" if current_inventory > max_sell:\n",
|
||||
" sell_units = max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = current_inventory\n",
|
||||
" current_inventory -= sell_units\n",
|
||||
" total_sell = sell_units * real_movement[i]\n",
|
||||
" initial_money += total_sell\n",
|
||||
" try:\n",
|
||||
" invest = (\n",
|
||||
" (real_movement[i] - real_movement[states_buy[-1]])\n",
|
||||
" / real_movement[states_buy[-1]]\n",
|
||||
" ) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (i, sell_units, total_sell, invest, initial_money)\n",
|
||||
" )\n",
|
||||
" states_sell.append(i)\n",
|
||||
" \n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 28: cannot sell anything, inventory 0\n",
|
||||
"day 29: cannot sell anything, inventory 0\n",
|
||||
"day 30: cannot sell anything, inventory 0\n",
|
||||
"day 44: cannot sell anything, inventory 0\n",
|
||||
"day 45: cannot sell anything, inventory 0\n",
|
||||
"day 47: cannot sell anything, inventory 0\n",
|
||||
"day 54: cannot sell anything, inventory 0\n",
|
||||
"day 55: cannot sell anything, inventory 0\n",
|
||||
"day 56: cannot sell anything, inventory 0\n",
|
||||
"day 85: cannot sell anything, inventory 0\n",
|
||||
"day 86: cannot sell anything, inventory 0\n",
|
||||
"day 87: cannot sell anything, inventory 0\n",
|
||||
"day 88: cannot sell anything, inventory 0\n",
|
||||
"day 89: cannot sell anything, inventory 0\n",
|
||||
"day 90: cannot sell anything, inventory 0\n",
|
||||
"day 91: cannot sell anything, inventory 0\n",
|
||||
"day 92: cannot sell anything, inventory 0\n",
|
||||
"day 96: buy 1 units at price 817.580017, total balance 9182.419983\n",
|
||||
"day 97: buy 1 units at price 814.429993, total balance 8367.989990\n",
|
||||
"day 117, sell 1 units at price 862.760010, investment 5.934214 %, total balance 9230.750000,\n",
|
||||
"day 118, sell 1 units at price 872.299988, investment 7.105582 %, total balance 10103.049988,\n",
|
||||
"day 120: cannot sell anything, inventory 0\n",
|
||||
"day 121: cannot sell anything, inventory 0\n",
|
||||
"day 122: cannot sell anything, inventory 0\n",
|
||||
"day 123: cannot sell anything, inventory 0\n",
|
||||
"day 124: cannot sell anything, inventory 0\n",
|
||||
"day 125: cannot sell anything, inventory 0\n",
|
||||
"day 127: cannot sell anything, inventory 0\n",
|
||||
"day 132: cannot sell anything, inventory 0\n",
|
||||
"day 133: cannot sell anything, inventory 0\n",
|
||||
"day 138: cannot sell anything, inventory 0\n",
|
||||
"day 139: cannot sell anything, inventory 0\n",
|
||||
"day 140: cannot sell anything, inventory 0\n",
|
||||
"day 141: cannot sell anything, inventory 0\n",
|
||||
"day 142: cannot sell anything, inventory 0\n",
|
||||
"day 146: cannot sell anything, inventory 0\n",
|
||||
"day 162: buy 1 units at price 927.330017, total balance 9175.719971\n",
|
||||
"day 164: buy 1 units at price 917.789978, total balance 8257.929993\n",
|
||||
"day 165: buy 1 units at price 908.729980, total balance 7349.200013\n",
|
||||
"day 166: buy 1 units at price 898.700012, total balance 6450.500001\n",
|
||||
"day 177, sell 1 units at price 970.890015, investment 8.032714 %, total balance 7421.390016,\n",
|
||||
"day 179, sell 1 units at price 972.919983, investment 8.258592 %, total balance 8394.309999,\n",
|
||||
"day 180, sell 1 units at price 980.340027, investment 9.084234 %, total balance 9374.650026,\n",
|
||||
"day 200: buy 1 units at price 906.659973, total balance 8467.990053\n",
|
||||
"day 226, sell 1 units at price 944.489990, investment 4.172459 %, total balance 9412.480043,\n",
|
||||
"day 227, sell 1 units at price 949.500000, investment 4.725038 %, total balance 10361.980043,\n",
|
||||
"day 228: cannot sell anything, inventory 0\n",
|
||||
"day 232: cannot sell anything, inventory 0\n",
|
||||
"day 233: cannot sell anything, inventory 0\n",
|
||||
"day 236: cannot sell anything, inventory 0\n",
|
||||
"day 238: cannot sell anything, inventory 0\n",
|
||||
"day 239: cannot sell anything, inventory 0\n",
|
||||
"day 240: cannot sell anything, inventory 0\n",
|
||||
"day 241: cannot sell anything, inventory 0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = buy_stock(df.Close, signals['signal'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XeYVEXWwOHf7TA5J3JQQpEkIyiCYMCAIoor5rDquOKKAXP2E13Dumtaw6osKqtrjhhQAUEEFBQDQqEgmck59UyH7497u5mBCT05nfd5eBz6prrdNXhPn6pThs/nQwgh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AhwDdKqWLMh3yN+eDt18t6/SDWw/drwDarf3THXApiHeZn/DPm3MX5tex/A+ZnXWi9d3UOSW2EVzEz1W9ac+r8bsYMQtZYw02/YP+akbX1pQN/N+pFa/0uZgXU/1nX/QUzqA/GUsysV5pSKquGfU7HDFgLMYO5J6m0hEjl/tnYvo75hUIeZv/JpWofegizQMsGa5/rgFla6zxr+91WO3cAXwGPWH0aq4/PwvxCIhezcM7Zlc7t/wJnGuZcVrTW+zB/RzZi/ptyaw3vjxBC1Mnw+Ro7ukMIIYSV1XtWa92nzp1Fi7MKdfwIDK8uiymEEEJ0dBL4CSFEA1jzcKZiZv26YM7Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df['Close']\n",
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,509 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Agent:\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip, batch_size):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" self.action_size = 3\n",
|
||||
" self.batch_size = batch_size\n",
|
||||
" self.memory = deque(maxlen = 1000)\n",
|
||||
" self.inventory = []\n",
|
||||
"\n",
|
||||
" self.gamma = 0.95\n",
|
||||
" self.epsilon = 0.5\n",
|
||||
" self.epsilon_min = 0.01\n",
|
||||
" self.epsilon_decay = 0.999\n",
|
||||
"\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.X = tf.placeholder(tf.float32, [None, self.state_size])\n",
|
||||
" self.Y = tf.placeholder(tf.float32, [None, self.action_size])\n",
|
||||
" feed = tf.layers.dense(self.X, 512, activation = tf.nn.relu)\n",
|
||||
" tensor_action, tensor_validation = tf.split(feed,2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, self.action_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.GradientDescentOptimizer(1e-5).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
"\n",
|
||||
" def act(self, state):\n",
|
||||
" if random.random() <= self.epsilon:\n",
|
||||
" return random.randrange(self.action_size)\n",
|
||||
" return np.argmax(\n",
|
||||
" self.sess.run(self.logits, feed_dict = {self.X: state})[0]\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])\n",
|
||||
"\n",
|
||||
" def replay(self, batch_size):\n",
|
||||
" mini_batch = []\n",
|
||||
" l = len(self.memory)\n",
|
||||
" for i in range(l - batch_size, l):\n",
|
||||
" mini_batch.append(self.memory[i])\n",
|
||||
" replay_size = len(mini_batch)\n",
|
||||
" X = np.empty((replay_size, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.action_size))\n",
|
||||
" states = np.array([a[0][0] for a in mini_batch])\n",
|
||||
" new_states = np.array([a[3][0] for a in mini_batch])\n",
|
||||
" Q = self.sess.run(self.logits, feed_dict = {self.X: states})\n",
|
||||
" Q_new = self.sess.run(self.logits, feed_dict = {self.X: new_states})\n",
|
||||
" for i in range(len(mini_batch)):\n",
|
||||
" state, action, reward, next_state, done = mini_batch[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action] = reward\n",
|
||||
" if not done:\n",
|
||||
" target[action] += self.gamma * np.amax(Q_new[i])\n",
|
||||
" X[i] = state\n",
|
||||
" Y[i] = target\n",
|
||||
" cost, _ = self.sess.run(\n",
|
||||
" [self.cost, self.optimizer], feed_dict = {self.X: X, self.Y: Y}\n",
|
||||
" )\n",
|
||||
" if self.epsilon > self.epsilon_min:\n",
|
||||
" self.epsilon *= self.epsilon_decay\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t] and t < (len(self.trend) - self.half_window):\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t] and t < (len(self.trend) - self.half_window):\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" self.memory.append((state, action, invest, \n",
|
||||
" next_state, starting_money < initial_money))\n",
|
||||
" state = next_state\n",
|
||||
" batch_size = min(self.batch_size, len(self.memory))\n",
|
||||
" cost = self.replay(batch_size)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:From <ipython-input-3-28bed545c0f8>:30: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"epoch: 10, total rewards: 231.100222.3, cost: 0.499693, total money: 10231.100222\n",
|
||||
"epoch: 20, total rewards: 195.875063.3, cost: 0.324152, total money: 10195.875063\n",
|
||||
"epoch: 30, total rewards: 219.615054.3, cost: 0.237771, total money: 10219.615054\n",
|
||||
"epoch: 40, total rewards: 56.505131.3, cost: 0.183305, total money: 10056.505131\n",
|
||||
"epoch: 50, total rewards: 190.745120.3, cost: 0.129967, total money: 10190.745120\n",
|
||||
"epoch: 60, total rewards: 165.275088.3, cost: 0.134246, total money: 10165.275088\n",
|
||||
"epoch: 70, total rewards: 201.795107.3, cost: 0.075016, total money: 10201.795107\n",
|
||||
"epoch: 80, total rewards: 187.545045.3, cost: 0.062454, total money: 10187.545045\n",
|
||||
"epoch: 90, total rewards: 206.835023.3, cost: 0.050687, total money: 10206.835023\n",
|
||||
"epoch: 100, total rewards: 199.895082.3, cost: 0.041359, total money: 10199.895082\n",
|
||||
"epoch: 110, total rewards: 184.405092.3, cost: 0.035289, total money: 10184.405092\n",
|
||||
"epoch: 120, total rewards: 242.405092.3, cost: 0.047248, total money: 10242.405092\n",
|
||||
"epoch: 130, total rewards: 148.405032.3, cost: 0.050786, total money: 10148.405032\n",
|
||||
"epoch: 140, total rewards: 225.724978.3, cost: 0.021171, total money: 10225.724978\n",
|
||||
"epoch: 150, total rewards: 168.344972.3, cost: 0.018388, total money: 10168.344972\n",
|
||||
"epoch: 160, total rewards: 230.095034.3, cost: 0.199324, total money: 10230.095034\n",
|
||||
"epoch: 170, total rewards: 206.275026.3, cost: 0.044696, total money: 10206.275026\n",
|
||||
"epoch: 180, total rewards: 364.895023.3, cost: 0.016494, total money: 10364.895023\n",
|
||||
"epoch: 190, total rewards: 220.664980.3, cost: 0.014381, total money: 10220.664980\n",
|
||||
"epoch: 200, total rewards: 175.284975.3, cost: 0.010883, total money: 10175.284975\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip, \n",
|
||||
" batch_size = batch_size)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 7: buy 1 unit at price 754.020020, total balance 9245.979980\n",
|
||||
"day 8: buy 1 unit at price 736.080017, total balance 8509.899963\n",
|
||||
"day 9, sell 1 unit at price 758.489990, investment 0.592818 %, total balance 9268.389953,\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 8497.159973\n",
|
||||
"day 12, sell 1 unit at price 760.539978, investment 3.323003 %, total balance 9257.699951,\n",
|
||||
"day 14, sell 1 unit at price 768.270020, investment -0.383797 %, total balance 10025.969971,\n",
|
||||
"day 22: buy 1 unit at price 762.520020, total balance 9263.449951\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 1.137017 %, total balance 10034.639953,\n",
|
||||
"day 28: buy 1 unit at price 796.099976, total balance 9238.539977\n",
|
||||
"day 29: buy 1 unit at price 797.070007, total balance 8441.469970\n",
|
||||
"day 31, sell 1 unit at price 790.799988, investment -0.665744 %, total balance 9232.269958,\n",
|
||||
"day 32: buy 1 unit at price 794.200012, total balance 8438.069946\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 7641.649963\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment -0.692539 %, total balance 8433.199951,\n",
|
||||
"day 39, sell 1 unit at price 782.789978, investment -1.436670 %, total balance 9215.989929,\n",
|
||||
"day 41, sell 1 unit at price 786.140015, investment -1.290772 %, total balance 10002.129944,\n",
|
||||
"day 48: buy 1 unit at price 806.359985, total balance 9195.769959\n",
|
||||
"day 49: buy 1 unit at price 807.880005, total balance 8387.889954\n",
|
||||
"day 50, sell 1 unit at price 804.609985, investment -0.217025 %, total balance 9192.499939,\n",
|
||||
"day 52: buy 1 unit at price 802.174988, total balance 8390.324951\n",
|
||||
"day 53, sell 1 unit at price 805.020020, investment -0.354011 %, total balance 9195.344971,\n",
|
||||
"day 56, sell 1 unit at price 835.669983, investment 4.175522 %, total balance 10031.014954,\n",
|
||||
"day 67: buy 1 unit at price 809.559998, total balance 9221.454956\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 8407.784973\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 1.195710 %, total balance 9227.024963,\n",
|
||||
"day 71: buy 1 unit at price 818.979980, total balance 8408.044983\n",
|
||||
"day 72, sell 1 unit at price 824.159973, investment 1.289219 %, total balance 9232.204956,\n",
|
||||
"day 76: buy 1 unit at price 831.330017, total balance 8400.874939\n",
|
||||
"day 77, sell 1 unit at price 828.640015, investment 1.179520 %, total balance 9229.514954,\n",
|
||||
"day 78: buy 1 unit at price 829.280029, total balance 8400.234925\n",
|
||||
"day 79, sell 1 unit at price 823.210022, investment -0.976747 %, total balance 9223.444947,\n",
|
||||
"day 80: buy 1 unit at price 835.239990, total balance 8388.204957\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment 0.162789 %, total balance 9218.834962,\n",
|
||||
"day 83, sell 1 unit at price 827.780029, investment -0.893152 %, total balance 10046.614991,\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 9201.075013\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 0.009463 %, total balance 10046.695008,\n",
|
||||
"day 91: buy 1 unit at price 848.780029, total balance 9197.914979\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance 8345.794984\n",
|
||||
"day 93: buy 1 unit at price 848.400024, total balance 7497.394960\n",
|
||||
"day 96, sell 1 unit at price 817.580017, investment -3.675865 %, total balance 8314.974977,\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 7500.544984\n",
|
||||
"day 100, sell 1 unit at price 831.409973, investment -2.430411 %, total balance 8331.954957,\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment -1.991988 %, total balance 9163.454957,\n",
|
||||
"day 103, sell 1 unit at price 838.549988, investment 2.961580 %, total balance 10002.004945,\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 9167.434938\n",
|
||||
"day 105: buy 1 unit at price 831.409973, total balance 8336.024965\n",
|
||||
"day 106, sell 1 unit at price 827.880005, investment -0.801611 %, total balance 9163.904970,\n",
|
||||
"day 107: buy 1 unit at price 824.669983, total balance 8339.234987\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance 7514.505007\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 6690.185000\n",
|
||||
"day 111: buy 1 unit at price 823.559998, total balance 5866.625002\n",
|
||||
"day 112, sell 1 unit at price 837.169983, investment 0.692800 %, total balance 6703.794985,\n",
|
||||
"day 113, sell 1 unit at price 836.820007, investment 1.473320 %, total balance 7540.614992,\n",
|
||||
"day 114, sell 1 unit at price 838.210022, investment 1.634479 %, total balance 8378.825014,\n",
|
||||
"day 115, sell 1 unit at price 841.650024, investment 2.102341 %, total balance 9220.475038,\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 2.383555 %, total balance 10063.665040,\n",
|
||||
"day 122: buy 1 unit at price 912.570007, total balance 9151.095033\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 0.424077 %, total balance 10067.535035,\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance 9135.875062\n",
|
||||
"day 126, sell 1 unit at price 927.130005, investment -0.486225 %, total balance 10063.005067,\n",
|
||||
"day 144: buy 1 unit at price 966.950012, total balance 9096.055055\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 0.894562 %, total balance 10071.655031,\n",
|
||||
"day 146: buy 1 unit at price 983.679993, total balance 9087.975038\n",
|
||||
"day 148: buy 1 unit at price 980.940002, total balance 8107.035036\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment -0.027450 %, total balance 9090.445009,\n",
|
||||
"day 151, sell 1 unit at price 942.900024, investment -3.877911 %, total balance 10033.345033,\n",
|
||||
"day 155: buy 1 unit at price 939.780029, total balance 9093.565004\n",
|
||||
"day 157, sell 1 unit at price 950.630005, investment 1.154523 %, total balance 10044.195009,\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 9078.604982\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment -1.379468 %, total balance 10030.875002,\n",
|
||||
"day 164: buy 1 unit at price 917.789978, total balance 9113.085024\n",
|
||||
"day 166, sell 1 unit at price 898.700012, investment -2.079993 %, total balance 10011.785036,\n",
|
||||
"day 168: buy 1 unit at price 906.690002, total balance 9105.095034\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 8175.005007\n",
|
||||
"day 172, sell 1 unit at price 943.830017, investment 4.096220 %, total balance 9118.835024,\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 1.835300 %, total balance 10065.994997,\n",
|
||||
"day 176: buy 1 unit at price 965.400024, total balance 9100.594973\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 0.778947 %, total balance 10073.514956,\n",
|
||||
"day 180: buy 1 unit at price 980.340027, total balance 9093.174929\n",
|
||||
"day 181, sell 1 unit at price 950.700012, investment -3.023442 %, total balance 10043.874941,\n",
|
||||
"day 194: buy 1 unit at price 914.390015, total balance 9129.484926\n",
|
||||
"day 195, sell 1 unit at price 922.669983, investment 0.905518 %, total balance 10052.154909,\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 9125.194887\n",
|
||||
"day 199: buy 1 unit at price 910.669983, total balance 8214.524904\n",
|
||||
"day 201, sell 1 unit at price 924.690002, investment -0.244889 %, total balance 9139.214906,\n",
|
||||
"day 202, sell 1 unit at price 927.000000, investment 1.793187 %, total balance 10066.214906,\n",
|
||||
"day 208: buy 1 unit at price 939.330017, total balance 9126.884889\n",
|
||||
"day 211, sell 1 unit at price 927.809998, investment -1.226408 %, total balance 10054.694887,\n",
|
||||
"day 214: buy 1 unit at price 929.080017, total balance 9125.614870\n",
|
||||
"day 216, sell 1 unit at price 935.090027, investment 0.646878 %, total balance 10060.704897,\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance 9145.704897\n",
|
||||
"day 221: buy 1 unit at price 931.580017, total balance 8214.124880\n",
|
||||
"day 222, sell 1 unit at price 932.450012, investment 1.907105 %, total balance 9146.574892,\n",
|
||||
"day 223, sell 1 unit at price 928.530029, investment -0.327399 %, total balance 10075.104921,\n",
|
||||
"day 224: buy 1 unit at price 920.969971, total balance 9154.134950\n",
|
||||
"day 226: buy 1 unit at price 944.489990, total balance 8209.644960\n",
|
||||
"day 227: buy 1 unit at price 949.500000, total balance 7260.144960\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 4.141287 %, total balance 8219.254945,\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 0.929605 %, total balance 9172.524965,\n",
|
||||
"day 230: buy 1 unit at price 957.789978, total balance 8214.734987\n",
|
||||
"day 231: buy 1 unit at price 951.679993, total balance 7263.054994\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 2.154821 %, total balance 8233.015016,\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 2.202992 %, total balance 9211.905031,\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment 2.660559 %, total balance 10188.905031,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4FVX6wPHv3JIe0kMJvR2aSBUbir2AYGFFXQu6yrroiq5lbWtZu79117Ws3bWgiKICirrYURQVEEEghyYtJCGE9HKTW35/zNxw0296ez/P40NyZ+bMmck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DZmmtA+dNllj3CsyAuSRgWx8OBZiVWEP8HgBWWs/HkZjFSl7HfCP/m3V//lzH/vdiFvrJw1xS472aztVMFmAWcPmiSnD+b8xnbblSqgDzmfYP1+2BOYcsH3MNua85tIzBv4GZSqkcpdQTDe2M1no15jPwFObPdhuHChTVd2yqdT07rHtZ0xDQ4zB/lh9hBkAlmL9/AFiFVH5vtfc15nOyyLoH7wIPaq39+z+CWTRoHeYzfANwnj5UffU0zAArB3PO5Ola6yyr7TLM4jmXWsdeAZwd8DfmAuvaCzB/5x7RVrXdII7FyrDPA/5mHePGnGv7BWZQHziUVggLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,495 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, output_size, layer_size, learning_rate):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" feed = tf.layers.dense(self.X, layer_size, activation = tf.nn.relu)\n",
|
||||
" tensor_action, tensor_validation = tf.split(feed,2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, output_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" self.cost = tf.reduce_sum(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 500\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.model = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.model_negative = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" self.trainable = tf.trainable_variables()\n",
|
||||
" \n",
|
||||
" def _assign(self):\n",
|
||||
" for i in range(len(self.trainable)//2):\n",
|
||||
" assign_op = self.trainable[i+len(self.trainable)//2].assign(self.trainable[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
"\n",
|
||||
" def _memorize(self, state, action, reward, new_state, done):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, done))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
"\n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action = self.get_predicted_action([state])\n",
|
||||
" return action\n",
|
||||
"\n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" Q = self.predict(states)\n",
|
||||
" Q_new = self.predict(new_states)\n",
|
||||
" Q_new_negative = self.sess.run(self.model_negative.logits, feed_dict={self.model_negative.X:new_states})\n",
|
||||
" replay_size = len(replay)\n",
|
||||
" X = np.empty((replay_size, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.OUTPUT_SIZE))\n",
|
||||
" for i in range(replay_size):\n",
|
||||
" state_r, action_r, reward_r, new_state_r, done_r = replay[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action_r] = reward_r\n",
|
||||
" if not done_r:\n",
|
||||
" target[action_r] += self.GAMMA * Q_new_negative[i, np.argmax(Q_new[i])]\n",
|
||||
" X[i] = state_r\n",
|
||||
" Y[i] = target\n",
|
||||
" return X, Y\n",
|
||||
"\n",
|
||||
" def predict(self, inputs):\n",
|
||||
" return self.sess.run(self.model.logits, feed_dict={self.model.X:inputs})\n",
|
||||
" \n",
|
||||
" def get_predicted_action(self, sequence):\n",
|
||||
" prediction = self.predict(np.array(sequence))[0]\n",
|
||||
" return np.argmax(prediction)\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign()\n",
|
||||
" \n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" \n",
|
||||
" self._memorize(state, action, invest, next_state, starting_money < initial_money)\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" state = next_state\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" X, Y = self._construct_memories(replay)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.model.cost, self.model.optimizer], \n",
|
||||
" feed_dict={self.model.X: X, self.model.Y:Y})\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:From <ipython-input-3-42f2d1e26a9d>:12: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"epoch: 10, total rewards: 1486.684997.3, cost: 0.694152, total money: 10514.124999\n",
|
||||
"epoch: 20, total rewards: 313.279660.3, cost: 0.878157, total money: 8354.909665\n",
|
||||
"epoch: 30, total rewards: 752.595089.3, cost: 0.320037, total money: 10752.595089\n",
|
||||
"epoch: 40, total rewards: 1159.299987.3, cost: 0.318166, total money: 10186.739989\n",
|
||||
"epoch: 50, total rewards: 993.220279.3, cost: 0.391151, total money: 4149.310245\n",
|
||||
"epoch: 60, total rewards: 1616.499880.3, cost: 0.307440, total money: 9630.939883\n",
|
||||
"epoch: 70, total rewards: 941.484560.3, cost: 0.332979, total money: 6969.054506\n",
|
||||
"epoch: 80, total rewards: 904.899903.3, cost: 0.718111, total money: 1132.559876\n",
|
||||
"epoch: 90, total rewards: 346.619873.3, cost: 0.482044, total money: 542.599852\n",
|
||||
"epoch: 100, total rewards: 141.554626.3, cost: 0.238426, total money: 6115.974608\n",
|
||||
"epoch: 110, total rewards: -159.529845.3, cost: 0.202412, total money: 8852.270143\n",
|
||||
"epoch: 120, total rewards: -37.579779.3, cost: 0.433529, total money: 8945.780206\n",
|
||||
"epoch: 130, total rewards: 1049.544800.3, cost: 0.408910, total money: 8099.664795\n",
|
||||
"epoch: 140, total rewards: 59.114809.3, cost: 0.028664, total money: 7098.904848\n",
|
||||
"epoch: 150, total rewards: 96.424866.3, cost: 0.070552, total money: 9079.784851\n",
|
||||
"epoch: 160, total rewards: 74.179754.3, cost: 0.044092, total money: 10074.179754\n",
|
||||
"epoch: 170, total rewards: 80.999883.3, cost: 0.018813, total money: 8047.249883\n",
|
||||
"epoch: 180, total rewards: 62.700011.3, cost: 0.083292, total money: 10062.700011\n",
|
||||
"epoch: 190, total rewards: 70.424991.3, cost: 0.013884, total money: 9053.315006\n",
|
||||
"epoch: 200, total rewards: 10.620115.3, cost: 0.030838, total money: 10010.620115\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 1: buy 1 unit at price 762.130005, total balance 9237.869995\n",
|
||||
"day 2, sell 1 unit at price 762.020020, investment -0.014431 %, total balance 9999.890015,\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 9228.660035\n",
|
||||
"day 12: buy 1 unit at price 760.539978, total balance 8468.120057\n",
|
||||
"day 13, sell 1 unit at price 769.200012, investment -0.263212 %, total balance 9237.320069,\n",
|
||||
"day 15, sell 1 unit at price 760.989990, investment 0.059170 %, total balance 9998.310059,\n",
|
||||
"day 34: buy 1 unit at price 794.559998, total balance 9203.750061\n",
|
||||
"day 35, sell 1 unit at price 791.260010, investment -0.415323 %, total balance 9995.010071,\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 9205.100098\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment 0.207620 %, total balance 9996.650086,\n",
|
||||
"day 38: buy 1 unit at price 785.049988, total balance 9211.600098\n",
|
||||
"day 40, sell 1 unit at price 771.820007, investment -1.685241 %, total balance 9983.420105,\n",
|
||||
"day 54: buy 1 unit at price 819.309998, total balance 9164.110107\n",
|
||||
"day 55, sell 1 unit at price 823.869995, investment 0.556566 %, total balance 9987.980102,\n",
|
||||
"day 62: buy 1 unit at price 798.530029, total balance 9189.450073\n",
|
||||
"day 64, sell 1 unit at price 801.340027, investment 0.351896 %, total balance 9990.790100,\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 9177.120117\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 0.684554 %, total balance 9996.360107,\n",
|
||||
"day 72: buy 1 unit at price 824.159973, total balance 9172.200134\n",
|
||||
"day 73, sell 1 unit at price 828.070007, investment 0.474427 %, total balance 10000.270141,\n",
|
||||
"day 74: buy 1 unit at price 831.659973, total balance 9168.610168\n",
|
||||
"day 75, sell 1 unit at price 830.760010, investment -0.108213 %, total balance 9999.370178,\n",
|
||||
"day 79: buy 1 unit at price 823.210022, total balance 9176.160156\n",
|
||||
"day 80, sell 1 unit at price 835.239990, investment 1.461349 %, total balance 10011.400146,\n",
|
||||
"day 90: buy 1 unit at price 847.200012, total balance 9164.200134\n",
|
||||
"day 91, sell 1 unit at price 848.780029, investment 0.186499 %, total balance 10012.980163,\n",
|
||||
"day 93: buy 1 unit at price 848.400024, total balance 9164.580139\n",
|
||||
"day 94: buy 1 unit at price 830.460022, total balance 8334.120117\n",
|
||||
"day 95, sell 1 unit at price 829.590027, investment -2.217114 %, total balance 9163.710144,\n",
|
||||
"day 96, sell 1 unit at price 817.580017, investment -1.550948 %, total balance 9981.290161,\n",
|
||||
"day 100: buy 1 unit at price 831.409973, total balance 9149.880188\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment 0.010828 %, total balance 9981.380188,\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 9146.810181\n",
|
||||
"day 106: buy 1 unit at price 827.880005, total balance 8318.930176\n",
|
||||
"day 107, sell 1 unit at price 824.669983, investment -1.186242 %, total balance 9143.600159,\n",
|
||||
"day 108, sell 1 unit at price 824.729980, investment -0.380493 %, total balance 9968.330139,\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 9144.010132\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -0.092198 %, total balance 9967.570130,\n",
|
||||
"day 115: buy 1 unit at price 841.650024, total balance 9125.920106\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 0.182971 %, total balance 9969.110108,\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance 9037.450135\n",
|
||||
"day 126, sell 1 unit at price 927.130005, investment -0.486225 %, total balance 9964.580140,\n",
|
||||
"day 127: buy 1 unit at price 934.299988, total balance 9030.280152\n",
|
||||
"day 128, sell 1 unit at price 932.169983, investment -0.227979 %, total balance 9962.450135,\n",
|
||||
"day 141: buy 1 unit at price 971.469971, total balance 8990.980164\n",
|
||||
"day 142, sell 1 unit at price 975.880005, investment 0.453955 %, total balance 9966.860169,\n",
|
||||
"day 152: buy 1 unit at price 953.400024, total balance 9013.460145\n",
|
||||
"day 153, sell 1 unit at price 950.760010, investment -0.276905 %, total balance 9964.220155,\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance 9006.850160\n",
|
||||
"day 157, sell 1 unit at price 950.630005, investment -0.704011 %, total balance 9957.480165,\n",
|
||||
"day 166: buy 1 unit at price 898.700012, total balance 9058.780153\n",
|
||||
"day 167, sell 1 unit at price 911.710022, investment 1.447648 %, total balance 9970.490175,\n",
|
||||
"day 172: buy 1 unit at price 943.830017, total balance 9026.660158\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 0.352813 %, total balance 9973.820131,\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 9043.320131\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 8112.490114\n",
|
||||
"day 187, sell 1 unit at price 930.390015, investment -0.011820 %, total balance 9042.880129,\n",
|
||||
"day 188, sell 1 unit at price 923.650024, investment -0.771354 %, total balance 9966.530153,\n",
|
||||
"day 193: buy 1 unit at price 907.239990, total balance 9059.290163\n",
|
||||
"day 194, sell 1 unit at price 914.390015, investment 0.788107 %, total balance 9973.680178,\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 9046.720156\n",
|
||||
"day 199, sell 1 unit at price 910.669983, investment -1.757362 %, total balance 9957.390139,\n",
|
||||
"day 211: buy 1 unit at price 927.809998, total balance 9029.580141\n",
|
||||
"day 212, sell 1 unit at price 935.950012, investment 0.877336 %, total balance 9965.530153,\n",
|
||||
"day 213: buy 1 unit at price 926.500000, total balance 9039.030153\n",
|
||||
"day 214, sell 1 unit at price 929.080017, investment 0.278469 %, total balance 9968.110170,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XeYFdX5wPHv3LK9N8pSFTgUpUoAUUTsilhA7C0akmCi5meJGmO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zKlbWrwdWhmi11loWWBZCCCFEpyRdPYUQoukMrNn48rG6em7Bmq1PCCGEEKJTkoyfEEIIIYQQQnRxkvETQgghhBBCiC7uaFzOIRRrSu9MrKmdhRBCCCGEEKI7cQK9sJaJ8TTmgKMx8BsPrOjoRgghhBBCCCFEBzsZ+KoxBY/GwC8TID+/FL+/c41PTEyMIi+vpOGCQjSRPFuiLcnzJdqKPFuiLcnzJdpSZ3++HA6D+PhIsGOjxjgaAz8fgN9vdrrAD+iUbRJdgzxboi3J8yXaijxboi3J8yXa0lHyfDV66JtM7iKEEEIIIYQQXZwEfkIIIYQQQgjRxR2NXT1r5fN5yc/Pweut7LA2ZGc78Pv9HXb+o5nD4SQ8PIqoqFgMw+jo5ggLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,448 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, self.state_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, self.OUTPUT_SIZE))\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(self.LAYER_SIZE, state_is_tuple = False)\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * self.LAYER_SIZE))\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" tensor_action, tensor_validation = tf.split(self.rnn[:,-1],2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, self.OUTPUT_SIZE)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" self.cost = tf.reduce_sum(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = self.LEARNING_RATE).minimize(self.cost)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
"\n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" Q = self.sess.run(self.logits, feed_dict={self.X:states, self.hidden_layer:init_values})\n",
|
||||
" Q_new = self.sess.run(self.logits, feed_dict={self.X:new_states, self.hidden_layer:init_values})\n",
|
||||
" replay_size = len(replay)\n",
|
||||
" X = np.empty((replay_size, 4, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.OUTPUT_SIZE))\n",
|
||||
" INIT_VAL = np.empty((replay_size, 2 * self.LAYER_SIZE))\n",
|
||||
" for i in range(replay_size):\n",
|
||||
" state_r, action_r, reward_r, new_state_r, dead_r, rnn_memory = replay[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action_r] = reward_r\n",
|
||||
" if not dead_r:\n",
|
||||
" target[action_r] += self.GAMMA * np.amax(Q_new[i])\n",
|
||||
" X[i] = state_r\n",
|
||||
" Y[i] = target\n",
|
||||
" INIT_VAL[i] = rnn_memory\n",
|
||||
" return X, Y, INIT_VAL\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action, last_state = self.sess.run([self.logits,self.last_state],\n",
|
||||
" feed_dict={self.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.logits,\n",
|
||||
" self.last_state],\n",
|
||||
" feed_dict={self.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" X, Y, INIT_VAL = self._construct_memories(replay)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.cost, self.optimizer], \n",
|
||||
" feed_dict={self.X: X, self.Y:Y,\n",
|
||||
" self.hidden_layer: INIT_VAL})\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" \n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2873435940>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:From <ipython-input-3-976c717fc00c>:35: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"epoch: 10, total rewards: 1303.755127.3, cost: 0.204159, total money: 2622.175109\n",
|
||||
"epoch: 20, total rewards: 1332.510133.3, cost: 2.512769, total money: 11332.510133\n",
|
||||
"epoch: 30, total rewards: 167.034789.3, cost: 0.204751, total money: 10167.034789\n",
|
||||
"epoch: 40, total rewards: 885.269897.3, cost: 0.095390, total money: 8848.889892\n",
|
||||
"epoch: 50, total rewards: 312.624996.3, cost: 0.415782, total money: 10312.624996\n",
|
||||
"epoch: 60, total rewards: 220.209960.3, cost: 0.119438, total money: 10220.209960\n",
|
||||
"epoch: 70, total rewards: 407.794859.3, cost: 0.983801, total money: 8417.984861\n",
|
||||
"epoch: 80, total rewards: 200.149718.3, cost: 0.235913, total money: 9226.819701\n",
|
||||
"epoch: 90, total rewards: 87.564821.3, cost: 0.034903, total money: 8097.894838\n",
|
||||
"epoch: 100, total rewards: 1056.600041.3, cost: 0.286240, total money: 11056.600041\n",
|
||||
"epoch: 110, total rewards: 537.204957.3, cost: 0.140037, total money: 7610.014955\n",
|
||||
"epoch: 120, total rewards: 263.944828.3, cost: 0.535866, total money: 9247.304813\n",
|
||||
"epoch: 130, total rewards: 387.030092.3, cost: 0.352989, total money: 8396.590090\n",
|
||||
"epoch: 140, total rewards: 207.069887.3, cost: 0.474047, total money: 10207.069887\n",
|
||||
"epoch: 150, total rewards: -119.230104.3, cost: 0.301262, total money: 9880.769896\n",
|
||||
"epoch: 160, total rewards: 21.299804.3, cost: 0.709494, total money: 10021.299804\n",
|
||||
"epoch: 170, total rewards: 241.145077.3, cost: 0.486697, total money: 10241.145077\n",
|
||||
"epoch: 180, total rewards: 5.329770.3, cost: 0.447255, total money: 7042.329770\n",
|
||||
"epoch: 190, total rewards: 126.395198.3, cost: 0.240739, total money: 9107.125178\n",
|
||||
"epoch: 200, total rewards: 91.499876.3, cost: 0.259028, total money: 8055.119871\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 53: buy 1 unit at price 805.020020, total balance 9194.979980\n",
|
||||
"day 54, sell 1 unit at price 819.309998, investment 1.775108 %, total balance 10014.289978,\n",
|
||||
"day 64: buy 1 unit at price 801.340027, total balance 9212.949951\n",
|
||||
"day 68, sell 1 unit at price 813.669983, investment 1.538667 %, total balance 10026.619934,\n",
|
||||
"day 79: buy 1 unit at price 823.210022, total balance 9203.409912\n",
|
||||
"day 82: buy 1 unit at price 829.080017, total balance 8374.329895\n",
|
||||
"day 84, sell 1 unit at price 831.909973, investment 1.056832 %, total balance 9206.239868,\n",
|
||||
"day 86, sell 1 unit at price 838.679993, investment 1.157907 %, total balance 10044.919861,\n",
|
||||
"day 111: buy 1 unit at price 823.559998, total balance 9221.359863\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 2.383555 %, total balance 10064.549865,\n",
|
||||
"day 167: buy 1 unit at price 911.710022, total balance 9152.839843\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment 0.754626 %, total balance 10071.429870,\n",
|
||||
"day 182: buy 1 unit at price 947.799988, total balance 9123.629882\n",
|
||||
"day 183, sell 1 unit at price 934.090027, investment -1.446504 %, total balance 10057.719909,\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 9127.219909\n",
|
||||
"day 187: buy 1 unit at price 930.390015, total balance 8196.829894\n",
|
||||
"day 188, sell 1 unit at price 923.650024, investment -0.736161 %, total balance 9120.479918,\n",
|
||||
"day 190, sell 1 unit at price 929.359985, investment -0.110709 %, total balance 10049.839903,\n",
|
||||
"day 206: buy 1 unit at price 921.289978, total balance 9128.549925\n",
|
||||
"day 207, sell 1 unit at price 929.570007, investment 0.898743 %, total balance 10058.119932,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4VFX6wPHvtPRe6FWRI6ACIiIiRcXey2Jvq+KuunZd29r7rrv+1oa9YceCirKoICACilQVDj0JkN7bzGRm7u+Pe2dIQhLSE5L38zw+knvPPffcyYHnvvOeYjMMAyGEEEIIIYQQXZe9oxsghBBCCCGEEKJtSeAnhBBCCCGEEF2cBH5CCCGEEEII0cVJ4CeEEEIIIYQQXZwEfkIIIYQQQgjRxUngJ4QQQgghhBBdnAR+QgjRSEqpN5VSj7RBvROVUrq16xUdSyl1uVLqx3rODVBKlSmlHO3dLiGEEN2Ts6MbIIQQrUUptR24Smv9XVuUbyta68WAau16refrCfitQz9prY+3ztmAh4ErgBhgFXCd1vr3eup6GDgTGAY8orV+oNq53sBLwGFAb2Cw1np7tfN9gReAiUCFdf2MaudPAx4HBgFrMX8nf1jnzgceBHoBHuAb4G9a6xKlVLhV71QgCdgC3KW1/qaRn8+bwA6t9b1tUb4hWut0zM+9QyilpgAztdb9Ouj+P1j3f7WBMqOA1zD73HrgSq316gbqOwLwWYd2aq1VtfN/A24BkoGNwE1a6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7XtBbUWKy1TrcCrUeB6kHui1ZdU60hkrXbbQBHa61/qOOxav/+l2Nm2u5QSj2NuVLjaZiZ0brKR2MGGLnWva6g5tzF1vQ1ZnbpIcyAPfg7/wozm3cJZiYUzECnDLOP/Akz6ClWSpWwu69kA8lKqXit9d6y2XvQWmcqpeYBTyul/mHdbzDQzxrKvDe1P8vafsDs2zcopWawO8M5v3ZB68uCccBCzDmH5wGTMP++g9nX71FKPYu5oM5UzMz7b9b1dsy/oy7AZvXlgNbaa52vt+8rpZIw50NuxeyH/8ZcKCpgXdtg37fKRGAODQ8GiNuAKcrcPmQC8N8GPichhGg0meMnhOhKHgfuVUoVKaVu01przCGMzwJ5mC/xpwVf6GqXB97GHNq3E3O44bIm3PsVzHl7azEXxfga8yXUb2XpbsAMDgsxM4tfNPMZD8CcF1eGufLjC1rrBXWUi8UMigoxn+dE4CStdb51/knMOUWrMVdcvBk4R2tdBGDNMZpRrb5XMIf6XQDcY/35kmrnK602gRmYVg/CTsB8MS7E3LbhRK11brXz/2e1QVtlrq52bjjwk1KqHDNo1cHzViB4DWagk6XMDdHLrGARpVR/zKGB9Q2pfQ1z/luRUupzq1+chrnwSR7myo+XVpszWLv8H5hD+5ZiBjIHszuwblXWfL5PMYOW96odL8UMJs7HzFhmYf5uw60ilwDbraDvL5gBPtYzvQ9stZ4nOJS4KS7FDJj+wPy9zcIcrtsY/wecq5QqVErtEdhYv4szrXsUYS7Icma1YOxuZa52CmbA9ghmAJ6H+SXHmdUyfG9jBsU/YK5s+l/MBW+Cv9dJmP31a8wMbyXm3+Wghvp+inVdOeYiMq9bCwEF7a3vgzm8891q2feXrHpzMYPGva4SK4QQjWEzjNaY2yyEEKI6K6s3Q2s9cK+FRZtQSl0MjNBa39XRbRFCCCE6mgR+QgjRCqwl2Y/GzBT0xJzTt0xrfVOLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,593 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, output_size, layer_size, learning_rate, name):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(layer_size, state_is_tuple = False)\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * layer_size))\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" tensor_action, tensor_validation = tf.split(self.rnn[:,-1],2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, output_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" self.cost = tf.reduce_sum(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.model = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE,\n",
|
||||
" 'real_model')\n",
|
||||
" self.model_negative = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE,\n",
|
||||
" 'negative_model')\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" self.trainable = tf.trainable_variables()\n",
|
||||
" \n",
|
||||
" def _assign(self, from_name, to_name):\n",
|
||||
" from_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=from_name)\n",
|
||||
" to_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=to_name)\n",
|
||||
" for i in range(len(from_w)):\n",
|
||||
" assign_op = to_w[i].assign(from_w[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
"\n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
"\n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action = self.get_predicted_action([state])\n",
|
||||
" return action\n",
|
||||
"\n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" Q = self.sess.run(self.model.logits, feed_dict={self.model.X:states, \n",
|
||||
" self.model.hidden_layer:init_values})\n",
|
||||
" Q_new = self.sess.run(self.model.logits, feed_dict={self.model.X:new_states, \n",
|
||||
" self.model.hidden_layer:init_values})\n",
|
||||
" Q_new_negative = self.sess.run(self.model_negative.logits, \n",
|
||||
" feed_dict={self.model_negative.X:new_states, \n",
|
||||
" self.model_negative.hidden_layer:init_values})\n",
|
||||
" replay_size = len(replay)\n",
|
||||
" X = np.empty((replay_size, 4, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.OUTPUT_SIZE))\n",
|
||||
" INIT_VAL = np.empty((replay_size, 2 * self.LAYER_SIZE))\n",
|
||||
" for i in range(replay_size):\n",
|
||||
" state_r, action_r, reward_r, new_state_r, dead_r, rnn_memory = replay[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action_r] = reward_r\n",
|
||||
" if not dead_r:\n",
|
||||
" target[action_r] += self.GAMMA * Q_new_negative[i, np.argmax(Q_new[i])]\n",
|
||||
" X[i] = state_r\n",
|
||||
" Y[i] = target\n",
|
||||
" INIT_VAL[i] = rnn_memory\n",
|
||||
" return X, Y, INIT_VAL\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action, last_state = self.sess.run([self.model.logits,self.model.last_state],\n",
|
||||
" feed_dict={self.model.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.model.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign('real_model', 'negative_model')\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.model.logits,\n",
|
||||
" self.model.last_state],\n",
|
||||
" feed_dict={self.model.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.model.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" \n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" X, Y, INIT_VAL = self._construct_memories(replay)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.model.cost, self.model.optimizer], \n",
|
||||
" feed_dict={self.model.X: X, self.model.Y:Y,\n",
|
||||
" self.model.hidden_layer: INIT_VAL})\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f39ffaed7b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:From <ipython-input-3-401815182242>:17: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f39ffaede80>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"epoch: 10, total rewards: 328.014401.3, cost: 0.233912, total money: 2446.714413\n",
|
||||
"epoch: 20, total rewards: 629.485052.3, cost: 0.592428, total money: 5723.605047\n",
|
||||
"epoch: 30, total rewards: 1222.065245.3, cost: 0.182284, total money: 7288.965209\n",
|
||||
"epoch: 40, total rewards: 719.309753.3, cost: 0.690094, total money: 3739.159728\n",
|
||||
"epoch: 50, total rewards: 328.994876.3, cost: 0.918951, total money: 2756.724856\n",
|
||||
"epoch: 60, total rewards: 1518.540281.3, cost: 0.226017, total money: 10545.210264\n",
|
||||
"epoch: 70, total rewards: 440.315127.3, cost: 0.145386, total money: 7494.335086\n",
|
||||
"epoch: 80, total rewards: 656.779966.3, cost: 0.113699, total money: 6666.949948\n",
|
||||
"epoch: 90, total rewards: 846.820129.3, cost: 0.444679, total money: 6860.080139\n",
|
||||
"epoch: 100, total rewards: 1044.679930.3, cost: 0.240218, total money: 9067.419920\n",
|
||||
"epoch: 110, total rewards: 207.934935.3, cost: 0.236219, total money: 10207.934935\n",
|
||||
"epoch: 120, total rewards: 6.745002.3, cost: 1.133358, total money: 10006.745002\n",
|
||||
"epoch: 130, total rewards: 586.910091.3, cost: 0.162622, total money: 4665.650081\n",
|
||||
"epoch: 140, total rewards: 1084.244877.3, cost: 0.630996, total money: 6178.484867\n",
|
||||
"epoch: 150, total rewards: 991.774842.3, cost: 1.439193, total money: 420.904786\n",
|
||||
"epoch: 160, total rewards: 714.735100.3, cost: 0.337296, total money: 5744.735038\n",
|
||||
"epoch: 170, total rewards: 1158.574706.3, cost: 0.186633, total money: 10185.244689\n",
|
||||
"epoch: 180, total rewards: 1120.314817.3, cost: 0.539594, total money: 7186.704770\n",
|
||||
"epoch: 190, total rewards: 230.760193.3, cost: 0.110742, total money: 4290.020202\n",
|
||||
"epoch: 200, total rewards: 218.420047.3, cost: 0.125164, total money: 10218.420047\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 9231.760010\n",
|
||||
"day 18, sell 1 unit at price 770.840027, investment 0.338441 %, total balance 10002.600037,\n",
|
||||
"day 20: buy 1 unit at price 747.919983, total balance 9254.680054\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 8504.180054\n",
|
||||
"day 23, sell 1 unit at price 759.109985, investment 1.496150 %, total balance 9263.290039,\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 2.756829 %, total balance 10034.480041,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 9245.210021\n",
|
||||
"day 28, sell 1 unit at price 796.099976, investment 0.865351 %, total balance 10041.309997,\n",
|
||||
"day 34: buy 1 unit at price 794.559998, total balance 9246.749999\n",
|
||||
"day 35, sell 1 unit at price 791.260010, investment -0.415323 %, total balance 10038.010009,\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 9248.100036\n",
|
||||
"day 38: buy 1 unit at price 785.049988, total balance 8463.050048\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 7691.230041\n",
|
||||
"day 41, sell 1 unit at price 786.140015, investment -0.477264 %, total balance 8477.370056,\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 7671.220032\n",
|
||||
"day 45, sell 1 unit at price 806.650024, investment 2.751422 %, total balance 8477.870056,\n",
|
||||
"day 48, sell 1 unit at price 806.359985, investment 4.475134 %, total balance 9284.230041,\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 0.214598 %, total balance 10092.110046,\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 9286.040039\n",
|
||||
"day 52, sell 1 unit at price 802.174988, investment -0.483211 %, total balance 10088.215027,\n",
|
||||
"day 57: buy 1 unit at price 832.150024, total balance 9256.065003\n",
|
||||
"day 58: buy 1 unit at price 823.309998, total balance 8432.755005\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 7637.059998\n",
|
||||
"day 63: buy 1 unit at price 801.489990, total balance 6835.570008\n",
|
||||
"day 64, sell 1 unit at price 801.340027, investment -3.702457 %, total balance 7636.910035,\n",
|
||||
"day 66, sell 1 unit at price 808.380005, investment -1.813411 %, total balance 8445.290040,\n",
|
||||
"day 67, sell 1 unit at price 809.559998, investment 1.742501 %, total balance 9254.850038,\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 8441.180055\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 2.365597 %, total balance 9261.630067,\n",
|
||||
"day 71: buy 1 unit at price 818.979980, total balance 8442.650087\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 7614.580080\n",
|
||||
"day 76, sell 1 unit at price 831.330017, investment 2.170417 %, total balance 8445.910097,\n",
|
||||
"day 77, sell 1 unit at price 828.640015, investment 1.179520 %, total balance 9274.550112,\n",
|
||||
"day 78: buy 1 unit at price 829.280029, total balance 8445.270083\n",
|
||||
"day 82: buy 1 unit at price 829.080017, total balance 7616.190066\n",
|
||||
"day 83: buy 1 unit at price 827.780029, total balance 6788.410037\n",
|
||||
"day 84: buy 1 unit at price 831.909973, total balance 5956.500064\n",
|
||||
"day 87: buy 1 unit at price 843.250000, total balance 5113.250064\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 4267.710086\n",
|
||||
"day 90: buy 1 unit at price 847.200012, total balance 3420.510074\n",
|
||||
"day 91, sell 1 unit at price 848.780029, investment 2.500999 %, total balance 4269.290103,\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance 3449.780093\n",
|
||||
"day 99, sell 1 unit at price 820.919983, investment -1.008109 %, total balance 4270.700076,\n",
|
||||
"day 100: buy 1 unit at price 831.409973, total balance 3439.290103\n",
|
||||
"day 103, sell 1 unit at price 838.549988, investment 1.142226 %, total balance 4277.840091,\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 3443.270084\n",
|
||||
"day 106: buy 1 unit at price 827.880005, total balance 2615.390079\n",
|
||||
"day 107, sell 1 unit at price 824.669983, investment -0.375709 %, total balance 3440.060062,\n",
|
||||
"day 108, sell 1 unit at price 824.729980, investment -0.863073 %, total balance 4264.790042,\n",
|
||||
"day 109, sell 1 unit at price 823.349976, investment -2.359920 %, total balance 5088.140018,\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 4263.820011\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -2.599520 %, total balance 5087.380009,\n",
|
||||
"day 114: buy 1 unit at price 838.210022, total balance 4249.169987\n",
|
||||
"day 115, sell 1 unit at price 841.650024, investment -0.655098 %, total balance 5090.820011,\n",
|
||||
"day 117, sell 1 unit at price 862.760010, investment 5.277544 %, total balance 5953.580021,\n",
|
||||
"day 118, sell 1 unit at price 872.299988, investment 4.918153 %, total balance 6825.880009,\n",
|
||||
"day 119: buy 1 unit at price 871.729980, total balance 5954.150029\n",
|
||||
"day 121, sell 1 unit at price 905.960022, investment 8.554107 %, total balance 6860.110051,\n",
|
||||
"day 122, sell 1 unit at price 912.570007, investment 10.229744 %, total balance 7772.680058,\n",
|
||||
"day 123: buy 1 unit at price 916.440002, total balance 6856.240056\n",
|
||||
"day 124, sell 1 unit at price 927.039978, investment 12.461177 %, total balance 7783.280034,\n",
|
||||
"day 125, sell 1 unit at price 931.659973, investment 11.148751 %, total balance 8714.940007,\n",
|
||||
"day 126, sell 1 unit at price 927.130005, investment 6.355182 %, total balance 9642.070012,\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 8713.289983\n",
|
||||
"day 131: buy 1 unit at price 932.219971, total balance 7781.070012\n",
|
||||
"day 134, sell 1 unit at price 919.619995, investment 0.346994 %, total balance 8700.690007,\n",
|
||||
"day 136: buy 1 unit at price 934.010010, total balance 7766.679997\n",
|
||||
"day 138, sell 1 unit at price 948.820007, investment 2.157667 %, total balance 8715.500004,\n",
|
||||
"day 139, sell 1 unit at price 954.960022, investment 2.439344 %, total balance 9670.460026,\n",
|
||||
"day 140, sell 1 unit at price 969.539978, investment 3.804024 %, total balance 10640.000004,\n",
|
||||
"day 141: buy 1 unit at price 971.469971, total balance 9668.530033\n",
|
||||
"day 142, sell 1 unit at price 975.880005, investment 0.453955 %, total balance 10644.410038,\n",
|
||||
"day 143: buy 1 unit at price 964.859985, total balance 9679.550053\n",
|
||||
"day 144, sell 1 unit at price 966.950012, investment 0.216615 %, total balance 10646.500065,\n",
|
||||
"day 145: buy 1 unit at price 975.599976, total balance 9670.900089\n",
|
||||
"day 146: buy 1 unit at price 983.679993, total balance 8687.220096\n",
|
||||
"day 148, sell 1 unit at price 980.940002, investment 0.547358 %, total balance 9668.160098,\n",
|
||||
"day 150, sell 1 unit at price 949.830017, investment -3.441157 %, total balance 10617.990115,\n",
|
||||
"day 152: buy 1 unit at price 953.400024, total balance 9664.590091\n",
|
||||
"day 154, sell 1 unit at price 942.309998, investment -1.163208 %, total balance 10606.900089,\n",
|
||||
"day 162: buy 1 unit at price 927.330017, total balance 9679.570072\n",
|
||||
"day 163: buy 1 unit at price 940.489990, total balance 8739.080082\n",
|
||||
"day 170: buy 1 unit at price 928.799988, total balance 7810.280094\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 2.138393 %, total balance 8757.440067,\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 7804.020084\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment 2.648623 %, total balance 8769.420108,\n",
|
||||
"day 177, sell 1 unit at price 970.890015, investment 4.531657 %, total balance 9740.310123,\n",
|
||||
"day 178: buy 1 unit at price 968.150024, total balance 8772.160099\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 2.045269 %, total balance 9745.080082,\n",
|
||||
"day 180, sell 1 unit at price 980.340027, investment 1.259103 %, total balance 10725.420109,\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 9794.920109\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 8864.090092\n",
|
||||
"day 187: buy 1 unit at price 930.390015, total balance 7933.700077\n",
|
||||
"day 188: buy 1 unit at price 923.650024, total balance 7010.050053\n",
|
||||
"day 191, sell 1 unit at price 926.789978, investment -0.398713 %, total balance 7936.840031,\n",
|
||||
"day 192, sell 1 unit at price 922.900024, investment -0.851927 %, total balance 8859.740055,\n",
|
||||
"day 195, sell 1 unit at price 922.669983, investment -0.829763 %, total balance 9782.410038,\n",
|
||||
"day 198: buy 1 unit at price 910.979980, total balance 8871.430058\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 7944.430058\n",
|
||||
"day 203, sell 1 unit at price 921.280029, investment -0.256590 %, total balance 8865.710087,\n",
|
||||
"day 205, sell 1 unit at price 913.809998, investment 0.310656 %, total balance 9779.520085,\n",
|
||||
"day 206, sell 1 unit at price 921.289978, investment -0.615968 %, total balance 10700.810063,\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance 9771.240056\n",
|
||||
"day 209, sell 1 unit at price 937.340027, investment 0.835872 %, total balance 10708.580083,\n",
|
||||
"day 212: buy 1 unit at price 935.950012, total balance 9772.630071\n",
|
||||
"day 213, sell 1 unit at price 926.500000, investment -1.009671 %, total balance 10699.130071,\n",
|
||||
"day 216: buy 1 unit at price 935.090027, total balance 9764.040044\n",
|
||||
"day 217: buy 1 unit at price 925.109985, total balance 8838.930059\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance 7923.930059\n",
|
||||
"day 221: buy 1 unit at price 931.580017, total balance 6992.350042\n",
|
||||
"day 222: buy 1 unit at price 932.450012, total balance 6059.900030\n",
|
||||
"day 223, sell 1 unit at price 928.530029, investment -0.701537 %, total balance 6988.430059,\n",
|
||||
"day 224, sell 1 unit at price 920.969971, investment -0.447516 %, total balance 7909.400030,\n",
|
||||
"day 225: buy 1 unit at price 924.859985, total balance 6984.540045\n",
|
||||
"day 226: buy 1 unit at price 944.489990, total balance 6040.050055\n",
|
||||
"day 227: buy 1 unit at price 949.500000, total balance 5090.550055\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 4.820763 %, total balance 6049.660040,\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 2.328303 %, total balance 7002.930060,\n",
|
||||
"day 230: buy 1 unit at price 957.789978, total balance 6045.140082\n",
|
||||
"day 235, sell 1 unit at price 972.599976, investment 4.305857 %, total balance 7017.740058,\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 6.962137 %, total balance 8006.990058,\n",
|
||||
"day 238: buy 1 unit at price 989.679993, total balance 7017.310065\n",
|
||||
"day 239, sell 1 unit at price 992.000000, investment 5.030229 %, total balance 8009.310065,\n",
|
||||
"day 240: buy 1 unit at price 992.179993, total balance 7017.130072\n",
|
||||
"day 241, sell 1 unit at price 992.809998, investment 4.561348 %, total balance 8009.940070,\n",
|
||||
"day 242, sell 1 unit at price 984.450012, investment 2.783495 %, total balance 8994.390082,\n",
|
||||
"day 244, sell 1 unit at price 968.450012, investment -2.145136 %, total balance 9962.840094,\n",
|
||||
"day 245, sell 1 unit at price 970.539978, investment -2.181057 %, total balance 10933.380072,\n",
|
||||
"day 248: buy 1 unit at price 1019.270020, total balance 9914.110052\n",
|
||||
"day 249, sell 1 unit at price 1017.109985, investment -0.211920 %, total balance 10931.220037,\n",
|
||||
"day 250: buy 1 unit at price 1016.640015, total balance 9914.580022\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd8VFX6+PHPvTOTHlIgofdyAkG6oiAKdgVBhZW1l3VZF13L2vvuWlbdn7t+LWtZdXVFERXFgnXtYlnBhggHaaElEEICKWTClN8f985kksxMJpWU5/168ZLceu7MDd7nPuc8x/D7/QghhBBCCCGE6LjMA90AIYQQQgghhBAtSwI/IYQQQgghhOjgJPATQgghhBBCiA5OAj8hhBBCCCGE6OAk8BNCCCGEEEKIDk4CPyGEEEIIIYTo4CTwE0KICJRSTyul7miB405RSunmPq5oe5RSfqXUkAjr3lZKndfabRJCCNE5OQ90A4QQorGUUpuAi7TW/22J7VuK1vozQLX0eZRS8cD/AacCLmAZcLHWepu9/mPgUMBj77J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Sws3qPaq3717uxaBWqerL5UXaxHyGEEKLTksBPCCEawR6DMw0r09UdK5vwldZaJlkWQgghRJsjXT2FEKJLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,568 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Actor:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, input_size))\n",
|
||||
" feed_actor = tf.layers.dense(self.X, size_layer, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_actor, output_size)\n",
|
||||
"\n",
|
||||
"class Critic:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer, learning_rate):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None, 1))\n",
|
||||
" feed_critic = tf.layers.dense(self.X, size_layer, activation = tf.nn.relu)\n",
|
||||
" feed_critic = tf.layers.dense(feed_critic, output_size, activation = tf.nn.relu) + self.Y\n",
|
||||
" feed_critic = tf.layers.dense(feed_critic, size_layer//2, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_critic, 1)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.REWARD - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.001\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
"\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.actor = Actor('actor-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.actor_target = Actor('actor-target', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.critic = Critic('critic-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.critic_target = Critic('critic-target', self.state_size, self.OUTPUT_SIZE, \n",
|
||||
" self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.grad_critic = tf.gradients(self.critic.logits, self.critic.Y)\n",
|
||||
" self.actor_critic_grad = tf.placeholder(tf.float32, [None, self.OUTPUT_SIZE])\n",
|
||||
" weights_actor = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='actor')\n",
|
||||
" self.grad_actor = tf.gradients(self.actor.logits, weights_actor, -self.actor_critic_grad)\n",
|
||||
" grads = zip(self.grad_actor, weights_actor)\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(self.LEARNING_RATE).apply_gradients(grads)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _assign(self, from_name, to_name):\n",
|
||||
" from_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=from_name)\n",
|
||||
" to_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=to_name)\n",
|
||||
" for i in range(len(from_w)):\n",
|
||||
" assign_op = to_w[i].assign(from_w[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" prediction = self.sess.run(self.actor.logits, feed_dict={self.actor.X:[state]})[0]\n",
|
||||
" action = np.argmax(prediction)\n",
|
||||
" return action\n",
|
||||
" \n",
|
||||
" def _construct_memories_and_train(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" Q = self.sess.run(self.actor.logits, feed_dict={self.actor.X: states})\n",
|
||||
" Q_target = self.sess.run(self.actor_target.logits, feed_dict={self.actor_target.X: states})\n",
|
||||
" grads = self.sess.run(self.grad_critic, feed_dict={self.critic.X:states, self.critic.Y:Q})[0]\n",
|
||||
" self.sess.run(self.optimizer, feed_dict={self.actor.X:states, self.actor_critic_grad:grads})\n",
|
||||
" \n",
|
||||
" rewards = np.array([a[2] for a in replay]).reshape((-1, 1))\n",
|
||||
" rewards_target = self.sess.run(self.critic_target.logits, \n",
|
||||
" feed_dict={self.critic_target.X:new_states,self.critic_target.Y:Q_target})\n",
|
||||
" for i in range(len(replay)):\n",
|
||||
" if not replay[0][-1]:\n",
|
||||
" rewards[i] += self.GAMMA * rewards_target[i]\n",
|
||||
" cost, _ = self.sess.run([self.critic.cost, self.critic.optimizer], \n",
|
||||
" feed_dict={self.critic.X:states, self.critic.Y:Q, self.critic.REWARD:rewards})\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign('actor-original', 'actor-target')\n",
|
||||
" self._assign('critic-original', 'critic-target')\n",
|
||||
" \n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" \n",
|
||||
" self._memorize(state, action, invest, next_state, starting_money < initial_money)\n",
|
||||
" state = next_state\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories_and_train(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch: 10, total rewards: 1539.185237.3, cost: 2.181347, total money: 1684.395196\n",
|
||||
"epoch: 20, total rewards: 1308.335026.3, cost: 658.992737, total money: 11308.335026\n",
|
||||
"epoch: 30, total rewards: 810.315002.3, cost: 19406.357422, total money: 5871.594971\n",
|
||||
"epoch: 40, total rewards: 380.889899.3, cost: 436790400.000000, total money: 7327.869879\n",
|
||||
"epoch: 50, total rewards: 676.170224.3, cost: 27570524160.000000, total money: 10676.170224\n",
|
||||
"epoch: 60, total rewards: 796.770199.3, cost: 935274741760.000000, total money: 10796.770199\n",
|
||||
"epoch: 70, total rewards: 47.440366.3, cost: 8344191369216.000000, total money: 7043.150388\n",
|
||||
"epoch: 80, total rewards: 450.169980.3, cost: 88121093914624.000000, total money: 6472.479916\n",
|
||||
"epoch: 90, total rewards: 443.664980.3, cost: 675454474256384.000000, total money: 9427.024965\n",
|
||||
"epoch: 100, total rewards: 350.460142.3, cost: 1153362061950976.000000, total money: 10350.460142\n",
|
||||
"epoch: 110, total rewards: 247.584961.3, cost: 6317238688677888.000000, total money: 9230.944946\n",
|
||||
"epoch: 120, total rewards: 138.510132.3, cost: 3956869119726321664.000000, total money: 8102.600097\n",
|
||||
"epoch: 130, total rewards: 410.025086.3, cost: 2205253088434978816.000000, total money: 10410.025086\n",
|
||||
"epoch: 140, total rewards: 513.814999.3, cost: 5849743807884558336.000000, total money: 9497.174984\n",
|
||||
"epoch: 150, total rewards: 876.734991.3, cost: 25442419893862400.000000, total money: 9860.094976\n",
|
||||
"epoch: 160, total rewards: 216.929627.3, cost: 73146239398445056.000000, total money: 9244.369629\n",
|
||||
"epoch: 170, total rewards: 26.000066.3, cost: 210379489706770432.000000, total money: 7992.250066\n",
|
||||
"epoch: 180, total rewards: 230.090269.3, cost: 378469838063927296.000000, total money: 8194.180234\n",
|
||||
"epoch: 190, total rewards: 31.099796.3, cost: 1333389845631860736.000000, total money: 6978.079776\n",
|
||||
"epoch: 200, total rewards: 158.599487.3, cost: 459357028892629008384.000000, total money: 10158.599487\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 9217.479980\n",
|
||||
"day 4: buy 1 unit at price 790.510010, total balance 8426.969970\n",
|
||||
"day 5, sell 1 unit at price 785.309998, investment 0.356538 %, total balance 9212.279968,\n",
|
||||
"day 6, sell 1 unit at price 762.559998, investment -3.535694 %, total balance 9974.839966,\n",
|
||||
"day 16: buy 1 unit at price 761.679993, total balance 9213.159973\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 8444.919983\n",
|
||||
"day 18, sell 1 unit at price 770.840027, investment 1.202609 %, total balance 9215.760010,\n",
|
||||
"day 19, sell 1 unit at price 758.039978, investment -1.327712 %, total balance 9973.799988,\n",
|
||||
"day 20: buy 1 unit at price 747.919983, total balance 9225.880005\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 8475.380005\n",
|
||||
"day 22, sell 1 unit at price 762.520020, investment 1.952085 %, total balance 9237.900025,\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 2.756829 %, total balance 10009.090027,\n",
|
||||
"day 25: buy 1 unit at price 776.419983, total balance 9232.670044\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 1.657607 %, total balance 10021.960022,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 9232.690002\n",
|
||||
"day 28: buy 1 unit at price 796.099976, total balance 8436.590026\n",
|
||||
"day 31, sell 1 unit at price 790.799988, investment 0.193846 %, total balance 9227.390014,\n",
|
||||
"day 32, sell 1 unit at price 794.200012, investment -0.238659 %, total balance 10021.590026,\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 9225.170043\n",
|
||||
"day 34, sell 1 unit at price 794.559998, investment -0.233543 %, total balance 10019.730041,\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 9229.820068\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment 0.207620 %, total balance 10021.370056,\n",
|
||||
"day 49: buy 1 unit at price 807.880005, total balance 9213.490051\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 8407.420044\n",
|
||||
"day 52, sell 1 unit at price 802.174988, investment -0.706171 %, total balance 9209.595032,\n",
|
||||
"day 53, sell 1 unit at price 805.020020, investment -0.130260 %, total balance 10014.615052,\n",
|
||||
"day 59: buy 1 unit at price 802.320007, total balance 9212.295045\n",
|
||||
"day 62, sell 1 unit at price 798.530029, investment -0.472377 %, total balance 10010.825074,\n",
|
||||
"day 63: buy 1 unit at price 801.489990, total balance 9209.335084\n",
|
||||
"day 64, sell 1 unit at price 801.340027, investment -0.018711 %, total balance 10010.675111,\n",
|
||||
"day 67: buy 1 unit at price 809.559998, total balance 9201.115113\n",
|
||||
"day 68, sell 1 unit at price 813.669983, investment 0.507681 %, total balance 10014.785096,\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 9186.715089\n",
|
||||
"day 74, sell 1 unit at price 831.659973, investment 0.433534 %, total balance 10018.375062,\n",
|
||||
"day 81: buy 1 unit at price 830.630005, total balance 9187.745057\n",
|
||||
"day 82, sell 1 unit at price 829.080017, investment -0.186604 %, total balance 10016.825074,\n",
|
||||
"day 87: buy 1 unit at price 843.250000, total balance 9173.575074\n",
|
||||
"day 88, sell 1 unit at price 845.539978, investment 0.271566 %, total balance 10019.115052,\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance 9166.995057\n",
|
||||
"day 93, sell 1 unit at price 848.400024, investment -0.436555 %, total balance 10015.395081,\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 9200.965088\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance 8381.455078\n",
|
||||
"day 99, sell 1 unit at price 820.919983, investment 0.796875 %, total balance 9202.375061,\n",
|
||||
"day 100: buy 1 unit at price 831.409973, total balance 8370.965088\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment 1.463068 %, total balance 9202.465088,\n",
|
||||
"day 102, sell 1 unit at price 829.559998, investment -0.222511 %, total balance 10032.025086,\n",
|
||||
"day 107: buy 1 unit at price 824.669983, total balance 9207.355103\n",
|
||||
"day 109, sell 1 unit at price 823.349976, investment -0.160065 %, total balance 10030.705079,\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 9206.385072\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -0.092198 %, total balance 10029.945070,\n",
|
||||
"day 115: buy 1 unit at price 841.650024, total balance 9188.295046\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 0.182971 %, total balance 10031.485048,\n",
|
||||
"day 119: buy 1 unit at price 871.729980, total balance 9159.755068\n",
|
||||
"day 120, sell 1 unit at price 874.250000, investment 0.289083 %, total balance 10034.005068,\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 9105.225039\n",
|
||||
"day 130, sell 1 unit at price 930.599976, investment 0.195950 %, total balance 10035.825015,\n",
|
||||
"day 137: buy 1 unit at price 941.859985, total balance 9093.965030\n",
|
||||
"day 138, sell 1 unit at price 948.820007, investment 0.738966 %, total balance 10042.785037,\n",
|
||||
"day 140: buy 1 unit at price 969.539978, total balance 9073.245059\n",
|
||||
"day 141, sell 1 unit at price 971.469971, investment 0.199063 %, total balance 10044.715030,\n",
|
||||
"day 144: buy 1 unit at price 966.950012, total balance 9077.765018\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 0.894562 %, total balance 10053.364994,\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance 9076.794987\n",
|
||||
"day 148, sell 1 unit at price 980.940002, investment 0.447484 %, total balance 10057.734989,\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance 9100.364994\n",
|
||||
"day 157, sell 1 unit at price 950.630005, investment -0.704011 %, total balance 10050.994999,\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 9085.404972\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment -1.379468 %, total balance 10037.674992,\n",
|
||||
"day 165: buy 1 unit at price 908.729980, total balance 9128.945012\n",
|
||||
"day 166: buy 1 unit at price 898.700012, total balance 8230.245000\n",
|
||||
"day 167, sell 1 unit at price 911.710022, investment 0.327935 %, total balance 9141.955022,\n",
|
||||
"day 168, sell 1 unit at price 906.690002, investment 0.889061 %, total balance 10048.645024,\n",
|
||||
"day 169: buy 1 unit at price 918.590027, total balance 9130.054997\n",
|
||||
"day 170, sell 1 unit at price 928.799988, investment 1.111482 %, total balance 10058.854985,\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 9128.764958\n",
|
||||
"day 172: buy 1 unit at price 943.830017, total balance 8184.934941\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 1.835300 %, total balance 9132.094914,\n",
|
||||
"day 174: buy 1 unit at price 955.989990, total balance 8176.104924\n",
|
||||
"day 176: buy 1 unit at price 965.400024, total balance 7210.704900\n",
|
||||
"day 177, sell 1 unit at price 970.890015, investment 2.867041 %, total balance 8181.594915,\n",
|
||||
"day 178, sell 1 unit at price 968.150024, investment 1.271983 %, total balance 9149.744939,\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 0.778947 %, total balance 10122.664922,\n",
|
||||
"day 182: buy 1 unit at price 947.799988, total balance 9174.864934\n",
|
||||
"day 183, sell 1 unit at price 934.090027, investment -1.446504 %, total balance 10108.954961,\n",
|
||||
"day 184: buy 1 unit at price 941.530029, total balance 9167.424932\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 8236.924932\n",
|
||||
"day 186, sell 1 unit at price 930.830017, investment -1.136449 %, total balance 9167.754949,\n",
|
||||
"day 187, sell 1 unit at price 930.390015, investment -0.011820 %, total balance 10098.144964,\n",
|
||||
"day 189: buy 1 unit at price 927.960022, total balance 9170.184942\n",
|
||||
"day 190, sell 1 unit at price 929.359985, investment 0.150865 %, total balance 10099.544927,\n",
|
||||
"day 192: buy 1 unit at price 922.900024, total balance 9176.644903\n",
|
||||
"day 193, sell 1 unit at price 907.239990, investment -1.696829 %, total balance 10083.884893,\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 9156.924871\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -1.723919 %, total balance 10067.904851,\n",
|
||||
"day 199: buy 1 unit at price 910.669983, total balance 9157.234868\n",
|
||||
"day 200, sell 1 unit at price 906.659973, investment -0.440336 %, total balance 10063.894841,\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 9136.894841\n",
|
||||
"day 203, sell 1 unit at price 921.280029, investment -0.617041 %, total balance 10058.174870,\n",
|
||||
"day 204: buy 1 unit at price 915.890015, total balance 9142.284855\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 8228.474857\n",
|
||||
"day 206, sell 1 unit at price 921.289978, investment 0.589586 %, total balance 9149.764835,\n",
|
||||
"day 207, sell 1 unit at price 929.570007, investment 1.724648 %, total balance 10079.334842,\n",
|
||||
"day 209: buy 1 unit at price 937.340027, total balance 9141.994815\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment -0.948430 %, total balance 10070.444827,\n",
|
||||
"day 211: buy 1 unit at price 927.809998, total balance 9142.634829\n",
|
||||
"day 212: buy 1 unit at price 935.950012, total balance 8206.684817\n",
|
||||
"day 213: buy 1 unit at price 926.500000, total balance 7280.184817\n",
|
||||
"day 214, sell 1 unit at price 929.080017, investment 0.136884 %, total balance 8209.264834,\n",
|
||||
"day 215: buy 1 unit at price 932.070007, total balance 7277.194827\n",
|
||||
"day 216: buy 1 unit at price 935.090027, total balance 6342.104800\n",
|
||||
"day 217, sell 1 unit at price 925.109985, investment -1.158184 %, total balance 7267.214785,\n",
|
||||
"day 218, sell 1 unit at price 920.289978, investment -0.670267 %, total balance 8187.504763,\n",
|
||||
"day 219, sell 1 unit at price 915.000000, investment -1.831408 %, total balance 9102.504763,\n",
|
||||
"day 221, sell 1 unit at price 931.580017, investment -0.375366 %, total balance 10034.084780,\n",
|
||||
"day 223: buy 1 unit at price 928.530029, total balance 9105.554751\n",
|
||||
"day 224: buy 1 unit at price 920.969971, total balance 8184.584780\n",
|
||||
"day 225, sell 1 unit at price 924.859985, investment -0.395253 %, total balance 9109.444765,\n",
|
||||
"day 226, sell 1 unit at price 944.489990, investment 2.553831 %, total balance 10053.934755,\n",
|
||||
"day 227: buy 1 unit at price 949.500000, total balance 9104.434755\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 1.012110 %, total balance 10063.544740,\n",
|
||||
"day 231: buy 1 unit at price 951.679993, total balance 9111.864747\n",
|
||||
"day 232: buy 1 unit at price 969.960022, total balance 8141.904725\n",
|
||||
"day 233: buy 1 unit at price 978.890015, total balance 7163.014710\n",
|
||||
"day 235, sell 1 unit at price 972.599976, investment 2.198216 %, total balance 8135.614686,\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 1.988739 %, total balance 9124.864686,\n",
|
||||
"day 238, sell 1 unit at price 989.679993, investment 1.102267 %, total balance 10114.544679,\n",
|
||||
"day 243: buy 1 unit at price 988.200012, total balance 9126.344667\n",
|
||||
"day 244: buy 1 unit at price 968.450012, total balance 8157.894655\n",
|
||||
"day 245: buy 1 unit at price 970.539978, total balance 7187.354677\n",
|
||||
"day 246, sell 1 unit at price 973.330017, investment -1.504756 %, total balance 8160.684694,\n",
|
||||
"day 247: buy 1 unit at price 972.559998, total balance 7188.124696\n",
|
||||
"day 248, sell 1 unit at price 1019.270020, investment 5.247561 %, total balance 8207.394716,\n",
|
||||
"day 249, sell 1 unit at price 1017.109985, investment 4.798361 %, total balance 9224.504701,\n",
|
||||
"day 250: buy 1 unit at price 1016.640015, total balance 8207.864686\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
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IwEyuj8yZW9qnKbnOwy4BnwmQ4MuzjKrC6zS3WWj8WtP9eu87NSqly+18gW2D/Hjymscp+DWAFE1VB+87ECi7KsAKlBznYda0P1tp8pVg3pUOBefYYTrCm1F+nlKqw2/sm9bMWj9vnugBropEq7HF39tizAv8/u/w+rbW/bYOwptdvQGudixXobbWvp/5YQelqrGtiHdZkQYuilL8Z69oos9u5JNy5WsnzWJntl+0xdX6/xpr4Y5WyJsh5n4NrTEa79kK/S02itX4dawbUF+3zfo+VgYrFh1gBToFSal8TThvtGgTruvhc158pFmhwPbfou4H1HazCGnv8mP3zTHvfWOAz+3peiTVhz9X2PhfW9VSI9fftl8ACrfVGe/8ArPGHZVjXn5eQTL79gOFkrPX+0FrnY41P/gHrb9btoa9dCNF9GT5fa88pIIQQnZ+d1XtEaz2k0cKiw7OzMt8B44OCTCGEEKLbkMBPCCEIjJOZjfX0vg/WmL5VWmtZCFkIIYQQnZ509RRCCIuBNfNkMVZXzw203rIAQgghhBDtSjJ+QgghhBBCCNHFScZPCCGEEEIIIbq4zricQzzW9OD5WFNRCyGEEEIIIUR34gD6AV9hLWXVqM4Y+E0DVrR3I4QQQgghhBCinR0LfBpLwc4Y+OUDFBdX4PV2rPGJvXunUFRU3nhBIZpIri3RluTLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,571 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Actor:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, input_size))\n",
|
||||
" feed_actor = tf.layers.dense(self.X, size_layer, activation = tf.nn.relu)\n",
|
||||
" tensor_action, tensor_validation = tf.split(feed_actor,2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, output_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + tf.subtract(feed_action,\n",
|
||||
" tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
"\n",
|
||||
"class Critic:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer, learning_rate):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None, 1))\n",
|
||||
" feed_critic = tf.layers.dense(self.X, size_layer, activation = tf.nn.relu)\n",
|
||||
" tensor_action, tensor_validation = tf.split(feed_critic,2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, output_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" feed_critic = feed_validation + tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" feed_critic = tf.nn.relu(feed_critic) + self.Y\n",
|
||||
" feed_critic = tf.layers.dense(feed_critic, size_layer//2, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_critic, 1)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.REWARD - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.001\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
"\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.actor = Actor('actor-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.actor_target = Actor('actor-target', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.critic = Critic('critic-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.critic_target = Critic('critic-target', self.state_size, self.OUTPUT_SIZE, \n",
|
||||
" self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.grad_critic = tf.gradients(self.critic.logits, self.critic.Y)\n",
|
||||
" self.actor_critic_grad = tf.placeholder(tf.float32, [None, self.OUTPUT_SIZE])\n",
|
||||
" weights_actor = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='actor')\n",
|
||||
" self.grad_actor = tf.gradients(self.actor.logits, weights_actor, -self.actor_critic_grad)\n",
|
||||
" grads = zip(self.grad_actor, weights_actor)\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(self.LEARNING_RATE).apply_gradients(grads)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _assign(self, from_name, to_name):\n",
|
||||
" from_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=from_name)\n",
|
||||
" to_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=to_name)\n",
|
||||
" for i in range(len(from_w)):\n",
|
||||
" assign_op = to_w[i].assign(from_w[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" prediction = self.sess.run(self.actor.logits, feed_dict={self.actor.X:[state]})[0]\n",
|
||||
" action = np.argmax(prediction)\n",
|
||||
" return action\n",
|
||||
" \n",
|
||||
" def _construct_memories_and_train(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" Q = self.sess.run(self.actor.logits, feed_dict={self.actor.X: states})\n",
|
||||
" Q_target = self.sess.run(self.actor_target.logits, feed_dict={self.actor_target.X: states})\n",
|
||||
" grads = self.sess.run(self.grad_critic, feed_dict={self.critic.X:states, self.critic.Y:Q})[0]\n",
|
||||
" self.sess.run(self.optimizer, feed_dict={self.actor.X:states, self.actor_critic_grad:grads})\n",
|
||||
" \n",
|
||||
" rewards = np.array([a[2] for a in replay]).reshape((-1, 1))\n",
|
||||
" rewards_target = self.sess.run(self.critic_target.logits, \n",
|
||||
" feed_dict={self.critic_target.X:new_states,self.critic_target.Y:Q_target})\n",
|
||||
" for i in range(len(replay)):\n",
|
||||
" if not replay[0][-1]:\n",
|
||||
" rewards[i] += self.GAMMA * rewards_target[i]\n",
|
||||
" cost, _ = self.sess.run([self.critic.cost, self.critic.optimizer], \n",
|
||||
" feed_dict={self.critic.X:states, self.critic.Y:Q, self.critic.REWARD:rewards})\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign('actor-original', 'actor-target')\n",
|
||||
" self._assign('critic-original', 'critic-target')\n",
|
||||
" \n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" \n",
|
||||
" self._memorize(state, action, invest, next_state, starting_money < initial_money)\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" state = next_state\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories_and_train(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:From <ipython-input-3-a50a3d0b4e36>:13: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"epoch: 10, total rewards: 707.200200.3, cost: 0.405626, total money: 9715.020207\n",
|
||||
"epoch: 20, total rewards: 1598.640143.3, cost: 30.734631, total money: 10581.530158\n",
|
||||
"epoch: 30, total rewards: 1271.279733.3, cost: 465.966644, total money: 10254.169748\n",
|
||||
"epoch: 40, total rewards: 611.054993.3, cost: 38.079464, total money: 2818.014953\n",
|
||||
"epoch: 50, total rewards: 1098.115172.3, cost: 71481.406250, total money: 1453.295102\n",
|
||||
"epoch: 60, total rewards: 575.370237.3, cost: 45955692.000000, total money: 9558.260252\n",
|
||||
"epoch: 70, total rewards: 1020.545110.3, cost: 244974075904.000000, total money: 10003.435125\n",
|
||||
"epoch: 80, total rewards: 824.555359.3, cost: 62751015698432.000000, total money: 4025.125366\n",
|
||||
"epoch: 90, total rewards: 182.215205.3, cost: 3949580517376.000000, total money: 10182.215205\n",
|
||||
"epoch: 100, total rewards: 861.215276.3, cost: 7310792458240.000000, total money: 7918.025274\n",
|
||||
"epoch: 110, total rewards: 68.690005.3, cost: 3184271573385216.000000, total money: 10068.690005\n",
|
||||
"epoch: 120, total rewards: 205.980352.3, cost: 224217291292672.000000, total money: 10205.980352\n",
|
||||
"epoch: 130, total rewards: 256.794983.3, cost: 363017178972160.000000, total money: 8275.784973\n",
|
||||
"epoch: 140, total rewards: 1586.720156.3, cost: 530019768074240.000000, total money: 11586.720156\n",
|
||||
"epoch: 150, total rewards: 824.849978.3, cost: 3151772092727296.000000, total money: 8881.750002\n",
|
||||
"epoch: 160, total rewards: 222.490291.3, cost: 6080023886823424.000000, total money: 9205.850276\n",
|
||||
"epoch: 170, total rewards: 37.630069.3, cost: 9586346603577344.000000, total money: 9020.990054\n",
|
||||
"epoch: 180, total rewards: 510.125126.3, cost: 22490134536519680.000000, total money: 5604.765140\n",
|
||||
"epoch: 190, total rewards: 639.559874.3, cost: 106721235701858304.000000, total money: 9669.019896\n",
|
||||
"epoch: 200, total rewards: 945.395079.3, cost: 31826508674760704.000000, total money: 384.445006\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 unit at price 768.700012, total balance 9231.299988\n",
|
||||
"day 2: buy 1 unit at price 762.020020, total balance 8469.279968\n",
|
||||
"day 3, sell 1 unit at price 782.520020, investment 1.797842 %, total balance 9251.799988,\n",
|
||||
"day 4, sell 1 unit at price 790.510010, investment 3.738746 %, total balance 10042.309998,\n",
|
||||
"day 5: buy 1 unit at price 785.309998, total balance 9257.000000\n",
|
||||
"day 8: buy 1 unit at price 736.080017, total balance 8520.919983\n",
|
||||
"day 11, sell 1 unit at price 771.229980, investment -1.792925 %, total balance 9292.149963,\n",
|
||||
"day 12: buy 1 unit at price 760.539978, total balance 8531.609985\n",
|
||||
"day 14: buy 1 unit at price 768.270020, total balance 7763.339965\n",
|
||||
"day 15: buy 1 unit at price 760.989990, total balance 7002.349975\n",
|
||||
"day 17, sell 1 unit at price 768.239990, investment 4.369087 %, total balance 7770.589965,\n",
|
||||
"day 20, sell 1 unit at price 747.919983, investment -1.659347 %, total balance 8518.509948,\n",
|
||||
"day 21, sell 1 unit at price 750.500000, investment -2.312991 %, total balance 9269.009948,\n",
|
||||
"day 22, sell 1 unit at price 762.520020, investment 0.201058 %, total balance 10031.529968,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 9242.259948\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 8445.839965\n",
|
||||
"day 34, sell 1 unit at price 794.559998, investment 0.670237 %, total balance 9240.399963,\n",
|
||||
"day 35, sell 1 unit at price 791.260010, investment -0.647896 %, total balance 10031.659973,\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 9241.750000\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment 0.207620 %, total balance 10033.299988,\n",
|
||||
"day 41: buy 1 unit at price 786.140015, total balance 9247.159973\n",
|
||||
"day 43: buy 1 unit at price 794.020020, total balance 8453.139953\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 7646.989929\n",
|
||||
"day 45, sell 1 unit at price 806.650024, investment 2.608951 %, total balance 8453.639953,\n",
|
||||
"day 47: buy 1 unit at price 807.909973, total balance 7645.729980\n",
|
||||
"day 48: buy 1 unit at price 806.359985, total balance 6839.369995\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 1.745546 %, total balance 7647.250000,\n",
|
||||
"day 55: buy 1 unit at price 823.869995, total balance 6823.380005\n",
|
||||
"day 56, sell 1 unit at price 835.669983, investment 3.661844 %, total balance 7659.049988,\n",
|
||||
"day 57, sell 1 unit at price 832.150024, investment 3.000341 %, total balance 8491.200012,\n",
|
||||
"day 58, sell 1 unit at price 823.309998, investment 2.102040 %, total balance 9314.510010,\n",
|
||||
"day 59: buy 1 unit at price 802.320007, total balance 8512.190003\n",
|
||||
"day 60, sell 1 unit at price 796.789978, investment -3.286928 %, total balance 9308.979981,\n",
|
||||
"day 62: buy 1 unit at price 798.530029, total balance 8510.449952\n",
|
||||
"day 63, sell 1 unit at price 801.489990, investment -0.103452 %, total balance 9311.939942,\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 2.593511 %, total balance 10131.179932,\n",
|
||||
"day 75: buy 1 unit at price 830.760010, total balance 9300.419922\n",
|
||||
"day 76, sell 1 unit at price 831.330017, investment 0.068613 %, total balance 10131.749939,\n",
|
||||
"day 77: buy 1 unit at price 828.640015, total balance 9303.109924\n",
|
||||
"day 78: buy 1 unit at price 829.280029, total balance 8473.829895\n",
|
||||
"day 80, sell 1 unit at price 835.239990, investment 0.796483 %, total balance 9309.069885,\n",
|
||||
"day 83: buy 1 unit at price 827.780029, total balance 8481.289856\n",
|
||||
"day 84, sell 1 unit at price 831.909973, investment 0.317136 %, total balance 9313.199829,\n",
|
||||
"day 86: buy 1 unit at price 838.679993, total balance 8474.519836\n",
|
||||
"day 87: buy 1 unit at price 843.250000, total balance 7631.269836\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 6785.729858\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 2.155158 %, total balance 7631.349853,\n",
|
||||
"day 91: buy 1 unit at price 848.780029, total balance 6782.569824\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance 5930.449829\n",
|
||||
"day 94, sell 1 unit at price 830.460022, investment -0.980108 %, total balance 6760.909851,\n",
|
||||
"day 95: buy 1 unit at price 829.590027, total balance 5931.319824\n",
|
||||
"day 96: buy 1 unit at price 817.580017, total balance 5113.739807\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 4299.309814\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance 3479.799804\n",
|
||||
"day 104, sell 1 unit at price 834.570007, investment -1.029350 %, total balance 4314.369811,\n",
|
||||
"day 105: buy 1 unit at price 831.409973, total balance 3482.959838\n",
|
||||
"day 106: buy 1 unit at price 827.880005, total balance 2655.079833\n",
|
||||
"day 107, sell 1 unit at price 824.669983, investment -2.468245 %, total balance 3479.749816,\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance 2655.019836\n",
|
||||
"day 110, sell 1 unit at price 824.320007, investment -2.881786 %, total balance 3479.339843,\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -3.351640 %, total balance 4302.899841,\n",
|
||||
"day 112: buy 1 unit at price 837.169983, total balance 3465.729858\n",
|
||||
"day 118, sell 1 unit at price 872.299988, investment 5.148321 %, total balance 4338.029846,\n",
|
||||
"day 121, sell 1 unit at price 905.960022, investment 10.809952 %, total balance 5243.989868,\n",
|
||||
"day 122, sell 1 unit at price 912.570007, investment 12.050147 %, total balance 6156.559875,\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 11.827798 %, total balance 7072.999877,\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance 6141.339904\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 5209.169921\n",
|
||||
"day 132, sell 1 unit at price 937.080017, investment 12.709740 %, total balance 6146.249938,\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 13.905396 %, total balance 7089.249938,\n",
|
||||
"day 134: buy 1 unit at price 919.619995, total balance 6169.629943\n",
|
||||
"day 136, sell 1 unit at price 934.010010, investment 13.250401 %, total balance 7103.639953,\n",
|
||||
"day 137, sell 1 unit at price 941.859985, investment 12.505226 %, total balance 8045.499938,\n",
|
||||
"day 139, sell 1 unit at price 954.960022, investment 2.500918 %, total balance 9000.459960,\n",
|
||||
"day 140: buy 1 unit at price 969.539978, total balance 8030.919982\n",
|
||||
"day 143: buy 1 unit at price 964.859985, total balance 7066.059997\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment 5.496850 %, total balance 8049.469970,\n",
|
||||
"day 150: buy 1 unit at price 949.830017, total balance 7099.639953\n",
|
||||
"day 152, sell 1 unit at price 953.400024, investment 3.673260 %, total balance 8053.039977,\n",
|
||||
"day 153, sell 1 unit at price 950.760010, investment -1.936998 %, total balance 9003.799987,\n",
|
||||
"day 154, sell 1 unit at price 942.309998, investment -2.337125 %, total balance 9946.109985,\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance 8988.739990\n",
|
||||
"day 157, sell 1 unit at price 950.630005, investment 0.084224 %, total balance 9939.369995,\n",
|
||||
"day 159, sell 1 unit at price 957.090027, investment -0.029243 %, total balance 10896.460022,\n",
|
||||
"day 161: buy 1 unit at price 952.270020, total balance 9944.190002\n",
|
||||
"day 163: buy 1 unit at price 940.489990, total balance 9003.700012\n",
|
||||
"day 167, sell 1 unit at price 911.710022, investment -4.259296 %, total balance 9915.410034,\n",
|
||||
"day 168: buy 1 unit at price 906.690002, total balance 9008.720032\n",
|
||||
"day 170, sell 1 unit at price 928.799988, investment -1.242969 %, total balance 9937.520020,\n",
|
||||
"day 171, sell 1 unit at price 930.090027, investment 2.580819 %, total balance 10867.610047,\n",
|
||||
"day 188: buy 1 unit at price 923.650024, total balance 9943.960023\n",
|
||||
"day 191, sell 1 unit at price 926.789978, investment 0.339951 %, total balance 10870.750001,\n",
|
||||
"day 196: buy 1 unit at price 922.219971, total balance 9948.530030\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 9021.570008\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -1.218797 %, total balance 9932.549988,\n",
|
||||
"day 199: buy 1 unit at price 910.669983, total balance 9021.880005\n",
|
||||
"day 202, sell 1 unit at price 927.000000, investment 0.004313 %, total balance 9948.880005,\n",
|
||||
"day 203: buy 1 unit at price 921.280029, total balance 9027.599976\n",
|
||||
"day 204, sell 1 unit at price 915.890015, investment 0.573208 %, total balance 9943.489991,\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 9029.679993\n",
|
||||
"day 206, sell 1 unit at price 921.289978, investment 0.001080 %, total balance 9950.969971,\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance 9021.399964\n",
|
||||
"day 208, sell 1 unit at price 939.330017, investment 2.792705 %, total balance 9960.729981,\n",
|
||||
"day 209: buy 1 unit at price 937.340027, total balance 9023.389954\n",
|
||||
"day 210: buy 1 unit at price 928.450012, total balance 8094.939942\n",
|
||||
"day 212: buy 1 unit at price 935.950012, total balance 7158.989930\n",
|
||||
"day 213, sell 1 unit at price 926.500000, investment -0.330261 %, total balance 8085.489930,\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance 7170.489930\n",
|
||||
"day 220, sell 1 unit at price 921.809998, investment -1.656819 %, total balance 8092.299928,\n",
|
||||
"day 221, sell 1 unit at price 931.580017, investment 0.337122 %, total balance 9023.879945,\n",
|
||||
"day 222, sell 1 unit at price 932.450012, investment -0.373952 %, total balance 9956.329957,\n",
|
||||
"day 223: buy 1 unit at price 928.530029, total balance 9027.799928\n",
|
||||
"day 226: buy 1 unit at price 944.489990, total balance 8083.309938\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 3.770492 %, total balance 9032.809938,\n",
|
||||
"day 228: buy 1 unit at price 959.109985, total balance 8073.699953\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 2.664426 %, total balance 9026.969973,\n",
|
||||
"day 231: buy 1 unit at price 951.679993, total balance 8075.289980\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 2.696697 %, total balance 9045.250002,\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 2.062332 %, total balance 10024.140017,\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment 2.660559 %, total balance 11001.140017,\n",
|
||||
"day 235: buy 1 unit at price 972.599976, total balance 10028.540041\n",
|
||||
"day 238: buy 1 unit at price 989.679993, total balance 9038.860048\n",
|
||||
"day 240, sell 1 unit at price 992.179993, investment 2.013162 %, total balance 10031.040041,\n",
|
||||
"day 241, sell 1 unit at price 992.809998, investment 0.316264 %, total balance 11023.850039,\n",
|
||||
"day 247: buy 1 unit at price 972.559998, total balance 10051.290041\n",
|
||||
"day 248, sell 1 unit at price 1019.270020, investment 4.802791 %, total balance 11070.560061,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XeYFdX5wPHvzL13+9K20KvAoUknqChij9LFiF1MdGNQ0cQSe4ktmpjkF3uMLWJBUVCDEuwoikoTRPYgfYFdWHbZXu+98/tj5l62792+7L6f5+GRnTlz5szduTjvvKcYlmUhhBBCCCGEEKLtMlu6AUIIIYQQQgghmpYEfkIIIYQQQ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w9ncigyNByfPAMOdalta1MVrrFOzf8+3YgVwKdjax1ucMrXUB9u9wlXP+46oo1hf7dxnIvBVi/64Dqr2nlFInqcprWM4CsijfFTXgAuwgKjCu9zzn+wH2RDsblVL52P8evEP5l0nXYn+PA5PwvIazPEMI9/adznXdiv39KaTy+NW/Ys9qG7ieh7ED/xTsGUNlWQchRLUMy2rKXjJCCNF2OG/7n9Fa9621sGh1lFIrsJcYkDFQQggh2h0Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,544 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Actor:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, input_size))\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * size_layer))\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X, cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" self.logits = tf.layers.dense(self.rnn[:,-1], output_size)\n",
|
||||
"\n",
|
||||
"class Critic:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer, learning_rate):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * size_layer))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None, 1))\n",
|
||||
" feed_critic = tf.layers.dense(self.X, size_layer, activation = tf.nn.relu)\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X, cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" feed_critic = tf.layers.dense(self.rnn[:,-1], output_size, activation = tf.nn.relu) + self.Y\n",
|
||||
" feed_critic = tf.layers.dense(feed_critic, size_layer//2, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_critic, 1)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.REWARD - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.001\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
"\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.actor = Actor('actor-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.actor_target = Actor('actor-target', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.critic = Critic('critic-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.critic_target = Critic('critic-target', self.state_size, self.OUTPUT_SIZE, \n",
|
||||
" self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.grad_critic = tf.gradients(self.critic.logits, self.critic.Y)\n",
|
||||
" self.actor_critic_grad = tf.placeholder(tf.float32, [None, self.OUTPUT_SIZE])\n",
|
||||
" weights_actor = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='actor')\n",
|
||||
" self.grad_actor = tf.gradients(self.actor.logits, weights_actor, -self.actor_critic_grad)\n",
|
||||
" grads = zip(self.grad_actor, weights_actor)\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(self.LEARNING_RATE).apply_gradients(grads)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _assign(self, from_name, to_name):\n",
|
||||
" from_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=from_name)\n",
|
||||
" to_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=to_name)\n",
|
||||
" for i in range(len(from_w)):\n",
|
||||
" assign_op = to_w[i].assign(from_w[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" prediction = self.sess.run(self.actor.logits, feed_dict={self.actor.X:[state]})[0]\n",
|
||||
" action = np.argmax(prediction)\n",
|
||||
" return action\n",
|
||||
" \n",
|
||||
" def _construct_memories_and_train(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" Q = self.sess.run(self.actor.logits, feed_dict={self.actor.X: states,\n",
|
||||
" self.actor.hidden_layer: init_values})\n",
|
||||
" Q_target = self.sess.run(self.actor_target.logits, feed_dict={self.actor_target.X: states,\n",
|
||||
" self.actor_target.hidden_layer: init_values})\n",
|
||||
" grads = self.sess.run(self.grad_critic, feed_dict={self.critic.X:states, self.critic.Y:Q,\n",
|
||||
" self.critic.hidden_layer: init_values})[0]\n",
|
||||
" self.sess.run(self.optimizer, feed_dict={self.actor.X:states, self.actor_critic_grad:grads,\n",
|
||||
" self.actor.hidden_layer: init_values})\n",
|
||||
" \n",
|
||||
" rewards = np.array([a[2] for a in replay]).reshape((-1, 1))\n",
|
||||
" rewards_target = self.sess.run(self.critic_target.logits, \n",
|
||||
" feed_dict={self.critic_target.X:new_states,self.critic_target.Y:Q_target,\n",
|
||||
" self.critic_target.hidden_layer: init_values})\n",
|
||||
" for i in range(len(replay)):\n",
|
||||
" if not replay[0][-2]:\n",
|
||||
" rewards[i] += self.GAMMA * rewards_target[i]\n",
|
||||
" cost, _ = self.sess.run([self.critic.cost, self.critic.optimizer], \n",
|
||||
" feed_dict={self.critic.X:states, self.critic.Y:Q, self.critic.REWARD:rewards,\n",
|
||||
" self.critic.hidden_layer: init_values})\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.actor.logits,\n",
|
||||
" self.actor.last_state],\n",
|
||||
" feed_dict={self.actor.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.actor.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign('actor-original', 'actor-target')\n",
|
||||
" self._assign('critic-original', 'critic-target')\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.actor.logits,\n",
|
||||
" self.actor.last_state],\n",
|
||||
" feed_dict={self.actor.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.actor.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories_and_train(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f46cd19b6d8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f46cd102ef0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f46ccc7ce10>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f46cc5685f8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"epoch: 10, total rewards: 1158.549991.3, cost: 0.046632, total money: 4247.099979\n",
|
||||
"epoch: 20, total rewards: 466.185119.3, cost: 0.035100, total money: 5537.135131\n",
|
||||
"epoch: 30, total rewards: 477.615173.3, cost: 0.330107, total money: 975.775206\n",
|
||||
"epoch: 40, total rewards: 1200.205012.3, cost: 0.215860, total money: 10180.934992\n",
|
||||
"epoch: 50, total rewards: 283.615237.3, cost: 0.116108, total money: 3314.845217\n",
|
||||
"epoch: 60, total rewards: 324.265078.3, cost: 0.435482, total money: 9334.585085\n",
|
||||
"epoch: 70, total rewards: 587.429873.3, cost: 0.749076, total money: 4785.129884\n",
|
||||
"epoch: 80, total rewards: 1248.729918.3, cost: 0.167420, total money: 663.739866\n",
|
||||
"epoch: 90, total rewards: 520.270204.3, cost: 0.006982, total money: 9503.630189\n",
|
||||
"epoch: 100, total rewards: 195.270142.3, cost: 0.153058, total money: 10195.270142\n",
|
||||
"epoch: 110, total rewards: 74.399840.3, cost: 0.350105, total money: 10074.399840\n",
|
||||
"epoch: 120, total rewards: 2842.805359.3, cost: 0.074852, total money: 7832.085327\n",
|
||||
"epoch: 130, total rewards: 509.049985.3, cost: 0.053447, total money: 8518.609983\n",
|
||||
"epoch: 140, total rewards: -2.900205.3, cost: 0.015182, total money: 8979.989810\n",
|
||||
"epoch: 150, total rewards: 93.080022.3, cost: 0.008775, total money: 10093.080022\n",
|
||||
"epoch: 160, total rewards: 89.794983.3, cost: 0.107893, total money: 10089.794983\n",
|
||||
"epoch: 170, total rewards: 222.045106.3, cost: 0.189179, total money: 10222.045106\n",
|
||||
"epoch: 180, total rewards: -57.619995.3, cost: 0.002425, total money: 8925.739990\n",
|
||||
"epoch: 190, total rewards: 21.009889.3, cost: 0.005919, total money: 10021.009889\n",
|
||||
"epoch: 200, total rewards: 201.354980.3, cost: 0.002352, total money: 10201.354980\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 unit at price 768.700012, total balance 9231.299988\n",
|
||||
"day 1, sell 1 unit at price 762.130005, investment -0.854691 %, total balance 9993.429993,\n",
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 9210.909973\n",
|
||||
"day 4, sell 1 unit at price 790.510010, investment 1.021059 %, total balance 10001.419983,\n",
|
||||
"day 22: buy 1 unit at price 762.520020, total balance 9238.899963\n",
|
||||
"day 23: buy 1 unit at price 759.109985, total balance 8479.789978\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 1.137017 %, total balance 9250.979980,\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 3.975708 %, total balance 10040.269958,\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 9249.469970\n",
|
||||
"day 32, sell 1 unit at price 794.200012, investment 0.429947 %, total balance 10043.669982,\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 9247.249999\n",
|
||||
"day 34, sell 1 unit at price 794.559998, investment -0.233543 %, total balance 10041.809997,\n",
|
||||
"day 39: buy 1 unit at price 782.789978, total balance 9259.020019\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 8487.200012\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 0.525051 %, total balance 9274.100036,\n",
|
||||
"day 45, sell 1 unit at price 806.650024, investment 4.512712 %, total balance 10080.750060,\n",
|
||||
"day 64: buy 1 unit at price 801.340027, total balance 9279.410033\n",
|
||||
"day 65, sell 1 unit at price 806.969971, investment 0.702566 %, total balance 10086.380004,\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 9272.710021\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 0.833265 %, total balance 10093.160033,\n",
|
||||
"day 103: buy 1 unit at price 838.549988, total balance 9254.610045\n",
|
||||
"day 104, sell 1 unit at price 834.570007, investment -0.474627 %, total balance 10089.180052,\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 9264.860045\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -0.092198 %, total balance 10088.420043,\n",
|
||||
"day 114: buy 1 unit at price 838.210022, total balance 9250.210021\n",
|
||||
"day 115, sell 1 unit at price 841.650024, investment 0.410399 %, total balance 10091.860045,\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 9159.690062\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 8230.910033\n",
|
||||
"day 131, sell 1 unit at price 932.219971, investment 0.005363 %, total balance 9163.130004,\n",
|
||||
"day 132, sell 1 unit at price 937.080017, investment 0.893644 %, total balance 10100.210021,\n",
|
||||
"day 144: buy 1 unit at price 966.950012, total balance 9133.260009\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 0.894562 %, total balance 10108.859985,\n",
|
||||
"day 148: buy 1 unit at price 980.940002, total balance 9127.919983\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment 0.251796 %, total balance 10111.329956,\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance 9168.429932\n",
|
||||
"day 153, sell 1 unit at price 950.760010, investment 0.833597 %, total balance 10119.189942,\n",
|
||||
"day 168: buy 1 unit at price 906.690002, total balance 9212.499940\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment 1.312469 %, total balance 10131.089967,\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 9200.999940\n",
|
||||
"day 172, sell 1 unit at price 943.830017, investment 1.477275 %, total balance 10144.829957,\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 9191.409974\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment 1.256533 %, total balance 10156.809998,\n",
|
||||
"day 178: buy 1 unit at price 968.150024, total balance 9188.659974\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 0.492688 %, total balance 10161.579957,\n",
|
||||
"day 192: buy 1 unit at price 922.900024, total balance 9238.679933\n",
|
||||
"day 193, sell 1 unit at price 907.239990, investment -1.696829 %, total balance 10145.919923,\n",
|
||||
"day 194: buy 1 unit at price 914.390015, total balance 9231.529908\n",
|
||||
"day 196: buy 1 unit at price 922.219971, total balance 8309.309937\n",
|
||||
"day 197, sell 1 unit at price 926.960022, investment 1.374688 %, total balance 9236.269959,\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -1.218797 %, total balance 10147.249939,\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance 9217.679932\n",
|
||||
"day 208: buy 1 unit at price 939.330017, total balance 8278.349915\n",
|
||||
"day 209, sell 1 unit at price 937.340027, investment 0.835872 %, total balance 9215.689942,\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment -1.158273 %, total balance 10144.139954,\n",
|
||||
"day 211: buy 1 unit at price 927.809998, total balance 9216.329956\n",
|
||||
"day 212, sell 1 unit at price 935.950012, investment 0.877336 %, total balance 10152.279968,\n",
|
||||
"day 214: buy 1 unit at price 929.080017, total balance 9223.199951\n",
|
||||
"day 215, sell 1 unit at price 932.070007, investment 0.321823 %, total balance 10155.269958,\n",
|
||||
"day 226: buy 1 unit at price 944.489990, total balance 9210.779968\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 0.530446 %, total balance 10160.279968,\n",
|
||||
"day 233: buy 1 unit at price 978.890015, total balance 9181.389953\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment -0.193077 %, total balance 10158.389953,\n",
|
||||
"day 243: buy 1 unit at price 988.200012, total balance 9170.189941\n",
|
||||
"day 244, sell 1 unit at price 968.450012, investment -1.998583 %, total balance 10138.639953,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpY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+11l2OyhEHvYLpnPmf//BLwW6XUB5iA8QYOVuhcjvn+LrBfrASD5M9qXMJVwBqt9Vr7xUik/T0agPleCCFEs0hXTyHE4ex+4A67a9mNduW/izEPWvsxAcGZWuuK2rbHPKSlYzI4PwJ1dd+qzTOYcXvrMVmdDwAv4LOzdAswwWEeJrP4fjOvcRjmQdqDGR/3hLbn96qjTaWY8V+323+/xF7XFxMwFQEbMA/a51TZ90pgEKZLZQbmIfyy4Eql1EZlVwu1x0+ehen+VmCf91KtdXDM4DRgg1KqGPO5fIApCBJ0M+b3sxvTFbBqO3rY2xdjCok8bxcfCbrObl8+phvs1dquHImZHuI1u03bMFnG0+zxaGCysitq/+gAM8bzRfv+mKvNfI+/x2RvM+3jXVDX9pi5CSPta1tJ9YIhre19zL2RZY8BBEBr/S5m+oLX7e6mP2CKEIHJ7j6DuSfTMS88/myvew4Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,555 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Actor:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, input_size))\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * size_layer))\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X, cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" tensor_action, tensor_validation = tf.split(self.rnn[:,-1],2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, output_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + tf.subtract(feed_action,\n",
|
||||
" tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
"\n",
|
||||
"class Critic:\n",
|
||||
" def __init__(self, name, input_size, output_size, size_layer, learning_rate):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * size_layer))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None, 1))\n",
|
||||
" feed_critic = tf.layers.dense(self.X, size_layer, activation = tf.nn.relu)\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X, cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" tensor_action, tensor_validation = tf.split(self.rnn[:,-1],2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, output_size)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" feed_critic = feed_validation + tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" feed_critic = tf.nn.relu(feed_critic) + self.Y\n",
|
||||
" feed_critic = tf.layers.dense(feed_critic, size_layer//2, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_critic, 1)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.REWARD - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.001\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
"\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.actor = Actor('actor-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.actor_target = Actor('actor-target', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE)\n",
|
||||
" self.critic = Critic('critic-original', self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.critic_target = Critic('critic-target', self.state_size, self.OUTPUT_SIZE, \n",
|
||||
" self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.grad_critic = tf.gradients(self.critic.logits, self.critic.Y)\n",
|
||||
" self.actor_critic_grad = tf.placeholder(tf.float32, [None, self.OUTPUT_SIZE])\n",
|
||||
" weights_actor = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='actor')\n",
|
||||
" self.grad_actor = tf.gradients(self.actor.logits, weights_actor, -self.actor_critic_grad)\n",
|
||||
" grads = zip(self.grad_actor, weights_actor)\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(self.LEARNING_RATE).apply_gradients(grads)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _assign(self, from_name, to_name):\n",
|
||||
" from_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=from_name)\n",
|
||||
" to_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=to_name)\n",
|
||||
" for i in range(len(from_w)):\n",
|
||||
" assign_op = to_w[i].assign(from_w[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" prediction = self.sess.run(self.actor.logits, feed_dict={self.actor.X:[state]})[0]\n",
|
||||
" action = np.argmax(prediction)\n",
|
||||
" return action\n",
|
||||
" \n",
|
||||
" def _construct_memories_and_train(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" Q = self.sess.run(self.actor.logits, feed_dict={self.actor.X: states,\n",
|
||||
" self.actor.hidden_layer: init_values})\n",
|
||||
" Q_target = self.sess.run(self.actor_target.logits, feed_dict={self.actor_target.X: states,\n",
|
||||
" self.actor_target.hidden_layer: init_values})\n",
|
||||
" grads = self.sess.run(self.grad_critic, feed_dict={self.critic.X:states, self.critic.Y:Q,\n",
|
||||
" self.critic.hidden_layer: init_values})[0]\n",
|
||||
" self.sess.run(self.optimizer, feed_dict={self.actor.X:states, self.actor_critic_grad:grads,\n",
|
||||
" self.actor.hidden_layer: init_values})\n",
|
||||
" \n",
|
||||
" rewards = np.array([a[2] for a in replay]).reshape((-1, 1))\n",
|
||||
" rewards_target = self.sess.run(self.critic_target.logits, \n",
|
||||
" feed_dict={self.critic_target.X:new_states,self.critic_target.Y:Q_target,\n",
|
||||
" self.critic_target.hidden_layer: init_values})\n",
|
||||
" for i in range(len(replay)):\n",
|
||||
" if not replay[0][-2]:\n",
|
||||
" rewards[i] += self.GAMMA * rewards_target[i]\n",
|
||||
" cost, _ = self.sess.run([self.critic.cost, self.critic.optimizer], \n",
|
||||
" feed_dict={self.critic.X:states, self.critic.Y:Q, self.critic.REWARD:rewards,\n",
|
||||
" self.critic.hidden_layer: init_values})\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.actor.logits,\n",
|
||||
" self.actor.last_state],\n",
|
||||
" feed_dict={self.actor.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.actor.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign('actor-original', 'actor-target')\n",
|
||||
" self._assign('critic-original', 'critic-target')\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.actor.logits,\n",
|
||||
" self.actor.last_state],\n",
|
||||
" feed_dict={self.actor.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.actor.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories_and_train(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f8ac3f890b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:From <ipython-input-3-b82c6dfdfdbf>:17: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f8a4343d2b0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f8a42e484e0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f8a42670c50>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"epoch: 10, total rewards: 1217.199710.3, cost: 0.428947, total money: 9258.459720\n",
|
||||
"epoch: 20, total rewards: 154.669988.3, cost: 0.205311, total money: 8167.020025\n",
|
||||
"epoch: 30, total rewards: 225.259892.3, cost: 0.080974, total money: 10225.259892\n",
|
||||
"epoch: 40, total rewards: 1857.994754.3, cost: 0.147440, total money: 7906.464724\n",
|
||||
"epoch: 50, total rewards: 864.365355.3, cost: 0.133079, total money: 3145.525327\n",
|
||||
"epoch: 60, total rewards: 252.179754.3, cost: 0.349886, total money: 10252.179754\n",
|
||||
"epoch: 70, total rewards: 2285.265256.3, cost: 0.122869, total money: 841.845272\n",
|
||||
"epoch: 80, total rewards: 2273.160095.3, cost: 0.042144, total money: 1779.580078\n",
|
||||
"epoch: 90, total rewards: 695.794921.3, cost: 0.652829, total money: 10695.794921\n",
|
||||
"epoch: 100, total rewards: -63.870359.3, cost: 0.026901, total money: 9936.129641\n",
|
||||
"epoch: 110, total rewards: 1660.049986.3, cost: 0.050525, total money: 236.529905\n",
|
||||
"epoch: 120, total rewards: 2137.930355.3, cost: 0.019048, total money: 635.270319\n",
|
||||
"epoch: 130, total rewards: 1263.700071.3, cost: 0.105621, total money: 836.610044\n",
|
||||
"epoch: 140, total rewards: 2582.234985.3, cost: 0.026973, total money: 1985.844970\n",
|
||||
"epoch: 150, total rewards: 1342.129822.3, cost: 0.045669, total money: 1933.479859\n",
|
||||
"epoch: 160, total rewards: 171.394838.3, cost: 0.186082, total money: 9198.064821\n",
|
||||
"epoch: 170, total rewards: 581.185307.3, cost: 0.243257, total money: 26.655338\n",
|
||||
"epoch: 180, total rewards: 109.954956.3, cost: 0.001933, total money: 9092.844971\n",
|
||||
"epoch: 190, total rewards: -85.549868.3, cost: 0.004746, total money: 9914.450132\n",
|
||||
"epoch: 200, total rewards: 94.994872.3, cost: 0.006849, total money: 10094.994872\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 unit at price 768.700012, total balance 9231.299988\n",
|
||||
"day 1, sell 1 unit at price 762.130005, investment -0.854691 %, total balance 9993.429993,\n",
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 9210.909973\n",
|
||||
"day 4, sell 1 unit at price 790.510010, investment 1.021059 %, total balance 10001.419983,\n",
|
||||
"day 22: buy 1 unit at price 762.520020, total balance 9238.899963\n",
|
||||
"day 23: buy 1 unit at price 759.109985, total balance 8479.789978\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 1.137017 %, total balance 9250.979980,\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 3.975708 %, total balance 10040.269958,\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 9249.469970\n",
|
||||
"day 32, sell 1 unit at price 794.200012, investment 0.429947 %, total balance 10043.669982,\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 9247.249999\n",
|
||||
"day 34, sell 1 unit at price 794.559998, investment -0.233543 %, total balance 10041.809997,\n",
|
||||
"day 39: buy 1 unit at price 782.789978, total balance 9259.020019\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 8487.200012\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 0.525051 %, total balance 9274.100036,\n",
|
||||
"day 45, sell 1 unit at price 806.650024, investment 4.512712 %, total balance 10080.750060,\n",
|
||||
"day 64: buy 1 unit at price 801.340027, total balance 9279.410033\n",
|
||||
"day 65, sell 1 unit at price 806.969971, investment 0.702566 %, total balance 10086.380004,\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 9272.710021\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 0.833265 %, total balance 10093.160033,\n",
|
||||
"day 103: buy 1 unit at price 838.549988, total balance 9254.610045\n",
|
||||
"day 104, sell 1 unit at price 834.570007, investment -0.474627 %, total balance 10089.180052,\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 9264.860045\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -0.092198 %, total balance 10088.420043,\n",
|
||||
"day 114: buy 1 unit at price 838.210022, total balance 9250.210021\n",
|
||||
"day 115, sell 1 unit at price 841.650024, investment 0.410399 %, total balance 10091.860045,\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 9159.690062\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 8230.910033\n",
|
||||
"day 131, sell 1 unit at price 932.219971, investment 0.005363 %, total balance 9163.130004,\n",
|
||||
"day 132, sell 1 unit at price 937.080017, investment 0.893644 %, total balance 10100.210021,\n",
|
||||
"day 144: buy 1 unit at price 966.950012, total balance 9133.260009\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 0.894562 %, total balance 10108.859985,\n",
|
||||
"day 148: buy 1 unit at price 980.940002, total balance 9127.919983\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment 0.251796 %, total balance 10111.329956,\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance 9168.429932\n",
|
||||
"day 153, sell 1 unit at price 950.760010, investment 0.833597 %, total balance 10119.189942,\n",
|
||||
"day 168: buy 1 unit at price 906.690002, total balance 9212.499940\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment 1.312469 %, total balance 10131.089967,\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 9200.999940\n",
|
||||
"day 172, sell 1 unit at price 943.830017, investment 1.477275 %, total balance 10144.829957,\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 9191.409974\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment 1.256533 %, total balance 10156.809998,\n",
|
||||
"day 178: buy 1 unit at price 968.150024, total balance 9188.659974\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 0.492688 %, total balance 10161.579957,\n",
|
||||
"day 192: buy 1 unit at price 922.900024, total balance 9238.679933\n",
|
||||
"day 193, sell 1 unit at price 907.239990, investment -1.696829 %, total balance 10145.919923,\n",
|
||||
"day 194: buy 1 unit at price 914.390015, total balance 9231.529908\n",
|
||||
"day 196: buy 1 unit at price 922.219971, total balance 8309.309937\n",
|
||||
"day 197, sell 1 unit at price 926.960022, investment 1.374688 %, total balance 9236.269959,\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -1.218797 %, total balance 10147.249939,\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance 9217.679932\n",
|
||||
"day 208: buy 1 unit at price 939.330017, total balance 8278.349915\n",
|
||||
"day 209, sell 1 unit at price 937.340027, investment 0.835872 %, total balance 9215.689942,\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment -1.158273 %, total balance 10144.139954,\n",
|
||||
"day 211: buy 1 unit at price 927.809998, total balance 9216.329956\n",
|
||||
"day 212, sell 1 unit at price 935.950012, investment 0.877336 %, total balance 10152.279968,\n",
|
||||
"day 214: buy 1 unit at price 929.080017, total balance 9223.199951\n",
|
||||
"day 215, sell 1 unit at price 932.070007, investment 0.321823 %, total balance 10155.269958,\n",
|
||||
"day 226: buy 1 unit at price 944.489990, total balance 9210.779968\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 0.530446 %, total balance 10160.279968,\n",
|
||||
"day 233: buy 1 unit at price 978.890015, total balance 9181.389953\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment -0.193077 %, total balance 10158.389953,\n",
|
||||
"day 243: buy 1 unit at price 988.200012, total balance 9170.189941\n",
|
||||
"day 244, sell 1 unit at price 968.450012, investment -1.998583 %, total balance 10138.639953,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd8ldX9wPHPc0f2Dgl7ChyWLKGCCOLWirgoWrdWsaX+0NZRV9VW62ptrXXgrAMHTrRFLS7UUlEBGSI57AAhISFk3ew7fn+c516SkL1Jvu/Xi5fkmee5eS4+3+d7zvdYgUAAIYQQQgghhBBdl6OjGyCEEEIIIYQQom1J4CeEEEIIIYQQXZwEfkIIIYQQQgjRxUngJ4QQQgghhBBdnAR+QgghhBBCCNHFSeAnhBBCCCGEEF2cBH5CCFEHpdQLSql72+C405VSurWPKzo3pdRypdRVday7TSn1bHu3SQghRPfh6ugGCCFEcymldgJXaa0/aYvt24rW+itAtfZxlVL3AGcDI4F7tdZ3V1l3PPAo0B/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x2RvM+3jXVDX9pi5CSPta1tJ9YIhre19zL2RZY8BBEBr/S5m+oLX7e6mP2CKEIHJ7j6DuSfTMS88/myvew4Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,587 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 500\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, self.state_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, self.state_size))\n",
|
||||
" self.ACTION = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.batch_size = tf.shape(self.ACTION)[0]\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('curiosity_model'):\n",
|
||||
" action = tf.reshape(self.ACTION, (-1,1))\n",
|
||||
" state_action = tf.concat([self.X, action], axis=1)\n",
|
||||
" save_state = tf.identity(self.Y)\n",
|
||||
" \n",
|
||||
" feed = tf.layers.dense(state_action, 32, activation=tf.nn.relu)\n",
|
||||
" self.curiosity_logits = tf.layers.dense(feed, self.state_size)\n",
|
||||
" self.curiosity_cost = tf.reduce_sum(tf.square(save_state - self.curiosity_logits), axis=1)\n",
|
||||
" \n",
|
||||
" self.curiosity_optimizer = tf.train.RMSPropOptimizer(self.LEARNING_RATE)\\\n",
|
||||
" .minimize(tf.reduce_mean(self.curiosity_cost))\n",
|
||||
" \n",
|
||||
" total_reward = tf.add(self.curiosity_cost, self.REWARD)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope(\"q_model\"):\n",
|
||||
" with tf.variable_scope(\"eval_net\"):\n",
|
||||
" x_action = tf.layers.dense(self.X, 128, tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(x_action, self.OUTPUT_SIZE)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope(\"target_net\"):\n",
|
||||
" y_action = tf.layers.dense(self.Y, 128, tf.nn.relu)\n",
|
||||
" y_q = tf.layers.dense(y_action, self.OUTPUT_SIZE)\n",
|
||||
" \n",
|
||||
" q_target = total_reward + self.GAMMA * tf.reduce_max(y_q, axis=1)\n",
|
||||
" action = tf.cast(self.ACTION, tf.int32)\n",
|
||||
" action_indices = tf.stack([tf.range(self.batch_size, dtype=tf.int32), action], axis=1)\n",
|
||||
" q = tf.gather_nd(params=self.logits, indices=action_indices)\n",
|
||||
" self.cost = tf.losses.mean_squared_error(labels=q_target, predictions=q)\n",
|
||||
" self.optimizer = tf.train.RMSPropOptimizer(self.LEARNING_RATE).minimize(\n",
|
||||
" self.cost, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, \"q_model/eval_net\"))\n",
|
||||
" \n",
|
||||
" t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_model/target_net')\n",
|
||||
" e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_model/eval_net')\n",
|
||||
" self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]\n",
|
||||
" \n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, done):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, done))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def predict(self, inputs):\n",
|
||||
" return self.sess.run(self.logits, feed_dict={self.X:inputs})\n",
|
||||
" \n",
|
||||
" def get_predicted_action(self, sequence):\n",
|
||||
" prediction = self.predict(np.array(sequence))[0]\n",
|
||||
" return np.argmax(prediction)\n",
|
||||
" \n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action = self.get_predicted_action([state])\n",
|
||||
" return action\n",
|
||||
" \n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" actions = np.array([a[1] for a in replay])\n",
|
||||
" rewards = np.array([a[2] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self.sess.run(self.target_replace_op)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.cost, self.optimizer], feed_dict = {\n",
|
||||
" self.X: states, self.Y: new_states, self.ACTION: actions, self.REWARD: rewards\n",
|
||||
" })\n",
|
||||
" \n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self.sess.run(self.curiosity_optimizer, feed_dict = {\n",
|
||||
" self.X: states, self.Y: new_states, self.ACTION: actions, self.REWARD: rewards\n",
|
||||
" })\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" \n",
|
||||
" self._memorize(state, action, invest, next_state, starting_money < initial_money)\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" state = next_state\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch: 10, total rewards: 2349.819823.3, cost: 69092.625000, total money: 12349.819823\n",
|
||||
"epoch: 20, total rewards: 648.444882.3, cost: 4775652.000000, total money: 6742.654903\n",
|
||||
"epoch: 30, total rewards: 1543.784977.3, cost: 26533.583984, total money: 7642.034916\n",
|
||||
"epoch: 40, total rewards: 1360.930418.3, cost: 871420.750000, total money: 695.580380\n",
|
||||
"epoch: 50, total rewards: 2233.069826.3, cost: 228718.296875, total money: 6354.209779\n",
|
||||
"epoch: 60, total rewards: 1573.414983.3, cost: 407432.843750, total money: 8625.614995\n",
|
||||
"epoch: 70, total rewards: -7.114931.3, cost: 32132.660156, total money: 5021.405088\n",
|
||||
"epoch: 80, total rewards: 798.045042.3, cost: 435778.562500, total money: 9780.935057\n",
|
||||
"epoch: 90, total rewards: 575.719967.3, cost: 72847.468750, total money: 9559.079952\n",
|
||||
"epoch: 100, total rewards: 338.655157.3, cost: 379671.968750, total money: 820.245184\n",
|
||||
"epoch: 110, total rewards: 277.220155.3, cost: 391019.375000, total money: 3452.330140\n",
|
||||
"epoch: 120, total rewards: 370.379826.3, cost: 429969.843750, total money: 7361.909793\n",
|
||||
"epoch: 130, total rewards: 441.860107.3, cost: 2082513.625000, total money: 2538.970093\n",
|
||||
"epoch: 140, total rewards: 709.099850.3, cost: 558315.562500, total money: 130.919796\n",
|
||||
"epoch: 150, total rewards: 159.675106.3, cost: 2904243.000000, total money: 481.725093\n",
|
||||
"epoch: 160, total rewards: 581.489981.3, cost: 1408646.250000, total money: 5631.309988\n",
|
||||
"epoch: 170, total rewards: 1768.579776.3, cost: 1693698.250000, total money: 15.189760\n",
|
||||
"epoch: 180, total rewards: 952.280210.3, cost: 1472623.250000, total money: 8990.750181\n",
|
||||
"epoch: 190, total rewards: 1418.655145.3, cost: 25627934.000000, total money: 3706.275139\n",
|
||||
"epoch: 200, total rewards: 272.595214.3, cost: 922414.500000, total money: 9255.485229\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 7: buy 1 unit at price 754.020020, total balance 9245.979980\n",
|
||||
"day 8: buy 1 unit at price 736.080017, total balance 8509.899963\n",
|
||||
"day 9: buy 1 unit at price 758.489990, total balance 7751.409973\n",
|
||||
"day 10: buy 1 unit at price 764.479980, total balance 6986.929993\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 6215.700013\n",
|
||||
"day 15, sell 1 unit at price 760.989990, investment 0.924375 %, total balance 6976.690003,\n",
|
||||
"day 18: buy 1 unit at price 770.840027, total balance 6205.849976\n",
|
||||
"day 19: buy 1 unit at price 758.039978, total balance 5447.809998\n",
|
||||
"day 22: buy 1 unit at price 762.520020, total balance 4685.289978\n",
|
||||
"day 23: buy 1 unit at price 759.109985, total balance 3926.179993\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 4.769860 %, total balance 4697.369995,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 3908.099975\n",
|
||||
"day 28, sell 1 unit at price 796.099976, investment 4.958534 %, total balance 4704.199951,\n",
|
||||
"day 29: buy 1 unit at price 797.070007, total balance 3907.129944\n",
|
||||
"day 30, sell 1 unit at price 797.849976, investment 4.365058 %, total balance 4704.979920,\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 3914.179932\n",
|
||||
"day 32, sell 1 unit at price 794.200012, investment 2.978363 %, total balance 4708.379944,\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 3911.959961\n",
|
||||
"day 35: buy 1 unit at price 791.260010, total balance 3120.699951\n",
|
||||
"day 36, sell 1 unit at price 789.909973, investment 2.473917 %, total balance 3910.609924,\n",
|
||||
"day 37: buy 1 unit at price 791.549988, total balance 3119.059936\n",
|
||||
"day 40, sell 1 unit at price 771.820007, investment 1.817850 %, total balance 3890.879943,\n",
|
||||
"day 41, sell 1 unit at price 786.140015, investment 3.097623 %, total balance 4677.019958,\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 3.660871 %, total balance 5463.919982,\n",
|
||||
"day 43, sell 1 unit at price 794.020020, investment 0.601822 %, total balance 6257.940002,\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 5451.789978\n",
|
||||
"day 45: buy 1 unit at price 806.650024, total balance 4645.139954\n",
|
||||
"day 47: buy 1 unit at price 807.909973, total balance 3837.229981\n",
|
||||
"day 48, sell 1 unit at price 806.359985, investment 1.165516 %, total balance 4643.589966,\n",
|
||||
"day 50, sell 1 unit at price 804.609985, investment 1.746332 %, total balance 5448.199951,\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 4642.129944\n",
|
||||
"day 52: buy 1 unit at price 802.174988, total balance 3839.954956\n",
|
||||
"day 54: buy 1 unit at price 819.309998, total balance 3020.644958\n",
|
||||
"day 55, sell 1 unit at price 823.869995, investment 3.446675 %, total balance 3844.514953,\n",
|
||||
"day 56: buy 1 unit at price 835.669983, total balance 3008.844970\n",
|
||||
"day 57: buy 1 unit at price 832.150024, total balance 2176.694946\n",
|
||||
"day 58: buy 1 unit at price 823.309998, total balance 1353.384948\n",
|
||||
"day 60, sell 1 unit at price 796.789978, investment 0.698881 %, total balance 2150.174926,\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 1354.479919\n",
|
||||
"day 62: buy 1 unit at price 798.530029, total balance 555.949890\n",
|
||||
"day 63, sell 1 unit at price 801.489990, investment 1.255764 %, total balance 1357.439880,\n",
|
||||
"day 64, sell 1 unit at price 801.340027, investment -0.596663 %, total balance 2158.779907,\n",
|
||||
"day 65, sell 1 unit at price 806.969971, investment 0.039664 %, total balance 2965.749878,\n",
|
||||
"day 66, sell 1 unit at price 808.380005, investment 0.058179 %, total balance 3774.129883,\n",
|
||||
"day 67: buy 1 unit at price 809.559998, total balance 2964.569885\n",
|
||||
"day 68, sell 1 unit at price 813.669983, investment 0.942843 %, total balance 3778.239868,\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 2.127342 %, total balance 4597.479858,\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 0.139143 %, total balance 5417.929870,\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 4589.859863\n",
|
||||
"day 75: buy 1 unit at price 830.760010, total balance 3759.099853\n",
|
||||
"day 76, sell 1 unit at price 831.330017, investment -0.519340 %, total balance 4590.429870,\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment -0.182662 %, total balance 5421.059875,\n",
|
||||
"day 82, sell 1 unit at price 829.080017, investment 0.700832 %, total balance 6250.139892,\n",
|
||||
"day 83, sell 1 unit at price 827.780029, investment 4.032327 %, total balance 7077.919921,\n",
|
||||
"day 84: buy 1 unit at price 831.909973, total balance 6246.009948\n",
|
||||
"day 87: buy 1 unit at price 843.250000, total balance 5402.759948\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 4557.219970\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 5.897081 %, total balance 5402.839965,\n",
|
||||
"day 92, sell 1 unit at price 852.119995, investment 5.257176 %, total balance 6254.959960,\n",
|
||||
"day 93: buy 1 unit at price 848.400024, total balance 5406.559936\n",
|
||||
"day 94: buy 1 unit at price 830.460022, total balance 4576.099914\n",
|
||||
"day 95, sell 1 unit at price 829.590027, investment 0.183562 %, total balance 5405.689941,\n",
|
||||
"day 99, sell 1 unit at price 820.919983, investment -1.184461 %, total balance 6226.609924,\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment -0.049281 %, total balance 7058.109924,\n",
|
||||
"day 103, sell 1 unit at price 838.549988, investment -0.557369 %, total balance 7896.659912,\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 7062.089905\n",
|
||||
"day 107, sell 1 unit at price 824.669983, investment -2.468245 %, total balance 7886.759888,\n",
|
||||
"day 109, sell 1 unit at price 823.349976, investment -2.952622 %, total balance 8710.109864,\n",
|
||||
"day 110, sell 1 unit at price 824.320007, investment -0.739351 %, total balance 9534.429871,\n",
|
||||
"day 111: buy 1 unit at price 823.559998, total balance 8710.869873\n",
|
||||
"day 112, sell 1 unit at price 837.169983, investment 0.311535 %, total balance 9548.039856,\n",
|
||||
"day 113, sell 1 unit at price 836.820007, investment 1.610084 %, total balance 10384.859863,\n",
|
||||
"day 122: buy 1 unit at price 912.570007, total balance 9472.289856\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 0.424077 %, total balance 10388.729858,\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 9456.559875\n",
|
||||
"day 129, sell 1 unit at price 928.780029, investment -0.363663 %, total balance 10385.339904,\n",
|
||||
"day 130: buy 1 unit at price 930.599976, total balance 9454.739928\n",
|
||||
"day 132: buy 1 unit at price 937.080017, total balance 8517.659911\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 1.332476 %, total balance 9460.659911,\n",
|
||||
"day 134, sell 1 unit at price 919.619995, investment -1.863237 %, total balance 10380.279906,\n",
|
||||
"day 140: buy 1 unit at price 969.539978, total balance 9410.739928\n",
|
||||
"day 141, sell 1 unit at price 971.469971, investment 0.199063 %, total balance 10382.209899,\n",
|
||||
"day 145: buy 1 unit at price 975.599976, total balance 9406.609923\n",
|
||||
"day 146: buy 1 unit at price 983.679993, total balance 8422.929930\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment 0.800533 %, total balance 9406.339903,\n",
|
||||
"day 150: buy 1 unit at price 949.830017, total balance 8456.509886\n",
|
||||
"day 151, sell 1 unit at price 942.900024, investment -4.145654 %, total balance 9399.409910,\n",
|
||||
"day 152, sell 1 unit at price 953.400024, investment 0.375857 %, total balance 10352.809934,\n",
|
||||
"day 159: buy 1 unit at price 957.090027, total balance 9395.719907\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 8430.129880\n",
|
||||
"day 161: buy 1 unit at price 952.270020, total balance 7477.859860\n",
|
||||
"day 164: buy 1 unit at price 917.789978, total balance 6560.069882\n",
|
||||
"day 167: buy 1 unit at price 911.710022, total balance 5648.359860\n",
|
||||
"day 170: buy 1 unit at price 928.799988, total balance 4719.559872\n",
|
||||
"day 171, sell 1 unit at price 930.090027, investment -2.821051 %, total balance 5649.649899,\n",
|
||||
"day 173: buy 1 unit at price 947.159973, total balance 4702.489926\n",
|
||||
"day 174: buy 1 unit at price 955.989990, total balance 3746.499936\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 2793.079953\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment -0.019677 %, total balance 3758.479977,\n",
|
||||
"day 177: buy 1 unit at price 970.890015, total balance 2787.589962\n",
|
||||
"day 178, sell 1 unit at price 968.150024, investment 1.667595 %, total balance 3755.739986,\n",
|
||||
"day 179: buy 1 unit at price 972.919983, total balance 2782.820003\n",
|
||||
"day 180: buy 1 unit at price 980.340027, total balance 1802.479976\n",
|
||||
"day 181: buy 1 unit at price 950.700012, total balance 851.779964\n",
|
||||
"day 183, sell 1 unit at price 934.090027, investment 1.776011 %, total balance 1785.869991,\n",
|
||||
"day 184, sell 1 unit at price 941.530029, investment 3.270778 %, total balance 2727.400020,\n",
|
||||
"day 185, sell 1 unit at price 930.500000, investment 0.183033 %, total balance 3657.900020,\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 2727.070003\n",
|
||||
"day 190: buy 1 unit at price 929.359985, total balance 1797.710018\n",
|
||||
"day 191: buy 1 unit at price 926.789978, total balance 870.920040\n",
|
||||
"day 192, sell 1 unit at price 922.900024, investment -2.561336 %, total balance 1793.820064,\n",
|
||||
"day 193, sell 1 unit at price 907.239990, investment -5.099426 %, total balance 2701.060054,\n",
|
||||
"day 194, sell 1 unit at price 914.390015, investment -4.093681 %, total balance 3615.450069,\n",
|
||||
"day 195: buy 1 unit at price 922.669983, total balance 2692.780086\n",
|
||||
"day 196, sell 1 unit at price 922.219971, investment -5.012931 %, total balance 3615.000057,\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 2688.040035\n",
|
||||
"day 198: buy 1 unit at price 910.979980, total balance 1777.060055\n",
|
||||
"day 199: buy 1 unit at price 910.669983, total balance 866.390072\n",
|
||||
"day 200, sell 1 unit at price 906.659973, investment -6.810427 %, total balance 1773.050045,\n",
|
||||
"day 201, sell 1 unit at price 924.690002, investment -5.676604 %, total balance 2697.740047,\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 1770.740047\n",
|
||||
"day 204, sell 1 unit at price 915.890015, investment -3.661512 %, total balance 2686.630062,\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment -0.255686 %, total balance 3615.080074,\n",
|
||||
"day 214: buy 1 unit at price 929.080017, total balance 2686.000057\n",
|
||||
"day 215: buy 1 unit at price 932.070007, total balance 1753.930050\n",
|
||||
"day 216: buy 1 unit at price 935.090027, total balance 818.840023\n",
|
||||
"day 220, sell 1 unit at price 921.809998, investment -0.812386 %, total balance 1740.650021,\n",
|
||||
"day 221, sell 1 unit at price 931.580017, investment 0.516842 %, total balance 2672.230038,\n",
|
||||
"day 222, sell 1 unit at price 932.450012, investment 1.059970 %, total balance 3604.680050,\n",
|
||||
"day 224, sell 1 unit at price 920.969971, investment -0.646204 %, total balance 4525.650021,\n",
|
||||
"day 226, sell 1 unit at price 944.489990, investment 3.678457 %, total balance 5470.140011,\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 4.263896 %, total balance 6419.640011,\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 3.463860 %, total balance 7378.749996,\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 2.603651 %, total balance 8332.020016,\n",
|
||||
"day 230, sell 1 unit at price 957.789978, investment 2.759446 %, total balance 9289.809994,\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 3.729052 %, total balance 10259.770016,\n",
|
||||
"day 235: buy 1 unit at price 972.599976, total balance 9287.170040\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 1.711909 %, total balance 10276.420040,\n",
|
||||
"day 237: buy 1 unit at price 987.830017, total balance 9288.590023\n",
|
||||
"day 238, sell 1 unit at price 989.679993, investment 0.187277 %, total balance 10278.270016,\n",
|
||||
"day 241: buy 1 unit at price 992.809998, total balance 9285.460018\n",
|
||||
"day 242, sell 1 unit at price 984.450012, investment -0.842053 %, total balance 10269.910030,\n",
|
||||
"day 245: buy 1 unit at price 970.539978, total balance 9299.370052\n",
|
||||
"day 246: buy 1 unit at price 973.330017, total balance 8326.040035\n",
|
||||
"day 247, sell 1 unit at price 972.559998, investment 0.208134 %, total balance 9298.600033,\n",
|
||||
"day 249: buy 1 unit at price 1017.109985, total balance 8281.490048\n",
|
||||
"day 250, sell 1 unit at price 1016.640015, investment 4.449672 %, total balance 9298.130063,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd81PX9wPHXfe8uO4SQIXsJfhiyoSwZ7gHiwIJ7VWOl1lHHz6qtWq1VW1vbWkfdFQeKAywO3APBCogokA8jEFYCIYSQAZfc+P3x/d5xudwll73ez8cjD8l3fr6XT+Ln/X1/hs3n8yGEEEIIIYQQov0yWroAQgghhBBCCCGalgR+QgghhBBCCNHOSeAnhBBCCCGEEO2cBH5CCCGEEEII0c5J4CeEEEIIIYQQ7ZwEfkIIIYQQQgjRzkngJ4QQFqXUC0qp+5vgulOUUrqxryvaFqXU5UqpryPs662UKlVK2Zu7XEIIIToGR0sXQAghoqWU2gZcpbX+uCmObypa668A1dT3UUq9D0wJ2hRj3l4Ps/b3BZ4HxgPbgev8n41S6ljgEWAMkKa1ttVyrxOAvwADgH3Ag1rrfwftzwD+DswAvMB7WuuLrH09gMetspYD92utnww690zgT0BfYC3mz3C9te984F6gK+AC3gd+rbU+aO0vDSlqPPC41vrXNT2Pde4L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ndWK/DDEJ6hT5LPezAzIalW9nBuqrx/lrrX2JO+BNONF1612HWdyAwC2csR36WNRkJfKm1Xml9/51S6lvMvyNrMOv75Egna63XYgaL/nt/Q1AmWmv9L8xZP/1jce8Cfgq+hvW37THM7sbpgF1rnauUysfsKi6EELWSwE8I0ZbswZrgwPI6cLtS6kTgS+AGzMbnNxGOT7b2F2J2tYoUOIXzOnCDFeCUAf8XtC8GsxFZALiVUqdjdnX8qdpVaqGUmglkY44VK8acvCTqiV2s7mJzgHOCt2utN1pByN1Kqbswg7ThmBkgf8My1noWlFJxgM/qPhjqe2CgMpd0+AzzM56JmRUBsyviX5RSl2Fm887BDJqXWdcejJnRcFllPQUYHPQMYzAb1F0wG8SLrUwgSqmLMLsfbrcCrT8CVYJipdQkzKxctRkelVI+4Hit9edhniu0vnyLmWm7TSn1CGbj/kxgXITjEzEDmALrXldQdSxXY3oPcwbKPwALggLT/wIPKqUu4UgmdCRmxmsLZpfP/2qti62st/+8PZjjyVK01qFdImtlTUCyFHhEmROnlGJmdXtaXZ9rE/pZRsUa/3Y8kYNCv5eB5UqpKZhje/+A+XKkxLqOA7NNZAfsVv13W0Hsd5h/Z0ZqrdcopUZhduP2zwo6H/NFzUmYvw/XYy5xssG69nDMANMA5mF2cX3B2heHOSZyHWYA/m/MLuVFIeW/Clht3d8BxFvjNnsDOXX4yIQQHZh09RRCtCV/Au6yupLdorXWwMWYY172YTbKz7S6W1U7HrN7VC5mRnA95gyD0Xoac9zeWszA5z3MiSw8VuPxeszgsAgzs7i4ns84EHOcWynmTI6Pa2vtriidjTljYrhzzsfsKuYfo3ie1rrA2tcHs8uaf4bTQ5jjkwBzjUB/FztrYowrMcc0HcTsYvcmVpcza9ziLMxxScWYa5SdpbXeZ13uVMzGahFmg/20oHKAuf7fAev+RZhjm/yGAN8oc1bFZdYxwfvhSMazSjc+pVQvzG50P4b5bMCLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,527 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 128\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, self.state_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, None, self.state_size))\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * self.LAYER_SIZE))\n",
|
||||
" self.ACTION = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.batch_size = tf.shape(self.ACTION)[0]\n",
|
||||
" self.seq_len = tf.shape(self.X)[1]\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('curiosity_model'):\n",
|
||||
" action = tf.reshape(self.ACTION, (-1,1,1))\n",
|
||||
" repeat_action = tf.tile(action, [1,self.seq_len,1])\n",
|
||||
" state_action = tf.concat([self.X, repeat_action], axis=-1)\n",
|
||||
" save_state = tf.identity(self.Y)\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(self.LAYER_SIZE, state_is_tuple = False)\n",
|
||||
" self.rnn,last_state = tf.nn.dynamic_rnn(inputs=state_action,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" self.curiosity_logits = tf.layers.dense(self.rnn[:,-1], self.state_size)\n",
|
||||
" self.curiosity_cost = tf.reduce_sum(tf.square(save_state[:,-1] - self.curiosity_logits), axis=1)\n",
|
||||
" \n",
|
||||
" self.curiosity_optimizer = tf.train.RMSPropOptimizer(self.LEARNING_RATE)\\\n",
|
||||
" .minimize(tf.reduce_mean(self.curiosity_cost))\n",
|
||||
" \n",
|
||||
" total_reward = tf.add(self.curiosity_cost, self.REWARD)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope(\"q_model\"):\n",
|
||||
" with tf.variable_scope(\"eval_net\"):\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(self.LAYER_SIZE, state_is_tuple = False)\n",
|
||||
" rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" self.logits = tf.layers.dense(rnn[:,-1], self.OUTPUT_SIZE)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope(\"target_net\"):\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(self.LAYER_SIZE, state_is_tuple = False)\n",
|
||||
" rnn,last_state = tf.nn.dynamic_rnn(inputs=self.Y,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" y_q = tf.layers.dense(rnn[:,-1], self.OUTPUT_SIZE)\n",
|
||||
" \n",
|
||||
" q_target = total_reward + self.GAMMA * tf.reduce_max(y_q, axis=1)\n",
|
||||
" action = tf.cast(self.ACTION, tf.int32)\n",
|
||||
" action_indices = tf.stack([tf.range(self.batch_size, dtype=tf.int32), action], axis=1)\n",
|
||||
" q = tf.gather_nd(params=self.logits, indices=action_indices)\n",
|
||||
" self.cost = tf.losses.mean_squared_error(labels=q_target, predictions=q)\n",
|
||||
" self.optimizer = tf.train.RMSPropOptimizer(self.LEARNING_RATE).minimize(\n",
|
||||
" self.cost, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, \"q_model/eval_net\"))\n",
|
||||
" \n",
|
||||
" t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_model/target_net')\n",
|
||||
" e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_model/eval_net')\n",
|
||||
" self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]\n",
|
||||
" \n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, done, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, done, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" actions = np.array([a[1] for a in replay])\n",
|
||||
" rewards = np.array([a[2] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self.sess.run(self.target_replace_op)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.cost, self.optimizer], feed_dict = {\n",
|
||||
" self.X: states, self.Y: new_states, self.ACTION: actions, self.REWARD: rewards,\n",
|
||||
" self.hidden_layer: init_values\n",
|
||||
" })\n",
|
||||
" \n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self.sess.run(self.curiosity_optimizer, feed_dict = {\n",
|
||||
" self.X: states, self.Y: new_states, self.ACTION: actions, self.REWARD: rewards,\n",
|
||||
" self.hidden_layer: init_values\n",
|
||||
" })\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.logits,\n",
|
||||
" self.last_state],\n",
|
||||
" feed_dict={self.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.logits,\n",
|
||||
" self.last_state],\n",
|
||||
" feed_dict={self.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7ff38845bba8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7ff2f112ed68>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7ff2f112eac8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"epoch: 10, total rewards: 685.860168.3, cost: 4139534.500000, total money: 977.580137\n",
|
||||
"epoch: 20, total rewards: 1724.255003.3, cost: 5132677.500000, total money: 5851.904966\n",
|
||||
"epoch: 30, total rewards: 493.970035.3, cost: 3979546.750000, total money: 8528.600039\n",
|
||||
"epoch: 40, total rewards: 1580.255128.3, cost: 5099559.000000, total money: 4018.855103\n",
|
||||
"epoch: 50, total rewards: 1467.990231.3, cost: 4410721.500000, total money: 8490.720211\n",
|
||||
"epoch: 60, total rewards: 1285.420161.3, cost: 3993190.000000, total money: 2688.440118\n",
|
||||
"epoch: 70, total rewards: 391.130068.3, cost: 3420379.000000, total money: 6491.710085\n",
|
||||
"epoch: 80, total rewards: 1276.110108.3, cost: 3443612.750000, total money: 3698.110047\n",
|
||||
"epoch: 90, total rewards: 672.475340.3, cost: 2882908.000000, total money: 208.605285\n",
|
||||
"epoch: 100, total rewards: 706.604982.3, cost: 3108476.500000, total money: 1169.724916\n",
|
||||
"epoch: 110, total rewards: 979.940367.3, cost: 2024909.750000, total money: 3200.720335\n",
|
||||
"epoch: 120, total rewards: 853.199893.3, cost: 4572564.500000, total money: 6070.309879\n",
|
||||
"epoch: 130, total rewards: 1339.975223.3, cost: 3904469.500000, total money: 7475.465274\n",
|
||||
"epoch: 140, total rewards: 1136.924864.3, cost: 4352429.000000, total money: 4448.164854\n",
|
||||
"epoch: 150, total rewards: 1499.745116.3, cost: 2398584.500000, total money: 3999.355042\n",
|
||||
"epoch: 160, total rewards: 481.755190.3, cost: 3168836.250000, total money: 7573.215212\n",
|
||||
"epoch: 170, total rewards: 1733.610290.3, cost: 1907320.875000, total money: 6940.950254\n",
|
||||
"epoch: 180, total rewards: 390.074828.3, cost: 2862924.000000, total money: 5516.364805\n",
|
||||
"epoch: 190, total rewards: 714.815121.3, cost: 2666878.750000, total money: 9726.615109\n",
|
||||
"epoch: 200, total rewards: 1474.129822.3, cost: 3016419.000000, total money: 1901.589906\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 unit at price 768.700012, total balance 9231.299988\n",
|
||||
"day 1, sell 1 unit at price 762.130005, investment -0.854691 %, total balance 9993.429993,\n",
|
||||
"day 4: buy 1 unit at price 790.510010, total balance 9202.919983\n",
|
||||
"day 5: buy 1 unit at price 785.309998, total balance 8417.609985\n",
|
||||
"day 8: buy 1 unit at price 736.080017, total balance 7681.529968\n",
|
||||
"day 9: buy 1 unit at price 758.489990, total balance 6923.039978\n",
|
||||
"day 11, sell 1 unit at price 771.229980, investment -2.438936 %, total balance 7694.269958,\n",
|
||||
"day 13: buy 1 unit at price 769.200012, total balance 6925.069946\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 6156.829956\n",
|
||||
"day 19, sell 1 unit at price 758.039978, investment -3.472517 %, total balance 6914.869934,\n",
|
||||
"day 25, sell 1 unit at price 776.419983, investment 5.480378 %, total balance 7691.289917,\n",
|
||||
"day 26: buy 1 unit at price 789.289978, total balance 6901.999939\n",
|
||||
"day 28: buy 1 unit at price 796.099976, total balance 6105.899963\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 5315.099975\n",
|
||||
"day 40, sell 1 unit at price 771.820007, investment 1.757441 %, total balance 6086.919982,\n",
|
||||
"day 46, sell 1 unit at price 804.789978, investment 4.626881 %, total balance 6891.709960,\n",
|
||||
"day 47, sell 1 unit at price 807.909973, investment 5.163749 %, total balance 7699.619933,\n",
|
||||
"day 50: buy 1 unit at price 804.609985, total balance 6895.009948\n",
|
||||
"day 57: buy 1 unit at price 832.150024, total balance 6062.859924\n",
|
||||
"day 58, sell 1 unit at price 823.309998, investment 4.310205 %, total balance 6886.169922,\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 6090.474915\n",
|
||||
"day 62: buy 1 unit at price 798.530029, total balance 5291.944886\n",
|
||||
"day 70: buy 1 unit at price 820.450012, total balance 4471.494874\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 3643.424867\n",
|
||||
"day 76: buy 1 unit at price 831.330017, total balance 2812.094850\n",
|
||||
"day 85: buy 1 unit at price 835.369995, total balance 1976.724855\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 6.220327 %, total balance 2822.344850,\n",
|
||||
"day 91: buy 1 unit at price 848.780029, total balance 1973.564821\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance 1154.054811\n",
|
||||
"day 100: buy 1 unit at price 831.409973, total balance 322.644838\n",
|
||||
"day 102, sell 1 unit at price 829.559998, investment 4.901367 %, total balance 1152.204836,\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment 2.355180 %, total balance 1975.764834,\n",
|
||||
"day 113: buy 1 unit at price 836.820007, total balance 1138.944827\n",
|
||||
"day 114, sell 1 unit at price 838.210022, investment 0.728234 %, total balance 1977.154849,\n",
|
||||
"day 117: buy 1 unit at price 862.760010, total balance 1114.394839\n",
|
||||
"day 118: buy 1 unit at price 872.299988, total balance 242.094851\n",
|
||||
"day 132, sell 1 unit at price 937.080017, investment 17.768744 %, total balance 1179.174868,\n",
|
||||
"day 138: buy 1 unit at price 948.820007, total balance 230.354861\n",
|
||||
"day 139, sell 1 unit at price 954.960022, investment 19.589745 %, total balance 1185.314883,\n",
|
||||
"day 140: buy 1 unit at price 969.539978, total balance 215.774905\n",
|
||||
"day 154, sell 1 unit at price 942.309998, investment 14.852823 %, total balance 1158.084903,\n",
|
||||
"day 158, sell 1 unit at price 959.450012, investment 15.865809 %, total balance 2117.534915,\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 1151.944888\n",
|
||||
"day 168: buy 1 unit at price 906.690002, total balance 245.254886\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment 10.496434 %, total balance 1163.844913,\n",
|
||||
"day 176: buy 1 unit at price 965.400024, total balance 198.444889\n",
|
||||
"day 189, sell 1 unit at price 927.960022, investment 11.083715 %, total balance 1126.404911,\n",
|
||||
"day 191: buy 1 unit at price 926.789978, total balance 199.614933\n",
|
||||
"day 195, sell 1 unit at price 922.669983, investment 8.705430 %, total balance 1122.284916,\n",
|
||||
"day 200, sell 1 unit at price 906.659973, investment 10.634399 %, total balance 2028.944889,\n",
|
||||
"day 201: buy 1 unit at price 924.690002, total balance 1104.254887\n",
|
||||
"day 202, sell 1 unit at price 927.000000, investment 11.497339 %, total balance 2031.254887,\n",
|
||||
"day 206: buy 1 unit at price 921.289978, total balance 1109.964909\n",
|
||||
"day 211: buy 1 unit at price 927.809998, total balance 182.154911\n",
|
||||
"day 220, sell 1 unit at price 921.809998, investment 10.156305 %, total balance 1103.964909,\n",
|
||||
"day 226, sell 1 unit at price 944.489990, investment 9.473084 %, total balance 2048.454899,\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 9.951851 %, total balance 3007.564884,\n",
|
||||
"day 230: buy 1 unit at price 957.789978, total balance 2049.774906\n",
|
||||
"day 231, sell 1 unit at price 951.679993, investment 0.301426 %, total balance 3001.454899,\n",
|
||||
"day 234: buy 1 unit at price 977.000000, total balance 2024.454899\n",
|
||||
"day 237: buy 1 unit at price 987.830017, total balance 1036.624882\n",
|
||||
"day 238, sell 1 unit at price 989.679993, investment 2.077275 %, total balance 2026.304875,\n",
|
||||
"day 240: buy 1 unit at price 992.179993, total balance 1034.124882\n",
|
||||
"day 241: buy 1 unit at price 992.809998, total balance 41.314884\n",
|
||||
"day 242, sell 1 unit at price 984.450012, investment 1.953208 %, total balance 1025.764896,\n",
|
||||
"day 248, sell 1 unit at price 1019.270020, investment 12.416594 %, total balance 2045.034916,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metad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XSmVp5S6QWutMQeReBwzq3Aa5mAuruq2B17DbI6XijkSYl19mCp6HrNP1iZgA+ZAMm7A48v4XI0ZHOZiZhaXNvIcB2M2OyvC7Iv0lNb6mwbsfzrmQ2Z1+9yEeZ1SMPsNVXwgHosZMBf7fm7GPCe01iVa63T/f766lWmt/QHT0UqpihmV8b79CzH/BufryiOnXuUrI813jm8BL9VVD5+FmA/rmZiZrYqjsr6GGaB+i9kX8H/AX7TW27TW7irnkAN4fZ89ypzbL46a7wn/CJDZSqn1vt/PxQyA0zBHqbxTH5wzstL2zXyP1MdbmIFvYI5DX1+8UzED5t2Y98ILmAEHmIMNbfH9Lf8LnKO1LtXmiJT3Aat936XJjajPzZjB9xqlVAHmPV7fPnwvAsN9x17cgGM26rtQzf18jq/uhZj32EPaN1qp1jqvyn3lAgq01v6m4y35XbgdM5N6C+a/g6W+ZRX9G7PvpP98HsDsL5gCfKxlWgchhI/FMJraukMIITonX8bmGa11/7aui2g6X1+wK7XW59a5sRBCCNHJSOAnhBA+vmZjMzGzft0x+2ut0Vpf26YVE0IIIYRoImnqKYQQB1kw503LxWzquZXK85AJIYQQQnRIkvETQgghhBBCiE5OMn5CCCGEEEII0cl1xOkcgjGHWt6POQS6EEIIIYQQQnQlNswRi3/BnMaqTh0x8JsIrGrrSgghhBBCCCFEGzsa+L4Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,866 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>signal</th>\n",
|
||||
" <th>short_ma</th>\n",
|
||||
" <th>long_ma</th>\n",
|
||||
" <th>positions</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>765.415008</td>\n",
|
||||
" <td>765.415008</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>764.283346</td>\n",
|
||||
" <td>764.283346</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>768.842514</td>\n",
|
||||
" <td>768.842514</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>773.176013</td>\n",
|
||||
" <td>773.176013</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>5</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>775.198344</td>\n",
|
||||
" <td>775.198344</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>6</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>774.175008</td>\n",
|
||||
" <td>773.392866</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>7</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>772.823344</td>\n",
|
||||
" <td>770.971260</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>8</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>768.500010</td>\n",
|
||||
" <td>767.094456</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>9</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>764.495005</td>\n",
|
||||
" <td>766.234009</td>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>10</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>760.156667</td>\n",
|
||||
" <td>766.074552</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>11</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>757.809998</td>\n",
|
||||
" <td>766.504171</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>12</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>757.473327</td>\n",
|
||||
" <td>765.824168</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>13</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>760.003326</td>\n",
|
||||
" <td>766.413335</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>14</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>765.368327</td>\n",
|
||||
" <td>766.934169</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>15</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>765.784993</td>\n",
|
||||
" <td>765.139999</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>16</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>765.318329</td>\n",
|
||||
" <td>762.737498</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>17</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>764.819997</td>\n",
|
||||
" <td>761.314997</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>18</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>766.536672</td>\n",
|
||||
" <td>762.005000</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>19</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>764.676666</td>\n",
|
||||
" <td>762.339996</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>20</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>761.284993</td>\n",
|
||||
" <td>763.326660</td>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>21</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>759.536662</td>\n",
|
||||
" <td>762.660828</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>22</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>759.676666</td>\n",
|
||||
" <td>762.497498</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>23</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>758.154999</td>\n",
|
||||
" <td>761.487498</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>24</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>758.213328</td>\n",
|
||||
" <td>762.375000</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>25</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>761.276662</td>\n",
|
||||
" <td>762.976664</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>26</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>768.171661</td>\n",
|
||||
" <td>764.728327</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>27</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>774.633331</td>\n",
|
||||
" <td>767.084997</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>28</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>780.229991</td>\n",
|
||||
" <td>769.953328</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>29</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>786.556661</td>\n",
|
||||
" <td>772.355830</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>...</th>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>222</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>924.373332</td>\n",
|
||||
" <td>927.728338</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>223</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>924.943339</td>\n",
|
||||
" <td>927.788340</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>224</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>925.056671</td>\n",
|
||||
" <td>926.540003</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>225</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>926.700002</td>\n",
|
||||
" <td>926.403335</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>226</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>930.480001</td>\n",
|
||||
" <td>927.687500</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>227</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>933.466664</td>\n",
|
||||
" <td>929.139999</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>228</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>937.909993</td>\n",
|
||||
" <td>931.141662</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>229</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>942.033325</td>\n",
|
||||
" <td>933.488332</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>230</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>948.169993</td>\n",
|
||||
" <td>936.613332</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>231</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>952.639994</td>\n",
|
||||
" <td>939.669998</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>232</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>956.885000</td>\n",
|
||||
" <td>943.682500</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>233</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>961.783335</td>\n",
|
||||
" <td>947.625000</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>234</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>964.765005</td>\n",
|
||||
" <td>951.337499</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>235</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>967.986664</td>\n",
|
||||
" <td>955.009995</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>236</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>973.230001</td>\n",
|
||||
" <td>960.699997</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>237</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>979.255005</td>\n",
|
||||
" <td>965.947500</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>238</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>982.541667</td>\n",
|
||||
" <td>969.713333</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>239</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>984.726664</td>\n",
|
||||
" <td>973.255000</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>240</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>987.256663</td>\n",
|
||||
" <td>976.010834</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>241</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>990.625000</td>\n",
|
||||
" <td>979.305832</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>242</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>989.825002</td>\n",
|
||||
" <td>981.527502</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>243</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>989.886668</td>\n",
|
||||
" <td>984.570836</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>244</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>986.348338</td>\n",
|
||||
" <td>984.445002</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>245</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>982.771667</td>\n",
|
||||
" <td>983.749166</td>\n",
|
||||
" <td>-1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>246</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>979.630005</td>\n",
|
||||
" <td>983.443334</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>247</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>976.255005</td>\n",
|
||||
" <td>983.440002</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>248</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>982.058339</td>\n",
|
||||
" <td>985.941671</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>249</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>986.876668</td>\n",
|
||||
" <td>988.381668</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>250</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>994.908335</td>\n",
|
||||
" <td>990.628337</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>251</th>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>1004.068339</td>\n",
|
||||
" <td>993.420003</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>252 rows × 4 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" signal short_ma long_ma positions\n",
|
||||
"0 0.0 768.700012 768.700012 NaN\n",
|
||||
"1 0.0 765.415008 765.415008 0.0\n",
|
||||
"2 0.0 764.283346 764.283346 0.0\n",
|
||||
"3 0.0 768.842514 768.842514 0.0\n",
|
||||
"4 0.0 773.176013 773.176013 0.0\n",
|
||||
"5 0.0 775.198344 775.198344 0.0\n",
|
||||
"6 1.0 774.175008 773.392866 1.0\n",
|
||||
"7 1.0 772.823344 770.971260 0.0\n",
|
||||
"8 1.0 768.500010 767.094456 0.0\n",
|
||||
"9 0.0 764.495005 766.234009 -1.0\n",
|
||||
"10 0.0 760.156667 766.074552 0.0\n",
|
||||
"11 0.0 757.809998 766.504171 0.0\n",
|
||||
"12 0.0 757.473327 765.824168 0.0\n",
|
||||
"13 0.0 760.003326 766.413335 0.0\n",
|
||||
"14 0.0 765.368327 766.934169 0.0\n",
|
||||
"15 1.0 765.784993 765.139999 1.0\n",
|
||||
"16 1.0 765.318329 762.737498 0.0\n",
|
||||
"17 1.0 764.819997 761.314997 0.0\n",
|
||||
"18 1.0 766.536672 762.005000 0.0\n",
|
||||
"19 1.0 764.676666 762.339996 0.0\n",
|
||||
"20 0.0 761.284993 763.326660 -1.0\n",
|
||||
"21 0.0 759.536662 762.660828 0.0\n",
|
||||
"22 0.0 759.676666 762.497498 0.0\n",
|
||||
"23 0.0 758.154999 761.487498 0.0\n",
|
||||
"24 0.0 758.213328 762.375000 0.0\n",
|
||||
"25 0.0 761.276662 762.976664 0.0\n",
|
||||
"26 1.0 768.171661 764.728327 1.0\n",
|
||||
"27 1.0 774.633331 767.084997 0.0\n",
|
||||
"28 1.0 780.229991 769.953328 0.0\n",
|
||||
"29 1.0 786.556661 772.355830 0.0\n",
|
||||
".. ... ... ... ...\n",
|
||||
"222 0.0 924.373332 927.728338 0.0\n",
|
||||
"223 0.0 924.943339 927.788340 0.0\n",
|
||||
"224 0.0 925.056671 926.540003 0.0\n",
|
||||
"225 1.0 926.700002 926.403335 1.0\n",
|
||||
"226 1.0 930.480001 927.687500 0.0\n",
|
||||
"227 1.0 933.466664 929.139999 0.0\n",
|
||||
"228 1.0 937.909993 931.141662 0.0\n",
|
||||
"229 1.0 942.033325 933.488332 0.0\n",
|
||||
"230 1.0 948.169993 936.613332 0.0\n",
|
||||
"231 1.0 952.639994 939.669998 0.0\n",
|
||||
"232 1.0 956.885000 943.682500 0.0\n",
|
||||
"233 1.0 961.783335 947.625000 0.0\n",
|
||||
"234 1.0 964.765005 951.337499 0.0\n",
|
||||
"235 1.0 967.986664 955.009995 0.0\n",
|
||||
"236 1.0 973.230001 960.699997 0.0\n",
|
||||
"237 1.0 979.255005 965.947500 0.0\n",
|
||||
"238 1.0 982.541667 969.713333 0.0\n",
|
||||
"239 1.0 984.726664 973.255000 0.0\n",
|
||||
"240 1.0 987.256663 976.010834 0.0\n",
|
||||
"241 1.0 990.625000 979.305832 0.0\n",
|
||||
"242 1.0 989.825002 981.527502 0.0\n",
|
||||
"243 1.0 989.886668 984.570836 0.0\n",
|
||||
"244 1.0 986.348338 984.445002 0.0\n",
|
||||
"245 0.0 982.771667 983.749166 -1.0\n",
|
||||
"246 0.0 979.630005 983.443334 0.0\n",
|
||||
"247 0.0 976.255005 983.440002 0.0\n",
|
||||
"248 0.0 982.058339 985.941671 0.0\n",
|
||||
"249 0.0 986.876668 988.381668 0.0\n",
|
||||
"250 1.0 994.908335 990.628337 1.0\n",
|
||||
"251 1.0 1004.068339 993.420003 0.0\n",
|
||||
"\n",
|
||||
"[252 rows x 4 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"short_window = int(0.025 * len(df))\n",
|
||||
"long_window = int(0.05 * len(df))\n",
|
||||
"\n",
|
||||
"signals = pd.DataFrame(index=df.index)\n",
|
||||
"signals['signal'] = 0.0\n",
|
||||
"\n",
|
||||
"signals['short_ma'] = df['Close'].rolling(window=short_window, min_periods=1, center=False).mean()\n",
|
||||
"signals['long_ma'] = df['Close'].rolling(window=long_window, min_periods=1, center=False).mean()\n",
|
||||
"\n",
|
||||
"signals['signal'][short_window:] = np.where(signals['short_ma'][short_window:] \n",
|
||||
" > signals['long_ma'][short_window:], 1.0, 0.0) \n",
|
||||
"signals['positions'] = signals['signal'].diff()\n",
|
||||
"\n",
|
||||
"signals"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def buy_stock(\n",
|
||||
" real_movement,\n",
|
||||
" signal,\n",
|
||||
" initial_money = 10000,\n",
|
||||
" max_buy = 1,\n",
|
||||
" max_sell = 1,\n",
|
||||
"):\n",
|
||||
" \"\"\"\n",
|
||||
" real_movement = actual movement in the real world\n",
|
||||
" delay = how much interval you want to delay to change our decision from buy to sell, vice versa\n",
|
||||
" initial_state = 1 is buy, 0 is sell\n",
|
||||
" initial_money = 1000, ignore what kind of currency\n",
|
||||
" max_buy = max quantity for share to buy\n",
|
||||
" max_sell = max quantity for share to sell\n",
|
||||
" \"\"\"\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" current_inventory = 0\n",
|
||||
"\n",
|
||||
" def buy(i, initial_money, current_inventory):\n",
|
||||
" shares = initial_money // real_movement[i]\n",
|
||||
" if shares < 1:\n",
|
||||
" print(\n",
|
||||
" 'day %d: total balances %f, not enough money to buy a unit price %f'\n",
|
||||
" % (i, initial_money, real_movement[i])\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" if shares > max_buy:\n",
|
||||
" buy_units = max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = shares\n",
|
||||
" initial_money -= buy_units * real_movement[i]\n",
|
||||
" current_inventory += buy_units\n",
|
||||
" print(\n",
|
||||
" 'day %d: buy %d units at price %f, total balance %f'\n",
|
||||
" % (i, buy_units, buy_units * real_movement[i], initial_money)\n",
|
||||
" )\n",
|
||||
" states_buy.append(0)\n",
|
||||
" return initial_money, current_inventory\n",
|
||||
"\n",
|
||||
" for i in range(real_movement.shape[0] - int(0.025 * len(df))):\n",
|
||||
" state = signal[i]\n",
|
||||
" if state == 1:\n",
|
||||
" initial_money, current_inventory = buy(\n",
|
||||
" i, initial_money, current_inventory\n",
|
||||
" )\n",
|
||||
" states_buy.append(i)\n",
|
||||
" elif state == -1:\n",
|
||||
" if current_inventory == 0:\n",
|
||||
" print('day %d: cannot sell anything, inventory 0' % (i))\n",
|
||||
" else:\n",
|
||||
" if current_inventory > max_sell:\n",
|
||||
" sell_units = max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = current_inventory\n",
|
||||
" current_inventory -= sell_units\n",
|
||||
" total_sell = sell_units * real_movement[i]\n",
|
||||
" initial_money += total_sell\n",
|
||||
" try:\n",
|
||||
" invest = (\n",
|
||||
" (real_movement[i] - real_movement[states_buy[-1]])\n",
|
||||
" / real_movement[states_buy[-1]]\n",
|
||||
" ) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (i, sell_units, total_sell, invest, initial_money)\n",
|
||||
" )\n",
|
||||
" states_sell.append(i)\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 6: buy 1 units at price 762.559998, total balance 9237.440002\n",
|
||||
"day 9, sell 1 units at price 758.489990, investment -0.533730 %, total balance 9995.929992,\n",
|
||||
"day 15: buy 1 units at price 760.989990, total balance 9234.940002\n",
|
||||
"day 20, sell 1 units at price 747.919983, investment -1.717501 %, total balance 9982.859985,\n",
|
||||
"day 26: buy 1 units at price 789.289978, total balance 9193.570007\n",
|
||||
"day 37, sell 1 units at price 791.549988, investment 0.286335 %, total balance 9985.119995,\n",
|
||||
"day 45: buy 1 units at price 806.650024, total balance 9178.469971\n",
|
||||
"day 62, sell 1 units at price 798.530029, investment -1.006632 %, total balance 9977.000000,\n",
|
||||
"day 69: buy 1 units at price 819.239990, total balance 9157.760010\n",
|
||||
"day 84, sell 1 units at price 831.909973, investment 1.546553 %, total balance 9989.669983,\n",
|
||||
"day 85: buy 1 units at price 835.369995, total balance 9154.299988\n",
|
||||
"day 96, sell 1 units at price 817.580017, investment -2.129593 %, total balance 9971.880005,\n",
|
||||
"day 104: buy 1 units at price 834.570007, total balance 9137.309998\n",
|
||||
"day 109, sell 1 units at price 823.349976, investment -1.344409 %, total balance 9960.659974,\n",
|
||||
"day 114: buy 1 units at price 838.210022, total balance 9122.449952\n",
|
||||
"day 151, sell 1 units at price 942.900024, investment 12.489710 %, total balance 10065.349976,\n",
|
||||
"day 160: buy 1 units at price 965.590027, total balance 9099.759949\n",
|
||||
"day 164, sell 1 units at price 917.789978, investment -4.950346 %, total balance 10017.549927,\n",
|
||||
"day 173: buy 1 units at price 947.159973, total balance 9070.389954\n",
|
||||
"day 184, sell 1 units at price 941.530029, investment -0.594403 %, total balance 10011.919983,\n",
|
||||
"day 204: buy 1 units at price 915.890015, total balance 9096.029968\n",
|
||||
"day 218, sell 1 units at price 920.289978, investment 0.480403 %, total balance 10016.319946,\n",
|
||||
"day 225: buy 1 units at price 924.859985, total balance 9091.459961\n",
|
||||
"day 245, sell 1 units at price 970.539978, investment 4.939125 %, total balance 10061.999939,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = buy_stock(df.Close, signals['positions'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4VFX6wPHvtPSQHnqTcmjSEQVFsGDHuvaCq7K77Iq6qD91rSvWXbur2BAVey8oolJFQGmCAocmJIT0Xmcy5ffHvROSkF6YkLyf5+GRzD333HPvnOC8855i8fl8CCGEEEIIIYRov6yBboAQQgghhBBCiNYlgZ8QQgghhBBCtHMS+AkhhBBCCCFEOyeBnxBCCCGEEEK0cxL4CSGEEEIIIUQ7J4GfEEIIIYQQQrRzEvgJIUQDKaXmK6XmtEK9JyildEvXKwJLKTVdKfVjLcd6KaWKlFK2w90uIYQQHZM90A0QQoiWopTaC1yvtf6+Ncq3Fq31SkC1Rt1KqZuAm4FEIAk4V2u9QynVFXgJGAt0BfpqrffWUc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YQQAhnqKYQQLcWCsY9bLsYws20YG5ELIYQQQgScZPyEEEIIIYQQop2TjJ8QQgghhBBCtHNH4nYOwRhLa6diLPUshBBLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df['Close']\n",
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,623 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 500\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, self.state_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, self.state_size))\n",
|
||||
" self.ACTION = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.REWARD = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.batch_size = tf.shape(self.ACTION)[0]\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('curiosity_model'):\n",
|
||||
" action = tf.reshape(self.ACTION, (-1,1))\n",
|
||||
" state_action = tf.concat([self.X, action], axis=1)\n",
|
||||
" save_state = tf.identity(self.Y)\n",
|
||||
" \n",
|
||||
" feed = tf.layers.dense(state_action, 32, activation=tf.nn.relu)\n",
|
||||
" self.curiosity_logits = tf.layers.dense(feed, self.state_size)\n",
|
||||
" self.curiosity_cost = tf.reduce_sum(tf.square(save_state - self.curiosity_logits), axis=1)\n",
|
||||
" \n",
|
||||
" self.curiosity_optimizer = tf.train.RMSPropOptimizer(self.LEARNING_RATE)\\\n",
|
||||
" .minimize(tf.reduce_mean(self.curiosity_cost))\n",
|
||||
" \n",
|
||||
" total_reward = tf.add(self.curiosity_cost, self.REWARD)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope(\"q_model\"):\n",
|
||||
" with tf.variable_scope(\"eval_net\"):\n",
|
||||
" x_action = tf.layers.dense(self.X, 128, tf.nn.relu)\n",
|
||||
" tensor_action, tensor_validation = tf.split(x_action,2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, self.OUTPUT_SIZE)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" self.logits = feed_validation + \\\n",
|
||||
" tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" \n",
|
||||
" with tf.variable_scope(\"target_net\"):\n",
|
||||
" y_action = tf.layers.dense(self.Y, 128, tf.nn.relu)\n",
|
||||
" tensor_action, tensor_validation = tf.split(y_action,2,1)\n",
|
||||
" feed_action = tf.layers.dense(tensor_action, self.OUTPUT_SIZE)\n",
|
||||
" feed_validation = tf.layers.dense(tensor_validation, 1)\n",
|
||||
" y_q = feed_validation + \\\n",
|
||||
" tf.subtract(feed_action,tf.reduce_mean(feed_action,axis=1,keep_dims=True))\n",
|
||||
" \n",
|
||||
" q_target = total_reward + self.GAMMA * tf.reduce_max(y_q, axis=1)\n",
|
||||
" action = tf.cast(self.ACTION, tf.int32)\n",
|
||||
" action_indices = tf.stack([tf.range(self.batch_size, dtype=tf.int32), action], axis=1)\n",
|
||||
" q = tf.gather_nd(params=self.logits, indices=action_indices)\n",
|
||||
" self.cost = tf.losses.mean_squared_error(labels=q_target, predictions=q)\n",
|
||||
" self.optimizer = tf.train.RMSPropOptimizer(self.LEARNING_RATE).minimize(\n",
|
||||
" self.cost, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, \"q_model/eval_net\"))\n",
|
||||
" \n",
|
||||
" t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_model/target_net')\n",
|
||||
" e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='q_model/eval_net')\n",
|
||||
" self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]\n",
|
||||
" \n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, done):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, done))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def predict(self, inputs):\n",
|
||||
" return self.sess.run(self.logits, feed_dict={self.X:inputs})\n",
|
||||
" \n",
|
||||
" def get_predicted_action(self, sequence):\n",
|
||||
" prediction = self.predict(np.array(sequence))[0]\n",
|
||||
" return np.argmax(prediction)\n",
|
||||
" \n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action = self.get_predicted_action([state])\n",
|
||||
" return action\n",
|
||||
" \n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" actions = np.array([a[1] for a in replay])\n",
|
||||
" rewards = np.array([a[2] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self.sess.run(self.target_replace_op)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.cost, self.optimizer], feed_dict = {\n",
|
||||
" self.X: states, self.Y: new_states, self.ACTION: actions, self.REWARD: rewards\n",
|
||||
" })\n",
|
||||
" \n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self.sess.run(self.curiosity_optimizer, feed_dict = {\n",
|
||||
" self.X: states, self.Y: new_states, self.ACTION: actions, self.REWARD: rewards\n",
|
||||
" })\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" \n",
|
||||
" self._memorize(state, action, invest, next_state, starting_money < initial_money)\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" state = next_state\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" cost = self._construct_memories(replay)\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:From <ipython-input-3-e49b5b607a66>:53: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"keep_dims is deprecated, use keepdims instead\n",
|
||||
"epoch: 10, total rewards: 698.460085.3, cost: 596251.000000, total money: 10698.460085\n",
|
||||
"epoch: 20, total rewards: 1683.164917.3, cost: 5890915.500000, total money: 6720.204895\n",
|
||||
"epoch: 30, total rewards: 1686.875004.3, cost: 75077.554688, total money: 6721.424992\n",
|
||||
"epoch: 40, total rewards: 541.999876.3, cost: 2707843.750000, total money: 9525.359861\n",
|
||||
"epoch: 50, total rewards: 1668.824950.3, cost: 32719.388672, total money: 7666.774900\n",
|
||||
"epoch: 60, total rewards: 751.654909.3, cost: 1165742.750000, total money: 8743.134889\n",
|
||||
"epoch: 70, total rewards: 1637.889772.3, cost: 325201.937500, total money: 6669.909730\n",
|
||||
"epoch: 80, total rewards: 587.055053.3, cost: 1527037.250000, total money: 892.705077\n",
|
||||
"epoch: 90, total rewards: 2170.969727.3, cost: 122936.546875, total money: 8204.549683\n",
|
||||
"epoch: 100, total rewards: 1565.850155.3, cost: 844705.187500, total money: 19.270138\n",
|
||||
"epoch: 110, total rewards: 1733.244930.3, cost: 557043.125000, total money: 6744.174861\n",
|
||||
"epoch: 120, total rewards: 1282.489866.3, cost: 3785043.750000, total money: 8328.149839\n",
|
||||
"epoch: 130, total rewards: 1260.559873.3, cost: 596946.312500, total money: 6319.639890\n",
|
||||
"epoch: 140, total rewards: 1346.769778.3, cost: 26543662.000000, total money: 10330.129763\n",
|
||||
"epoch: 150, total rewards: 2415.594848.3, cost: 851761.625000, total money: 9467.174865\n",
|
||||
"epoch: 160, total rewards: 1033.800112.3, cost: 3596937.500000, total money: 9044.600099\n",
|
||||
"epoch: 170, total rewards: 1597.439823.3, cost: 511038.375000, total money: 93.789859\n",
|
||||
"epoch: 180, total rewards: 1736.860354.3, cost: 3795484.000000, total money: 1011.990359\n",
|
||||
"epoch: 190, total rewards: 1682.540215.3, cost: 657330.250000, total money: 8675.460198\n",
|
||||
"epoch: 200, total rewards: 875.094668.3, cost: 30907612.000000, total money: 10875.094668\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 9217.479980\n",
|
||||
"day 4: buy 1 unit at price 790.510010, total balance 8426.969970\n",
|
||||
"day 5: buy 1 unit at price 785.309998, total balance 7641.659972\n",
|
||||
"day 6: buy 1 unit at price 762.559998, total balance 6879.099974\n",
|
||||
"day 7: buy 1 unit at price 754.020020, total balance 6125.079954\n",
|
||||
"day 9: buy 1 unit at price 758.489990, total balance 5366.589964\n",
|
||||
"day 10, sell 1 unit at price 764.479980, investment -2.305377 %, total balance 6131.069944,\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 5359.839964\n",
|
||||
"day 13: buy 1 unit at price 769.200012, total balance 4590.639952\n",
|
||||
"day 15: buy 1 unit at price 760.989990, total balance 3829.649962\n",
|
||||
"day 16: buy 1 unit at price 761.679993, total balance 3067.969969\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 2299.729979\n",
|
||||
"day 19: buy 1 unit at price 758.039978, total balance 1541.690001\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 791.190001\n",
|
||||
"day 22, sell 1 unit at price 762.520020, investment -3.540751 %, total balance 1553.710021,\n",
|
||||
"day 23, sell 1 unit at price 759.109985, investment -3.336264 %, total balance 2312.820006,\n",
|
||||
"day 24: buy 1 unit at price 771.190002, total balance 1541.630004\n",
|
||||
"day 25, sell 1 unit at price 776.419983, investment 1.817560 %, total balance 2318.049987,\n",
|
||||
"day 27, sell 1 unit at price 789.270020, investment 4.674942 %, total balance 3107.320007,\n",
|
||||
"day 29, sell 1 unit at price 797.070007, investment 5.086424 %, total balance 3904.390014,\n",
|
||||
"day 30, sell 1 unit at price 797.849976, investment 3.451629 %, total balance 4702.239990,\n",
|
||||
"day 32: buy 1 unit at price 794.200012, total balance 3908.039978\n",
|
||||
"day 33, sell 1 unit at price 796.419983, investment 3.538738 %, total balance 4704.459961,\n",
|
||||
"day 34, sell 1 unit at price 794.559998, investment 4.411360 %, total balance 5499.019959,\n",
|
||||
"day 38: buy 1 unit at price 785.049988, total balance 4713.969971\n",
|
||||
"day 39, sell 1 unit at price 782.789978, investment 2.771503 %, total balance 5496.759949,\n",
|
||||
"day 40, sell 1 unit at price 771.820007, investment 0.466002 %, total balance 6268.579956,\n",
|
||||
"day 41: buy 1 unit at price 786.140015, total balance 5482.439941\n",
|
||||
"day 45: buy 1 unit at price 806.650024, total balance 4675.789917\n",
|
||||
"day 47, sell 1 unit at price 807.909973, investment 6.578808 %, total balance 5483.699890,\n",
|
||||
"day 50: buy 1 unit at price 804.609985, total balance 4679.089905\n",
|
||||
"day 51, sell 1 unit at price 806.070007, investment 7.404398 %, total balance 5485.159912,\n",
|
||||
"day 52, sell 1 unit at price 802.174988, investment 4.017815 %, total balance 6287.334900,\n",
|
||||
"day 53: buy 1 unit at price 805.020020, total balance 5482.314880\n",
|
||||
"day 54, sell 1 unit at price 819.309998, investment 3.161670 %, total balance 6301.624878,\n",
|
||||
"day 56: buy 1 unit at price 835.669983, total balance 5465.954895\n",
|
||||
"day 58, sell 1 unit at price 823.309998, investment 4.873576 %, total balance 6289.264893,\n",
|
||||
"day 59, sell 1 unit at price 802.320007, investment 2.058157 %, total balance 7091.584900,\n",
|
||||
"day 60: buy 1 unit at price 796.789978, total balance 6294.794922\n",
|
||||
"day 61, sell 1 unit at price 795.695007, investment -1.358088 %, total balance 7090.489929,\n",
|
||||
"day 62, sell 1 unit at price 798.530029, investment -0.755640 %, total balance 7889.019958,\n",
|
||||
"day 63, sell 1 unit at price 801.489990, investment -0.438502 %, total balance 8690.509948,\n",
|
||||
"day 66: buy 1 unit at price 808.380005, total balance 7882.129943\n",
|
||||
"day 67, sell 1 unit at price 809.559998, investment -3.124437 %, total balance 8691.689941,\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 7878.019958\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 2.817557 %, total balance 8697.259948,\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 1.493111 %, total balance 9517.709960,\n",
|
||||
"day 72, sell 1 unit at price 824.159973, investment 1.289219 %, total balance 10341.869933,\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 9513.799926\n",
|
||||
"day 74, sell 1 unit at price 831.659973, investment 0.433534 %, total balance 10345.459899,\n",
|
||||
"day 75: buy 1 unit at price 830.760010, total balance 9514.699889\n",
|
||||
"day 76: buy 1 unit at price 831.330017, total balance 8683.369872\n",
|
||||
"day 78: buy 1 unit at price 829.280029, total balance 7854.089843\n",
|
||||
"day 79: buy 1 unit at price 823.210022, total balance 7030.879821\n",
|
||||
"day 80: buy 1 unit at price 835.239990, total balance 6195.639831\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment -0.015649 %, total balance 7026.269836,\n",
|
||||
"day 82, sell 1 unit at price 829.080017, investment -0.270651 %, total balance 7855.349853,\n",
|
||||
"day 84, sell 1 unit at price 831.909973, investment 0.317136 %, total balance 8687.259826,\n",
|
||||
"day 85: buy 1 unit at price 835.369995, total balance 7851.889831\n",
|
||||
"day 86: buy 1 unit at price 838.679993, total balance 7013.209838\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 6167.669860\n",
|
||||
"day 89: buy 1 unit at price 845.619995, total balance 5322.049865\n",
|
||||
"day 90, sell 1 unit at price 847.200012, investment 2.914200 %, total balance 6169.249877,\n",
|
||||
"day 91: buy 1 unit at price 848.780029, total balance 5320.469848\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance 4468.349853\n",
|
||||
"day 93, sell 1 unit at price 848.400024, investment 1.575599 %, total balance 5316.749877,\n",
|
||||
"day 94: buy 1 unit at price 830.460022, total balance 4486.289855\n",
|
||||
"day 95: buy 1 unit at price 829.590027, total balance 3656.699828\n",
|
||||
"day 96, sell 1 unit at price 817.580017, investment -2.129593 %, total balance 4474.279845,\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 3659.849852\n",
|
||||
"day 98, sell 1 unit at price 819.510010, investment -2.285733 %, total balance 4479.359862,\n",
|
||||
"day 100: buy 1 unit at price 831.409973, total balance 3647.949889\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment -1.660475 %, total balance 4479.449889,\n",
|
||||
"day 103: buy 1 unit at price 838.549988, total balance 3640.899901\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 2806.329894\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment -1.680426 %, total balance 3637.739867,\n",
|
||||
"day 106: buy 1 unit at price 827.880005, total balance 2809.859862\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance 1985.129882\n",
|
||||
"day 109: buy 1 unit at price 823.349976, total balance 1161.779906\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 337.459899\n",
|
||||
"day 112, sell 1 unit at price 837.169983, investment -1.367851 %, total balance 1174.629882,\n",
|
||||
"day 113: buy 1 unit at price 836.820007, total balance 337.809875\n",
|
||||
"day 115, sell 1 unit at price 841.650024, investment -1.228697 %, total balance 1179.459899,\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 1.532883 %, total balance 2022.649901,\n",
|
||||
"day 117, sell 1 unit at price 862.760010, investment 3.998358 %, total balance 2885.409911,\n",
|
||||
"day 118: buy 1 unit at price 872.299988, total balance 2013.109923\n",
|
||||
"day 119, sell 1 unit at price 871.729980, investment 7.035594 %, total balance 2884.839903,\n",
|
||||
"day 120, sell 1 unit at price 874.250000, investment 5.152696 %, total balance 3759.089903,\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 9.288655 %, total balance 4675.529905,\n",
|
||||
"day 124: buy 1 unit at price 927.039978, total balance 3748.489927\n",
|
||||
"day 126, sell 1 unit at price 927.130005, investment 11.090741 %, total balance 4675.619932,\n",
|
||||
"day 127, sell 1 unit at price 934.299988, investment 12.854518 %, total balance 5609.919920,\n",
|
||||
"day 128, sell 1 unit at price 932.169983, investment 13.027294 %, total balance 6542.089903,\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 5613.309874\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 14.532098 %, total balance 6556.309874,\n",
|
||||
"day 137, sell 1 unit at price 941.859985, investment 14.259023 %, total balance 7498.169859,\n",
|
||||
"day 140, sell 1 unit at price 969.539978, investment 15.860038 %, total balance 8467.709837,\n",
|
||||
"day 141: buy 1 unit at price 971.469971, total balance 7496.239866\n",
|
||||
"day 142, sell 1 unit at price 975.880005, investment 11.874357 %, total balance 8472.119871,\n",
|
||||
"day 143, sell 1 unit at price 964.859985, investment 4.079652 %, total balance 9436.979856,\n",
|
||||
"day 144, sell 1 unit at price 966.950012, investment 4.109690 %, total balance 10403.929868,\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 0.425129 %, total balance 11379.529844,\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance 10402.959837\n",
|
||||
"day 148: buy 1 unit at price 980.940002, total balance 9422.019835\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment 0.700407 %, total balance 10405.429808,\n",
|
||||
"day 150: buy 1 unit at price 949.830017, total balance 9455.599791\n",
|
||||
"day 151, sell 1 unit at price 942.900024, investment -3.877911 %, total balance 10398.499815,\n",
|
||||
"day 152, sell 1 unit at price 953.400024, investment 0.375857 %, total balance 11351.899839,\n",
|
||||
"day 153: buy 1 unit at price 950.760010, total balance 10401.139829\n",
|
||||
"day 155, sell 1 unit at price 939.780029, investment -1.154864 %, total balance 11340.919858,\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance 10383.549863\n",
|
||||
"day 157: buy 1 unit at price 950.630005, total balance 9432.919858\n",
|
||||
"day 158: buy 1 unit at price 959.450012, total balance 8473.469846\n",
|
||||
"day 159, sell 1 unit at price 957.090027, investment -0.029243 %, total balance 9430.559873,\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 8464.969846\n",
|
||||
"day 162, sell 1 unit at price 927.330017, investment -2.451005 %, total balance 9392.299863,\n",
|
||||
"day 163: buy 1 unit at price 940.489990, total balance 8451.809873\n",
|
||||
"day 164: buy 1 unit at price 917.789978, total balance 7534.019895\n",
|
||||
"day 165: buy 1 unit at price 908.729980, total balance 6625.289915\n",
|
||||
"day 166, sell 1 unit at price 898.700012, investment -6.331752 %, total balance 7523.989927,\n",
|
||||
"day 167, sell 1 unit at price 911.710022, investment -5.580008 %, total balance 8435.699949,\n",
|
||||
"day 169: buy 1 unit at price 918.590027, total balance 7517.109922\n",
|
||||
"day 170: buy 1 unit at price 928.799988, total balance 6588.309934\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 5658.219907\n",
|
||||
"day 172: buy 1 unit at price 943.830017, total balance 4714.389890\n",
|
||||
"day 173: buy 1 unit at price 947.159973, total balance 3767.229917\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 2813.809934\n",
|
||||
"day 177: buy 1 unit at price 970.890015, total balance 1842.919919\n",
|
||||
"day 178, sell 1 unit at price 968.150024, investment 2.941024 %, total balance 2811.069943,\n",
|
||||
"day 179: buy 1 unit at price 972.919983, total balance 1838.149960\n",
|
||||
"day 180: buy 1 unit at price 980.340027, total balance 857.809933\n",
|
||||
"day 187, sell 1 unit at price 930.390015, investment 1.372867 %, total balance 1788.199948,\n",
|
||||
"day 189, sell 1 unit at price 927.960022, investment 2.116145 %, total balance 2716.159970,\n",
|
||||
"day 190, sell 1 unit at price 929.359985, investment 1.172444 %, total balance 3645.519955,\n",
|
||||
"day 191, sell 1 unit at price 926.789978, investment -0.216409 %, total balance 4572.309933,\n",
|
||||
"day 192, sell 1 unit at price 922.900024, investment -0.773044 %, total balance 5495.209957,\n",
|
||||
"day 193: buy 1 unit at price 907.239990, total balance 4587.969967\n",
|
||||
"day 196, sell 1 unit at price 922.219971, investment -2.289612 %, total balance 5510.189938,\n",
|
||||
"day 197, sell 1 unit at price 926.960022, investment -2.132686 %, total balance 6437.149960,\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -4.451344 %, total balance 7348.129940,\n",
|
||||
"day 200, sell 1 unit at price 906.659973, investment -6.615584 %, total balance 8254.789913,\n",
|
||||
"day 201: buy 1 unit at price 924.690002, total balance 7330.099911\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 6403.099911\n",
|
||||
"day 204: buy 1 unit at price 915.890015, total balance 5487.209896\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 4573.399898\n",
|
||||
"day 206: buy 1 unit at price 921.289978, total balance 3652.109920\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance 2722.539913\n",
|
||||
"day 208: buy 1 unit at price 939.330017, total balance 1783.209896\n",
|
||||
"day 209: buy 1 unit at price 937.340027, total balance 845.869869\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment -4.570774 %, total balance 1774.319881,\n",
|
||||
"day 211: buy 1 unit at price 927.809998, total balance 846.509883\n",
|
||||
"day 215, sell 1 unit at price 932.070007, investment -4.923804 %, total balance 1778.579890,\n",
|
||||
"day 218: buy 1 unit at price 920.289978, total balance 858.289912\n",
|
||||
"day 219, sell 1 unit at price 915.000000, investment 0.855343 %, total balance 1773.289912,\n",
|
||||
"day 220, sell 1 unit at price 921.809998, investment -0.311456 %, total balance 2695.099910,\n",
|
||||
"day 221, sell 1 unit at price 931.580017, investment 0.494069 %, total balance 3626.679927,\n",
|
||||
"day 223: buy 1 unit at price 928.530029, total balance 2698.149898\n",
|
||||
"day 225, sell 1 unit at price 924.859985, investment 0.979372 %, total balance 3623.009883,\n",
|
||||
"day 228: buy 1 unit at price 959.109985, total balance 2663.899898\n",
|
||||
"day 230, sell 1 unit at price 957.789978, investment 4.812814 %, total balance 3621.689876,\n",
|
||||
"day 231, sell 1 unit at price 951.679993, investment 3.298637 %, total balance 4573.369869,\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 4.345021 %, total balance 5543.329891,\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 4.211512 %, total balance 6522.219906,\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment 4.231119 %, total balance 7499.219906,\n",
|
||||
"day 235, sell 1 unit at price 972.599976, investment 4.827495 %, total balance 8471.819882,\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 7.493293 %, total balance 9461.069882,\n",
|
||||
"day 239: buy 1 unit at price 992.000000, total balance 8469.069882\n",
|
||||
"day 240, sell 1 unit at price 992.179993, investment 6.854917 %, total balance 9461.249875,\n",
|
||||
"day 242, sell 1 unit at price 984.450012, investment 2.642036 %, total balance 10445.699887,\n",
|
||||
"day 243: buy 1 unit at price 988.200012, total balance 9457.499875\n",
|
||||
"day 245, sell 1 unit at price 970.539978, investment -2.163309 %, total balance 10428.039853,\n",
|
||||
"day 246, sell 1 unit at price 973.330017, investment -1.504756 %, total balance 11401.369870,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCB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S6kGsG/qzgPFR1k/BCmDy7GNdRuX+W/XpTWAhVj+7xSGB6f+A+5RSF2NlTsH6rIuxrq9zgf9prQ/ZGfDAdnux+pC1j5IxjskedGQZ8KBS6nb7eH2A7nYZdHXC38sq7HJJEyuQdtnXaznWd6ZryKo9sB4kBAa3Cfcq8Cel1Gyshwl3AGu11jn2cRxYwZwTMO3jeO2HO8fYr3+J9V25Fus6CwyusgjrAc7JWNfvtVhTkqyz9z0C68GBiTVCaResrF3g+9cfKwDtAfwDq9S8IKz9VwBfa62/VdZgTEl2H86ewOZY76EQonWTUk8hRFP3R6wb+YNKqRu01hq4CKtvy36sG/Gz7LKqKutjBQK5WBnBH7FGFYzXk1j99tZiBSRvYgU6XjtLdy1WcFiAlVl8o5bnOAB4D+tm+XPgUW3P0RWlTWVY5X232j9fDKC13qe13hP4Z6+/3+4jBtaNZhJWP6MXqDq6aRlHyiVzOFLKh1LqcaVUYK49A6uv5D6sG+vrgHmB/mJ2H8YZWH2RDmHNSzZLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,633 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class neuralnetwork:\n",
|
||||
" def __init__(self, id_, hidden_size = 128):\n",
|
||||
" self.W1 = np.random.randn(window_size, hidden_size) / np.sqrt(window_size)\n",
|
||||
" self.W2 = np.random.randn(hidden_size, 3) / np.sqrt(hidden_size)\n",
|
||||
" self.fitness = 0\n",
|
||||
" self.id = id_\n",
|
||||
"\n",
|
||||
"def relu(X):\n",
|
||||
" return np.maximum(X, 0)\n",
|
||||
" \n",
|
||||
"def softmax(X):\n",
|
||||
" e_x = np.exp(X - np.max(X, axis=-1, keepdims=True))\n",
|
||||
" return e_x / np.sum(e_x, axis=-1, keepdims=True)\n",
|
||||
"\n",
|
||||
"def feed_forward(X, nets):\n",
|
||||
" a1 = np.dot(X, nets.W1)\n",
|
||||
" z1 = relu(a1)\n",
|
||||
" a2 = np.dot(z1, nets.W2)\n",
|
||||
" return softmax(a2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class NeuroEvolution:\n",
|
||||
" def __init__(self, population_size, mutation_rate, model_generator,\n",
|
||||
" state_size, window_size, trend, skip, initial_money):\n",
|
||||
" self.population_size = population_size\n",
|
||||
" self.mutation_rate = mutation_rate\n",
|
||||
" self.model_generator = model_generator\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" self.initial_money = initial_money\n",
|
||||
" \n",
|
||||
" def _initialize_population(self):\n",
|
||||
" self.population = []\n",
|
||||
" for i in range(self.population_size):\n",
|
||||
" self.population.append(self.model_generator(i))\n",
|
||||
" \n",
|
||||
" def mutate(self, individual, scale=1.0):\n",
|
||||
" mutation_mask = np.random.binomial(1, p=self.mutation_rate, size=individual.W1.shape)\n",
|
||||
" individual.W1 += np.random.normal(loc=0, scale=scale, size=individual.W1.shape) * mutation_mask\n",
|
||||
" mutation_mask = np.random.binomial(1, p=self.mutation_rate, size=individual.W2.shape)\n",
|
||||
" individual.W2 += np.random.normal(loc=0, scale=scale, size=individual.W2.shape) * mutation_mask\n",
|
||||
" return individual\n",
|
||||
" \n",
|
||||
" def inherit_weights(self, parent, child):\n",
|
||||
" child.W1 = parent.W1.copy()\n",
|
||||
" child.W2 = parent.W2.copy()\n",
|
||||
" return child\n",
|
||||
" \n",
|
||||
" def crossover(self, parent1, parent2):\n",
|
||||
" child1 = self.model_generator((parent1.id+1)*10)\n",
|
||||
" child1 = self.inherit_weights(parent1, child1)\n",
|
||||
" child2 = self.model_generator((parent2.id+1)*10)\n",
|
||||
" child2 = self.inherit_weights(parent2, child2)\n",
|
||||
" # first W\n",
|
||||
" n_neurons = child1.W1.shape[1]\n",
|
||||
" cutoff = np.random.randint(0, n_neurons)\n",
|
||||
" child1.W1[:, cutoff:] = parent2.W1[:, cutoff:].copy()\n",
|
||||
" child2.W1[:, cutoff:] = parent1.W1[:, cutoff:].copy()\n",
|
||||
" # second W\n",
|
||||
" n_neurons = child1.W2.shape[1]\n",
|
||||
" cutoff = np.random.randint(0, n_neurons)\n",
|
||||
" child1.W2[:, cutoff:] = parent2.W2[:, cutoff:].copy()\n",
|
||||
" child2.W2[:, cutoff:] = parent1.W2[:, cutoff:].copy()\n",
|
||||
" return child1, child2\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])\n",
|
||||
" \n",
|
||||
" def act(self, p, state):\n",
|
||||
" logits = feed_forward(state, p)\n",
|
||||
" return np.argmax(logits, 1)[0]\n",
|
||||
" \n",
|
||||
" def buy(self, individual):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" inventory = []\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" \n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(individual, state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((self.trend[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, self.trend[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" state = next_state\n",
|
||||
" \n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def calculate_fitness(self):\n",
|
||||
" for i in range(self.population_size):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" inventory = []\n",
|
||||
" \n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(self.population[i], state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
"\n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
"\n",
|
||||
" state = next_state\n",
|
||||
" invest = ((starting_money - initial_money) / initial_money) * 100\n",
|
||||
" self.population[i].fitness = invest\n",
|
||||
" \n",
|
||||
" def evolve(self, generations=20, checkpoint= 5):\n",
|
||||
" self._initialize_population()\n",
|
||||
" n_winners = int(self.population_size * 0.4)\n",
|
||||
" n_parents = self.population_size - n_winners\n",
|
||||
" for epoch in range(generations):\n",
|
||||
" self.calculate_fitness()\n",
|
||||
" fitnesses = [i.fitness for i in self.population]\n",
|
||||
" sort_fitness = np.argsort(fitnesses)[::-1]\n",
|
||||
" self.population = [self.population[i] for i in sort_fitness]\n",
|
||||
" fittest_individual = self.population[0]\n",
|
||||
" if (epoch+1) % checkpoint == 0:\n",
|
||||
" print('epoch %d, fittest individual %d with accuracy %f'%(epoch+1, sort_fitness[0], \n",
|
||||
" fittest_individual.fitness))\n",
|
||||
" next_population = [self.population[i] for i in range(n_winners)]\n",
|
||||
" total_fitness = np.sum([np.abs(i.fitness) for i in self.population])\n",
|
||||
" parent_probabilities = [np.abs(i.fitness / total_fitness) for i in self.population]\n",
|
||||
" parents = np.random.choice(self.population, size=n_parents, p=parent_probabilities, replace=False)\n",
|
||||
" for i in np.arange(0, len(parents), 2):\n",
|
||||
" child1, child2 = self.crossover(parents[i], parents[i+1])\n",
|
||||
" next_population += [self.mutate(child1), self.mutate(child2)]\n",
|
||||
" self.population = next_population\n",
|
||||
" return fittest_individual"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"population_size = 100\n",
|
||||
"generations = 100\n",
|
||||
"mutation_rate = 0.1\n",
|
||||
"neural_evolve = NeuroEvolution(population_size, mutation_rate, neuralnetwork,\n",
|
||||
" window_size, window_size, close, skip, initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch 5, fittest individual 0 with accuracy 10.849749\n",
|
||||
"epoch 10, fittest individual 0 with accuracy 11.095000\n",
|
||||
"epoch 15, fittest individual 0 with accuracy 11.095000\n",
|
||||
"epoch 20, fittest individual 93 with accuracy 13.756802\n",
|
||||
"epoch 25, fittest individual 95 with accuracy 23.728605\n",
|
||||
"epoch 30, fittest individual 0 with accuracy 23.728605\n",
|
||||
"epoch 35, fittest individual 0 with accuracy 23.728605\n",
|
||||
"epoch 40, fittest individual 0 with accuracy 23.728605\n",
|
||||
"epoch 45, fittest individual 0 with accuracy 23.728605\n",
|
||||
"epoch 50, fittest individual 0 with accuracy 23.728605\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fittest_nets = neural_evolve.evolve(50)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 1: buy 1 unit at price 762.130005, total balance 9237.869995\n",
|
||||
"day 2: buy 1 unit at price 762.020020, total balance 8475.849975\n",
|
||||
"day 3, sell 1 unit at price 782.520020, investment 2.675399 %, total balance 9258.369995,\n",
|
||||
"day 5, sell 1 unit at price 785.309998, investment 3.056347 %, total balance 10043.679993,\n",
|
||||
"day 6: buy 1 unit at price 762.559998, total balance 9281.119995\n",
|
||||
"day 7: buy 1 unit at price 754.020020, total balance 8527.099975\n",
|
||||
"day 8: buy 1 unit at price 736.080017, total balance 7791.019958\n",
|
||||
"day 9: buy 1 unit at price 758.489990, total balance 7032.529968\n",
|
||||
"day 10, sell 1 unit at price 764.479980, investment 0.251781 %, total balance 7797.009948,\n",
|
||||
"day 12: buy 1 unit at price 760.539978, total balance 7036.469970\n",
|
||||
"day 14: buy 1 unit at price 768.270020, total balance 6268.199950\n",
|
||||
"day 15: buy 1 unit at price 760.989990, total balance 5507.209960\n",
|
||||
"day 16: buy 1 unit at price 761.679993, total balance 4745.529967\n",
|
||||
"day 17, sell 1 unit at price 768.239990, investment 1.885888 %, total balance 5513.769957,\n",
|
||||
"day 18, sell 1 unit at price 770.840027, investment 4.722314 %, total balance 6284.609984,\n",
|
||||
"day 20: buy 1 unit at price 747.919983, total balance 5536.690001\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 4786.190001\n",
|
||||
"day 22: buy 1 unit at price 762.520020, total balance 4023.669981\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 1.674381 %, total balance 4794.859983,\n",
|
||||
"day 25, sell 1 unit at price 776.419983, investment 2.087991 %, total balance 5571.279966,\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 2.736012 %, total balance 6360.569944,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 5571.299924\n",
|
||||
"day 28: buy 1 unit at price 796.099976, total balance 4775.199948\n",
|
||||
"day 29: buy 1 unit at price 797.070007, total balance 3978.129941\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 3187.329953\n",
|
||||
"day 32, sell 1 unit at price 794.200012, investment 4.364055 %, total balance 3981.529965,\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 3185.109982\n",
|
||||
"day 34, sell 1 unit at price 794.559998, investment 4.316774 %, total balance 3979.669980,\n",
|
||||
"day 35, sell 1 unit at price 791.260010, investment 5.794741 %, total balance 4770.929990,\n",
|
||||
"day 37: buy 1 unit at price 791.549988, total balance 3979.380002\n",
|
||||
"day 38, sell 1 unit at price 785.049988, investment 4.603596 %, total balance 4764.429990,\n",
|
||||
"day 39: buy 1 unit at price 782.789978, total balance 3981.640012\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 3209.820005\n",
|
||||
"day 41: buy 1 unit at price 786.140015, total balance 2423.679990\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 3.197294 %, total balance 3210.580014,\n",
|
||||
"day 43: buy 1 unit at price 794.020020, total balance 2416.559994\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 1610.409970\n",
|
||||
"day 46, sell 1 unit at price 804.789978, investment 1.966369 %, total balance 2415.199948,\n",
|
||||
"day 47, sell 1 unit at price 807.909973, investment 1.483482 %, total balance 3223.109921,\n",
|
||||
"day 48: buy 1 unit at price 806.359985, total balance 2416.749936\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 1.356217 %, total balance 3224.629941,\n",
|
||||
"day 50, sell 1 unit at price 804.609985, investment 1.746332 %, total balance 4029.239926,\n",
|
||||
"day 51, sell 1 unit at price 806.070007, investment 1.211675 %, total balance 4835.309933,\n",
|
||||
"day 52: buy 1 unit at price 802.174988, total balance 4033.134945\n",
|
||||
"day 53: buy 1 unit at price 805.020020, total balance 3228.114925\n",
|
||||
"day 54, sell 1 unit at price 819.309998, investment 3.507044 %, total balance 4047.424923,\n",
|
||||
"day 55, sell 1 unit at price 823.869995, investment 5.247898 %, total balance 4871.294918,\n",
|
||||
"day 56, sell 1 unit at price 835.669983, investment 8.272651 %, total balance 5706.964901,\n",
|
||||
"day 57, sell 1 unit at price 832.150024, investment 5.852648 %, total balance 6539.114925,\n",
|
||||
"day 58: buy 1 unit at price 823.309998, total balance 5715.804927\n",
|
||||
"day 59: buy 1 unit at price 802.320007, total balance 4913.484920\n",
|
||||
"day 60: buy 1 unit at price 796.789978, total balance 4116.694942\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 3320.999935\n",
|
||||
"day 63: buy 1 unit at price 801.489990, total balance 2519.509945\n",
|
||||
"day 65: buy 1 unit at price 806.969971, total balance 1712.539974\n",
|
||||
"day 66, sell 1 unit at price 808.380005, investment 1.808517 %, total balance 2520.919979,\n",
|
||||
"day 67: buy 1 unit at price 809.559998, total balance 1711.359981\n",
|
||||
"day 68, sell 1 unit at price 813.669983, investment 0.932824 %, total balance 2525.029964,\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 1.597302 %, total balance 3344.269954,\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 2.278184 %, total balance 4164.719966,\n",
|
||||
"day 71: buy 1 unit at price 818.979980, total balance 3345.739986\n",
|
||||
"day 72: buy 1 unit at price 824.159973, total balance 2521.580013\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 1693.510006\n",
|
||||
"day 75, sell 1 unit at price 830.760010, investment 3.197435 %, total balance 2524.270016,\n",
|
||||
"day 76, sell 1 unit at price 831.330017, investment 0.974119 %, total balance 3355.600033,\n",
|
||||
"day 77, sell 1 unit at price 828.640015, investment 3.280488 %, total balance 4184.240048,\n",
|
||||
"day 78, sell 1 unit at price 829.280029, investment 4.077618 %, total balance 5013.520077,\n",
|
||||
"day 79, sell 1 unit at price 823.210022, investment 3.457985 %, total balance 5836.730099,\n",
|
||||
"day 80: buy 1 unit at price 835.239990, total balance 5001.490109\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment 3.635730 %, total balance 5832.120114,\n",
|
||||
"day 83: buy 1 unit at price 827.780029, total balance 5004.340085\n",
|
||||
"day 84: buy 1 unit at price 831.909973, total balance 4172.430112\n",
|
||||
"day 85: buy 1 unit at price 835.369995, total balance 3337.060117\n",
|
||||
"day 86, sell 1 unit at price 838.679993, investment 3.929517 %, total balance 4175.740110,\n",
|
||||
"day 87, sell 1 unit at price 843.250000, investment 4.161520 %, total balance 5018.990110,\n",
|
||||
"day 88, sell 1 unit at price 845.539978, investment 3.243058 %, total balance 5864.530088,\n",
|
||||
"day 89: buy 1 unit at price 845.619995, total balance 5018.910093\n",
|
||||
"day 90, sell 1 unit at price 847.200012, investment 2.795578 %, total balance 5866.110105,\n",
|
||||
"day 91: buy 1 unit at price 848.780029, total balance 5017.330076\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance 4165.210081\n",
|
||||
"day 93, sell 1 unit at price 848.400024, investment 2.455108 %, total balance 5013.610105,\n",
|
||||
"day 95: buy 1 unit at price 829.590027, total balance 4184.020078\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 3369.590085\n",
|
||||
"day 98, sell 1 unit at price 819.510010, investment -1.883289 %, total balance 4189.100095,\n",
|
||||
"day 100: buy 1 unit at price 831.409973, total balance 3357.690122\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment 0.449391 %, total balance 4189.190122,\n",
|
||||
"day 102: buy 1 unit at price 829.559998, total balance 3359.630124\n",
|
||||
"day 103: buy 1 unit at price 838.549988, total balance 2521.080136\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 1686.510129\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment -0.060103 %, total balance 2517.920102,\n",
|
||||
"day 106: buy 1 unit at price 827.880005, total balance 1690.040097\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance 865.310117\n",
|
||||
"day 109: buy 1 unit at price 823.349976, total balance 41.960141\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance -782.359866\n",
|
||||
"day 112, sell 1 unit at price 837.169983, investment 0.215472 %, total balance 54.810117,\n",
|
||||
"day 113, sell 1 unit at price 836.820007, investment -1.040655 %, total balance 891.630124,\n",
|
||||
"day 114, sell 1 unit at price 838.210022, investment -1.245318 %, total balance 1729.840146,\n",
|
||||
"day 115: buy 1 unit at price 841.650024, total balance 888.190122\n",
|
||||
"day 117: buy 1 unit at price 862.760010, total balance 25.430112\n",
|
||||
"day 118, sell 1 unit at price 872.299988, investment 2.368210 %, total balance 897.730100,\n",
|
||||
"day 119: buy 1 unit at price 871.729980, total balance 26.000120\n",
|
||||
"day 120, sell 1 unit at price 874.250000, investment 5.383379 %, total balance 900.250120,\n",
|
||||
"day 121, sell 1 unit at price 905.960022, investment 11.238539 %, total balance 1806.210142,\n",
|
||||
"day 122, sell 1 unit at price 912.570007, investment 9.761734 %, total balance 2718.780149,\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 10.473022 %, total balance 3635.220151,\n",
|
||||
"day 124, sell 1 unit at price 927.039978, investment 10.552739 %, total balance 4562.260129,\n",
|
||||
"day 125, sell 1 unit at price 931.659973, investment 11.633532 %, total balance 5493.920102,\n",
|
||||
"day 126, sell 1 unit at price 927.130005, investment 11.988452 %, total balance 6421.050107,\n",
|
||||
"day 127, sell 1 unit at price 934.299988, investment 13.285561 %, total balance 7355.350095,\n",
|
||||
"day 128, sell 1 unit at price 932.169983, investment 13.216738 %, total balance 8287.520078,\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 7358.740049\n",
|
||||
"day 130, sell 1 unit at price 930.599976, investment 12.893047 %, total balance 8289.340025,\n",
|
||||
"day 132: buy 1 unit at price 937.080017, total balance 7352.260008\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 12.041819 %, total balance 8295.260008,\n",
|
||||
"day 134: buy 1 unit at price 919.619995, total balance 7375.640013\n",
|
||||
"day 135: buy 1 unit at price 930.239990, total balance 6445.400023\n",
|
||||
"day 136: buy 1 unit at price 934.010010, total balance 5511.390013\n",
|
||||
"day 138: buy 1 unit at price 948.820007, total balance 4562.570006\n",
|
||||
"day 140: buy 1 unit at price 969.539978, total balance 3593.030028\n",
|
||||
"day 142, sell 1 unit at price 975.880005, investment 13.111409 %, total balance 4568.910033,\n",
|
||||
"day 143, sell 1 unit at price 964.859985, investment 10.683355 %, total balance 5533.770018,\n",
|
||||
"day 144, sell 1 unit at price 966.950012, investment 4.109690 %, total balance 6500.720030,\n",
|
||||
"day 146, sell 1 unit at price 983.679993, investment 4.972892 %, total balance 7484.400023,\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance 6507.830016\n",
|
||||
"day 148, sell 1 unit at price 980.940002, investment 6.667972 %, total balance 7488.770018,\n",
|
||||
"day 149: buy 1 unit at price 983.409973, total balance 6505.360045\n",
|
||||
"day 150, sell 1 unit at price 949.830017, investment 2.105911 %, total balance 7455.190062,\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance 6512.290038\n",
|
||||
"day 152, sell 1 unit at price 953.400024, investment 2.075996 %, total balance 7465.690062,\n",
|
||||
"day 154: buy 1 unit at price 942.309998, total balance 6523.380064\n",
|
||||
"day 155: buy 1 unit at price 939.780029, total balance 5583.600035\n",
|
||||
"day 157: buy 1 unit at price 950.630005, total balance 4632.970030\n",
|
||||
"day 158, sell 1 unit at price 959.450012, investment 1.120339 %, total balance 5592.420042,\n",
|
||||
"day 159: buy 1 unit at price 957.090027, total balance 4635.330015\n",
|
||||
"day 160, sell 1 unit at price 965.590027, investment -0.407405 %, total balance 5600.920042,\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment -2.488300 %, total balance 6553.190062,\n",
|
||||
"day 163: buy 1 unit at price 940.489990, total balance 5612.700072\n",
|
||||
"day 165: buy 1 unit at price 908.729980, total balance 4703.970092\n",
|
||||
"day 166, sell 1 unit at price 898.700012, investment -8.613901 %, total balance 5602.670104,\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment -2.578216 %, total balance 6521.260131,\n",
|
||||
"day 170: buy 1 unit at price 928.799988, total balance 5592.460143\n",
|
||||
"day 171, sell 1 unit at price 930.090027, investment -1.296810 %, total balance 6522.550170,\n",
|
||||
"day 172: buy 1 unit at price 943.830017, total balance 5578.720153\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 0.785284 %, total balance 6525.880126,\n",
|
||||
"day 174: buy 1 unit at price 955.989990, total balance 5569.890136\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 4616.470153\n",
|
||||
"day 177, sell 1 unit at price 970.890015, investment 2.131219 %, total balance 5587.360168,\n",
|
||||
"day 178, sell 1 unit at price 968.150024, investment 1.155586 %, total balance 6555.510192,\n",
|
||||
"day 180, sell 1 unit at price 980.340027, investment 4.237157 %, total balance 7535.850219,\n",
|
||||
"day 181, sell 1 unit at price 950.700012, investment 4.618537 %, total balance 8486.550231,\n",
|
||||
"day 182: buy 1 unit at price 947.799988, total balance 7538.750243\n",
|
||||
"day 183, sell 1 unit at price 934.090027, investment 0.569556 %, total balance 8472.840270,\n",
|
||||
"day 185, sell 1 unit at price 930.500000, investment -1.412332 %, total balance 9403.340270,\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 8472.510253\n",
|
||||
"day 187: buy 1 unit at price 930.390015, total balance 7542.120238\n",
|
||||
"day 188, sell 1 unit at price 923.650024, investment -3.382877 %, total balance 8465.770262,\n",
|
||||
"day 189: buy 1 unit at price 927.960022, total balance 7537.810240\n",
|
||||
"day 191: buy 1 unit at price 926.789978, total balance 6611.020262\n",
|
||||
"day 192, sell 1 unit at price 922.900024, investment -3.201103 %, total balance 7533.920286,\n",
|
||||
"day 193: buy 1 unit at price 907.239990, total balance 6626.680296\n",
|
||||
"day 195: buy 1 unit at price 922.669983, total balance 5704.010313\n",
|
||||
"day 197, sell 1 unit at price 926.960022, investment -2.198773 %, total balance 6630.970335,\n",
|
||||
"day 199, sell 1 unit at price 910.669983, investment -2.165813 %, total balance 7541.640318,\n",
|
||||
"day 201, sell 1 unit at price 924.690002, investment -0.612648 %, total balance 8466.330320,\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 7539.330320\n",
|
||||
"day 204, sell 1 unit at price 915.890015, investment -1.300703 %, total balance 8455.220335,\n",
|
||||
"day 205, sell 1 unit at price 913.809998, investment -1.400531 %, total balance 9369.030333,\n",
|
||||
"day 206: buy 1 unit at price 921.289978, total balance 8447.740355\n",
|
||||
"day 207, sell 1 unit at price 929.570007, investment 2.461313 %, total balance 9377.310362,\n",
|
||||
"day 208: buy 1 unit at price 939.330017, total balance 8437.980345\n",
|
||||
"day 209, sell 1 unit at price 937.340027, investment 1.589956 %, total balance 9375.320372,\n",
|
||||
"day 211, sell 1 unit at price 927.809998, investment 0.087378 %, total balance 10303.130370,\n",
|
||||
"day 212: buy 1 unit at price 935.950012, total balance 9367.180358\n",
|
||||
"day 213: buy 1 unit at price 926.500000, total balance 8440.680358\n",
|
||||
"day 214, sell 1 unit at price 929.080017, investment 0.845558 %, total balance 9369.760375,\n",
|
||||
"day 215, sell 1 unit at price 932.070007, investment -0.772892 %, total balance 10301.830382,\n",
|
||||
"day 217: buy 1 unit at price 925.109985, total balance 9376.720397\n",
|
||||
"day 218: buy 1 unit at price 920.289978, total balance 8456.430419\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance 7541.430419\n",
|
||||
"day 220, sell 1 unit at price 921.809998, investment -1.510766 %, total balance 8463.240417,\n",
|
||||
"day 223: buy 1 unit at price 928.530029, total balance 7534.710388\n",
|
||||
"day 224: buy 1 unit at price 920.969971, total balance 6613.740417\n",
|
||||
"day 225: buy 1 unit at price 924.859985, total balance 5688.880432\n",
|
||||
"day 226, sell 1 unit at price 944.489990, investment 1.941715 %, total balance 6633.370422,\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 2.636445 %, total balance 7582.870422,\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 3.583658 %, total balance 8536.140442,\n",
|
||||
"day 230, sell 1 unit at price 957.789978, investment 4.676500 %, total balance 9493.930420,\n",
|
||||
"day 231: buy 1 unit at price 951.679993, total balance 8542.250427\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 4.461890 %, total balance 9512.210449,\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 6.289026 %, total balance 10491.100464,\n",
|
||||
"day 235, sell 1 unit at price 972.599976, investment 5.161861 %, total balance 11463.700440,\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 3.947756 %, total balance 12452.950440,\n",
|
||||
"day 238: buy 1 unit at price 989.679993, total balance 11463.270447\n",
|
||||
"day 239, sell 1 unit at price 992.000000, investment 0.234420 %, total balance 12455.270447,\n",
|
||||
"day 243: buy 1 unit at price 988.200012, total balance 11467.070435\n",
|
||||
"day 244: buy 1 unit at price 968.450012, total balance 10498.620423\n",
|
||||
"day 245: buy 1 unit at price 970.539978, total balance 9528.080445\n",
|
||||
"day 246, sell 1 unit at price 973.330017, investment -1.504756 %, total balance 10501.410462,\n",
|
||||
"day 248, sell 1 unit at price 1019.270020, investment 5.247561 %, total balance 11520.680482,\n",
|
||||
"day 249, sell 1 unit at price 1017.109985, investment 4.798361 %, total balance 12537.790467,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = neural_evolve.buy(fittest_nets)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUg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jwmbrF9zU/ZP/8La8zaw/bxf4uVcQzO9k3DCu7/G9KuXlhdQ3vr8AVfQu+dN4G/Katc/kKswhwDsT7DcNunYVWcLMR6wL+Nqhme+vISVnbJP5bQbz7wpT227j2sbN9JWONAK+z/fg/rHijm+H22B2ssWoLWurwW7fkCKFJK/Qorg1yO1fUyyZ+drUboZ1mJPXZxOfB3rXWVsZ9KqSlYfw8OYXU7nYs1VjbcsS7Cunc2cnzs5lf+giz2Nt2wqtL+NEKTZgOfaKtYUWg7fVgvjcZg3Z8/BrAzrouB3ytrupNhWC+lxtn7Rvz+hvgLcJfWulQptRUYrazKuhPs/YUQIiwZ4yeEaOr+CNyhlDqklLpFa62xxjH9DWt8zYVYRSHKw22PFWTkYWUEv8caIxOrf2KN21sDfIUVCLgBjx0YzcUKCA5iZRaX1PIa+2I9jBdjFc94RGv93zDbjcMqJnMOcEgpVWz/848rHI4VAJfY/7uW42OMfFjdOnfa7f0zVsXJQJvtgG0mYQqiKKVuV0qFzl92JbA4TJCYiBWMFGMFBJ9iPQD7PY7VVXUtVqC81F6G1vqQ1rrA/w8rgDjiD+y01suxHuiXYhXX6YP12Ye267kwmZruHL8Xwvk/YIZS6qBS6mGt9X6sz/tmrBcLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,680 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from sklearn.neighbors import NearestNeighbors\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"\n",
|
||||
"novelty_search_threshold = 6\n",
|
||||
"novelty_log_maxlen = 1000\n",
|
||||
"backlog_maxsize = 500\n",
|
||||
"novelty_log_add_amount = 3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class neuralnetwork:\n",
|
||||
" def __init__(self, id_, hidden_size = 128):\n",
|
||||
" self.W1 = np.random.randn(window_size, hidden_size) / np.sqrt(window_size)\n",
|
||||
" self.W2 = np.random.randn(hidden_size, 3) / np.sqrt(hidden_size)\n",
|
||||
" self.fitness = 0\n",
|
||||
" self.last_features = None\n",
|
||||
" self.id = id_\n",
|
||||
"\n",
|
||||
"def relu(X):\n",
|
||||
" return np.maximum(X, 0)\n",
|
||||
" \n",
|
||||
"def softmax(X):\n",
|
||||
" e_x = np.exp(X - np.max(X, axis=-1, keepdims=True))\n",
|
||||
" return e_x / np.sum(e_x, axis=-1, keepdims=True)\n",
|
||||
"\n",
|
||||
"def feed_forward(X, nets):\n",
|
||||
" a1 = np.dot(X, nets.W1)\n",
|
||||
" z1 = relu(a1)\n",
|
||||
" a2 = np.dot(z1, nets.W2)\n",
|
||||
" return softmax(a2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class NeuroEvolution:\n",
|
||||
" def __init__(self, population_size, mutation_rate, model_generator,\n",
|
||||
" state_size, window_size, trend, skip, initial_money):\n",
|
||||
" self.population_size = population_size\n",
|
||||
" self.mutation_rate = mutation_rate\n",
|
||||
" self.model_generator = model_generator\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" self.initial_money = initial_money\n",
|
||||
" self.generation_backlog = []\n",
|
||||
" self.novel_backlog = []\n",
|
||||
" self.novel_pop = []\n",
|
||||
" \n",
|
||||
" def _initialize_population(self):\n",
|
||||
" self.population = []\n",
|
||||
" for i in range(self.population_size):\n",
|
||||
" self.population.append(self.model_generator(i))\n",
|
||||
" \n",
|
||||
" def _memorize(self, q, i, limit):\n",
|
||||
" q.append(i)\n",
|
||||
" if len(q) > limit:\n",
|
||||
" q.pop()\n",
|
||||
" \n",
|
||||
" def mutate(self, individual, scale=1.0):\n",
|
||||
" mutation_mask = np.random.binomial(1, p=self.mutation_rate, size=individual.W1.shape)\n",
|
||||
" individual.W1 += np.random.normal(loc=0, scale=scale, size=individual.W1.shape) * mutation_mask\n",
|
||||
" mutation_mask = np.random.binomial(1, p=self.mutation_rate, size=individual.W2.shape)\n",
|
||||
" individual.W2 += np.random.normal(loc=0, scale=scale, size=individual.W2.shape) * mutation_mask\n",
|
||||
" return individual\n",
|
||||
" \n",
|
||||
" def inherit_weights(self, parent, child):\n",
|
||||
" child.W1 = parent.W1.copy()\n",
|
||||
" child.W2 = parent.W2.copy()\n",
|
||||
" return child\n",
|
||||
" \n",
|
||||
" def crossover(self, parent1, parent2):\n",
|
||||
" child1 = self.model_generator((parent1.id+1)*10)\n",
|
||||
" child1 = self.inherit_weights(parent1, child1)\n",
|
||||
" child2 = self.model_generator((parent2.id+1)*10)\n",
|
||||
" child2 = self.inherit_weights(parent2, child2)\n",
|
||||
" # first W\n",
|
||||
" n_neurons = child1.W1.shape[1]\n",
|
||||
" cutoff = np.random.randint(0, n_neurons)\n",
|
||||
" child1.W1[:, cutoff:] = parent2.W1[:, cutoff:].copy()\n",
|
||||
" child2.W1[:, cutoff:] = parent1.W1[:, cutoff:].copy()\n",
|
||||
" # second W\n",
|
||||
" n_neurons = child1.W2.shape[1]\n",
|
||||
" cutoff = np.random.randint(0, n_neurons)\n",
|
||||
" child1.W2[:, cutoff:] = parent2.W2[:, cutoff:].copy()\n",
|
||||
" child2.W2[:, cutoff:] = parent1.W2[:, cutoff:].copy()\n",
|
||||
" return child1, child2\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])\n",
|
||||
" \n",
|
||||
" def act(self, p, state):\n",
|
||||
" logits = feed_forward(state, p)\n",
|
||||
" return np.argmax(logits, 1)[0]\n",
|
||||
" \n",
|
||||
" def buy(self, individual):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" inventory = []\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" \n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(individual, state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((self.trend[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, self.trend[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" state = next_state\n",
|
||||
" \n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def calculate_fitness(self):\n",
|
||||
" for i in range(self.population_size):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" inventory = []\n",
|
||||
" \n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(self.population[i], state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
"\n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
"\n",
|
||||
" state = next_state\n",
|
||||
" invest = ((starting_money - initial_money) / initial_money) * 100\n",
|
||||
" self.population[i].fitness = invest\n",
|
||||
" self.population[i].last_features = self.population[i].W2.flatten()\n",
|
||||
" \n",
|
||||
" def evaluate(self, individual, backlog, pop, k = 4):\n",
|
||||
" score = 0\n",
|
||||
" if len(backlog):\n",
|
||||
" x = np.array(backlog)\n",
|
||||
" nn = NearestNeighbors(n_neighbors = k, metric = 'euclidean').fit(np.array(backlog))\n",
|
||||
" d, _ = nn.kneighbors([individual])\n",
|
||||
" score += np.mean(d)\n",
|
||||
" \n",
|
||||
" if len(pop):\n",
|
||||
" nn = NearestNeighbors(n_neighbors = k, metric = 'euclidean').fit(np.array(pop))\n",
|
||||
" d, _ = nn.kneighbors([individual])\n",
|
||||
" score += np.mean(d)\n",
|
||||
" \n",
|
||||
" return score\n",
|
||||
" \n",
|
||||
" def evolve(self, generations=20, checkpoint= 5):\n",
|
||||
" self._initialize_population()\n",
|
||||
" n_winners = int(self.population_size * 0.4)\n",
|
||||
" n_parents = self.population_size - n_winners\n",
|
||||
" for epoch in range(generations):\n",
|
||||
" self.calculate_fitness()\n",
|
||||
" scores = [self.evaluate(p.last_features, self.novel_backlog, self.novel_pop) for p in self.population]\n",
|
||||
" sort_fitness = np.argsort(scores)[::-1]\n",
|
||||
" self.population = [self.population[i] for i in sort_fitness]\n",
|
||||
" fittest_individual = self.population[0]\n",
|
||||
" if (epoch+1) % checkpoint == 0:\n",
|
||||
" print('epoch %d, fittest individual %d with accuracy %f'%(epoch+1, sort_fitness[0], \n",
|
||||
" fittest_individual.fitness))\n",
|
||||
" next_population = [self.population[i] for i in range(n_winners)]\n",
|
||||
" total_fitness = np.sum([np.abs(i.fitness) for i in self.population])\n",
|
||||
" parent_probabilities = [np.abs(i.fitness / total_fitness) for i in self.population]\n",
|
||||
" parents = np.random.choice(self.population, size=n_parents, p=parent_probabilities, replace=False)\n",
|
||||
" \n",
|
||||
" for p in next_population:\n",
|
||||
" if p.last_features is not None:\n",
|
||||
" self._memorize(self.novel_pop, p.last_features, backlog_maxsize)\n",
|
||||
" if np.random.randint(0,10) < novelty_search_threshold:\n",
|
||||
" self._memorize(self.novel_backlog, p.last_features, novelty_log_maxlen)\n",
|
||||
" \n",
|
||||
" for i in np.arange(0, len(parents), 2):\n",
|
||||
" child1, child2 = self.crossover(parents[i], parents[i+1])\n",
|
||||
" next_population += [self.mutate(child1), self.mutate(child2)]\n",
|
||||
" self.population = next_population\n",
|
||||
" \n",
|
||||
" if np.random.randint(0,10) < novelty_search_threshold:\n",
|
||||
" pop_sorted = sorted(self.population, key=lambda p: p.fitness, reverse=True)\n",
|
||||
" self.generation_backlog.append(pop_sorted[0])\n",
|
||||
" print('novel add fittest, score: %f, backlog size: %d'%(pop_sorted[0].fitness, \n",
|
||||
" len(self.generation_backlog)))\n",
|
||||
" generation_backlog_temp = self.generation_backlog\n",
|
||||
" if len(self.generation_backlog) > backlog_maxsize:\n",
|
||||
" generation_backlog_temp = random.sample(generation_backlog, backlog_maxsize)\n",
|
||||
" for p in generation_backlog_temp:\n",
|
||||
" if p.last_features is not None:\n",
|
||||
" self._memorize(self.novel_backlog, p.last_features, novelty_log_maxlen)\n",
|
||||
" \n",
|
||||
" return fittest_individual"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"population_size = 100\n",
|
||||
"generations = 100\n",
|
||||
"mutation_rate = 0.1\n",
|
||||
"neural_evolve = NeuroEvolution(population_size, mutation_rate, neuralnetwork,\n",
|
||||
" window_size, window_size, close, skip, initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"novel add fittest, score: 5.960001, backlog size: 16\n",
|
||||
"novel add fittest, score: 2.560349, backlog size: 17\n",
|
||||
"epoch 5, fittest individual 86 with accuracy -99.353801\n",
|
||||
"novel add fittest, score: 2.073401, backlog size: 18\n",
|
||||
"epoch 10, fittest individual 53 with accuracy -99.622801\n",
|
||||
"novel add fittest, score: 9.773855, backlog size: 19\n",
|
||||
"novel add fittest, score: 1.068502, backlog size: 20\n",
|
||||
"novel add fittest, score: 1.733602, backlog size: 21\n",
|
||||
"epoch 15, fittest individual 49 with accuracy -94.018300\n",
|
||||
"novel add fittest, score: 1.439049, backlog size: 22\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 23\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 24\n",
|
||||
"novel add fittest, score: 3.052850, backlog size: 25\n",
|
||||
"epoch 20, fittest individual 83 with accuracy -42.284500\n",
|
||||
"novel add fittest, score: 3.284498, backlog size: 26\n",
|
||||
"novel add fittest, score: 3.284498, backlog size: 27\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 28\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 29\n",
|
||||
"epoch 25, fittest individual 43 with accuracy -99.809850\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 30\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 31\n",
|
||||
"novel add fittest, score: 4.712602, backlog size: 32\n",
|
||||
"novel add fittest, score: 4.712602, backlog size: 33\n",
|
||||
"epoch 30, fittest individual 51 with accuracy -94.734501\n",
|
||||
"novel add fittest, score: 4.712602, backlog size: 34\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 35\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 36\n",
|
||||
"epoch 35, fittest individual 74 with accuracy -99.895853\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 37\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 38\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 39\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 40\n",
|
||||
"epoch 40, fittest individual 50 with accuracy -99.900900\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 41\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 42\n",
|
||||
"epoch 45, fittest individual 98 with accuracy -92.305952\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 43\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 44\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 45\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 46\n",
|
||||
"epoch 50, fittest individual 55 with accuracy -99.841901\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 47\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 48\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 49\n",
|
||||
"epoch 55, fittest individual 0 with accuracy -99.351002\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 50\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 51\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 52\n",
|
||||
"epoch 60, fittest individual 56 with accuracy -91.532553\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 53\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 54\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 55\n",
|
||||
"epoch 65, fittest individual 0 with accuracy -99.389200\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 56\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 57\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 58\n",
|
||||
"epoch 70, fittest individual 68 with accuracy -90.999901\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 59\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 60\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 61\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 62\n",
|
||||
"epoch 75, fittest individual 50 with accuracy -98.881400\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 63\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 64\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 65\n",
|
||||
"epoch 80, fittest individual 0 with accuracy -91.959200\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 66\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 67\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 68\n",
|
||||
"epoch 85, fittest individual 0 with accuracy -94.175699\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 69\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 70\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 71\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 72\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 73\n",
|
||||
"epoch 90, fittest individual 60 with accuracy -93.196199\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 74\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 75\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 76\n",
|
||||
"epoch 95, fittest individual 66 with accuracy -93.122201\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 77\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 78\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 79\n",
|
||||
"epoch 100, fittest individual 52 with accuracy -93.193801\n",
|
||||
"novel add fittest, score: 0.000000, backlog size: 80\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fittest_nets = neural_evolve.evolve(100)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 1: buy 1 unit at price 762.130005, total balance 9237.869995\n",
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 8455.349975\n",
|
||||
"day 4: buy 1 unit at price 790.510010, total balance 7664.839965\n",
|
||||
"day 5, sell 1 unit at price 785.309998, investment 3.041475 %, total balance 8450.149963,\n",
|
||||
"day 9: buy 1 unit at price 758.489990, total balance 7691.659973\n",
|
||||
"day 10: buy 1 unit at price 764.479980, total balance 6927.179993\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 6155.950013\n",
|
||||
"day 15: buy 1 unit at price 760.989990, total balance 5394.960023\n",
|
||||
"day 16: buy 1 unit at price 761.679993, total balance 4633.280030\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 3865.040040\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 3114.540040\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 0.865148 %, total balance 3903.830018,\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 3113.030030\n",
|
||||
"day 37: buy 1 unit at price 791.549988, total balance 2321.480042\n",
|
||||
"day 39: buy 1 unit at price 782.789978, total balance 1538.690064\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 766.870057\n",
|
||||
"day 43: buy 1 unit at price 794.020020, total balance -27.149963\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance -833.299987\n",
|
||||
"day 45: buy 1 unit at price 806.650024, total balance -1639.950011\n",
|
||||
"day 48: buy 1 unit at price 806.359985, total balance -2446.309996\n",
|
||||
"day 49: buy 1 unit at price 807.880005, total balance -3254.190001\n",
|
||||
"day 50: buy 1 unit at price 804.609985, total balance -4058.799986\n",
|
||||
"day 52: buy 1 unit at price 802.174988, total balance -4860.974974\n",
|
||||
"day 53: buy 1 unit at price 805.020020, total balance -5665.994994\n",
|
||||
"day 54, sell 1 unit at price 819.309998, investment 3.643216 %, total balance -4846.684996,\n",
|
||||
"day 55: buy 1 unit at price 823.869995, total balance -5670.554991\n",
|
||||
"day 60: buy 1 unit at price 796.789978, total balance -6467.344969\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance -7263.039976\n",
|
||||
"day 63: buy 1 unit at price 801.489990, total balance -8064.529966\n",
|
||||
"day 65: buy 1 unit at price 806.969971, total balance -8871.499937\n",
|
||||
"day 66: buy 1 unit at price 808.380005, total balance -9679.879942\n",
|
||||
"day 67: buy 1 unit at price 809.559998, total balance -10489.439940\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance -11303.109923\n",
|
||||
"day 69: buy 1 unit at price 819.239990, total balance -12122.349913\n",
|
||||
"day 73, sell 1 unit at price 828.070007, investment 9.173492 %, total balance -11294.279906,\n",
|
||||
"day 80: buy 1 unit at price 835.239990, total balance -12129.519896\n",
|
||||
"day 84: buy 1 unit at price 831.909973, total balance -12961.429869\n",
|
||||
"day 85, sell 1 unit at price 835.369995, investment 9.272972 %, total balance -12126.059874,\n",
|
||||
"day 86, sell 1 unit at price 838.679993, investment 8.745772 %, total balance -11287.379881,\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance -12132.919859\n",
|
||||
"day 89: buy 1 unit at price 845.619995, total balance -12978.539854\n",
|
||||
"day 90: buy 1 unit at price 847.200012, total balance -13825.739866\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance -14677.859861\n",
|
||||
"day 93: buy 1 unit at price 848.400024, total balance -15526.259885\n",
|
||||
"day 94: buy 1 unit at price 830.460022, total balance -16356.719907\n",
|
||||
"day 96: buy 1 unit at price 817.580017, total balance -17174.299924\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance -17988.729917\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance -18808.239927\n",
|
||||
"day 99: buy 1 unit at price 820.919983, total balance -19629.159910\n",
|
||||
"day 101, sell 1 unit at price 831.500000, investment 9.265563 %, total balance -18797.659910,\n",
|
||||
"day 102: buy 1 unit at price 829.559998, total balance -19627.219908\n",
|
||||
"day 103, sell 1 unit at price 838.549988, investment 10.092164 %, total balance -18788.669920,\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance -19623.239927\n",
|
||||
"day 105: buy 1 unit at price 831.409973, total balance -20454.649900\n",
|
||||
"day 107: buy 1 unit at price 824.669983, total balance -21279.319883\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance -22104.049863\n",
|
||||
"day 109: buy 1 unit at price 823.349976, total balance -22927.399839\n",
|
||||
"day 112: buy 1 unit at price 837.169983, total balance -23764.569822\n",
|
||||
"day 116: buy 1 unit at price 843.190002, total balance -24607.759824\n",
|
||||
"day 119: buy 1 unit at price 871.729980, total balance -25479.489804\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance -26411.149777\n",
|
||||
"day 127: buy 1 unit at price 934.299988, total balance -27345.449765\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance -28274.229794\n",
|
||||
"day 130: buy 1 unit at price 930.599976, total balance -29204.829770\n",
|
||||
"day 133: buy 1 unit at price 943.000000, total balance -30147.829770\n",
|
||||
"day 134: buy 1 unit at price 919.619995, total balance -31067.449765\n",
|
||||
"day 137, sell 1 unit at price 941.859985, investment 22.599708 %, total balance -30125.589780,\n",
|
||||
"day 139: buy 1 unit at price 954.960022, total balance -31080.549802\n",
|
||||
"day 140: buy 1 unit at price 969.539978, total balance -32050.089780\n",
|
||||
"day 141, sell 1 unit at price 971.469971, investment 29.443034 %, total balance -31078.619809,\n",
|
||||
"day 142: buy 1 unit at price 975.880005, total balance -32054.499814\n",
|
||||
"day 143: buy 1 unit at price 964.859985, total balance -33019.359799\n",
|
||||
"day 145: buy 1 unit at price 975.599976, total balance -33994.959775\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance -34971.529782\n",
|
||||
"day 148: buy 1 unit at price 980.940002, total balance -35952.469784\n",
|
||||
"day 149: buy 1 unit at price 983.409973, total balance -36935.879757\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance -37878.779781\n",
|
||||
"day 152: buy 1 unit at price 953.400024, total balance -38832.179805\n",
|
||||
"day 153: buy 1 unit at price 950.760010, total balance -39782.939815\n",
|
||||
"day 154: buy 1 unit at price 942.309998, total balance -40725.249813\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance -41682.619808\n",
|
||||
"day 157, sell 1 unit at price 950.630005, investment 20.211181 %, total balance -40731.989803,\n",
|
||||
"day 158: buy 1 unit at price 959.450012, total balance -41691.439815\n",
|
||||
"day 159: buy 1 unit at price 957.090027, total balance -42648.529842\n",
|
||||
"day 164: buy 1 unit at price 917.789978, total balance -43566.319820\n",
|
||||
"day 165: buy 1 unit at price 908.729980, total balance -44475.049800\n",
|
||||
"day 167: buy 1 unit at price 911.710022, total balance -45386.759822\n",
|
||||
"day 168, sell 1 unit at price 906.690002, investment 14.546146 %, total balance -44480.069820,\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment 17.348210 %, total balance -43561.479793,\n",
|
||||
"day 170: buy 1 unit at price 928.799988, total balance -44490.279781\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 22.717728 %, total balance -43543.119808,\n",
|
||||
"day 184: buy 1 unit at price 941.530029, total balance -44484.649837\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance -45415.479854\n",
|
||||
"day 187: buy 1 unit at price 930.390015, total balance -46345.869869\n",
|
||||
"day 190: buy 1 unit at price 929.359985, total balance -47275.229854\n",
|
||||
"day 191: buy 1 unit at price 926.789978, total balance -48202.019832\n",
|
||||
"day 194: buy 1 unit at price 914.390015, total balance -49116.409847\n",
|
||||
"day 196: buy 1 unit at price 922.219971, total balance -50038.629818\n",
|
||||
"day 198: buy 1 unit at price 910.979980, total balance -50949.609798\n",
|
||||
"day 201: buy 1 unit at price 924.690002, total balance -51874.299800\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance -52801.299800\n",
|
||||
"day 206, sell 1 unit at price 921.289978, investment 16.028558 %, total balance -51880.009822,\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance -52809.579829\n",
|
||||
"day 208: buy 1 unit at price 939.330017, total balance -53748.909846\n",
|
||||
"day 213: buy 1 unit at price 926.500000, total balance -54675.409846\n",
|
||||
"day 218: buy 1 unit at price 920.289978, total balance -55595.699824\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance -56510.699824\n",
|
||||
"day 222: buy 1 unit at price 932.450012, total balance -57443.149836\n",
|
||||
"day 224: buy 1 unit at price 920.969971, total balance -58364.119807\n",
|
||||
"day 227: buy 1 unit at price 949.500000, total balance -59313.619807\n",
|
||||
"day 228: buy 1 unit at price 959.109985, total balance -60272.729792\n",
|
||||
"day 229: buy 1 unit at price 953.270020, total balance -61225.999812\n",
|
||||
"day 232: buy 1 unit at price 969.960022, total balance -62195.959834\n",
|
||||
"day 238: buy 1 unit at price 989.679993, total balance -63185.639827\n",
|
||||
"day 240: buy 1 unit at price 992.179993, total balance -64177.819820\n",
|
||||
"day 242: buy 1 unit at price 984.450012, total balance -65162.269832\n",
|
||||
"day 244: buy 1 unit at price 968.450012, total balance -66130.719844\n",
|
||||
"day 247: buy 1 unit at price 972.559998, total balance -67103.279842\n",
|
||||
"day 248: buy 1 unit at price 1019.270020, total balance -68122.549862\n",
|
||||
"day 249: buy 1 unit at price 1017.109985, total balance -69139.659847\n",
|
||||
"day 250: buy 1 unit at price 1016.640015, total balance -70156.299862\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = neural_evolve.buy(fittest_nets)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJ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Up+zse80T6OkzH9fsI+6fY24sxGbmmgVNL3sKUTX2Myab9IWRbIDtTBHjt7N9J2MFKWyilTseUKG7BZCF9NGQmW7MUM75tASYjcjWQp7XebZ/7PUwgeCWm43sWIWWxSql4wGX/GKeUitda19rbzsVk06oxgfNFNHRYv8GMp7xVKfUgJsN4LCZDFM5TmDLUE+zOaej1x2PGl63DBNfPYsoDS+3t+wNl9j8nYTqlgc7rRswkHxdgSgsHYrIWX0XTTjsz9AAmq7K1aaPtz3WBDj/hSiFm/FSs1rrOngzlLeB+pdQlmJkTf0/DjI2N9rdfSwFK7KDvJ5gHFfMjfIbtprUusq/lRcwMjRvs1/OVUvMxQeEdmMzXGMxkOF8rpX6BGZe4E/PAxKLh3gx819obBD6N+awu1VpnK7PEwWFa6w9bO9B+7xOVUk4d3SRIYL4rv7AfDBRhHibE0ND+V4DblVLLMA8Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def abcd(trend, skip_loop = 4, ma = 7):\n",
|
||||
" ma = pd.Series(trend).rolling(ma).mean().values\n",
|
||||
" x = []\n",
|
||||
" for a in range(ma.shape[0]):\n",
|
||||
" for b in range(a, ma.shape[0], skip_loop):\n",
|
||||
" for c in range(b, ma.shape[0], skip_loop):\n",
|
||||
" for d in range(c, ma.shape[0], skip_loop):\n",
|
||||
" if ma[b] > ma[a] and \\\n",
|
||||
" (ma[c] < ma[b] and ma[c] > ma[a]) \\\n",
|
||||
" and ma[d] > ma[b]:\n",
|
||||
" x.append([a,b,c,d])\n",
|
||||
" x_np = np.array(x)\n",
|
||||
" ac = x_np[:,0].tolist() + x_np[:,2].tolist()\n",
|
||||
" bd = x_np[:,1].tolist() + x_np[:,3].tolist()\n",
|
||||
" ac_set = set(ac)\n",
|
||||
" bd_set = set(bd)\n",
|
||||
" signal = np.zeros(len(trend))\n",
|
||||
" buy = list(ac_set - bd_set)\n",
|
||||
" sell = list(list(bd_set - ac_set))\n",
|
||||
" signal[buy] = 1.0\n",
|
||||
" signal[sell] = -1.0\n",
|
||||
" return signal"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"CPU times: user 1.08 s, sys: 8 ms, total: 1.09 s\n",
|
||||
"Wall time: 1.09 s\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%time\n",
|
||||
"signal = abcd(df['Close'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def buy_stock(\n",
|
||||
" real_movement,\n",
|
||||
" signal,\n",
|
||||
" initial_money = 10000,\n",
|
||||
" max_buy = 1,\n",
|
||||
" max_sell = 1,\n",
|
||||
"):\n",
|
||||
" \"\"\"\n",
|
||||
" real_movement = actual movement in the real world\n",
|
||||
" delay = how much interval you want to delay to change our decision from buy to sell, vice versa\n",
|
||||
" initial_state = 1 is buy, 0 is sell\n",
|
||||
" initial_money = 10000, ignore what kind of currency\n",
|
||||
" max_buy = max quantity for share to buy\n",
|
||||
" max_sell = max quantity for share to sell\n",
|
||||
" \"\"\"\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" states_money = []\n",
|
||||
" current_inventory = 0\n",
|
||||
" \n",
|
||||
" def buy(i, initial_money, current_inventory):\n",
|
||||
" shares = initial_money // real_movement[i]\n",
|
||||
" if shares < 1:\n",
|
||||
" print(\n",
|
||||
" 'day %d: total balances %f, not enough money to buy a unit price %f'\n",
|
||||
" % (i, initial_money, real_movement[i])\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" if shares > max_buy:\n",
|
||||
" buy_units = max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = shares\n",
|
||||
" initial_money -= buy_units * real_movement[i]\n",
|
||||
" current_inventory += buy_units\n",
|
||||
" print(\n",
|
||||
" 'day %d: buy %d units at price %f, total balance %f'\n",
|
||||
" % (i, buy_units, buy_units * real_movement[i], initial_money)\n",
|
||||
" )\n",
|
||||
" states_buy.append(0)\n",
|
||||
" return initial_money, current_inventory\n",
|
||||
" \n",
|
||||
" for i in range(real_movement.shape[0]):\n",
|
||||
" state = signal[i]\n",
|
||||
" if state == 1:\n",
|
||||
" initial_money, current_inventory = buy(\n",
|
||||
" i, initial_money, current_inventory\n",
|
||||
" )\n",
|
||||
" states_buy.append(i)\n",
|
||||
" elif state == -1:\n",
|
||||
" if current_inventory == 0:\n",
|
||||
" print('day %d: cannot sell anything, inventory 0' % (i))\n",
|
||||
" else:\n",
|
||||
" if current_inventory > max_sell:\n",
|
||||
" sell_units = max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = current_inventory\n",
|
||||
" current_inventory -= sell_units\n",
|
||||
" total_sell = sell_units * real_movement[i]\n",
|
||||
" initial_money += total_sell\n",
|
||||
" try:\n",
|
||||
" invest = (\n",
|
||||
" (real_movement[i] - real_movement[states_buy[-1]])\n",
|
||||
" / real_movement[states_buy[-1]]\n",
|
||||
" ) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (i, sell_units, total_sell, invest, initial_money)\n",
|
||||
" )\n",
|
||||
" states_sell.append(i)\n",
|
||||
" states_money.append(initial_money)\n",
|
||||
" \n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest, states_money"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 6: buy 1 units at price 762.559998, total balance 9237.440002\n",
|
||||
"day 7: buy 1 units at price 754.020020, total balance 8483.419982\n",
|
||||
"day 8: buy 1 units at price 736.080017, total balance 7747.339965\n",
|
||||
"day 9: buy 1 units at price 758.489990, total balance 6988.849975\n",
|
||||
"day 10: buy 1 units at price 764.479980, total balance 6224.369995\n",
|
||||
"day 11: buy 1 units at price 771.229980, total balance 5453.140015\n",
|
||||
"day 12: buy 1 units at price 760.539978, total balance 4692.600037\n",
|
||||
"day 13: buy 1 units at price 769.200012, total balance 3923.400025\n",
|
||||
"day 14: buy 1 units at price 768.270020, total balance 3155.130005\n",
|
||||
"day 15: buy 1 units at price 760.989990, total balance 2394.140015\n",
|
||||
"day 19: buy 1 units at price 758.039978, total balance 1636.100037\n",
|
||||
"day 21: buy 1 units at price 750.500000, total balance 885.600037\n",
|
||||
"day 22: buy 1 units at price 762.520020, total balance 123.080017\n",
|
||||
"day 23: total balances 123.080017, not enough money to buy a unit price 759.109985\n",
|
||||
"day 24: total balances 123.080017, not enough money to buy a unit price 771.190002\n",
|
||||
"day 25: total balances 123.080017, not enough money to buy a unit price 776.419983\n",
|
||||
"day 26: total balances 123.080017, not enough money to buy a unit price 789.289978\n",
|
||||
"day 27: total balances 123.080017, not enough money to buy a unit price 789.270020\n",
|
||||
"day 43: total balances 123.080017, not enough money to buy a unit price 794.020020\n",
|
||||
"day 148, sell 1 units at price 980.940002, investment 23.540966 %, total balance 1104.020019,\n",
|
||||
"day 149, sell 1 units at price 983.409973, investment 23.852038 %, total balance 2087.429992,\n",
|
||||
"day 239, sell 1 units at price 992.000000, investment 24.933878 %, total balance 3079.429992,\n",
|
||||
"day 240, sell 1 units at price 992.179993, investment 24.956546 %, total balance 4071.609985,\n",
|
||||
"day 241, sell 1 units at price 992.809998, investment 25.035890 %, total balance 5064.419983,\n",
|
||||
"day 242, sell 1 units at price 984.450012, investment 23.983021 %, total balance 6048.869995,\n",
|
||||
"day 243, sell 1 units at price 988.200012, investment 24.455302 %, total balance 7037.070007,\n",
|
||||
"day 244, sell 1 units at price 968.450012, investment 21.967959 %, total balance 8005.520019,\n",
|
||||
"day 245, sell 1 units at price 970.539978, investment 22.231172 %, total balance 8976.059997,\n",
|
||||
"day 248, sell 1 units at price 1019.270020, investment 28.368302 %, total balance 9995.330017,\n",
|
||||
"day 249, sell 1 units at price 1017.109985, investment 28.096264 %, total balance 11012.440002,\n",
|
||||
"day 250, sell 1 units at price 1016.640015, investment 28.037076 %, total balance 12029.080017,\n",
|
||||
"day 251, sell 1 units at price 1025.500000, investment 29.152915 %, total balance 13054.580017,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest, states_money = buy_stock(df.Close, signal)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XeYFEX6wPHvpM3LZnKUUAQloyJBFHPAeGI60yl63Al6qD/1PMOJGC6ZRVFMKAcCIop6GEgioCRRQpFhSZtzmNkJvz+6Z1hgw2xO7+d5fNydrq6unq1d+p23gsXn8yGEEEIIIYQQovmyNnQDhBBCCCGEEELULQn8hBBCCCGEEKKZk8BPCCGEEEIIIZo5CfyEEEIIIYQQopmTwE8IIYQQQgghmjkJ/IQQQgghhBCimZPATwjRoiml3lNKTa2DekcppXRt1yuaLqXUMqXUneUce1Qp9XZ9t0kIIUTLYW/oBgghREWUUvuAO7XW39ZF+bqitV4JqNquVym1FDgVCAX2Ao9rrT8rdfxG4FkgEfgGuENrnWkeWwacCbjN4oe01ie1USk1E7gd6Km13lVOO3xAIeDfDPa/Wus7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shBBCNDMS+AkhRDmUUuHAORhZvzYYn96Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df['Close']\n",
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4UAAAE3CAYAAAAdTeT3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xt8nHWd9//XTDJJWppC20xpSqEtAlcBaTkUEYsIKioKPSoFV1C7mMWqrK5H/KGsN6wi9x5uDwis4soCQkWl4LK35/VG6+JKAa2HXCD0RDtpQyltE0jSzMzvj8yUHnLOnOf1fDz66OT6XjPzSfpl6Kfv6/p+I+l0GkmSJElSdYoWuwBJkiRJUvHYFEqSJElSFbMplCRJkqQqZlMoSZIkSVXMplCSJEmSqphNoSRJkiRVMZtCSZIkSapiNoWSJEmSVMVsCiVJkiSpitkUSpIkSVIVsymUJEmSpCpWW+wCcqweOBNIAMki1yJJkiRJhVYDNAO/BbqH84RKawrPBH5Z7CIkS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xLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(states_money, color='r', lw=2.)\n",
|
||||
"plt.plot(states_money, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(states_money, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def buy_stock(\n",
|
||||
" real_movement,\n",
|
||||
" delay = 5,\n",
|
||||
" initial_state = 1,\n",
|
||||
" initial_money = 10000,\n",
|
||||
" max_buy = 1,\n",
|
||||
" max_sell = 1,\n",
|
||||
"):\n",
|
||||
" \"\"\"\n",
|
||||
" real_movement = actual movement in the real world\n",
|
||||
" delay = how much interval you want to delay to change our decision from buy to sell, vice versa\n",
|
||||
" initial_state = 1 is buy, 0 is sell\n",
|
||||
" initial_money = 1000, ignore what kind of currency\n",
|
||||
" max_buy = max quantity for share to buy\n",
|
||||
" max_sell = max quantity for share to sell\n",
|
||||
" \"\"\"\n",
|
||||
" starting_money = initial_money\n",
|
||||
" delay_change_decision = delay\n",
|
||||
" current_decision = 0\n",
|
||||
" state = initial_state\n",
|
||||
" current_val = real_movement[0]\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" current_inventory = 0\n",
|
||||
"\n",
|
||||
" def buy(i, initial_money, current_inventory):\n",
|
||||
" shares = initial_money // real_movement[i]\n",
|
||||
" if shares < 1:\n",
|
||||
" print(\n",
|
||||
" 'day %d: total balances %f, not enough money to buy a unit price %f'\n",
|
||||
" % (i, initial_money, real_movement[i])\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" if shares > max_buy:\n",
|
||||
" buy_units = max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = shares\n",
|
||||
" initial_money -= buy_units * real_movement[i]\n",
|
||||
" current_inventory += buy_units\n",
|
||||
" print(\n",
|
||||
" 'day %d: buy %d units at price %f, total balance %f'\n",
|
||||
" % (i, buy_units, buy_units * real_movement[i], initial_money)\n",
|
||||
" )\n",
|
||||
" states_buy.append(0)\n",
|
||||
" return initial_money, current_inventory\n",
|
||||
"\n",
|
||||
" if state == 1:\n",
|
||||
" initial_money, current_inventory = buy(\n",
|
||||
" 0, initial_money, current_inventory\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" for i in range(1, real_movement.shape[0], 1):\n",
|
||||
" if real_movement[i] < current_val and state == 0:\n",
|
||||
" if current_decision < delay_change_decision:\n",
|
||||
" current_decision += 1\n",
|
||||
" else:\n",
|
||||
" state = 1\n",
|
||||
" initial_money, current_inventory = buy(\n",
|
||||
" i, initial_money, current_inventory\n",
|
||||
" )\n",
|
||||
" current_decision = 0\n",
|
||||
" states_buy.append(i)\n",
|
||||
" if real_movement[i] > current_val and state == 1:\n",
|
||||
" if current_decision < delay_change_decision:\n",
|
||||
" current_decision += 1\n",
|
||||
" else:\n",
|
||||
" state = 0\n",
|
||||
"\n",
|
||||
" if current_inventory == 0:\n",
|
||||
" print('day %d: cannot sell anything, inventory 0' % (i))\n",
|
||||
" else:\n",
|
||||
" if current_inventory > max_sell:\n",
|
||||
" sell_units = max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = current_inventory\n",
|
||||
" current_inventory -= sell_units\n",
|
||||
" total_sell = sell_units * real_movement[i]\n",
|
||||
" initial_money += total_sell\n",
|
||||
" try:\n",
|
||||
" invest = (\n",
|
||||
" (real_movement[i] - real_movement[states_buy[-1]])\n",
|
||||
" / real_movement[states_buy[-1]]\n",
|
||||
" ) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (i, sell_units, total_sell, invest, initial_money)\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" current_decision = 0\n",
|
||||
" states_sell.append(i)\n",
|
||||
" current_val = real_movement[i]\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 units at price 768.700012, total balance 9231.299988\n",
|
||||
"day 11, sell 1 units at price 771.229980, investment 0.329123 %, total balance 10002.529968,\n",
|
||||
"day 20: buy 1 units at price 747.919983, total balance 9254.609985\n",
|
||||
"day 26, sell 1 units at price 789.289978, investment 5.531340 %, total balance 10043.899963,\n",
|
||||
"day 36: buy 1 units at price 789.909973, total balance 9253.989990\n",
|
||||
"day 44, sell 1 units at price 806.150024, investment 2.055937 %, total balance 10060.140014,\n",
|
||||
"day 57: buy 1 units at price 832.150024, total balance 9227.989990\n",
|
||||
"day 67, sell 1 units at price 809.559998, investment -2.714658 %, total balance 10037.549988,\n",
|
||||
"day 81: buy 1 units at price 830.630005, total balance 9206.919983\n",
|
||||
"day 88, sell 1 units at price 845.539978, investment 1.795020 %, total balance 10052.459961,\n",
|
||||
"day 97: buy 1 units at price 814.429993, total balance 9238.029968\n",
|
||||
"day 103, sell 1 units at price 838.549988, investment 2.961580 %, total balance 10076.579956,\n",
|
||||
"day 109: buy 1 units at price 823.349976, total balance 9253.229980\n",
|
||||
"day 116, sell 1 units at price 843.190002, investment 2.409671 %, total balance 10096.419982,\n",
|
||||
"day 134: buy 1 units at price 919.619995, total balance 9176.799987\n",
|
||||
"day 139, sell 1 units at price 954.960022, investment 3.842895 %, total balance 10131.760009,\n",
|
||||
"day 153: buy 1 units at price 950.760010, total balance 9180.999999\n",
|
||||
"day 167, sell 1 units at price 911.710022, investment -4.107239 %, total balance 10092.710021,\n",
|
||||
"day 182: buy 1 units at price 947.799988, total balance 9144.910033\n",
|
||||
"day 194, sell 1 units at price 914.390015, investment -3.525002 %, total balance 10059.300048,\n",
|
||||
"day 203: buy 1 units at price 921.280029, total balance 9138.020019\n",
|
||||
"day 214, sell 1 units at price 929.080017, investment 0.846647 %, total balance 10067.100036,\n",
|
||||
"day 224: buy 1 units at price 920.969971, total balance 9146.130065\n",
|
||||
"day 230, sell 1 units at price 957.789978, investment 3.997960 %, total balance 10103.920043,\n",
|
||||
"day 242: buy 1 units at price 984.450012, total balance 9119.470031\n",
|
||||
"day 251, sell 1 units at price 1025.500000, investment 4.169840 %, total balance 10144.970031,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = buy_stock(df.Close, initial_state = 1, \n",
|
||||
" delay = 4, initial_money = 10000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XW4VNX6wPHv1OkOumsRSiMdih0YeDGuHXh/eK+J7TWu2NcuvCgWBgYmBgYCIoiU9KI5hzjdNefMzP79sWcO5xxOd7yf5+GR2Xvttdee2YP7nXeFxTAMhBBCCCGEEEK0XtamboAQQgghhBBCiIYlgZ8QQgghhBBCtHIS+AkhhBBCCCFEKyeBnxBCCCGEEEK0chL4CSGEEEIIIUQrJ4GfEEIIIYQQQrRyEvgJIUQllFJvK6XmNEC9E5VSur7rFc2XUmqKUupgJftzlFK9GrNNQggh2g57UzdACCHqQim1H7hOa/1TQ5RvKFrrFYCq73qVUo8A5wEDgDla64cqKDcfuBroq7XeXWZfX2Az8KnW+rIKjo8AXgDO8G561XcupVQ3YFuZQ4KB2VrrZ7xlLgUeB2KAH4FrtNZp3n1RwJvAqUAKcI/W+gPvvhOBF4GugBtYDvxTa33Iu38GcAswFFijtZ5S8bt1zDU9BPSp6JrrWr4qWuuQ+qinNpRSPYB9gENr7WqC878NHNRa319JmWrd22WO8QP+AkK11l3K2X8F8A5wvdb6De+2prq3hwAfAO2Bx7TWz3q3O4DfgAu11vFVXbMQQlREMn5CCNG67AbuBBZXVEApNQHoXUkdrwB/VnGe54AgoA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MGVaTMDNbJWdZ7Yw5Bivbe34PpbMul3uv+TXMMXD53vfEt6ZbNBV/xr6xU6lKKd+YvEswA+DDmLNUPqiPrgFZqnw9f+bV8QFmpvoD3wZvEHQ2ZlCxD/M78AZmJhvMyYO2ej/LF4CLfZ8/Zpfnld7vxphatOcuzEloViulsjDv2equVfkmMNB77i8qKFPhvV3y/tRau8rcy2mAx/varbXOKLO/EMgq0QW6qe5tn1eAm0sEtPdg3ldbMdf3S0AIIWrBYhj12btCCCHaLm+GZ67WuntTt0UcS5nrF96otS5vvJYQQgjRqkngJ4QQteQd53MiZtavPeb4rtVa61uatGFCCCGEEGVIV08hhKg9C+Y6a+mYXT23U3rdMiGEEEKIZkEyfkIIIYQQQgjRyknGTwghhBBCCCFauZa4nIM/MApzGvKyUzgLIYQQQgghRGtnAzoCf2IuS1Wllhj4jQJWNHUjhBBCCCGEEKKJTQR+q07Blhj4HQFIT8/F42le4xOjo0NITc2puqAQNST3lmhIcn+JhiL3lmhIcn+JhtTc7y+r1UJkZDB4Y6PqaImBnxvA4zGaXeAHNMs2idZB7i3RkOT+Eg1F7i3RkOT+Eg2phdxf1R76JpO7CCGEEEIIIUQrJ4GfEEIIIYQQQrRyLbGrZ7ncbhfp6cm4XIVN1oakJCsej6fJzt+SWa02AgNDCAkJx2KxNHVzhBBCCCGEaFVaTeCXnp5MQEAQwcEdmixwsNutuFwS+NWUYRi43S6yszNIT08mKqpdUzdJCCGEEEKIVqXVBH4uV2GTBn2i9iwWC3a7g4iIaBITDzZ1c4QQQgghRBt10UXnofWOKssp1Z+FC79ohBbVn1Y1xk+CvpbNYrECLWL2JCGEEEILine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df['Close']\n",
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,526 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 1e-4\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" GAMMA = 0.9\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
"\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, self.state_size))\n",
|
||||
" self.REWARDS = tf.placeholder(tf.float32, (None))\n",
|
||||
" self.ACTIONS = tf.placeholder(tf.int32, (None))\n",
|
||||
" feed_forward = tf.layers.dense(self.X, self.LAYER_SIZE, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_forward, self.OUTPUT_SIZE, activation = tf.nn.softmax)\n",
|
||||
" input_y = tf.one_hot(self.ACTIONS, self.OUTPUT_SIZE)\n",
|
||||
" loglike = tf.log((input_y * (input_y - self.logits) + (1 - input_y) * (input_y + self.logits)) + 1)\n",
|
||||
" rewards = tf.tile(tf.reshape(self.REWARDS, (-1,1)), [1, self.OUTPUT_SIZE])\n",
|
||||
" self.cost = -tf.reduce_mean(loglike * (rewards + 1)) \n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = self.LEARNING_RATE).minimize(self.cost)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def predict(self, inputs):\n",
|
||||
" return self.sess.run(self.logits, feed_dict={self.X:inputs})\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])\n",
|
||||
" \n",
|
||||
" def discount_rewards(self, r):\n",
|
||||
" discounted_r = np.zeros_like(r)\n",
|
||||
" running_add = 0\n",
|
||||
" for t in reversed(range(0, r.size)):\n",
|
||||
" running_add = running_add * self.GAMMA + r[t]\n",
|
||||
" discounted_r[t] = running_add\n",
|
||||
" return discounted_r\n",
|
||||
" \n",
|
||||
" def get_predicted_action(self, sequence):\n",
|
||||
" prediction = self.predict(np.array(sequence))[0]\n",
|
||||
" return np.argmax(prediction)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.get_predicted_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t] and t < (len(self.trend) - self.half_window):\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" ep_history = []\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.get_predicted_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" if action == 1 and starting_money >= self.trend[t] and t < (len(self.trend) - self.half_window):\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= close[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" ep_history.append([state,action,starting_money,next_state])\n",
|
||||
" state = next_state\n",
|
||||
" ep_history = np.array(ep_history)\n",
|
||||
" ep_history[:,2] = self.discount_rewards(ep_history[:,2])\n",
|
||||
" cost, _ = self.sess.run([self.cost, self.optimizer], feed_dict={self.X:np.vstack(ep_history[:,0]),\n",
|
||||
" self.REWARDS:ep_history[:,2],\n",
|
||||
" self.ACTIONS:ep_history[:,1]})\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch: 10, total rewards: 1781.590144.3, cost: -3782.833740, total money: 7062.900203\n",
|
||||
"epoch: 20, total rewards: 1808.720396.3, cost: -6238.727539, total money: 10819.470396\n",
|
||||
"epoch: 30, total rewards: 644.675288.3, cost: -10399.220703, total money: 10644.675288\n",
|
||||
"epoch: 40, total rewards: 1696.944943.3, cost: -9798.079102, total money: 11696.944943\n",
|
||||
"epoch: 50, total rewards: 593.719845.3, cost: -13938.982422, total money: 10593.719845\n",
|
||||
"epoch: 60, total rewards: 634.539913.3, cost: -14890.398438, total money: 9645.289913\n",
|
||||
"epoch: 70, total rewards: 1586.160156.3, cost: -10411.115234, total money: 11586.160156\n",
|
||||
"epoch: 80, total rewards: 368.749937.3, cost: -15986.910156, total money: 10368.749937\n",
|
||||
"epoch: 90, total rewards: 733.844603.3, cost: -15352.789062, total money: 8857.304625\n",
|
||||
"epoch: 100, total rewards: 645.715148.3, cost: -15976.339844, total money: 10645.715148\n",
|
||||
"epoch: 110, total rewards: 994.814937.3, cost: -11198.958984, total money: 4471.054988\n",
|
||||
"epoch: 120, total rewards: 1771.289852.3, cost: -6539.313477, total money: 5164.829891\n",
|
||||
"epoch: 130, total rewards: 1643.744995.3, cost: -11630.438477, total money: 11643.744995\n",
|
||||
"epoch: 140, total rewards: 1877.095029.3, cost: -7103.230957, total money: 9104.255063\n",
|
||||
"epoch: 150, total rewards: 481.749932.3, cost: -18531.593750, total money: 10481.749932\n",
|
||||
"epoch: 160, total rewards: 638.035152.3, cost: -16995.314453, total money: 10638.035152\n",
|
||||
"epoch: 170, total rewards: 1188.049925.3, cost: -13348.065430, total money: 10263.189940\n",
|
||||
"epoch: 180, total rewards: 633.885008.3, cost: -14666.952148, total money: 10633.885008\n",
|
||||
"epoch: 190, total rewards: 1675.079952.3, cost: -9106.298828, total money: 5977.189998\n",
|
||||
"epoch: 200, total rewards: 567.955136.3, cost: -17828.587891, total money: 10567.955136\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"agent = Agent(state_size = window_size,\n",
|
||||
" window_size = window_size,\n",
|
||||
" trend = close,\n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 12: buy 1 unit at price 760.539978, total balance 9239.460022\n",
|
||||
"day 19, sell 1 unit at price 758.039978, investment -0.328714 %, total balance 9997.500000,\n",
|
||||
"day 23: buy 1 unit at price 759.109985, total balance 9238.390015\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 3.975708 %, total balance 10027.679993,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 9238.409973\n",
|
||||
"day 28, sell 1 unit at price 796.099976, investment 0.865351 %, total balance 10034.509949,\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 9243.709961\n",
|
||||
"day 33, sell 1 unit at price 796.419983, investment 0.710672 %, total balance 10040.129944,\n",
|
||||
"day 35: buy 1 unit at price 791.260010, total balance 9248.869934\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 8458.959961\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment 0.036648 %, total balance 9250.509949,\n",
|
||||
"day 38: buy 1 unit at price 785.049988, total balance 8465.459961\n",
|
||||
"day 39: buy 1 unit at price 782.789978, total balance 7682.669983\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 6910.849976\n",
|
||||
"day 41, sell 1 unit at price 786.140015, investment -0.477264 %, total balance 7696.989991,\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 0.235658 %, total balance 8483.890015,\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 7677.739991\n",
|
||||
"day 46: buy 1 unit at price 804.789978, total balance 6872.950013\n",
|
||||
"day 50: buy 1 unit at price 804.609985, total balance 6068.340028\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 5262.270021\n",
|
||||
"day 52, sell 1 unit at price 802.174988, investment 2.476400 %, total balance 6064.445009,\n",
|
||||
"day 53, sell 1 unit at price 805.020020, investment 4.301523 %, total balance 6869.465029,\n",
|
||||
"day 54, sell 1 unit at price 819.309998, investment 1.632447 %, total balance 7688.775027,\n",
|
||||
"day 56: buy 1 unit at price 835.669983, total balance 6853.105044\n",
|
||||
"day 57: buy 1 unit at price 832.150024, total balance 6020.955020\n",
|
||||
"day 59, sell 1 unit at price 802.320007, investment -0.306909 %, total balance 6823.275027,\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 6027.580020\n",
|
||||
"day 63: buy 1 unit at price 801.489990, total balance 5226.090030\n",
|
||||
"day 64, sell 1 unit at price 801.340027, investment -0.406403 %, total balance 6027.430057,\n",
|
||||
"day 65: buy 1 unit at price 806.969971, total balance 5220.460086\n",
|
||||
"day 71: buy 1 unit at price 818.979980, total balance 4401.480106\n",
|
||||
"day 72: buy 1 unit at price 824.159973, total balance 3577.320133\n",
|
||||
"day 73, sell 1 unit at price 828.070007, investment 2.729291 %, total balance 4405.390140,\n",
|
||||
"day 74, sell 1 unit at price 831.659973, investment -0.479856 %, total balance 5237.050113,\n",
|
||||
"day 78, sell 1 unit at price 829.280029, investment -0.344889 %, total balance 6066.330142,\n",
|
||||
"day 79: buy 1 unit at price 823.210022, total balance 5243.120120\n",
|
||||
"day 80: buy 1 unit at price 835.239990, total balance 4407.880130\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment 4.390501 %, total balance 5238.510135,\n",
|
||||
"day 83: buy 1 unit at price 827.780029, total balance 4410.730106\n",
|
||||
"day 86, sell 1 unit at price 838.679993, investment 4.640108 %, total balance 5249.410099,\n",
|
||||
"day 88, sell 1 unit at price 845.539978, investment 4.779609 %, total balance 6094.950077,\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 3.252829 %, total balance 6940.570072,\n",
|
||||
"day 90: buy 1 unit at price 847.200012, total balance 6093.370060\n",
|
||||
"day 92, sell 1 unit at price 852.119995, investment 3.392548 %, total balance 6945.490055,\n",
|
||||
"day 93, sell 1 unit at price 848.400024, investment 3.059973 %, total balance 7793.890079,\n",
|
||||
"day 94: buy 1 unit at price 830.460022, total balance 6963.430057\n",
|
||||
"day 95, sell 1 unit at price 829.590027, investment -0.676448 %, total balance 7793.020084,\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance 6973.510074\n",
|
||||
"day 101: buy 1 unit at price 831.500000, total balance 6142.010074\n",
|
||||
"day 102: buy 1 unit at price 829.559998, total balance 5312.450076\n",
|
||||
"day 103, sell 1 unit at price 838.549988, investment 1.301065 %, total balance 6151.000064,\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment -1.863791 %, total balance 6982.410037,\n",
|
||||
"day 106: buy 1 unit at price 827.880005, total balance 6154.530032\n",
|
||||
"day 108, sell 1 unit at price 824.729980, investment -0.689984 %, total balance 6979.260012,\n",
|
||||
"day 109: buy 1 unit at price 823.349976, total balance 6155.910036\n",
|
||||
"day 110, sell 1 unit at price 824.320007, investment 0.586936 %, total balance 6980.230043,\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -0.954901 %, total balance 7803.790041,\n",
|
||||
"day 113, sell 1 unit at price 836.820007, investment 0.875164 %, total balance 8640.610048,\n",
|
||||
"day 114: buy 1 unit at price 838.210022, total balance 7802.400026\n",
|
||||
"day 115: buy 1 unit at price 841.650024, total balance 6960.750002\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 1.849301 %, total balance 7803.940004,\n",
|
||||
"day 117, sell 1 unit at price 862.760010, investment 4.786547 %, total balance 8666.700014,\n",
|
||||
"day 118, sell 1 unit at price 872.299988, investment 4.066996 %, total balance 9539.000002,\n",
|
||||
"day 119: buy 1 unit at price 871.729980, total balance 8667.270022\n",
|
||||
"day 120, sell 1 unit at price 874.250000, investment 3.873341 %, total balance 9541.520022,\n",
|
||||
"day 121: buy 1 unit at price 905.960022, total balance 8635.560000\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 5.128884 %, total balance 9552.000002,\n",
|
||||
"day 124: buy 1 unit at price 927.039978, total balance 8624.960024\n",
|
||||
"day 127: buy 1 unit at price 934.299988, total balance 7690.660036\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 6758.490053\n",
|
||||
"day 129, sell 1 unit at price 928.780029, investment 2.518876 %, total balance 7687.270082,\n",
|
||||
"day 130: buy 1 unit at price 930.599976, total balance 6756.670106\n",
|
||||
"day 131: buy 1 unit at price 932.219971, total balance 5824.450135\n",
|
||||
"day 132: buy 1 unit at price 937.080017, total balance 4887.370118\n",
|
||||
"day 133: buy 1 unit at price 943.000000, total balance 3944.370118\n",
|
||||
"day 134, sell 1 unit at price 919.619995, investment -0.800395 %, total balance 4863.990113,\n",
|
||||
"day 135: buy 1 unit at price 930.239990, total balance 3933.750123\n",
|
||||
"day 136: buy 1 unit at price 934.010010, total balance 2999.740113\n",
|
||||
"day 137, sell 1 unit at price 941.859985, investment 0.809162 %, total balance 3941.600098,\n",
|
||||
"day 138, sell 1 unit at price 948.820007, investment 1.786157 %, total balance 4890.420105,\n",
|
||||
"day 139: buy 1 unit at price 954.960022, total balance 3935.460083\n",
|
||||
"day 140, sell 1 unit at price 969.539978, investment 4.184397 %, total balance 4905.000061,\n",
|
||||
"day 141: buy 1 unit at price 971.469971, total balance 3933.530090\n",
|
||||
"day 142: buy 1 unit at price 975.880005, total balance 2957.650085\n",
|
||||
"day 143, sell 1 unit at price 964.859985, investment 3.501321 %, total balance 3922.510070,\n",
|
||||
"day 145: buy 1 unit at price 975.599976, total balance 2946.910094\n",
|
||||
"day 146, sell 1 unit at price 983.679993, investment 4.972892 %, total balance 3930.590087,\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance 2954.020080\n",
|
||||
"day 148, sell 1 unit at price 980.940002, investment 4.023330 %, total balance 3934.960082,\n",
|
||||
"day 149, sell 1 unit at price 983.409973, investment 5.715728 %, total balance 4918.370055,\n",
|
||||
"day 150: buy 1 unit at price 949.830017, total balance 3968.540038\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance 3025.640014\n",
|
||||
"day 153, sell 1 unit at price 950.760010, investment 1.793343 %, total balance 3976.400024,\n",
|
||||
"day 154: buy 1 unit at price 942.309998, total balance 3034.090026\n",
|
||||
"day 155: buy 1 unit at price 939.780029, total balance 2094.309997\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance 1136.940002\n",
|
||||
"day 157, sell 1 unit at price 950.630005, investment -0.453424 %, total balance 2087.570007,\n",
|
||||
"day 158: buy 1 unit at price 959.450012, total balance 1128.119995\n",
|
||||
"day 159: buy 1 unit at price 957.090027, total balance 171.029968\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment -1.976381 %, total balance 1123.299988,\n",
|
||||
"day 164, sell 1 unit at price 917.789978, investment -5.952579 %, total balance 2041.089966,\n",
|
||||
"day 165, sell 1 unit at price 908.729980, investment -6.854243 %, total balance 2949.819946,\n",
|
||||
"day 166: buy 1 unit at price 898.700012, total balance 2051.119934\n",
|
||||
"day 168, sell 1 unit at price 906.690002, investment -7.155658 %, total balance 2957.809936,\n",
|
||||
"day 170: buy 1 unit at price 928.799988, total balance 2029.009948\n",
|
||||
"day 172, sell 1 unit at price 943.830017, investment -0.631692 %, total balance 2972.839965,\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 0.451792 %, total balance 3919.999938,\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 2966.579955\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment 2.450364 %, total balance 3931.979979,\n",
|
||||
"day 177, sell 1 unit at price 970.890015, investment 3.310348 %, total balance 4902.869994,\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 1.624240 %, total balance 5875.789977,\n",
|
||||
"day 182, sell 1 unit at price 947.799988, investment -1.214240 %, total balance 6823.589965,\n",
|
||||
"day 184: buy 1 unit at price 941.530029, total balance 5882.059936\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 4951.559936\n",
|
||||
"day 186, sell 1 unit at price 930.830017, investment -2.743735 %, total balance 5882.389953,\n",
|
||||
"day 187: buy 1 unit at price 930.390015, total balance 4951.999938\n",
|
||||
"day 188, sell 1 unit at price 923.650024, investment 2.776234 %, total balance 5875.649962,\n",
|
||||
"day 189: buy 1 unit at price 927.960022, total balance 4947.689940\n",
|
||||
"day 191, sell 1 unit at price 926.789978, investment -0.216409 %, total balance 5874.479918,\n",
|
||||
"day 192, sell 1 unit at price 922.900024, investment -3.201103 %, total balance 6797.379942,\n",
|
||||
"day 194, sell 1 unit at price 914.390015, investment -2.882544 %, total balance 7711.769957,\n",
|
||||
"day 195, sell 1 unit at price 922.669983, investment -0.841485 %, total balance 8634.439940,\n",
|
||||
"day 196: buy 1 unit at price 922.219971, total balance 7712.219969\n",
|
||||
"day 198: buy 1 unit at price 910.979980, total balance 6801.239989\n",
|
||||
"day 199, sell 1 unit at price 910.669983, investment -2.119545 %, total balance 7711.909972,\n",
|
||||
"day 201: buy 1 unit at price 924.690002, total balance 6787.219970\n",
|
||||
"day 202, sell 1 unit at price 927.000000, investment -0.103455 %, total balance 7714.219970,\n",
|
||||
"day 203, sell 1 unit at price 921.280029, investment -0.101922 %, total balance 8635.499999,\n",
|
||||
"day 204: buy 1 unit at price 915.890015, total balance 7719.609984\n",
|
||||
"day 206: buy 1 unit at price 921.289978, total balance 6798.320006\n",
|
||||
"day 207, sell 1 unit at price 929.570007, investment 2.040663 %, total balance 7727.890013,\n",
|
||||
"day 208, sell 1 unit at price 939.330017, investment 1.583235 %, total balance 8667.220030,\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment 1.371343 %, total balance 9595.670042,\n",
|
||||
"day 213: buy 1 unit at price 926.500000, total balance 8669.170042\n",
|
||||
"day 215: buy 1 unit at price 932.070007, total balance 7737.100035\n",
|
||||
"day 217, sell 1 unit at price 925.109985, investment 0.414637 %, total balance 8662.210020,\n",
|
||||
"day 218: buy 1 unit at price 920.289978, total balance 7741.920042\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance 6826.920042\n",
|
||||
"day 221: buy 1 unit at price 931.580017, total balance 5895.340025\n",
|
||||
"day 222, sell 1 unit at price 932.450012, investment 0.642203 %, total balance 6827.790037,\n",
|
||||
"day 223, sell 1 unit at price 928.530029, investment -0.379797 %, total balance 7756.320066,\n",
|
||||
"day 224: buy 1 unit at price 920.969971, total balance 6835.350095\n",
|
||||
"day 225: buy 1 unit at price 924.859985, total balance 5910.490110\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 3.174002 %, total balance 6859.990110,\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 4.820763 %, total balance 7819.100095,\n",
|
||||
"day 230: buy 1 unit at price 957.789978, total balance 6861.310117\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 5.078469 %, total balance 7840.200132,\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment 6.083806 %, total balance 8817.200132,\n",
|
||||
"day 235: buy 1 unit at price 972.599976, total balance 7844.600156\n",
|
||||
"day 236: buy 1 unit at price 989.250000, total balance 6855.350156\n",
|
||||
"day 237, sell 1 unit at price 987.830017, investment 6.808602 %, total balance 7843.180173,\n",
|
||||
"day 240, sell 1 unit at price 992.179993, investment 3.590559 %, total balance 8835.360166,\n",
|
||||
"day 243, sell 1 unit at price 988.200012, investment 1.603952 %, total balance 9823.560178,\n",
|
||||
"day 245, sell 1 unit at price 970.539978, investment -1.891334 %, total balance 10794.100156,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xl8VNXZwPHfvTMTCEkIIQv7vpxA2EEREcTdCoIKimtdatOW+rrUta2trVq1vrVva23VWtEqiqAi2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QQjSQYVmN7d0hhBACguO7ntBaD2jttojalFKnAD/UWl9W58pCCCFEOyOBnxBCNJAz3ug07KxfD+zxXZ9orW9u1YYJIYQQQoSRrp5CCNFwBvY8a8XYXT03UHPeMiGEEEKINkEyfkIIIYQQQgjRzknGTwghhBBCCCHaueNxOodOwAnY82fVZ14pIYQQQgghhGgPXNjz7/4Hu4J1nY7HwO8E4IPWboQQQgghhBBCtLJpwIfxrHg8Bn4FAMXF5fj9bWt8Ynp6MkVFZXWvKEQ9ybUlmpNcX6K5yLUlmpNcX6I5tfXryzQN0tKSwImN4nE8Bn4+AL/fanOBH9Am2yTaB7m2RHOS60s0F7m2RHOS60s0p+Pk+op76JsUdxFCCCGEEEKIdk4CPyGEEEIIIYRLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,497 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Agent:\n",
|
||||
" def __init__(self, state_size, window_size, trend, skip, batch_size):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" self.action_size = 3\n",
|
||||
" self.batch_size = batch_size\n",
|
||||
" self.memory = deque(maxlen = 1000)\n",
|
||||
" self.inventory = []\n",
|
||||
"\n",
|
||||
" self.gamma = 0.95\n",
|
||||
" self.epsilon = 0.5\n",
|
||||
" self.epsilon_min = 0.01\n",
|
||||
" self.epsilon_decay = 0.999\n",
|
||||
"\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.X = tf.placeholder(tf.float32, [None, self.state_size])\n",
|
||||
" self.Y = tf.placeholder(tf.float32, [None, self.action_size])\n",
|
||||
" feed = tf.layers.dense(self.X, 256, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed, self.action_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.GradientDescentOptimizer(1e-5).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
"\n",
|
||||
" def act(self, state):\n",
|
||||
" if random.random() <= self.epsilon:\n",
|
||||
" return random.randrange(self.action_size)\n",
|
||||
" return np.argmax(\n",
|
||||
" self.sess.run(self.logits, feed_dict = {self.X: state})[0]\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])\n",
|
||||
"\n",
|
||||
" def replay(self, batch_size):\n",
|
||||
" mini_batch = []\n",
|
||||
" l = len(self.memory)\n",
|
||||
" for i in range(l - batch_size, l):\n",
|
||||
" mini_batch.append(self.memory[i])\n",
|
||||
" replay_size = len(mini_batch)\n",
|
||||
" X = np.empty((replay_size, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.action_size))\n",
|
||||
" states = np.array([a[0][0] for a in mini_batch])\n",
|
||||
" new_states = np.array([a[3][0] for a in mini_batch])\n",
|
||||
" Q = self.sess.run(self.logits, feed_dict = {self.X: states})\n",
|
||||
" Q_new = self.sess.run(self.logits, feed_dict = {self.X: new_states})\n",
|
||||
" for i in range(len(mini_batch)):\n",
|
||||
" state, action, reward, next_state, done = mini_batch[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action] = reward\n",
|
||||
" if not done:\n",
|
||||
" target[action] += self.gamma * np.amax(Q_new[i])\n",
|
||||
" X[i] = state\n",
|
||||
" Y[i] = target\n",
|
||||
" cost, _ = self.sess.run(\n",
|
||||
" [self.cost, self.optimizer], feed_dict = {self.X: X, self.Y: Y}\n",
|
||||
" )\n",
|
||||
" if self.epsilon > self.epsilon_min:\n",
|
||||
" self.epsilon *= self.epsilon_decay\n",
|
||||
" return cost\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t] and t < (len(self.trend) - self.half_window):\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t] and t < (len(self.trend) - self.half_window):\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" self.memory.append((state, action, invest, \n",
|
||||
" next_state, starting_money < initial_money))\n",
|
||||
" state = next_state\n",
|
||||
" batch_size = min(self.batch_size, len(self.memory))\n",
|
||||
" cost = self.replay(batch_size)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch: 10, total rewards: 274.710201.3, cost: 0.810730, total money: 10274.710201\n",
|
||||
"epoch: 20, total rewards: 161.429929.3, cost: 0.406487, total money: 10161.429929\n",
|
||||
"epoch: 30, total rewards: 89.659849.3, cost: 0.225568, total money: 10089.659849\n",
|
||||
"epoch: 40, total rewards: 121.209836.3, cost: 0.152499, total money: 10121.209836\n",
|
||||
"epoch: 50, total rewards: 94.869810.3, cost: 0.120762, total money: 10094.869810\n",
|
||||
"epoch: 60, total rewards: 123.609922.3, cost: 0.097353, total money: 10123.609922\n",
|
||||
"epoch: 70, total rewards: 130.149901.3, cost: 0.131718, total money: 10130.149901\n",
|
||||
"epoch: 80, total rewards: 55.369871.3, cost: 0.072531, total money: 10055.369871\n",
|
||||
"epoch: 90, total rewards: 177.780026.3, cost: 0.062346, total money: 10177.780026\n",
|
||||
"epoch: 100, total rewards: 151.249997.3, cost: 0.056566, total money: 10151.249997\n",
|
||||
"epoch: 110, total rewards: 101.629942.3, cost: 0.050717, total money: 10101.629942\n",
|
||||
"epoch: 120, total rewards: 138.329892.3, cost: 0.075178, total money: 10138.329892\n",
|
||||
"epoch: 130, total rewards: 187.559812.3, cost: 0.039170, total money: 10187.559812\n",
|
||||
"epoch: 140, total rewards: 125.699889.3, cost: 0.035156, total money: 10125.699889\n",
|
||||
"epoch: 150, total rewards: 138.249876.3, cost: 0.403965, total money: 10138.249876\n",
|
||||
"epoch: 160, total rewards: 141.329832.3, cost: 0.029966, total money: 10141.329832\n",
|
||||
"epoch: 170, total rewards: 179.989928.3, cost: 0.027219, total money: 10179.989928\n",
|
||||
"epoch: 180, total rewards: 191.619871.3, cost: 0.025002, total money: 10191.619871\n",
|
||||
"epoch: 190, total rewards: 191.929868.3, cost: 0.149151, total money: 10191.929868\n",
|
||||
"epoch: 200, total rewards: 113.759886.3, cost: 0.021398, total money: 10113.759886\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip, \n",
|
||||
" batch_size = batch_size)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 4: buy 1 unit at price 790.510010, total balance 9209.489990\n",
|
||||
"day 5, sell 1 unit at price 785.309998, investment -0.657805 %, total balance 9994.799988,\n",
|
||||
"day 14: buy 1 unit at price 768.270020, total balance 9226.529968\n",
|
||||
"day 16, sell 1 unit at price 761.679993, investment -0.857775 %, total balance 9988.209961,\n",
|
||||
"day 22: buy 1 unit at price 762.520020, total balance 9225.689941\n",
|
||||
"day 23, sell 1 unit at price 759.109985, investment -0.447206 %, total balance 9984.799926,\n",
|
||||
"day 28: buy 1 unit at price 796.099976, total balance 9188.699950\n",
|
||||
"day 29, sell 1 unit at price 797.070007, investment 0.121848 %, total balance 9985.769957,\n",
|
||||
"day 32: buy 1 unit at price 794.200012, total balance 9191.569945\n",
|
||||
"day 35: buy 1 unit at price 791.260010, total balance 8400.309935\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 7610.399962\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment -0.333672 %, total balance 8401.949950,\n",
|
||||
"day 38, sell 1 unit at price 785.049988, investment -0.784827 %, total balance 9186.999938,\n",
|
||||
"day 39, sell 1 unit at price 782.789978, investment -0.901368 %, total balance 9969.789916,\n",
|
||||
"day 42: buy 1 unit at price 786.900024, total balance 9182.889892\n",
|
||||
"day 43: buy 1 unit at price 794.020020, total balance 8388.869872\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 7582.719848\n",
|
||||
"day 45: buy 1 unit at price 806.650024, total balance 6776.069824\n",
|
||||
"day 46: buy 1 unit at price 804.789978, total balance 5971.279846\n",
|
||||
"day 48, sell 1 unit at price 806.359985, investment 2.472990 %, total balance 6777.639831,\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 1.745546 %, total balance 7585.519836,\n",
|
||||
"day 50, sell 1 unit at price 804.609985, investment -0.191036 %, total balance 8390.129821,\n",
|
||||
"day 51, sell 1 unit at price 806.070007, investment -0.071904 %, total balance 9196.199828,\n",
|
||||
"day 53: buy 1 unit at price 805.020020, total balance 8391.179808\n",
|
||||
"day 56, sell 1 unit at price 835.669983, investment 3.837027 %, total balance 9226.849791,\n",
|
||||
"day 57, sell 1 unit at price 832.150024, investment 3.370103 %, total balance 10058.999815,\n",
|
||||
"day 63: buy 1 unit at price 801.489990, total balance 9257.509825\n",
|
||||
"day 66: buy 1 unit at price 808.380005, total balance 8449.129820\n",
|
||||
"day 67, sell 1 unit at price 809.559998, investment 1.006876 %, total balance 9258.689818,\n",
|
||||
"day 68, sell 1 unit at price 813.669983, investment 0.654392 %, total balance 10072.359801,\n",
|
||||
"day 71: buy 1 unit at price 818.979980, total balance 9253.379821\n",
|
||||
"day 72: buy 1 unit at price 824.159973, total balance 8429.219848\n",
|
||||
"day 73: buy 1 unit at price 828.070007, total balance 7601.149841\n",
|
||||
"day 74: buy 1 unit at price 831.659973, total balance 6769.489868\n",
|
||||
"day 75: buy 1 unit at price 830.760010, total balance 5938.729858\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment 1.422504 %, total balance 6769.359863,\n",
|
||||
"day 82, sell 1 unit at price 829.080017, investment 0.596977 %, total balance 7598.439880,\n",
|
||||
"day 85, sell 1 unit at price 835.369995, investment 0.881567 %, total balance 8433.809875,\n",
|
||||
"day 87, sell 1 unit at price 843.250000, investment 1.393602 %, total balance 9277.059875,\n",
|
||||
"day 88, sell 1 unit at price 845.539978, investment 1.779090 %, total balance 10122.599853,\n",
|
||||
"day 92: buy 1 unit at price 852.119995, total balance 9270.479858\n",
|
||||
"day 93, sell 1 unit at price 848.400024, investment -0.436555 %, total balance 10118.879882,\n",
|
||||
"day 99: buy 1 unit at price 820.919983, total balance 9297.959899\n",
|
||||
"day 101: buy 1 unit at price 831.500000, total balance 8466.459899\n",
|
||||
"day 104, sell 1 unit at price 834.570007, investment 1.662772 %, total balance 9301.029906,\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment -0.010827 %, total balance 10132.439879,\n",
|
||||
"day 111: buy 1 unit at price 823.559998, total balance 9308.879881\n",
|
||||
"day 113, sell 1 unit at price 836.820007, investment 1.610084 %, total balance 10145.699888,\n",
|
||||
"day 116: buy 1 unit at price 843.190002, total balance 9302.509886\n",
|
||||
"day 117: buy 1 unit at price 862.760010, total balance 8439.749876\n",
|
||||
"day 118: buy 1 unit at price 872.299988, total balance 7567.449888\n",
|
||||
"day 119, sell 1 unit at price 871.729980, investment 3.384762 %, total balance 8439.179868,\n",
|
||||
"day 120: buy 1 unit at price 874.250000, total balance 7564.929868\n",
|
||||
"day 121: buy 1 unit at price 905.960022, total balance 6658.969846\n",
|
||||
"day 122, sell 1 unit at price 912.570007, investment 5.773332 %, total balance 7571.539853,\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 5.060187 %, total balance 8487.979855,\n",
|
||||
"day 124, sell 1 unit at price 927.039978, investment 6.038316 %, total balance 9415.019833,\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance 8483.359860\n",
|
||||
"day 126: buy 1 unit at price 927.130005, total balance 7556.229855\n",
|
||||
"day 127: buy 1 unit at price 934.299988, total balance 6621.929867\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 5689.759884\n",
|
||||
"day 129, sell 1 unit at price 928.780029, investment 2.518876 %, total balance 6618.539913,\n",
|
||||
"day 130, sell 1 unit at price 930.599976, investment -0.113775 %, total balance 7549.139889,\n",
|
||||
"day 131: buy 1 unit at price 932.219971, total balance 6616.919918\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 1.711734 %, total balance 7559.919918,\n",
|
||||
"day 134, sell 1 unit at price 919.619995, investment -1.571229 %, total balance 8479.539913,\n",
|
||||
"day 136, sell 1 unit at price 934.010010, investment 0.197392 %, total balance 9413.549923,\n",
|
||||
"day 137, sell 1 unit at price 941.859985, investment 1.034092 %, total balance 10355.409908,\n",
|
||||
"day 139: buy 1 unit at price 954.960022, total balance 9400.449886\n",
|
||||
"day 141, sell 1 unit at price 971.469971, investment 1.728863 %, total balance 10371.919857,\n",
|
||||
"day 144: buy 1 unit at price 966.950012, total balance 9404.969845\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 0.894562 %, total balance 10380.569821,\n",
|
||||
"day 149: buy 1 unit at price 983.409973, total balance 9397.159848\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance 8454.259824\n",
|
||||
"day 154, sell 1 unit at price 942.309998, investment -4.179333 %, total balance 9396.569822,\n",
|
||||
"day 157: buy 1 unit at price 950.630005, total balance 8445.939817\n",
|
||||
"day 159, sell 1 unit at price 957.090027, investment 1.504932 %, total balance 9403.029844,\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 8437.439817\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment 0.172519 %, total balance 9389.709837,\n",
|
||||
"day 162, sell 1 unit at price 927.330017, investment -3.962345 %, total balance 10317.039854,\n",
|
||||
"day 173: buy 1 unit at price 947.159973, total balance 9369.879881\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment 1.925762 %, total balance 10335.279905,\n",
|
||||
"day 180: buy 1 unit at price 980.340027, total balance 9354.939878\n",
|
||||
"day 184, sell 1 unit at price 941.530029, investment -3.958830 %, total balance 10296.469907,\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 9365.969907\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 8435.139890\n",
|
||||
"day 187, sell 1 unit at price 930.390015, investment -0.011820 %, total balance 9365.529905,\n",
|
||||
"day 189, sell 1 unit at price 927.960022, investment -0.308326 %, total balance 10293.489927,\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 9366.529905\n",
|
||||
"day 200, sell 1 unit at price 906.659973, investment -2.189959 %, total balance 10273.189878,\n",
|
||||
"day 203: buy 1 unit at price 921.280029, total balance 9351.909849\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 8438.099851\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment 0.778263 %, total balance 9366.549863,\n",
|
||||
"day 213, sell 1 unit at price 926.500000, investment 1.388692 %, total balance 10293.049863,\n",
|
||||
"day 229: buy 1 unit at price 953.270020, total balance 9339.779843\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 1.750816 %, total balance 10309.739865,\n",
|
||||
"day 234: buy 1 unit at price 977.000000, total balance 9332.739865\n",
|
||||
"day 239, sell 1 unit at price 992.000000, investment 1.535312 %, total balance 10324.739865,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4VFX6wPHvnZJGCiGF3hQ4NCkCC4gidl0VVFiwi66ioqKuZW1rxbo/XdeK3VUsYAN2UewFUVCaIMKhB0ghIYT0TDLl98e9EyZlkkkjIXk/z8MDmXvvuWdmbnjue99z3mP4fD6EEEIIIYQQQrRetubugBBCCCGEEEKIpiWBnxBCCCGEEEK0chL4CSGEEEIIIUQrJ4GfEEIIIYQQQrRyEvgJIYQQQgghRCsngZ8QQgghhBBCtHIS+AkhRBBKqTeVUrOboN3jlFK6sdsVLZtSaqdS6uQg2+Yopf5xqPskhBCi7XA0dweEEKK+lFI7gSu11l81xf5NRWu9FFCN3a5S6ltgMBAO7ADu1VovtLadCdxpbS8B/gfcrLXOr9RGB0Cb3dTHBjnPHODigJecQKnWOsbaPhc4CWgHZABPaK1fDTj+SuAOoBPwI3CF1jrN2hYO/Bs412p3GXCN1jo14PjzgfuAHlb707XWS5VSvaz3XRjQt8e11g+F8Nn5j3Vqrd2NvX9ttNbXNLSNhlBKfQfMDfyeDuG5J1jn7lbDPjcDNwCJQAEwD7itus9eKTUGeAgYAXiA74BZWuv0SvuFAb8BMf5zK6WOAz6r1GQ7YIrW+iPr2nsA89p1Wf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G7QWv+30uGfYd4M+03DCoprCPrADGwfr9SPZMz5Z//DzAidjDks0T80MQKzGMYGzGD5Zcwhs/7M2K/ApcpcXqAImIk5/2uftf0N4AYr01KGWXjkf1bbo4EDmMNF4zGrIX5nDUtEKXU/MEFrPaGa95KF+bkeAWy2XnsP+LtS6jNr+yPAPG0W76lu/xjM7yhXKdWVgOGFTeBdzOt7DOaQVL85wMNKqcusYCEJOEZrvVApdQLm0MU/MIfRlnHwWqr8u1FXr2BmGr8CfsH8fZoA/BBCtnUv5vy2OP93VZkyl0FYpLXOVEoNxBx2+3mQfbsC3wDPaa3nVNr8OwEPaTCHgT+HOU83sMLnJcBPlYrKoJS6CFiqtd5lBZcPY2XprO1DMK8HG+a12xmzIE4o177flZhZ3LXWEN1I6z33oGoFWyGECJnM8RNCHM4eBe5RSh1QSt2qtdaYhSeexbzBPRuzmEtpdfsDb2EO30vFvBleXuUMwb2COW9vHbAGM3viBjzWje4szOAwBzOIWhSkndr0xcxcFAA/Ay9oa32vSgzMIaGZmDewNwLTtNarre23YA5nfU0pVWD92QBgzUnL8P/BDF7KrH+jlOph7d/DfzKl1FjMjGfgMg5gZm+uxRy+loM5z/KmgOGmEZhBSwFmgPAzZsVDv1sx1xncYr2PP2Ou6ef3EGZwuBlz2Yk1mDffYAYuSzCHz/2OGdRfEHBsd8xy/FVorYusdpZZ18cYzGIlb2M+RNhh9euGGvZ/ADOAyAUWY85tbCrvYWaWvgkIisEcaroIc2hwPuY1Pdra1glzDlke5mf3vfX+/MdNUUrlKKWeqWtntNYrMYf8Pof5vW/lYIGi2o7dZL2f7dZnWd0Q0HHAeqVUIebv2qeYxVkAUEptsIIyMAOnI4D7A67Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,673 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import time\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import random\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"seaborn==0.9.0\n",
|
||||
"pandas==0.23.4\n",
|
||||
"numpy==1.14.5\n",
|
||||
"matplotlib==3.0.2\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pkg_resources\n",
|
||||
"import types\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_imports():\n",
|
||||
" for name, val in globals().items():\n",
|
||||
" if isinstance(val, types.ModuleType):\n",
|
||||
" name = val.__name__.split('.')[0]\n",
|
||||
" elif isinstance(val, type):\n",
|
||||
" name = val.__module__.split('.')[0]\n",
|
||||
" poorly_named_packages = {'PIL': 'Pillow', 'sklearn': 'scikit-learn'}\n",
|
||||
" if name in poorly_named_packages.keys():\n",
|
||||
" name = poorly_named_packages[name]\n",
|
||||
" yield name\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"imports = list(set(get_imports()))\n",
|
||||
"requirements = []\n",
|
||||
"for m in pkg_resources.working_set:\n",
|
||||
" if m.project_name in imports and m.project_name != 'pip':\n",
|
||||
" requirements.append((m.project_name, m.version))\n",
|
||||
"\n",
|
||||
"for r in requirements:\n",
|
||||
" print('{}=={}'.format(*r))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Deep_Evolution_Strategy:\n",
|
||||
"\n",
|
||||
" inputs = None\n",
|
||||
"\n",
|
||||
" def __init__(\n",
|
||||
" self, weights, reward_function, population_size, sigma, learning_rate\n",
|
||||
" ):\n",
|
||||
" self.weights = weights\n",
|
||||
" self.reward_function = reward_function\n",
|
||||
" self.population_size = population_size\n",
|
||||
" self.sigma = sigma\n",
|
||||
" self.learning_rate = learning_rate\n",
|
||||
"\n",
|
||||
" def _get_weight_from_population(self, weights, population):\n",
|
||||
" weights_population = []\n",
|
||||
" for index, i in enumerate(population):\n",
|
||||
" jittered = self.sigma * i\n",
|
||||
" weights_population.append(weights[index] + jittered)\n",
|
||||
" return weights_population\n",
|
||||
"\n",
|
||||
" def get_weights(self):\n",
|
||||
" return self.weights\n",
|
||||
"\n",
|
||||
" def train(self, epoch = 100, print_every = 1):\n",
|
||||
" lasttime = time.time()\n",
|
||||
" for i in range(epoch):\n",
|
||||
" population = []\n",
|
||||
" rewards = np.zeros(self.population_size)\n",
|
||||
" for k in range(self.population_size):\n",
|
||||
" x = []\n",
|
||||
" for w in self.weights:\n",
|
||||
" x.append(np.random.randn(*w.shape))\n",
|
||||
" population.append(x)\n",
|
||||
" for k in range(self.population_size):\n",
|
||||
" weights_population = self._get_weight_from_population(\n",
|
||||
" self.weights, population[k]\n",
|
||||
" )\n",
|
||||
" rewards[k] = self.reward_function(weights_population)\n",
|
||||
" rewards = (rewards - np.mean(rewards)) / (np.std(rewards) + 1e-7)\n",
|
||||
" for index, w in enumerate(self.weights):\n",
|
||||
" A = np.array([p[index] for p in population])\n",
|
||||
" self.weights[index] = (\n",
|
||||
" w\n",
|
||||
" + self.learning_rate\n",
|
||||
" / (self.population_size * self.sigma)\n",
|
||||
" * np.dot(A.T, rewards).T\n",
|
||||
" )\n",
|
||||
" if (i + 1) % print_every == 0:\n",
|
||||
" print(\n",
|
||||
" 'iter %d. reward: %f'\n",
|
||||
" % (i + 1, self.reward_function(self.weights))\n",
|
||||
" )\n",
|
||||
" print('time taken to train:', time.time() - lasttime, 'seconds')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, layer_size, output_size):\n",
|
||||
" self.weights = [\n",
|
||||
" np.random.randn(input_size, layer_size),\n",
|
||||
" np.random.randn(layer_size, output_size),\n",
|
||||
" np.random.randn(1, layer_size),\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" def predict(self, inputs):\n",
|
||||
" feed = np.dot(inputs, self.weights[0]) + self.weights[-1]\n",
|
||||
" decision = np.dot(feed, self.weights[1])\n",
|
||||
" return decision\n",
|
||||
"\n",
|
||||
" def get_weights(self):\n",
|
||||
" return self.weights\n",
|
||||
"\n",
|
||||
" def set_weights(self, weights):\n",
|
||||
" self.weights = weights"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" POPULATION_SIZE = 15\n",
|
||||
" SIGMA = 0.1\n",
|
||||
" LEARNING_RATE = 0.03\n",
|
||||
"\n",
|
||||
" def __init__(self, model, window_size, trend, skip, initial_money):\n",
|
||||
" self.model = model\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" self.initial_money = initial_money\n",
|
||||
" self.es = Deep_Evolution_Strategy(\n",
|
||||
" self.model.get_weights(),\n",
|
||||
" self.get_reward,\n",
|
||||
" self.POPULATION_SIZE,\n",
|
||||
" self.SIGMA,\n",
|
||||
" self.LEARNING_RATE,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" def act(self, sequence):\n",
|
||||
" decision = self.model.predict(np.array(sequence))\n",
|
||||
" return np.argmax(decision[0])\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])\n",
|
||||
"\n",
|
||||
" def get_reward(self, weights):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" self.model.weights = weights\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" inventory = []\n",
|
||||
" quantity = 0\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= close[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
"\n",
|
||||
" state = next_state\n",
|
||||
" return ((starting_money - initial_money) / initial_money) * 100\n",
|
||||
"\n",
|
||||
" def fit(self, iterations, checkpoint):\n",
|
||||
" self.es.train(iterations, print_every = checkpoint)\n",
|
||||
"\n",
|
||||
" def buy(self):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self.act(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" state = next_state\n",
|
||||
"\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"iter 10. reward: 8.610248\n",
|
||||
"iter 20. reward: 12.257399\n",
|
||||
"iter 30. reward: 7.689600\n",
|
||||
"iter 40. reward: 18.719300\n",
|
||||
"iter 50. reward: 16.883897\n",
|
||||
"iter 60. reward: 18.100399\n",
|
||||
"iter 70. reward: 17.280399\n",
|
||||
"iter 80. reward: 15.865947\n",
|
||||
"iter 90. reward: 17.435298\n",
|
||||
"iter 100. reward: 22.108749\n",
|
||||
"iter 110. reward: 21.537897\n",
|
||||
"iter 120. reward: 21.986898\n",
|
||||
"iter 130. reward: 22.303096\n",
|
||||
"iter 140. reward: 27.540547\n",
|
||||
"iter 150. reward: 24.151497\n",
|
||||
"iter 160. reward: 26.339196\n",
|
||||
"iter 170. reward: 26.184596\n",
|
||||
"iter 180. reward: 25.859546\n",
|
||||
"iter 190. reward: 28.623797\n",
|
||||
"iter 200. reward: 30.171547\n",
|
||||
"iter 210. reward: 29.712899\n",
|
||||
"iter 220. reward: 28.880399\n",
|
||||
"iter 230. reward: 29.221448\n",
|
||||
"iter 240. reward: 26.622551\n",
|
||||
"iter 250. reward: 21.736548\n",
|
||||
"iter 260. reward: 32.192049\n",
|
||||
"iter 270. reward: 31.077749\n",
|
||||
"iter 280. reward: 30.869947\n",
|
||||
"iter 290. reward: 30.829648\n",
|
||||
"iter 300. reward: 32.587899\n",
|
||||
"iter 310. reward: 32.627998\n",
|
||||
"iter 320. reward: 32.198498\n",
|
||||
"iter 330. reward: 31.940298\n",
|
||||
"iter 340. reward: 32.789998\n",
|
||||
"iter 350. reward: 33.619697\n",
|
||||
"iter 360. reward: 32.738196\n",
|
||||
"iter 370. reward: 34.456997\n",
|
||||
"iter 380. reward: 34.972598\n",
|
||||
"iter 390. reward: 34.632198\n",
|
||||
"iter 400. reward: 32.573597\n",
|
||||
"iter 410. reward: 35.826097\n",
|
||||
"iter 420. reward: 33.999698\n",
|
||||
"iter 430. reward: 35.530297\n",
|
||||
"iter 440. reward: 35.589196\n",
|
||||
"iter 450. reward: 32.944796\n",
|
||||
"iter 460. reward: 36.473798\n",
|
||||
"iter 470. reward: 38.662997\n",
|
||||
"iter 480. reward: 37.648599\n",
|
||||
"iter 490. reward: 37.361099\n",
|
||||
"iter 500. reward: 37.407198\n",
|
||||
"time taken to train: 33.66626238822937 seconds\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"initial_money = 10000\n",
|
||||
"\n",
|
||||
"model = Model(input_size = window_size, layer_size = 500, output_size = 3)\n",
|
||||
"agent = Agent(model = model, \n",
|
||||
" window_size = window_size,\n",
|
||||
" trend = close,\n",
|
||||
" skip = skip,\n",
|
||||
" initial_money = initial_money)\n",
|
||||
"agent.fit(iterations = 500, checkpoint = 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 1: buy 1 unit at price 762.130005, total balance 9237.869995\n",
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 8455.349975\n",
|
||||
"day 4, sell 1 unit at price 790.510010, investment 3.723775 %, total balance 9245.859985,\n",
|
||||
"day 5, sell 1 unit at price 785.309998, investment 0.356538 %, total balance 10031.169983,\n",
|
||||
"day 6: buy 1 unit at price 762.559998, total balance 9268.609985\n",
|
||||
"day 10: buy 1 unit at price 764.479980, total balance 8504.130005\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 7732.900025\n",
|
||||
"day 12: buy 1 unit at price 760.539978, total balance 6972.360047\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 6204.120057\n",
|
||||
"day 18: buy 1 unit at price 770.840027, total balance 5433.280030\n",
|
||||
"day 19: buy 1 unit at price 758.039978, total balance 4675.240052\n",
|
||||
"day 20: buy 1 unit at price 747.919983, total balance 3927.320069\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 3176.820069\n",
|
||||
"day 24, sell 1 unit at price 771.190002, investment 1.131715 %, total balance 3948.010071,\n",
|
||||
"day 25: buy 1 unit at price 776.419983, total balance 3171.590088\n",
|
||||
"day 26, sell 1 unit at price 789.289978, investment 3.245343 %, total balance 3960.880066,\n",
|
||||
"day 27: buy 1 unit at price 789.270020, total balance 3171.610046\n",
|
||||
"day 29, sell 1 unit at price 797.070007, investment 3.350496 %, total balance 3968.680053,\n",
|
||||
"day 30, sell 1 unit at price 797.849976, investment 4.905725 %, total balance 4766.530029,\n",
|
||||
"day 31, sell 1 unit at price 790.799988, investment 2.936582 %, total balance 5557.330017,\n",
|
||||
"day 32: buy 1 unit at price 794.200012, total balance 4763.130005\n",
|
||||
"day 33: buy 1 unit at price 796.419983, total balance 3966.710022\n",
|
||||
"day 34: buy 1 unit at price 794.559998, total balance 3172.150024\n",
|
||||
"day 36: buy 1 unit at price 789.909973, total balance 2382.240051\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment 2.686674 %, total balance 3173.790039,\n",
|
||||
"day 40: buy 1 unit at price 771.820007, total balance 2401.970032\n",
|
||||
"day 41: buy 1 unit at price 786.140015, total balance 1615.830017\n",
|
||||
"day 42: buy 1 unit at price 786.900024, total balance 828.929993\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 22.779969\n",
|
||||
"day 45, sell 1 unit at price 806.650024, investment 6.412597 %, total balance 829.429993,\n",
|
||||
"day 48, sell 1 unit at price 806.359985, investment 7.813670 %, total balance 1635.789978,\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 7.645570 %, total balance 2443.669983,\n",
|
||||
"day 50, sell 1 unit at price 804.609985, investment 3.630767 %, total balance 3248.279968,\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 2442.209961\n",
|
||||
"day 52: buy 1 unit at price 802.174988, total balance 1640.034973\n",
|
||||
"day 53: buy 1 unit at price 805.020020, total balance 835.014953\n",
|
||||
"day 56, sell 1 unit at price 835.669983, investment 5.878845 %, total balance 1670.684936,\n",
|
||||
"day 57, sell 1 unit at price 832.150024, investment 4.778395 %, total balance 2502.834960,\n",
|
||||
"day 59: buy 1 unit at price 802.320007, total balance 1700.514953\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 904.819946\n",
|
||||
"day 62: buy 1 unit at price 798.530029, total balance 106.289917\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 2.865323 %, total balance 925.529907,\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 3.258409 %, total balance 1745.979919,\n",
|
||||
"day 71: buy 1 unit at price 818.979980, total balance 926.999939\n",
|
||||
"day 72: buy 1 unit at price 824.159973, total balance 102.839966\n",
|
||||
"day 74, sell 1 unit at price 831.659973, investment 5.285412 %, total balance 934.499939,\n",
|
||||
"day 76, sell 1 unit at price 831.330017, investment 7.710348 %, total balance 1765.829956,\n",
|
||||
"day 77, sell 1 unit at price 828.640015, investment 5.406162 %, total balance 2594.469971,\n",
|
||||
"day 78, sell 1 unit at price 829.280029, investment 5.385691 %, total balance 3423.750000,\n",
|
||||
"day 79: buy 1 unit at price 823.210022, total balance 2600.539978\n",
|
||||
"day 80: buy 1 unit at price 835.239990, total balance 1765.299988\n",
|
||||
"day 81: buy 1 unit at price 830.630005, total balance 934.669983\n",
|
||||
"day 83: buy 1 unit at price 827.780029, total balance 106.889954\n",
|
||||
"day 85, sell 1 unit at price 835.369995, investment 3.624632 %, total balance 942.259949,\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 96.719971\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 4.906520 %, total balance 942.339966,\n",
|
||||
"day 90, sell 1 unit at price 847.200012, investment 5.612868 %, total balance 1789.539978,\n",
|
||||
"day 92, sell 1 unit at price 852.119995, investment 5.850783 %, total balance 2641.659973,\n",
|
||||
"day 93, sell 1 unit at price 848.400024, investment 5.743346 %, total balance 3490.059997,\n",
|
||||
"day 95: buy 1 unit at price 829.590027, total balance 2660.469970\n",
|
||||
"day 96: buy 1 unit at price 817.580017, total balance 1842.889953\n",
|
||||
"day 98: buy 1 unit at price 819.510010, total balance 1023.379943\n",
|
||||
"day 99: buy 1 unit at price 820.919983, total balance 202.459960\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment 4.488525 %, total balance 1033.869933,\n",
|
||||
"day 107: buy 1 unit at price 824.669983, total balance 209.199950\n",
|
||||
"day 113, sell 1 unit at price 836.820007, investment 4.795058 %, total balance 1046.019957,\n",
|
||||
"day 115: buy 1 unit at price 841.650024, total balance 204.369933\n",
|
||||
"day 116, sell 1 unit at price 843.190002, investment 2.956119 %, total balance 1047.559935,\n",
|
||||
"day 118: buy 1 unit at price 872.299988, total balance 175.259947\n",
|
||||
"day 119, sell 1 unit at price 871.729980, investment 5.771939 %, total balance 1046.989927,\n",
|
||||
"day 120: buy 1 unit at price 874.250000, total balance 172.739927\n",
|
||||
"day 121, sell 1 unit at price 905.960022, investment 10.052113 %, total balance 1078.699949,\n",
|
||||
"day 122, sell 1 unit at price 912.570007, investment 9.258419 %, total balance 1991.269956,\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 10.330712 %, total balance 2907.709958,\n",
|
||||
"day 124, sell 1 unit at price 927.039978, investment 11.991102 %, total balance 3834.749936,\n",
|
||||
"day 125, sell 1 unit at price 931.659973, investment 10.185207 %, total balance 4766.409909,\n",
|
||||
"day 126, sell 1 unit at price 927.130005, investment 11.757612 %, total balance 5693.539914,\n",
|
||||
"day 127, sell 1 unit at price 934.299988, investment 14.276275 %, total balance 6627.839902,\n",
|
||||
"day 128, sell 1 unit at price 932.169983, investment 13.747236 %, total balance 7560.009885,\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 6631.229856\n",
|
||||
"day 130: buy 1 unit at price 930.599976, total balance 5700.629880\n",
|
||||
"day 132: buy 1 unit at price 937.080017, total balance 4763.549863\n",
|
||||
"day 133: buy 1 unit at price 943.000000, total balance 3820.549863\n",
|
||||
"day 136: buy 1 unit at price 934.010010, total balance 2886.539853\n",
|
||||
"day 137, sell 1 unit at price 941.859985, investment 14.732252 %, total balance 3828.399838,\n",
|
||||
"day 139, sell 1 unit at price 954.960022, investment 15.799052 %, total balance 4783.359860,\n",
|
||||
"day 140, sell 1 unit at price 969.539978, investment 15.195146 %, total balance 5752.899838,\n",
|
||||
"day 141, sell 1 unit at price 971.469971, investment 11.368793 %, total balance 6724.369809,\n",
|
||||
"day 142, sell 1 unit at price 975.880005, investment 11.624822 %, total balance 7700.249814,\n",
|
||||
"day 143, sell 1 unit at price 964.859985, investment 3.884661 %, total balance 8665.109799,\n",
|
||||
"day 144, sell 1 unit at price 966.950012, investment 3.906086 %, total balance 9632.059811,\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment 4.110637 %, total balance 10607.659787,\n",
|
||||
"day 146, sell 1 unit at price 983.679993, investment 4.313891 %, total balance 11591.339780,\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance 10614.769773\n",
|
||||
"day 148, sell 1 unit at price 980.940002, investment 5.024571 %, total balance 11595.709775,\n",
|
||||
"day 153, sell 1 unit at price 950.760010, investment -2.642923 %, total balance 12546.469785,\n",
|
||||
"day 154: buy 1 unit at price 942.309998, total balance 11604.159787\n",
|
||||
"day 155: buy 1 unit at price 939.780029, total balance 10664.379758\n",
|
||||
"day 156: buy 1 unit at price 957.369995, total balance 9707.009763\n",
|
||||
"day 158, sell 1 unit at price 959.450012, investment 1.818936 %, total balance 10666.459775,\n",
|
||||
"day 160, sell 1 unit at price 965.590027, investment 2.746387 %, total balance 11632.049802,\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment -0.532707 %, total balance 12584.319822,\n",
|
||||
"day 163: buy 1 unit at price 940.489990, total balance 11643.829832\n",
|
||||
"day 164: buy 1 unit at price 917.789978, total balance 10726.039854\n",
|
||||
"day 166: buy 1 unit at price 898.700012, total balance 9827.339842\n",
|
||||
"day 167: buy 1 unit at price 911.710022, total balance 8915.629820\n",
|
||||
"day 168: buy 1 unit at price 906.690002, total balance 8008.939818\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 7078.849791\n",
|
||||
"day 172, sell 1 unit at price 943.830017, investment 0.355137 %, total balance 8022.679808,\n",
|
||||
"day 174, sell 1 unit at price 955.989990, investment 4.162174 %, total balance 8978.669798,\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 8025.249815\n",
|
||||
"day 176, sell 1 unit at price 965.400024, investment 7.421833 %, total balance 8990.649839,\n",
|
||||
"day 178, sell 1 unit at price 968.150024, investment 6.190565 %, total balance 9958.799863,\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 7.304589 %, total balance 10931.719846,\n",
|
||||
"day 180, sell 1 unit at price 980.340027, investment 5.402703 %, total balance 11912.059873,\n",
|
||||
"day 181, sell 1 unit at price 950.700012, investment -0.285286 %, total balance 12862.759885,\n",
|
||||
"day 183: buy 1 unit at price 934.090027, total balance 11928.669858\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 10998.169858\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 10067.339841\n",
|
||||
"day 187: buy 1 unit at price 930.390015, total balance 9136.949826\n",
|
||||
"day 188: buy 1 unit at price 923.650024, total balance 8213.299802\n",
|
||||
"day 192: buy 1 unit at price 922.900024, total balance 7290.399778\n",
|
||||
"day 194: buy 1 unit at price 914.390015, total balance 6376.009763\n",
|
||||
"day 195: buy 1 unit at price 922.669983, total balance 5453.339780\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 4526.379758\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 3599.379758\n",
|
||||
"day 203: buy 1 unit at price 921.280029, total balance 2678.099729\n",
|
||||
"day 204: buy 1 unit at price 915.890015, total balance 1762.209714\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 848.399716\n",
|
||||
"day 210, sell 1 unit at price 928.450012, investment -0.603798 %, total balance 1776.849728,\n",
|
||||
"day 212, sell 1 unit at price 935.950012, investment 0.585708 %, total balance 2712.799740,\n",
|
||||
"day 214: buy 1 unit at price 929.080017, total balance 1783.719723\n",
|
||||
"day 215: buy 1 unit at price 932.070007, total balance 851.649716\n",
|
||||
"day 216, sell 1 unit at price 935.090027, investment 0.457657 %, total balance 1786.739743,\n",
|
||||
"day 217: buy 1 unit at price 925.109985, total balance 861.629758\n",
|
||||
"day 222, sell 1 unit at price 932.450012, investment 0.221412 %, total balance 1794.079770,\n",
|
||||
"day 223: buy 1 unit at price 928.530029, total balance 865.549741\n",
|
||||
"day 227, sell 1 unit at price 949.500000, investment 2.798676 %, total balance 1815.049741,\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 3.923498 %, total balance 2774.159726,\n",
|
||||
"day 229: buy 1 unit at price 953.270020, total balance 1820.889706\n",
|
||||
"day 230, sell 1 unit at price 957.789978, investment 4.746329 %, total balance 2778.679684,\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 5.125347 %, total balance 3748.639706,\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 5.602183 %, total balance 4727.529721,\n",
|
||||
"day 234, sell 1 unit at price 977.000000, investment 5.393743 %, total balance 5704.529721,\n",
|
||||
"day 235: buy 1 unit at price 972.599976, total balance 4731.929745\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 7.377775 %, total balance 5721.179745,\n",
|
||||
"day 237, sell 1 unit at price 987.830017, investment 7.854655 %, total balance 6709.009762,\n",
|
||||
"day 238, sell 1 unit at price 989.679993, investment 8.302601 %, total balance 7698.689755,\n",
|
||||
"day 239, sell 1 unit at price 992.000000, investment 6.772289 %, total balance 8690.689755,\n",
|
||||
"day 241, sell 1 unit at price 992.809998, investment 6.516677 %, total balance 9683.499753,\n",
|
||||
"day 242, sell 1 unit at price 984.450012, investment 6.414375 %, total balance 10667.949765,\n",
|
||||
"day 243, sell 1 unit at price 988.200012, investment 6.426285 %, total balance 11656.149777,\n",
|
||||
"day 244: buy 1 unit at price 968.450012, total balance 10687.699765\n",
|
||||
"day 248, sell 1 unit at price 1019.270020, investment 6.923537 %, total balance 11706.969785,\n",
|
||||
"day 249, sell 1 unit at price 1017.109985, investment 4.576394 %, total balance 12724.079770,\n",
|
||||
"day 250, sell 1 unit at price 1016.640015, investment 4.975993 %, total balance 13740.719785,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd8VFX6+PHPvTOTBBJqCr2o4ImAdAREEOwKUkTFhmVXs7usi7qWr7quuqvr6u7Pra5l7SsWFLGs2NuqKCqgiECO0msghABJSJuZ+/vj3hlmJjOTSW/P+/XiJdx67uROvM99nnOOYVkWQgghhBBCCCFaL7OpGyCEEEIIIYQQomFJ4CeEEEIIIYQQrZwEfkIIIYQQQgjRykngJ4QQQgghhBCtnAR+QgghhBBCCNHKSeAnhBBCCCGEEK2cBH5CiDZNKfWUUuruBjjuRKWUru/jipZLKXW5UuqzGOv6KqWKlVKuxm6XEEKItsHd1A0QQoh4lFKbgSu11u83xPYNRWv9KaDq+7hKqY+AIUAysAm4XWv9mrPuVuDWkM1dznZZWuu9IcfoCmi7mfqEkOUnA/8C+gJfApdrrbdEaUNfYG3E4lTgBq31/Uqp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0y5HXAbK11oLTxGuyLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,548 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, output_size, layer_size, learning_rate):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" feed_forward = tf.layers.dense(self.X, layer_size, activation = tf.nn.relu)\n",
|
||||
" self.logits = tf.layers.dense(feed_forward, output_size)\n",
|
||||
" self.cost = tf.reduce_sum(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 500\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.model = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.model_negative = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" self.trainable = tf.trainable_variables()\n",
|
||||
" \n",
|
||||
" def _assign(self):\n",
|
||||
" for i in range(len(self.trainable)//2):\n",
|
||||
" assign_op = self.trainable[i+len(self.trainable)//2].assign(self.trainable[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
"\n",
|
||||
" def _memorize(self, state, action, reward, new_state, done):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, done))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
"\n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action = self.get_predicted_action([state])\n",
|
||||
" return action\n",
|
||||
"\n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" Q = self.predict(states)\n",
|
||||
" Q_new = self.predict(new_states)\n",
|
||||
" Q_new_negative = self.sess.run(self.model_negative.logits, feed_dict={self.model_negative.X:new_states})\n",
|
||||
" replay_size = len(replay)\n",
|
||||
" X = np.empty((replay_size, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.OUTPUT_SIZE))\n",
|
||||
" for i in range(replay_size):\n",
|
||||
" state_r, action_r, reward_r, new_state_r, done_r = replay[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action_r] = reward_r\n",
|
||||
" if not done_r:\n",
|
||||
" target[action_r] += self.GAMMA * Q_new_negative[i, np.argmax(Q_new[i])]\n",
|
||||
" X[i] = state_r\n",
|
||||
" Y[i] = target\n",
|
||||
" return X, Y\n",
|
||||
"\n",
|
||||
" def predict(self, inputs):\n",
|
||||
" return self.sess.run(self.model.logits, feed_dict={self.model.X:inputs})\n",
|
||||
" \n",
|
||||
" def get_predicted_action(self, sequence):\n",
|
||||
" prediction = self.predict(np.array(sequence))[0]\n",
|
||||
" return np.argmax(prediction)\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" state = next_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign()\n",
|
||||
" \n",
|
||||
" action = self._select_action(state)\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" \n",
|
||||
" self._memorize(state, action, invest, next_state, starting_money < initial_money)\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" state = next_state\n",
|
||||
" X, Y = self._construct_memories(replay)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.model.cost, self.model.optimizer], \n",
|
||||
" feed_dict={self.model.X: X, self.model.Y:Y})\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch: 10, total rewards: 1241.885127.3, cost: 1.110860, total money: 1744.875178\n",
|
||||
"epoch: 20, total rewards: 89.105106.3, cost: 0.649060, total money: 8097.275088\n",
|
||||
"epoch: 30, total rewards: 719.079470.3, cost: 0.823131, total money: 9699.809450\n",
|
||||
"epoch: 40, total rewards: 684.040043.3, cost: 1.931746, total money: 134.750004\n",
|
||||
"epoch: 50, total rewards: 1744.829771.3, cost: 0.895153, total money: 11744.829771\n",
|
||||
"epoch: 60, total rewards: 149.195010.3, cost: 1.097174, total money: 5196.854982\n",
|
||||
"epoch: 70, total rewards: 1389.289786.3, cost: 0.860031, total money: 9399.319754\n",
|
||||
"epoch: 80, total rewards: 529.019898.3, cost: 0.305593, total money: 10529.019898\n",
|
||||
"epoch: 90, total rewards: 1285.264893.3, cost: 1.882383, total money: 9251.514893\n",
|
||||
"epoch: 100, total rewards: 409.474970.3, cost: 0.146280, total money: 551.414972\n",
|
||||
"epoch: 110, total rewards: 1074.725155.3, cost: 0.661549, total money: 2231.475154\n",
|
||||
"epoch: 120, total rewards: 1713.854676.3, cost: 1.219318, total money: 11713.854676\n",
|
||||
"epoch: 130, total rewards: 871.945621.3, cost: 1.460638, total money: 8947.665652\n",
|
||||
"epoch: 140, total rewards: 1564.314818.3, cost: 1.133385, total money: 2767.354796\n",
|
||||
"epoch: 150, total rewards: 855.729796.3, cost: 1.886093, total money: 10855.729796\n",
|
||||
"epoch: 160, total rewards: 302.970157.3, cost: 0.642825, total money: 6320.700137\n",
|
||||
"epoch: 170, total rewards: 512.139521.3, cost: 3.411159, total money: 1801.649470\n",
|
||||
"epoch: 180, total rewards: 769.354739.3, cost: 0.379282, total money: 10769.354739\n",
|
||||
"epoch: 190, total rewards: 332.274720.3, cost: 1.111366, total money: 10332.274720\n",
|
||||
"epoch: 200, total rewards: 395.419923.3, cost: 0.270106, total money: 5401.389893\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 7: buy 1 unit at price 754.020020, total balance 9245.979980\n",
|
||||
"day 9, sell 1 unit at price 758.489990, investment 0.592818 %, total balance 10004.469970,\n",
|
||||
"day 10: buy 1 unit at price 764.479980, total balance 9239.989990\n",
|
||||
"day 11: buy 1 unit at price 771.229980, total balance 8468.760010\n",
|
||||
"day 12, sell 1 unit at price 760.539978, investment -0.515383 %, total balance 9229.299988,\n",
|
||||
"day 13, sell 1 unit at price 769.200012, investment -0.263212 %, total balance 9998.500000,\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 9230.260010\n",
|
||||
"day 19, sell 1 unit at price 758.039978, investment -1.327712 %, total balance 9988.299988,\n",
|
||||
"day 21: buy 1 unit at price 750.500000, total balance 9237.799988\n",
|
||||
"day 22, sell 1 unit at price 762.520020, investment 1.601602 %, total balance 10000.320008,\n",
|
||||
"day 26: buy 1 unit at price 789.289978, total balance 9211.030030\n",
|
||||
"day 27, sell 1 unit at price 789.270020, investment -0.002529 %, total balance 10000.300050,\n",
|
||||
"day 30: buy 1 unit at price 797.849976, total balance 9202.450074\n",
|
||||
"day 33, sell 1 unit at price 796.419983, investment -0.179231 %, total balance 9998.870057,\n",
|
||||
"day 41: buy 1 unit at price 786.140015, total balance 9212.730042\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 0.096676 %, total balance 9999.630066,\n",
|
||||
"day 45: buy 1 unit at price 806.650024, total balance 9192.980042\n",
|
||||
"day 46: buy 1 unit at price 804.789978, total balance 8388.190064\n",
|
||||
"day 47, sell 1 unit at price 807.909973, investment 0.156195 %, total balance 9196.100037,\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 0.383954 %, total balance 10003.980042,\n",
|
||||
"day 54: buy 1 unit at price 819.309998, total balance 9184.670044\n",
|
||||
"day 55, sell 1 unit at price 823.869995, investment 0.556566 %, total balance 10008.540039,\n",
|
||||
"day 61: buy 1 unit at price 795.695007, total balance 9212.845032\n",
|
||||
"day 62, sell 1 unit at price 798.530029, investment 0.356295 %, total balance 10011.375061,\n",
|
||||
"day 64: buy 1 unit at price 801.340027, total balance 9210.035034\n",
|
||||
"day 65, sell 1 unit at price 806.969971, investment 0.702566 %, total balance 10017.005005,\n",
|
||||
"day 66: buy 1 unit at price 808.380005, total balance 9208.625000\n",
|
||||
"day 67: buy 1 unit at price 809.559998, total balance 8399.065002\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 7585.395019\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 1.343426 %, total balance 8404.635009,\n",
|
||||
"day 70, sell 1 unit at price 820.450012, investment 1.345177 %, total balance 9225.085021,\n",
|
||||
"day 71, sell 1 unit at price 818.979980, investment 0.652598 %, total balance 10044.065001,\n",
|
||||
"day 74: buy 1 unit at price 831.659973, total balance 9212.405028\n",
|
||||
"day 76, sell 1 unit at price 831.330017, investment -0.039674 %, total balance 10043.735045,\n",
|
||||
"day 79: buy 1 unit at price 823.210022, total balance 9220.525023\n",
|
||||
"day 80, sell 1 unit at price 835.239990, investment 1.461349 %, total balance 10055.765013,\n",
|
||||
"day 83: buy 1 unit at price 827.780029, total balance 9227.984984\n",
|
||||
"day 84, sell 1 unit at price 831.909973, investment 0.498918 %, total balance 10059.894957,\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 9245.464964\n",
|
||||
"day 98, sell 1 unit at price 819.510010, investment 0.623751 %, total balance 10064.974974,\n",
|
||||
"day 102: buy 1 unit at price 829.559998, total balance 9235.414976\n",
|
||||
"day 103, sell 1 unit at price 838.549988, investment 1.083706 %, total balance 10073.964964,\n",
|
||||
"day 104: buy 1 unit at price 834.570007, total balance 9239.394957\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment -0.378642 %, total balance 10070.804930,\n",
|
||||
"day 107: buy 1 unit at price 824.669983, total balance 9246.134947\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance 8421.404967\n",
|
||||
"day 109, sell 1 unit at price 823.349976, investment -0.160065 %, total balance 9244.754943,\n",
|
||||
"day 110, sell 1 unit at price 824.320007, investment -0.049710 %, total balance 10069.074950,\n",
|
||||
"day 113: buy 1 unit at price 836.820007, total balance 9232.254943\n",
|
||||
"day 114, sell 1 unit at price 838.210022, investment 0.166107 %, total balance 10070.464965,\n",
|
||||
"day 117: buy 1 unit at price 862.760010, total balance 9207.704955\n",
|
||||
"day 118, sell 1 unit at price 872.299988, investment 1.105751 %, total balance 10080.004943,\n",
|
||||
"day 120: buy 1 unit at price 874.250000, total balance 9205.754943\n",
|
||||
"day 123, sell 1 unit at price 916.440002, investment 4.825851 %, total balance 10122.194945,\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance 9190.534972\n",
|
||||
"day 126: buy 1 unit at price 927.130005, total balance 8263.404967\n",
|
||||
"day 127, sell 1 unit at price 934.299988, investment 0.283367 %, total balance 9197.704955,\n",
|
||||
"day 128, sell 1 unit at price 932.169983, investment 0.543611 %, total balance 10129.874938,\n",
|
||||
"day 132: buy 1 unit at price 937.080017, total balance 9192.794921\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 0.631748 %, total balance 10135.794921,\n",
|
||||
"day 135: buy 1 unit at price 930.239990, total balance 9205.554931\n",
|
||||
"day 138, sell 1 unit at price 948.820007, investment 1.997336 %, total balance 10154.374938,\n",
|
||||
"day 139: buy 1 unit at price 954.960022, total balance 9199.414916\n",
|
||||
"day 140, sell 1 unit at price 969.539978, investment 1.526761 %, total balance 10168.954894,\n",
|
||||
"day 141: buy 1 unit at price 971.469971, total balance 9197.484923\n",
|
||||
"day 143, sell 1 unit at price 964.859985, investment -0.680411 %, total balance 10162.344908,\n",
|
||||
"day 153: buy 1 unit at price 950.760010, total balance 9211.584898\n",
|
||||
"day 154, sell 1 unit at price 942.309998, investment -0.888764 %, total balance 10153.894896,\n",
|
||||
"day 157: buy 1 unit at price 950.630005, total balance 9203.264891\n",
|
||||
"day 158, sell 1 unit at price 959.450012, investment 0.927807 %, total balance 10162.714903,\n",
|
||||
"day 161: buy 1 unit at price 952.270020, total balance 9210.444883\n",
|
||||
"day 162: buy 1 unit at price 927.330017, total balance 8283.114866\n",
|
||||
"day 163, sell 1 unit at price 940.489990, investment -1.237047 %, total balance 9223.604856,\n",
|
||||
"day 164, sell 1 unit at price 917.789978, investment -1.028764 %, total balance 10141.394834,\n",
|
||||
"day 171: buy 1 unit at price 930.090027, total balance 9211.304807\n",
|
||||
"day 172, sell 1 unit at price 943.830017, investment 1.477275 %, total balance 10155.134824,\n",
|
||||
"day 178: buy 1 unit at price 968.150024, total balance 9186.984800\n",
|
||||
"day 179, sell 1 unit at price 972.919983, investment 0.492688 %, total balance 10159.904783,\n",
|
||||
"day 180: buy 1 unit at price 980.340027, total balance 9179.564756\n",
|
||||
"day 181: buy 1 unit at price 950.700012, total balance 8228.864744\n",
|
||||
"day 182: buy 1 unit at price 947.799988, total balance 7281.064756\n",
|
||||
"day 183: buy 1 unit at price 934.090027, total balance 6346.974729\n",
|
||||
"day 185: buy 1 unit at price 930.500000, total balance 5416.474729\n",
|
||||
"day 186, sell 1 unit at price 930.830017, investment -5.050290 %, total balance 6347.304746,\n",
|
||||
"day 188: buy 1 unit at price 923.650024, total balance 5423.654722\n",
|
||||
"day 189: buy 1 unit at price 927.960022, total balance 4495.694700\n",
|
||||
"day 190, sell 1 unit at price 929.359985, investment -2.244665 %, total balance 5425.054685,\n",
|
||||
"day 193, sell 1 unit at price 907.239990, investment -4.279384 %, total balance 6332.294675,\n",
|
||||
"day 195, sell 1 unit at price 922.669983, investment -1.222585 %, total balance 7254.964658,\n",
|
||||
"day 196, sell 1 unit at price 922.219971, investment -0.889847 %, total balance 8177.184629,\n",
|
||||
"day 197, sell 1 unit at price 926.960022, investment 0.358361 %, total balance 9104.144651,\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -1.829825 %, total balance 10015.124631,\n",
|
||||
"day 202: buy 1 unit at price 927.000000, total balance 9088.124631\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 8174.314633\n",
|
||||
"day 207, sell 1 unit at price 929.570007, investment 0.277239 %, total balance 9103.884640,\n",
|
||||
"day 208, sell 1 unit at price 939.330017, investment 2.792705 %, total balance 10043.214657,\n",
|
||||
"day 215: buy 1 unit at price 932.070007, total balance 9111.144650\n",
|
||||
"day 217: buy 1 unit at price 925.109985, total balance 8186.034665\n",
|
||||
"day 218: buy 1 unit at price 920.289978, total balance 7265.744687\n",
|
||||
"day 219: buy 1 unit at price 915.000000, total balance 6350.744687\n",
|
||||
"day 220: buy 1 unit at price 921.809998, total balance 5428.934689\n",
|
||||
"day 221, sell 1 unit at price 931.580017, investment -0.052570 %, total balance 6360.514706,\n",
|
||||
"day 222: buy 1 unit at price 932.450012, total balance 5428.064694\n",
|
||||
"day 223: buy 1 unit at price 928.530029, total balance 4499.534665\n",
|
||||
"day 224, sell 1 unit at price 920.969971, investment -0.447516 %, total balance 5420.504636,\n",
|
||||
"day 225: buy 1 unit at price 924.859985, total balance 4495.644651\n",
|
||||
"day 226: buy 1 unit at price 944.489990, total balance 3551.154661\n",
|
||||
"day 228, sell 1 unit at price 959.109985, investment 4.218236 %, total balance 4510.264646,\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 4.182516 %, total balance 5463.534666,\n",
|
||||
"day 230, sell 1 unit at price 957.789978, investment 3.903188 %, total balance 6421.324644,\n",
|
||||
"day 231, sell 1 unit at price 951.679993, investment 2.062307 %, total balance 7373.004637,\n",
|
||||
"day 232, sell 1 unit at price 969.960022, investment 4.461890 %, total balance 8342.964659,\n",
|
||||
"day 235: buy 1 unit at price 972.599976, total balance 7370.364683\n",
|
||||
"day 236, sell 1 unit at price 989.250000, investment 6.962137 %, total balance 8359.614683,\n",
|
||||
"day 238: buy 1 unit at price 989.679993, total balance 7369.934690\n",
|
||||
"day 241: buy 1 unit at price 992.809998, total balance 6377.124692\n",
|
||||
"day 242, sell 1 unit at price 984.450012, investment 4.230857 %, total balance 7361.574704,\n",
|
||||
"day 243: buy 1 unit at price 988.200012, total balance 6373.374692\n",
|
||||
"day 244: buy 1 unit at price 968.450012, total balance 5404.924680\n",
|
||||
"day 245, sell 1 unit at price 970.539978, investment -0.211803 %, total balance 6375.464658,\n",
|
||||
"day 246: buy 1 unit at price 973.330017, total balance 5402.134641\n",
|
||||
"day 247, sell 1 unit at price 972.559998, investment -1.729852 %, total balance 6374.694639,\n",
|
||||
"day 248: buy 1 unit at price 1019.270020, total balance 5355.424619\n",
|
||||
"day 249, sell 1 unit at price 1017.109985, investment 2.447597 %, total balance 6372.534604,\n",
|
||||
"day 250: buy 1 unit at price 1016.640015, total balance 5355.894589\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xd4VFX6wPHvnZJeCCn0KnCA0MtSFAR7QdQVZW2r7mpW0VVX7LrqrujuuutvXde1rGvHgr1hryBFmggCOUgLLSEhJCEJpEz5/XFnhslkJpkkk0Lyfp6Hx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f91vMzwnMz/Ayv/V3YQ7E8zzmQ3sOR7LFgf6F2dzuGsys2fWeIP1fmAN7bAFmaK33B9secyCOTzCzOeWYzfB20Qy01rlKqWXA8fiNfKq13uUJrB/CzAo6MQOVazCDiJuAFzEzS2s58t34CjM7laeUcmmtG9Q81RNYz8DsS7cd87PVhD+tQK3vfT3HO4TZXBQAZY5GWqG1LvCsP6DM0YAfx8w0ZwNnez87zwsJtNZXezLwIe996v7eQB33nNZ6g1LqaswAMBUz6L7Cs50d82/WYMzPKRvze7LZU7d8/3P2ZBz3e5pxe5edAHTy9vnVWq/wvLjahXn9Z9V1HYUQHYvhdje1dYcQQrRPnqzek1rrwOZ+4iiLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,458 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, self.state_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, self.OUTPUT_SIZE))\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(self.LAYER_SIZE, state_is_tuple = False)\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * self.LAYER_SIZE))\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" self.logits = tf.layers.dense(self.rnn[:,-1], self.OUTPUT_SIZE)\n",
|
||||
" self.cost = tf.reduce_sum(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = self.LEARNING_RATE).minimize(self.cost)\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" \n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
"\n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" Q = self.sess.run(self.logits, feed_dict={self.X:states, self.hidden_layer:init_values})\n",
|
||||
" Q_new = self.sess.run(self.logits, feed_dict={self.X:new_states, self.hidden_layer:init_values})\n",
|
||||
" replay_size = len(replay)\n",
|
||||
" X = np.empty((replay_size, 4, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.OUTPUT_SIZE))\n",
|
||||
" INIT_VAL = np.empty((replay_size, 2 * self.LAYER_SIZE))\n",
|
||||
" for i in range(replay_size):\n",
|
||||
" state_r, action_r, reward_r, new_state_r, dead_r, rnn_memory = replay[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action_r] = reward_r\n",
|
||||
" if not dead_r:\n",
|
||||
" target[action_r] += self.GAMMA * np.amax(Q_new[i])\n",
|
||||
" X[i] = state_r\n",
|
||||
" Y[i] = target\n",
|
||||
" INIT_VAL[i] = rnn_memory\n",
|
||||
" return X, Y, INIT_VAL\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action, last_state = self.sess.run([self.logits,self.last_state],\n",
|
||||
" feed_dict={self.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.logits,\n",
|
||||
" self.last_state],\n",
|
||||
" feed_dict={self.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" X, Y, INIT_VAL = self._construct_memories(replay)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.cost, self.optimizer], \n",
|
||||
" feed_dict={self.X: X, self.Y:Y,\n",
|
||||
" self.hidden_layer: INIT_VAL})\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" \n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7fef003b2d30>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"epoch: 10, total rewards: 449.400388.3, cost: 0.117951, total money: 7420.680355\n",
|
||||
"epoch: 20, total rewards: 513.109983.3, cost: 0.187314, total money: 7552.130003\n",
|
||||
"epoch: 30, total rewards: 1755.114813.3, cost: 0.337607, total money: 6759.834784\n",
|
||||
"epoch: 40, total rewards: 545.719909.3, cost: 0.555657, total money: 9529.079894\n",
|
||||
"epoch: 50, total rewards: 593.435182.3, cost: 0.399239, total money: 6611.165162\n",
|
||||
"epoch: 60, total rewards: 285.174678.3, cost: 0.071772, total money: 6314.564631\n",
|
||||
"epoch: 70, total rewards: 169.200014.3, cost: 0.796504, total money: 4264.030030\n",
|
||||
"epoch: 80, total rewards: 520.019840.3, cost: 0.567794, total money: 6501.959842\n",
|
||||
"epoch: 90, total rewards: 498.320189.3, cost: 0.245750, total money: 9481.210204\n",
|
||||
"epoch: 100, total rewards: 1572.605044.3, cost: 1.142984, total money: 11572.605044\n",
|
||||
"epoch: 110, total rewards: 297.584960.3, cost: 0.973414, total money: 10297.584960\n",
|
||||
"epoch: 120, total rewards: 912.394901.3, cost: 2.032860, total money: 6987.034854\n",
|
||||
"epoch: 130, total rewards: 22.109988.3, cost: 0.097879, total money: 10022.109988\n",
|
||||
"epoch: 140, total rewards: 471.779909.3, cost: 0.532008, total money: 10471.779909\n",
|
||||
"epoch: 150, total rewards: 215.255126.3, cost: 0.236825, total money: 10215.255126\n",
|
||||
"epoch: 160, total rewards: 147.780093.3, cost: 0.432537, total money: 9174.450076\n",
|
||||
"epoch: 170, total rewards: 203.309817.3, cost: 0.413111, total money: 10203.309817\n",
|
||||
"epoch: 180, total rewards: 76.350403.3, cost: 0.132205, total money: 8084.520385\n",
|
||||
"epoch: 190, total rewards: 173.749880.3, cost: 1.325852, total money: 10173.749880\n",
|
||||
"epoch: 200, total rewards: 4.325196.3, cost: 0.500293, total money: 8987.685181\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 13: buy 1 unit at price 769.200012, total balance 9230.799988\n",
|
||||
"day 14: buy 1 unit at price 768.270020, total balance 8462.529968\n",
|
||||
"day 15, sell 1 unit at price 760.989990, investment -1.067346 %, total balance 9223.519958,\n",
|
||||
"day 17: buy 1 unit at price 768.239990, total balance 8455.279968\n",
|
||||
"day 18, sell 1 unit at price 770.840027, investment 0.334519 %, total balance 9226.119995,\n",
|
||||
"day 19, sell 1 unit at price 758.039978, investment -1.327712 %, total balance 9984.159973,\n",
|
||||
"day 29: buy 1 unit at price 797.070007, total balance 9187.089966\n",
|
||||
"day 30: buy 1 unit at price 797.849976, total balance 8389.239990\n",
|
||||
"day 33, sell 1 unit at price 796.419983, investment -0.081552 %, total balance 9185.659973,\n",
|
||||
"day 34: buy 1 unit at price 794.559998, total balance 8391.099975\n",
|
||||
"day 36, sell 1 unit at price 789.909973, investment -0.995175 %, total balance 9181.009948,\n",
|
||||
"day 37, sell 1 unit at price 791.549988, investment -0.378827 %, total balance 9972.559936,\n",
|
||||
"day 39: buy 1 unit at price 782.789978, total balance 9189.769958\n",
|
||||
"day 40, sell 1 unit at price 771.820007, investment -1.401394 %, total balance 9961.589965,\n",
|
||||
"day 46: buy 1 unit at price 804.789978, total balance 9156.799987\n",
|
||||
"day 47: buy 1 unit at price 807.909973, total balance 8348.890014\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment 0.383954 %, total balance 9156.770019,\n",
|
||||
"day 50, sell 1 unit at price 804.609985, investment -0.408460 %, total balance 9961.380004,\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 9155.309997\n",
|
||||
"day 54, sell 1 unit at price 819.309998, investment 1.642536 %, total balance 9974.619995,\n",
|
||||
"day 110: buy 1 unit at price 824.320007, total balance 9150.299988\n",
|
||||
"day 111, sell 1 unit at price 823.559998, investment -0.092198 %, total balance 9973.859986,\n",
|
||||
"day 128: buy 1 unit at price 932.169983, total balance 9041.690003\n",
|
||||
"day 129: buy 1 unit at price 928.780029, total balance 8112.909974\n",
|
||||
"day 130, sell 1 unit at price 930.599976, investment -0.168425 %, total balance 9043.509950,\n",
|
||||
"day 131, sell 1 unit at price 932.219971, investment 0.370372 %, total balance 9975.729921,\n",
|
||||
"day 173: buy 1 unit at price 947.159973, total balance 9028.569948\n",
|
||||
"day 175, sell 1 unit at price 953.419983, investment 0.660924 %, total balance 9981.989931,\n",
|
||||
"day 182: buy 1 unit at price 947.799988, total balance 9034.189943\n",
|
||||
"day 183, sell 1 unit at price 934.090027, investment -1.446504 %, total balance 9968.279970,\n",
|
||||
"day 197: buy 1 unit at price 926.960022, total balance 9041.319948\n",
|
||||
"day 198, sell 1 unit at price 910.979980, investment -1.723919 %, total balance 9952.299928,\n",
|
||||
"day 204: buy 1 unit at price 915.890015, total balance 9036.409913\n",
|
||||
"day 205, sell 1 unit at price 913.809998, investment -0.227103 %, total balance 9950.219911,\n",
|
||||
"day 207: buy 1 unit at price 929.570007, total balance 9020.649904\n",
|
||||
"day 209, sell 1 unit at price 937.340027, investment 0.835872 %, total balance 9957.989931,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA34AAAFBCAYAAAAomn1FAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XeYVNX5wPHv1O2dpVcFjoBSBKKAIPYarNFoNGJUTNCfGlvU2LsmGmM3dsWCXRRFVBAQAaUjwqEvC2zvdWZn5v7+uHeG3WV7Z/f9PI8Pu/eee865s3fXeec9xWYYBkIIIYQQQgghOi97e3dACCG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BVDZVsHIUST2AyjuaM9hBBCWFm9F7XW1YcKinailLoTyNLWNhpCCCFEVyaBnxBCNIE1Z+o4zKxfD8w5fcu11je0a8eEEEIIIWogQz2FEKJpbMB9mEM512AutnB3u/ZICCGEEKIWkvETQgghhBBCiE5OMn5CCCGEEEII0ckdjNs5hGGuxpWGuQS4EEIIIYQQQnQlDszVrH/B3JqqXgdj4DceWNLenRBCCCGEEEKIdjYZ+LEhBQ/GwC8NIC+vhECgY81PTEqKJienuP6CQjSSPFuiNcnzJVqLPFuiNcnzJVpLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,598 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from collections import deque\n",
|
||||
"import random\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, output_size, layer_size, learning_rate, name):\n",
|
||||
" with tf.variable_scope(name):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, input_size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" cell = tf.nn.rnn_cell.LSTMCell(layer_size, state_is_tuple = False)\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, (None, 2 * layer_size))\n",
|
||||
" self.rnn,self.last_state = tf.nn.dynamic_rnn(inputs=self.X,cell=cell,\n",
|
||||
" dtype=tf.float32,\n",
|
||||
" initial_state=self.hidden_layer)\n",
|
||||
" self.logits = tf.layers.dense(self.rnn[:,-1], output_size)\n",
|
||||
" self.cost = tf.reduce_sum(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(self.cost)\n",
|
||||
" \n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" LEARNING_RATE = 0.003\n",
|
||||
" BATCH_SIZE = 32\n",
|
||||
" LAYER_SIZE = 256\n",
|
||||
" OUTPUT_SIZE = 3\n",
|
||||
" EPSILON = 0.5\n",
|
||||
" DECAY_RATE = 0.005\n",
|
||||
" MIN_EPSILON = 0.1\n",
|
||||
" GAMMA = 0.99\n",
|
||||
" MEMORIES = deque()\n",
|
||||
" COPY = 1000\n",
|
||||
" T_COPY = 0\n",
|
||||
" MEMORY_SIZE = 300\n",
|
||||
" \n",
|
||||
" def __init__(self, state_size, window_size, trend, skip):\n",
|
||||
" self.state_size = state_size\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.half_window = window_size // 2\n",
|
||||
" self.trend = trend\n",
|
||||
" self.skip = skip\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" self.INITIAL_FEATURES = np.zeros((4, self.state_size))\n",
|
||||
" self.model = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE,\n",
|
||||
" 'real_model')\n",
|
||||
" self.model_negative = Model(self.state_size, self.OUTPUT_SIZE, self.LAYER_SIZE, self.LEARNING_RATE,\n",
|
||||
" 'negative_model')\n",
|
||||
" self.sess = tf.InteractiveSession()\n",
|
||||
" self.sess.run(tf.global_variables_initializer())\n",
|
||||
" self.trainable = tf.trainable_variables()\n",
|
||||
" \n",
|
||||
" def _assign(self, from_name, to_name):\n",
|
||||
" from_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=from_name)\n",
|
||||
" to_w = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=to_name)\n",
|
||||
" for i in range(len(from_w)):\n",
|
||||
" assign_op = to_w[i].assign(from_w[i])\n",
|
||||
" self.sess.run(assign_op)\n",
|
||||
"\n",
|
||||
" def _memorize(self, state, action, reward, new_state, dead, rnn_state):\n",
|
||||
" self.MEMORIES.append((state, action, reward, new_state, dead, rnn_state))\n",
|
||||
" if len(self.MEMORIES) > self.MEMORY_SIZE:\n",
|
||||
" self.MEMORIES.popleft()\n",
|
||||
"\n",
|
||||
" def _select_action(self, state):\n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action = self.get_predicted_action([state])\n",
|
||||
" return action\n",
|
||||
"\n",
|
||||
" def _construct_memories(self, replay):\n",
|
||||
" states = np.array([a[0] for a in replay])\n",
|
||||
" new_states = np.array([a[3] for a in replay])\n",
|
||||
" init_values = np.array([a[-1] for a in replay])\n",
|
||||
" Q = self.sess.run(self.model.logits, feed_dict={self.model.X:states, \n",
|
||||
" self.model.hidden_layer:init_values})\n",
|
||||
" Q_new = self.sess.run(self.model.logits, feed_dict={self.model.X:new_states, \n",
|
||||
" self.model.hidden_layer:init_values})\n",
|
||||
" Q_new_negative = self.sess.run(self.model_negative.logits, \n",
|
||||
" feed_dict={self.model_negative.X:new_states, \n",
|
||||
" self.model_negative.hidden_layer:init_values})\n",
|
||||
" replay_size = len(replay)\n",
|
||||
" X = np.empty((replay_size, 4, self.state_size))\n",
|
||||
" Y = np.empty((replay_size, self.OUTPUT_SIZE))\n",
|
||||
" INIT_VAL = np.empty((replay_size, 2 * self.LAYER_SIZE))\n",
|
||||
" for i in range(replay_size):\n",
|
||||
" state_r, action_r, reward_r, new_state_r, dead_r, rnn_memory = replay[i]\n",
|
||||
" target = Q[i]\n",
|
||||
" target[action_r] = reward_r\n",
|
||||
" if not dead_r:\n",
|
||||
" target[action_r] += self.GAMMA * Q_new_negative[i, np.argmax(Q_new[i])]\n",
|
||||
" X[i] = state_r\n",
|
||||
" Y[i] = target\n",
|
||||
" INIT_VAL[i] = rnn_memory\n",
|
||||
" return X, Y, INIT_VAL\n",
|
||||
" \n",
|
||||
" def get_state(self, t):\n",
|
||||
" window_size = self.window_size + 1\n",
|
||||
" d = t - window_size + 1\n",
|
||||
" block = self.trend[d : t + 1] if d >= 0 else -d * [self.trend[0]] + self.trend[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(window_size - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array(res)\n",
|
||||
" \n",
|
||||
" def buy(self, initial_money):\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" action, last_state = self.sess.run([self.model.logits,self.model.last_state],\n",
|
||||
" feed_dict={self.model.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.model.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and initial_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" initial_money -= self.trend[t]\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print('day %d: buy 1 unit at price %f, total balance %f'% (t, self.trend[t], initial_money))\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory):\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" initial_money += self.trend[t]\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((close[t] - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell 1 unit at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, close[t], invest, initial_money)\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" total_gains = initial_money - starting_money\n",
|
||||
" return states_buy, states_sell, total_gains, invest\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def train(self, iterations, checkpoint, initial_money):\n",
|
||||
" for i in range(iterations):\n",
|
||||
" total_profit = 0\n",
|
||||
" inventory = []\n",
|
||||
" state = self.get_state(0)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" init_value = np.zeros((1, 2 * self.LAYER_SIZE))\n",
|
||||
" for k in range(self.INITIAL_FEATURES.shape[0]):\n",
|
||||
" self.INITIAL_FEATURES[k,:] = state\n",
|
||||
" for t in range(0, len(self.trend) - 1, self.skip):\n",
|
||||
" if (self.T_COPY + 1) % self.COPY == 0:\n",
|
||||
" self._assign('real_model', 'negative_model')\n",
|
||||
" \n",
|
||||
" if np.random.rand() < self.EPSILON:\n",
|
||||
" action = np.random.randint(self.OUTPUT_SIZE)\n",
|
||||
" else:\n",
|
||||
" action, last_state = self.sess.run([self.model.logits,\n",
|
||||
" self.model.last_state],\n",
|
||||
" feed_dict={self.model.X:[self.INITIAL_FEATURES],\n",
|
||||
" self.model.hidden_layer:init_value})\n",
|
||||
" action, init_value = np.argmax(action[0]), last_state\n",
|
||||
" \n",
|
||||
" next_state = self.get_state(t + 1)\n",
|
||||
" \n",
|
||||
" if action == 1 and starting_money >= self.trend[t]:\n",
|
||||
" inventory.append(self.trend[t])\n",
|
||||
" starting_money -= self.trend[t]\n",
|
||||
" \n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" total_profit += self.trend[t] - bought_price\n",
|
||||
" starting_money += self.trend[t]\n",
|
||||
" \n",
|
||||
" invest = ((starting_money - initial_money) / initial_money)\n",
|
||||
" new_state = np.append([self.get_state(t + 1)], self.INITIAL_FEATURES[:3, :], axis = 0)\n",
|
||||
" \n",
|
||||
" self._memorize(self.INITIAL_FEATURES, action, invest, new_state, \n",
|
||||
" starting_money < initial_money, init_value[0])\n",
|
||||
" self.INITIAL_FEATURES = new_state\n",
|
||||
" batch_size = min(len(self.MEMORIES), self.BATCH_SIZE)\n",
|
||||
" replay = random.sample(self.MEMORIES, batch_size)\n",
|
||||
" X, Y, INIT_VAL = self._construct_memories(replay)\n",
|
||||
" \n",
|
||||
" cost, _ = self.sess.run([self.model.cost, self.model.optimizer], \n",
|
||||
" feed_dict={self.model.X: X, self.model.Y:Y,\n",
|
||||
" self.model.hidden_layer: INIT_VAL})\n",
|
||||
" self.T_COPY += 1\n",
|
||||
" self.EPSILON = self.MIN_EPSILON + (1.0 - self.MIN_EPSILON) * np.exp(-self.DECAY_RATE * i)\n",
|
||||
" if (i+1) % checkpoint == 0:\n",
|
||||
" print('epoch: %d, total rewards: %f.3, cost: %f, total money: %f'%(i + 1, total_profit, cost,\n",
|
||||
" starting_money))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7fb85fd10940>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7fb85f9de7b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"epoch: 10, total rewards: 1305.274912.3, cost: 0.402263, total money: 777.284860\n",
|
||||
"epoch: 20, total rewards: 582.070375.3, cost: 0.782595, total money: 804.650331\n",
|
||||
"epoch: 30, total rewards: 420.380369.3, cost: 1.481925, total money: 80.210326\n",
|
||||
"epoch: 40, total rewards: 1502.554748.3, cost: 0.343374, total money: 2823.564757\n",
|
||||
"epoch: 50, total rewards: 589.170222.3, cost: 0.370314, total money: 6597.640193\n",
|
||||
"epoch: 60, total rewards: 1069.864985.3, cost: 0.733583, total money: 10052.755000\n",
|
||||
"epoch: 70, total rewards: 900.360168.3, cost: 0.154633, total money: 8866.610168\n",
|
||||
"epoch: 80, total rewards: 625.559509.3, cost: 0.573019, total money: 9652.999511\n",
|
||||
"epoch: 90, total rewards: 966.905028.3, cost: 0.080430, total money: 6971.785033\n",
|
||||
"epoch: 100, total rewards: 784.169802.3, cost: 0.568819, total money: 10784.169802\n",
|
||||
"epoch: 110, total rewards: 658.149963.3, cost: 0.052230, total money: 9641.509948\n",
|
||||
"epoch: 120, total rewards: 615.210201.3, cost: 0.802322, total money: 9595.940181\n",
|
||||
"epoch: 130, total rewards: 623.289978.3, cost: 0.278659, total money: 10623.289978\n",
|
||||
"epoch: 140, total rewards: 595.960078.3, cost: 0.094435, total money: 10595.960078\n",
|
||||
"epoch: 150, total rewards: 594.979550.3, cost: 0.360762, total money: 1819.289547\n",
|
||||
"epoch: 160, total rewards: 794.614687.3, cost: 1.058314, total money: 3118.034730\n",
|
||||
"epoch: 170, total rewards: 1225.854981.3, cost: 0.226553, total money: 5322.584961\n",
|
||||
"epoch: 180, total rewards: 1099.610169.3, cost: 0.275357, total money: 6189.200135\n",
|
||||
"epoch: 190, total rewards: 857.554813.3, cost: 0.417154, total money: 7946.004825\n",
|
||||
"epoch: 200, total rewards: 1049.100096.3, cost: 0.839669, total money: 3317.970090\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"initial_money = 10000\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"batch_size = 32\n",
|
||||
"agent = Agent(state_size = window_size, \n",
|
||||
" window_size = window_size, \n",
|
||||
" trend = close, \n",
|
||||
" skip = skip)\n",
|
||||
"agent.train(iterations = 200, checkpoint = 10, initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 unit at price 768.700012, total balance 9231.299988\n",
|
||||
"day 1, sell 1 unit at price 762.130005, investment -0.854691 %, total balance 9993.429993,\n",
|
||||
"day 3: buy 1 unit at price 782.520020, total balance 9210.909973\n",
|
||||
"day 4, sell 1 unit at price 790.510010, investment 1.021059 %, total balance 10001.419983,\n",
|
||||
"day 5: buy 1 unit at price 785.309998, total balance 9216.109985\n",
|
||||
"day 6, sell 1 unit at price 762.559998, investment -2.896945 %, total balance 9978.669983,\n",
|
||||
"day 7: buy 1 unit at price 754.020020, total balance 9224.649963\n",
|
||||
"day 8, sell 1 unit at price 736.080017, investment -2.379248 %, total balance 9960.729980,\n",
|
||||
"day 13: buy 1 unit at price 769.200012, total balance 9191.529968\n",
|
||||
"day 16, sell 1 unit at price 761.679993, investment -0.977642 %, total balance 9953.209961,\n",
|
||||
"day 19: buy 1 unit at price 758.039978, total balance 9195.169983\n",
|
||||
"day 20, sell 1 unit at price 747.919983, investment -1.335021 %, total balance 9943.089966,\n",
|
||||
"day 24: buy 1 unit at price 771.190002, total balance 9171.899964\n",
|
||||
"day 28: buy 1 unit at price 796.099976, total balance 8375.799988\n",
|
||||
"day 29: buy 1 unit at price 797.070007, total balance 7578.729981\n",
|
||||
"day 31: buy 1 unit at price 790.799988, total balance 6787.929993\n",
|
||||
"day 32, sell 1 unit at price 794.200012, investment 2.983702 %, total balance 7582.130005,\n",
|
||||
"day 33, sell 1 unit at price 796.419983, investment 0.040197 %, total balance 8378.549988,\n",
|
||||
"day 35, sell 1 unit at price 791.260010, investment -0.728919 %, total balance 9169.809998,\n",
|
||||
"day 36, sell 1 unit at price 789.909973, investment -0.112546 %, total balance 9959.719971,\n",
|
||||
"day 38: buy 1 unit at price 785.049988, total balance 9174.669983\n",
|
||||
"day 41: buy 1 unit at price 786.140015, total balance 8388.529968\n",
|
||||
"day 42, sell 1 unit at price 786.900024, investment 0.235658 %, total balance 9175.429992,\n",
|
||||
"day 43, sell 1 unit at price 794.020020, investment 1.002367 %, total balance 9969.450012,\n",
|
||||
"day 44: buy 1 unit at price 806.150024, total balance 9163.299988\n",
|
||||
"day 46, sell 1 unit at price 804.789978, investment -0.168709 %, total balance 9968.089966,\n",
|
||||
"day 47: buy 1 unit at price 807.909973, total balance 9160.179993\n",
|
||||
"day 49, sell 1 unit at price 807.880005, investment -0.003709 %, total balance 9968.059998,\n",
|
||||
"day 51: buy 1 unit at price 806.070007, total balance 9161.989991\n",
|
||||
"day 52: buy 1 unit at price 802.174988, total balance 8359.815003\n",
|
||||
"day 54: buy 1 unit at price 819.309998, total balance 7540.505005\n",
|
||||
"day 55, sell 1 unit at price 823.869995, investment 2.208243 %, total balance 8364.375000,\n",
|
||||
"day 56: buy 1 unit at price 835.669983, total balance 7528.705017\n",
|
||||
"day 57: buy 1 unit at price 832.150024, total balance 6696.554993\n",
|
||||
"day 58, sell 1 unit at price 823.309998, investment 2.634713 %, total balance 7519.864991,\n",
|
||||
"day 59, sell 1 unit at price 802.320007, investment -2.073695 %, total balance 8322.184998,\n",
|
||||
"day 61, sell 1 unit at price 795.695007, investment -4.783584 %, total balance 9117.880005,\n",
|
||||
"day 62, sell 1 unit at price 798.530029, investment -4.040136 %, total balance 9916.410034,\n",
|
||||
"day 68: buy 1 unit at price 813.669983, total balance 9102.740051\n",
|
||||
"day 69, sell 1 unit at price 819.239990, investment 0.684554 %, total balance 9921.980041,\n",
|
||||
"day 76: buy 1 unit at price 831.330017, total balance 9090.650024\n",
|
||||
"day 77: buy 1 unit at price 828.640015, total balance 8262.010009\n",
|
||||
"day 79, sell 1 unit at price 823.210022, investment -0.976747 %, total balance 9085.220031,\n",
|
||||
"day 81, sell 1 unit at price 830.630005, investment 0.240151 %, total balance 9915.850036,\n",
|
||||
"day 86: buy 1 unit at price 838.679993, total balance 9077.170043\n",
|
||||
"day 88: buy 1 unit at price 845.539978, total balance 8231.630065\n",
|
||||
"day 89, sell 1 unit at price 845.619995, investment 0.827491 %, total balance 9077.250060,\n",
|
||||
"day 91, sell 1 unit at price 848.780029, investment 0.383193 %, total balance 9926.030089,\n",
|
||||
"day 95: buy 1 unit at price 829.590027, total balance 9096.440062\n",
|
||||
"day 96, sell 1 unit at price 817.580017, investment -1.447704 %, total balance 9914.020079,\n",
|
||||
"day 97: buy 1 unit at price 814.429993, total balance 9099.590086\n",
|
||||
"day 101: buy 1 unit at price 831.500000, total balance 8268.090086\n",
|
||||
"day 102: buy 1 unit at price 829.559998, total balance 7438.530088\n",
|
||||
"day 104, sell 1 unit at price 834.570007, investment 2.472897 %, total balance 8273.100095,\n",
|
||||
"day 105, sell 1 unit at price 831.409973, investment -0.010827 %, total balance 9104.510068,\n",
|
||||
"day 106, sell 1 unit at price 827.880005, investment -0.202516 %, total balance 9932.390073,\n",
|
||||
"day 108: buy 1 unit at price 824.729980, total balance 9107.660093\n",
|
||||
"day 109, sell 1 unit at price 823.349976, investment -0.167328 %, total balance 9931.010069,\n",
|
||||
"day 114: buy 1 unit at price 838.210022, total balance 9092.800047\n",
|
||||
"day 117, sell 1 unit at price 862.760010, investment 2.928859 %, total balance 9955.560057,\n",
|
||||
"day 121: buy 1 unit at price 905.960022, total balance 9049.600035\n",
|
||||
"day 122: buy 1 unit at price 912.570007, total balance 8137.030028\n",
|
||||
"day 124: buy 1 unit at price 927.039978, total balance 7209.990050\n",
|
||||
"day 125: buy 1 unit at price 931.659973, total balance 6278.330077\n",
|
||||
"day 130, sell 1 unit at price 930.599976, investment 2.719762 %, total balance 7208.930053,\n",
|
||||
"day 131, sell 1 unit at price 932.219971, investment 2.153256 %, total balance 8141.150024,\n",
|
||||
"day 132, sell 1 unit at price 937.080017, investment 1.083021 %, total balance 9078.230041,\n",
|
||||
"day 133, sell 1 unit at price 943.000000, investment 1.217185 %, total balance 10021.230041,\n",
|
||||
"day 137: buy 1 unit at price 941.859985, total balance 9079.370056\n",
|
||||
"day 138, sell 1 unit at price 948.820007, investment 0.738966 %, total balance 10028.190063,\n",
|
||||
"day 142: buy 1 unit at price 975.880005, total balance 9052.310058\n",
|
||||
"day 143: buy 1 unit at price 964.859985, total balance 8087.450073\n",
|
||||
"day 144: buy 1 unit at price 966.950012, total balance 7120.500061\n",
|
||||
"day 145, sell 1 unit at price 975.599976, investment -0.028695 %, total balance 8096.100037,\n",
|
||||
"day 146, sell 1 unit at price 983.679993, investment 1.950543 %, total balance 9079.780030,\n",
|
||||
"day 147: buy 1 unit at price 976.570007, total balance 8103.210023\n",
|
||||
"day 148: buy 1 unit at price 980.940002, total balance 7122.270021\n",
|
||||
"day 150, sell 1 unit at price 949.830017, investment -1.770515 %, total balance 8072.100038,\n",
|
||||
"day 151: buy 1 unit at price 942.900024, total balance 7129.200014\n",
|
||||
"day 152, sell 1 unit at price 953.400024, investment -2.372588 %, total balance 8082.600038,\n",
|
||||
"day 153: buy 1 unit at price 950.760010, total balance 7131.840028\n",
|
||||
"day 154, sell 1 unit at price 942.309998, investment -3.938060 %, total balance 8074.150026,\n",
|
||||
"day 155, sell 1 unit at price 939.780029, investment -0.330894 %, total balance 9013.930055,\n",
|
||||
"day 156, sell 1 unit at price 957.369995, investment 0.695232 %, total balance 9971.300050,\n",
|
||||
"day 159: buy 1 unit at price 957.090027, total balance 9014.210023\n",
|
||||
"day 160: buy 1 unit at price 965.590027, total balance 8048.619996\n",
|
||||
"day 161, sell 1 unit at price 952.270020, investment -0.503611 %, total balance 9000.890016,\n",
|
||||
"day 162: buy 1 unit at price 927.330017, total balance 8073.559999\n",
|
||||
"day 163: buy 1 unit at price 940.489990, total balance 7133.070009\n",
|
||||
"day 165: buy 1 unit at price 908.729980, total balance 6224.340029\n",
|
||||
"day 167: buy 1 unit at price 911.710022, total balance 5312.630007\n",
|
||||
"day 169, sell 1 unit at price 918.590027, investment -4.867490 %, total balance 6231.220034,\n",
|
||||
"day 170, sell 1 unit at price 928.799988, investment 0.158516 %, total balance 7160.020022,\n",
|
||||
"day 173, sell 1 unit at price 947.159973, investment 0.709203 %, total balance 8107.179995,\n",
|
||||
"day 174: buy 1 unit at price 955.989990, total balance 7151.190005\n",
|
||||
"day 175: buy 1 unit at price 953.419983, total balance 6197.770022\n",
|
||||
"day 176: buy 1 unit at price 965.400024, total balance 5232.369998\n",
|
||||
"day 177, sell 1 unit at price 970.890015, investment 6.840320 %, total balance 6203.260013,\n",
|
||||
"day 178: buy 1 unit at price 968.150024, total balance 5235.109989\n",
|
||||
"day 179: buy 1 unit at price 972.919983, total balance 4262.190006\n",
|
||||
"day 180: buy 1 unit at price 980.340027, total balance 3281.849979\n",
|
||||
"day 181, sell 1 unit at price 950.700012, investment 4.276578 %, total balance 4232.549991,\n",
|
||||
"day 182, sell 1 unit at price 947.799988, investment -0.856704 %, total balance 5180.349979,\n",
|
||||
"day 184, sell 1 unit at price 941.530029, investment -1.247085 %, total balance 6121.880008,\n",
|
||||
"day 185, sell 1 unit at price 930.500000, investment -3.615084 %, total balance 7052.380008,\n",
|
||||
"day 186: buy 1 unit at price 930.830017, total balance 6121.549991\n",
|
||||
"day 190: buy 1 unit at price 929.359985, total balance 5192.190006\n",
|
||||
"day 191, sell 1 unit at price 926.789978, investment -4.272070 %, total balance 6118.979984,\n",
|
||||
"day 192, sell 1 unit at price 922.900024, investment -5.141220 %, total balance 7041.880008,\n",
|
||||
"day 193, sell 1 unit at price 907.239990, investment -7.456600 %, total balance 7949.119998,\n",
|
||||
"day 196: buy 1 unit at price 922.219971, total balance 7026.900027\n",
|
||||
"day 198: buy 1 unit at price 910.979980, total balance 6115.920047\n",
|
||||
"day 199, sell 1 unit at price 910.669983, investment -2.165813 %, total balance 7026.590030,\n",
|
||||
"day 200, sell 1 unit at price 906.659973, investment -2.442542 %, total balance 7933.250003,\n",
|
||||
"day 201, sell 1 unit at price 924.690002, investment 0.267835 %, total balance 8857.940005,\n",
|
||||
"day 204, sell 1 unit at price 915.890015, investment 0.538984 %, total balance 9773.830020,\n",
|
||||
"day 205: buy 1 unit at price 913.809998, total balance 8860.020022\n",
|
||||
"day 206, sell 1 unit at price 921.289978, investment 0.818549 %, total balance 9781.310000,\n",
|
||||
"day 209: buy 1 unit at price 937.340027, total balance 8843.969973\n",
|
||||
"day 210: buy 1 unit at price 928.450012, total balance 7915.519961\n",
|
||||
"day 211, sell 1 unit at price 927.809998, investment -1.016710 %, total balance 8843.329959,\n",
|
||||
"day 212: buy 1 unit at price 935.950012, total balance 7907.379947\n",
|
||||
"day 214, sell 1 unit at price 929.080017, investment 0.067856 %, total balance 8836.459964,\n",
|
||||
"day 216, sell 1 unit at price 935.090027, investment -0.091884 %, total balance 9771.549991,\n",
|
||||
"day 217: buy 1 unit at price 925.109985, total balance 8846.440006\n",
|
||||
"day 219, sell 1 unit at price 915.000000, investment -1.092841 %, total balance 9761.440006,\n",
|
||||
"day 220: buy 1 unit at price 921.809998, total balance 8839.630008\n",
|
||||
"day 221: buy 1 unit at price 931.580017, total balance 7908.049991\n",
|
||||
"day 222: buy 1 unit at price 932.450012, total balance 6975.599979\n",
|
||||
"day 226, sell 1 unit at price 944.489990, investment 2.460376 %, total balance 7920.089969,\n",
|
||||
"day 227: buy 1 unit at price 949.500000, total balance 6970.589969\n",
|
||||
"day 229, sell 1 unit at price 953.270020, investment 2.328303 %, total balance 7923.859989,\n",
|
||||
"day 230: buy 1 unit at price 957.789978, total balance 6966.070011\n",
|
||||
"day 231: buy 1 unit at price 951.679993, total balance 6014.390018\n",
|
||||
"day 232: buy 1 unit at price 969.960022, total balance 5044.429996\n",
|
||||
"day 233, sell 1 unit at price 978.890015, investment 4.980428 %, total balance 6023.320011,\n",
|
||||
"day 234: buy 1 unit at price 977.000000, total balance 5046.320011\n",
|
||||
"day 237, sell 1 unit at price 987.830017, investment 4.036863 %, total balance 6034.150028,\n",
|
||||
"day 239, sell 1 unit at price 992.000000, investment 3.571767 %, total balance 7026.150028,\n",
|
||||
"day 240, sell 1 unit at price 992.179993, investment 4.255632 %, total balance 8018.330021,\n",
|
||||
"day 243, sell 1 unit at price 988.200012, investment 1.880489 %, total balance 9006.530033,\n",
|
||||
"day 244, sell 1 unit at price 968.450012, investment -0.875127 %, total balance 9974.980045,\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"states_buy, states_sell, total_gains, invest = agent.buy(initial_money = initial_money)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||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LIapyvlArNRNyFFXzmYwVaf9ZaLwxZrrEehntjddfcw77s2Tisbq67sb6D88PGmt2M9X1txhrLd3rIsjFYgXul/d8fsa4tWEHOHKzAawfWA/xpIZNn9GXf9xvJ34AzlVKlSqmH7XM/BSvDWmK36xSt9Y5I69vnuRArm5OP1Q22Rabc11oXYn3/E7G6/QU+34wVPNyGdR02Azdh/f/dxCoFsBXYiRVsX2lv+glWpqlIKRU4v4a0x4t1rUZjBSY7gH9iZc3jEf6zGI/ZWC8HtmNlQIMz8CqlJilrhtKARt/79n39ANbYv01Y3+1d9rYT7fM+HtgVsu9J9rYrsCbOedFuZ7rd7nrPIcQ/sGZmDcyQ+1use34FVn2/SCVThBCiXobf39TeHkIIIeys3uNa6/CugqKNKKXuAIq1XUZDCCGE6Mwk8BNCiEawx0wdjZX16441pm+x1vq6Nm2YEEIIIUQE0tVTCCEaxwDuweou9h2wEmtCBiGEEEKIdkcyfkIIIYQQQgjRwUnGTwghhBBCCCE6uP2xnEMiVr2nQqxpzYUQQgghhBCiM3FgzYT9LVZpqnrtj4HfBODztm6EEEIIIYQQQrSxScAX8ay4PwZ+hQClpZX4fO1rfGJWVholJRX1ryhEA8m9JVqS3F+ipci9JVqS3F+iJbX3+8s0DTILine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,5))\n",
|
||||
"plt.plot(close, color='r', lw=2.)\n",
|
||||
"plt.plot(close, '^', markersize=10, color='m', label = 'buying signal', markevery = states_buy)\n",
|
||||
"plt.plot(close, 'v', markersize=10, color='k', label = 'selling signal', markevery = states_sell)\n",
|
||||
"plt.title('total gains %f, total investment %f%%'%(total_gains, invest))\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,940 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlgAAAE3CAYAAAB2LD/OAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAIABJREFUeJzt3Xuc3HV97/HXzOxsNtlcCUsSMCCKfqUcwQhBboKHSz0ohVovYCqIFrxUPdTTINbLKbW1LVrk2Hq0FqQoURHpwwsQqxjKEaQFuQgq+gURaIBAYhrIZpO9z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/LIaZ+fpNbkKUJJkqTEDFiSJEmJGbAkSZISM2BJkiQlZsCSJElKzIAlSZKUmAFLkiQpMQOWJElSYgYsSZKkxAxYkiRJiRmwJEmSEjNgSZIkJWbAkiRJSsyAJUmSlJgBS5IkKbG2qSwcQngb8Gflh9+LMa6d+pQkSZIa26SPYIUQ5gF/D5wAHAa8OoRwcqqJSZIkNaqpnCLMlZfvBPLlf7tSTEqSJKmRTfoUYYyxO4TwceBXlILVLcDttS6/dOn8yQ5ds66uBXUfYzZr5fpbuXaYPfUP9vcwNNRLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 720x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plt.figure(figsize = (10, 5))\n",
|
||||
"bins = np.linspace(-10, 10, 100)\n",
|
||||
"\n",
|
||||
"solution = np.random.randn(100)\n",
|
||||
"w = np.random.randn(100)\n",
|
||||
"\n",
|
||||
"plt.hist(solution, bins, alpha = 0.5, label = 'solution', color = 'r')\n",
|
||||
"plt.hist(w, bins, alpha = 0.5, label = 'random', color = 'y')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"iter 1000. w: 0.0952791586701015, solution: 0.5720518054873052, reward: -20.148099\n",
|
||||
"iter 2000. w: 0.5750455468679501, solution: 0.5720518054873052, reward: -0.008058\n",
|
||||
"iter 3000. w: 0.5751585748688035, solution: 0.5720518054873052, reward: -0.008793\n",
|
||||
"iter 4000. w: 0.5665604300033952, solution: 0.5720518054873052, reward: -0.007711\n",
|
||||
"iter 5000. w: 0.5619489293298067, solution: 0.5720518054873052, reward: -0.005604\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def f(w):\n",
|
||||
" return -np.sum(np.square(solution - w))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"npop = 50\n",
|
||||
"sigma = 0.1\n",
|
||||
"alpha = 0.001\n",
|
||||
"\n",
|
||||
"for i in range(5000):\n",
|
||||
"\n",
|
||||
" if (i + 1) % 1000 == 0:\n",
|
||||
" print(\n",
|
||||
" 'iter %d. w: %s, solution: %s, reward: %f'\n",
|
||||
" % (i + 1, str(w[-1]), str(solution[-1]), f(w))\n",
|
||||
" )\n",
|
||||
" N = np.random.randn(npop, 100)\n",
|
||||
" R = np.zeros(npop)\n",
|
||||
" for j in range(npop):\n",
|
||||
" w_try = w + sigma * N[j]\n",
|
||||
" R[j] = f(w_try)\n",
|
||||
"\n",
|
||||
" A = (R - np.mean(R)) / np.std(R)\n",
|
||||
" w = w + alpha / (npop * sigma) * np.dot(N.T, A)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlgAAAE3CAYAAAB2LD/OAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAIABJREFUeJzt3XmcXFWd9/FPLd2dpBNClnYSHTDgcjAsyr4YwTExAwjqjCgPOiKyKQIjImpmlBlGH9TRGZlHUZZIREVBBxBBIoJBEFRUICAKnFEhwEiUGANJOumtqp4/qhJSvaQr1afX+rxfL150/+65955f3erOt++9VZUplUpIkiQpnexoT0CSJGmiMWBJkiQlZsCSJElKzIAlSZK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ol2tvXk883J9umZ7AkSRpjZsxoY926NWzc+OygY7PZ7HbfA2qiS9V/Pt/MjBltCWZU2V6yLUmSpCRyuTyzZ8+taexEe4uKHTVW+/cSoSRJUmIGLEmSpMQMWJILine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 720x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"'''\n",
|
||||
"I want to compare my first two individuals with my real w\n",
|
||||
"'''\n",
|
||||
"plt.figure(figsize=(10,5))\n",
|
||||
"\n",
|
||||
"sigma = 0.1\n",
|
||||
"N = np.random.randn(npop, 100)\n",
|
||||
"individuals = []\n",
|
||||
"for j in range(2):\n",
|
||||
" individuals.append(w + sigma * N[j])\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"plt.hist(w, bins, alpha=0.5, label='w',color='r')\n",
|
||||
"plt.hist(individuals[0], bins, alpha=0.5, label='individual 1')\n",
|
||||
"plt.hist(individuals[1], bins, alpha=0.5, label='individual 2')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2017-10-16</td>\n",
|
||||
" <td>992.099976</td>\n",
|
||||
" <td>993.906982</td>\n",
|
||||
" <td>984.000000</td>\n",
|
||||
" <td>992.000000</td>\n",
|
||||
" <td>992.000000</td>\n",
|
||||
" <td>910500</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2017-10-17</td>\n",
|
||||
" <td>990.289978</td>\n",
|
||||
" <td>996.440002</td>\n",
|
||||
" <td>988.590027</td>\n",
|
||||
" <td>992.179993</td>\n",
|
||||
" <td>992.179993</td>\n",
|
||||
" <td>1290200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2017-10-18</td>\n",
|
||||
" <td>991.770020</td>\n",
|
||||
" <td>996.719971</td>\n",
|
||||
" <td>986.974976</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>992.809998</td>\n",
|
||||
" <td>1057600</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2017-10-19</td>\n",
|
||||
" <td>986.000000</td>\n",
|
||||
" <td>988.880005</td>\n",
|
||||
" <td>978.390015</td>\n",
|
||||
" <td>984.450012</td>\n",
|
||||
" <td>984.450012</td>\n",
|
||||
" <td>1313600</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2017-10-20</td>\n",
|
||||
" <td>989.440002</td>\n",
|
||||
" <td>991.000000</td>\n",
|
||||
" <td>984.580017</td>\n",
|
||||
" <td>988.200012</td>\n",
|
||||
" <td>988.200012</td>\n",
|
||||
" <td>1183200</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2017-10-16 992.099976 993.906982 984.000000 992.000000 992.000000 \n",
|
||||
"1 2017-10-17 990.289978 996.440002 988.590027 992.179993 992.179993 \n",
|
||||
"2 2017-10-18 991.770020 996.719971 986.974976 992.809998 992.809998 \n",
|
||||
"3 2017-10-19 986.000000 988.880005 978.390015 984.450012 984.450012 \n",
|
||||
"4 2017-10-20 989.440002 991.000000 984.580017 988.200012 988.200012 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 910500 \n",
|
||||
"1 1290200 \n",
|
||||
"2 1057600 \n",
|
||||
"3 1313600 \n",
|
||||
"4 1183200 "
|
||||
]
|
||||
},
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"google = pd.read_csv('/Users/huseinzolkepli/Desktop/GOOG.csv')\n",
|
||||
"google.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_state(data, t, n):\n",
|
||||
" d = t - n + 1\n",
|
||||
" block = data[d : t + 1] if d >= 0 else -d * [data[0]] + data[: t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(n - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 60,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"close = google.Close.values.tolist()\n",
|
||||
"get_state(close, 0, 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0. , 0. , 0. , 0. , 0. , 0. ,\n",
|
||||
" 0. , 0. , 0.179993]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 61,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"get_state(close, 1, 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 62,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0. , 0. , 0. , 0. , 0. , 0. ,\n",
|
||||
" 0. , 0.179993, 0.630005]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 62,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"get_state(close, 2, 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 63,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Deep_Evolution_Strategy:\n",
|
||||
" def __init__(\n",
|
||||
" self, weights, reward_function, population_size, sigma, learning_rate\n",
|
||||
" ):\n",
|
||||
" self.weights = weights\n",
|
||||
" self.reward_function = reward_function\n",
|
||||
" self.population_size = population_size\n",
|
||||
" self.sigma = sigma\n",
|
||||
" self.learning_rate = learning_rate\n",
|
||||
"\n",
|
||||
" def _get_weight_from_population(self, weights, population):\n",
|
||||
" weights_population = []\n",
|
||||
" for index, i in enumerate(population):\n",
|
||||
" jittered = self.sigma * i\n",
|
||||
" weights_population.append(weights[index] + jittered)\n",
|
||||
" return weights_population\n",
|
||||
"\n",
|
||||
" def get_weights(self):\n",
|
||||
" return self.weights\n",
|
||||
"\n",
|
||||
" def train(self, epoch = 100, print_every = 1):\n",
|
||||
" lasttime = time.time()\n",
|
||||
" for i in range(epoch):\n",
|
||||
" population = []\n",
|
||||
" rewards = np.zeros(self.population_size)\n",
|
||||
" for k in range(self.population_size):\n",
|
||||
" x = []\n",
|
||||
" for w in self.weights:\n",
|
||||
" x.append(np.random.randn(*w.shape))\n",
|
||||
" population.append(x)\n",
|
||||
" for k in range(self.population_size):\n",
|
||||
" weights_population = self._get_weight_from_population(\n",
|
||||
" self.weights, population[k]\n",
|
||||
" )\n",
|
||||
" rewards[k] = self.reward_function(weights_population)\n",
|
||||
" rewards = (rewards - np.mean(rewards)) / np.std(rewards)\n",
|
||||
" for index, w in enumerate(self.weights):\n",
|
||||
" A = np.array([p[index] for p in population])\n",
|
||||
" self.weights[index] = (\n",
|
||||
" w\n",
|
||||
" + self.learning_rate\n",
|
||||
" / (self.population_size * self.sigma)\n",
|
||||
" * np.dot(A.T, rewards).T\n",
|
||||
" )\n",
|
||||
" if (i + 1) % print_every == 0:\n",
|
||||
" print(\n",
|
||||
" 'iter %d. reward: %f'\n",
|
||||
" % (i + 1, self.reward_function(self.weights))\n",
|
||||
" )\n",
|
||||
" print('time taken to train:', time.time() - lasttime, 'seconds')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 64,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, layer_size, output_size):\n",
|
||||
" self.weights = [\n",
|
||||
" np.random.randn(input_size, layer_size),\n",
|
||||
" np.random.randn(layer_size, output_size),\n",
|
||||
" np.random.randn(layer_size, 1),\n",
|
||||
" np.random.randn(1, layer_size),\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" def predict(self, inputs):\n",
|
||||
" feed = np.dot(inputs, self.weights[0]) + self.weights[-1]\n",
|
||||
" decision = np.dot(feed, self.weights[1])\n",
|
||||
" buy = np.dot(feed, self.weights[2])\n",
|
||||
" return decision, buy\n",
|
||||
"\n",
|
||||
" def get_weights(self):\n",
|
||||
" return self.weights\n",
|
||||
"\n",
|
||||
" def set_weights(self, weights):\n",
|
||||
" self.weights = weights"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 65,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"window_size = 30\n",
|
||||
"model = Model(window_size, 500, 3)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 67,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"-89.2658852200001"
|
||||
]
|
||||
},
|
||||
"execution_count": 67,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"initial_money = 10000\n",
|
||||
"starting_money = initial_money\n",
|
||||
"len_close = len(close) - 1\n",
|
||||
"weight = model\n",
|
||||
"skip = 1\n",
|
||||
"\n",
|
||||
"state = get_state(close, 0, window_size + 1)\n",
|
||||
"inventory = []\n",
|
||||
"quantity = 0\n",
|
||||
"\n",
|
||||
"max_buy = 5\n",
|
||||
"max_sell = 5\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def act(model, sequence):\n",
|
||||
" decision, buy = model.predict(np.array(sequence))\n",
|
||||
" return np.argmax(decision[0]), int(buy[0])\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"for t in range(0, len_close, skip):\n",
|
||||
" action, buy = act(weight, state)\n",
|
||||
" next_state = get_state(close, t + 1, window_size + 1)\n",
|
||||
" if action == 1 and initial_money >= close[t]:\n",
|
||||
" if buy < 0:\n",
|
||||
" buy = 1\n",
|
||||
" if buy > max_buy:\n",
|
||||
" buy_units = max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = buy\n",
|
||||
" total_buy = buy_units * close[t]\n",
|
||||
" initial_money -= total_buy\n",
|
||||
" inventory.append(total_buy)\n",
|
||||
" quantity += buy_units\n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" if quantity > max_sell:\n",
|
||||
" sell_units = max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = quantity\n",
|
||||
" quantity -= sell_units\n",
|
||||
" total_sell = sell_units * close[t]\n",
|
||||
" initial_money += total_sell\n",
|
||||
"\n",
|
||||
" state = next_state\n",
|
||||
"((initial_money - starting_money) / starting_money) * 100"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 77,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import time\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" POPULATION_SIZE = 15\n",
|
||||
" SIGMA = 0.1\n",
|
||||
" LEARNING_RATE = 0.03\n",
|
||||
"\n",
|
||||
" def __init__(\n",
|
||||
" self, model, money, max_buy, max_sell, close, window_size, skip\n",
|
||||
" ):\n",
|
||||
" self.window_size = window_size\n",
|
||||
" self.skip = skip\n",
|
||||
" self.close = close\n",
|
||||
" self.model = model\n",
|
||||
" self.initial_money = money\n",
|
||||
" self.max_buy = max_buy\n",
|
||||
" self.max_sell = max_sell\n",
|
||||
" self.es = Deep_Evolution_Strategy(\n",
|
||||
" self.model.get_weights(),\n",
|
||||
" self.get_reward,\n",
|
||||
" self.POPULATION_SIZE,\n",
|
||||
" self.SIGMA,\n",
|
||||
" self.LEARNING_RATE,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" def act(self, sequence):\n",
|
||||
" decision, buy = self.model.predict(np.array(sequence))\n",
|
||||
" return np.argmax(decision[0]), int(buy[0])\n",
|
||||
"\n",
|
||||
" def get_reward(self, weights):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" len_close = len(self.close) - 1\n",
|
||||
"\n",
|
||||
" self.model.weights = weights\n",
|
||||
" state = get_state(self.close, 0, self.window_size + 1)\n",
|
||||
" inventory = []\n",
|
||||
" quantity = 0\n",
|
||||
" for t in range(0, len_close, self.skip):\n",
|
||||
" action, buy = self.act(state)\n",
|
||||
" next_state = get_state(self.close, t + 1, self.window_size + 1)\n",
|
||||
" if action == 1 and initial_money >= self.close[t]:\n",
|
||||
" if buy < 0:\n",
|
||||
" buy = 1\n",
|
||||
" if buy > self.max_buy:\n",
|
||||
" buy_units = self.max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = buy\n",
|
||||
" total_buy = buy_units * self.close[t]\n",
|
||||
" initial_money -= total_buy\n",
|
||||
" inventory.append(total_buy)\n",
|
||||
" quantity += buy_units\n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" if quantity > self.max_sell:\n",
|
||||
" sell_units = self.max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = quantity\n",
|
||||
" quantity -= sell_units\n",
|
||||
" total_sell = sell_units * self.close[t]\n",
|
||||
" initial_money += total_sell\n",
|
||||
"\n",
|
||||
" state = next_state\n",
|
||||
" return ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
"\n",
|
||||
" def fit(self, iterations, checkpoint):\n",
|
||||
" self.es.train(iterations, print_every = checkpoint)\n",
|
||||
"\n",
|
||||
" def buy(self):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" len_close = len(self.close) - 1\n",
|
||||
" state = get_state(self.close, 0, self.window_size + 1)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" quantity = 0\n",
|
||||
" for t in range(0, len_close, self.skip):\n",
|
||||
" action, buy = self.act(state)\n",
|
||||
" next_state = get_state(self.close, t + 1, self.window_size + 1)\n",
|
||||
" if action == 1 and initial_money >= self.close[t]:\n",
|
||||
" if buy < 0:\n",
|
||||
" buy = 1\n",
|
||||
" if buy > self.max_buy:\n",
|
||||
" buy_units = self.max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = buy\n",
|
||||
" total_buy = buy_units * self.close[t]\n",
|
||||
" initial_money -= total_buy\n",
|
||||
" inventory.append(total_buy)\n",
|
||||
" quantity += buy_units\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print(\n",
|
||||
" 'day %d: buy %d units at price %f, total balance %f'\n",
|
||||
" % (t, buy_units, total_buy, initial_money)\n",
|
||||
" )\n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" if quantity > self.max_sell:\n",
|
||||
" sell_units = self.max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = quantity\n",
|
||||
" if sell_units < 1:\n",
|
||||
" continue\n",
|
||||
" quantity -= sell_units\n",
|
||||
" total_sell = sell_units * self.close[t]\n",
|
||||
" initial_money += total_sell\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((total_sell - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, sell_units, total_sell, invest, initial_money)\n",
|
||||
" )\n",
|
||||
" state = next_state\n",
|
||||
"\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" print(\n",
|
||||
" '\\ntotal gained %f, total investment %f %%'\n",
|
||||
" % (initial_money - starting_money, invest)\n",
|
||||
" )\n",
|
||||
" plt.figure(figsize = (20, 10))\n",
|
||||
" plt.plot(close, label = 'true close', c = 'g')\n",
|
||||
" plt.plot(\n",
|
||||
" close, 'X', label = 'predict buy', markevery = states_buy, c = 'b'\n",
|
||||
" )\n",
|
||||
" plt.plot(\n",
|
||||
" close, 'o', label = 'predict sell', markevery = states_sell, c = 'r'\n",
|
||||
" )\n",
|
||||
" plt.legend()\n",
|
||||
" plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 78,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = Model(input_size = window_size, layer_size = 500, output_size = 3)\n",
|
||||
"agent = Agent(\n",
|
||||
" model = model,\n",
|
||||
" money = 10000,\n",
|
||||
" max_buy = 5,\n",
|
||||
" max_sell = 5,\n",
|
||||
" close = close,\n",
|
||||
" window_size = window_size,\n",
|
||||
" skip = 1,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 79,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"iter 10. reward: 36.181611\n",
|
||||
"iter 20. reward: 50.767101\n",
|
||||
"iter 30. reward: 65.467698\n",
|
||||
"iter 40. reward: 71.316103\n",
|
||||
"iter 50. reward: 82.881994\n",
|
||||
"iter 60. reward: 84.293704\n",
|
||||
"iter 70. reward: 78.501997\n",
|
||||
"iter 80. reward: 94.488579\n",
|
||||
"iter 90. reward: 86.526799\n",
|
||||
"iter 100. reward: 85.882890\n",
|
||||
"iter 110. reward: 86.063284\n",
|
||||
"iter 120. reward: 90.334301\n",
|
||||
"iter 130. reward: 85.850098\n",
|
||||
"iter 140. reward: 91.399606\n",
|
||||
"iter 150. reward: 87.862805\n",
|
||||
"iter 160. reward: 97.226486\n",
|
||||
"iter 170. reward: 86.767297\n",
|
||||
"iter 180. reward: 97.016782\n",
|
||||
"iter 190. reward: 97.843791\n",
|
||||
"iter 200. reward: 89.146606\n",
|
||||
"iter 210. reward: 96.508885\n",
|
||||
"iter 220. reward: 97.765979\n",
|
||||
"iter 230. reward: 98.256375\n",
|
||||
"iter 240. reward: 99.942482\n",
|
||||
"iter 250. reward: 94.536183\n",
|
||||
"iter 260. reward: 96.916185\n",
|
||||
"iter 270. reward: 93.193185\n",
|
||||
"iter 280. reward: 100.844085\n",
|
||||
"iter 290. reward: 100.994682\n",
|
||||
"iter 300. reward: 101.523774\n",
|
||||
"iter 310. reward: 102.090896\n",
|
||||
"iter 320. reward: 102.176091\n",
|
||||
"iter 330. reward: 92.306981\n",
|
||||
"iter 340. reward: 105.409190\n",
|
||||
"iter 350. reward: 103.159886\n",
|
||||
"iter 360. reward: 99.091287\n",
|
||||
"iter 370. reward: 108.475085\n",
|
||||
"iter 380. reward: 102.349682\n",
|
||||
"iter 390. reward: 110.289382\n",
|
||||
"iter 400. reward: 103.371389\n",
|
||||
"iter 410. reward: 110.951287\n",
|
||||
"iter 420. reward: 111.561078\n",
|
||||
"iter 430. reward: 112.275285\n",
|
||||
"iter 440. reward: 113.112587\n",
|
||||
"iter 450. reward: 110.838887\n",
|
||||
"iter 460. reward: 111.243782\n",
|
||||
"iter 470. reward: 112.924874\n",
|
||||
"iter 480. reward: 111.705677\n",
|
||||
"iter 490. reward: 110.903074\n",
|
||||
"iter 500. reward: 112.986871\n",
|
||||
"time taken to train: 60.56475520133972 seconds\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"agent.fit(iterations = 500, checkpoint = 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 80,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 0: buy 1 units at price 992.000000, total balance 9008.000000\n",
|
||||
"day 1: buy 1 units at price 992.179993, total balance 8015.820007\n",
|
||||
"day 2: buy 1 units at price 992.809998, total balance 7023.010009\n",
|
||||
"day 3: buy 5 units at price 4922.250060, total balance 2100.759949\n",
|
||||
"day 4, sell 5 units at price 4941.000060, investment 398.084683 %, total balance 7041.760009,\n",
|
||||
"day 5: buy 5 units at price 4842.250060, total balance 2199.509949\n",
|
||||
"day 7: buy 5 units at price 4866.650085, total balance -2667.140136\n",
|
||||
"day 9, sell 5 units at price 5096.350100, investment 413.651770 %, total balance 2429.209964,\n",
|
||||
"day 10: buy 5 units at price 5085.549925, total balance -2656.339961\n",
|
||||
"day 12, sell 5 units at price 5127.500000, investment 416.463373 %, total balance 2471.160039,\n",
|
||||
"day 13, sell 5 units at price 5127.899780, investment 4.177962 %, total balance 7599.059819,\n",
|
||||
"day 14, sell 3 units at price 3097.439940, investment -36.033045 %, total balance 10696.499759,\n",
|
||||
"day 22: buy 1 units at price 1020.909973, total balance 9675.589786\n",
|
||||
"day 24: buy 1 units at price 1019.090027, total balance 8656.499759\n",
|
||||
"day 25: buy 5 units at price 5091.900025, total balance 3564.599734\n",
|
||||
"day 27: buy 5 units at price 5179.799805, total balance -1615.200071\n",
|
||||
"day 29, sell 5 units at price 5271.049805, investment 416.308974 %, total balance 3655.849734,\n",
|
||||
"day 30, sell 5 units at price 5237.050170, investment 413.894752 %, total balance 8892.899904,\n",
|
||||
"day 33: buy 5 units at price 5050.849915, total balance 3842.049989\n",
|
||||
"day 35: buy 5 units at price 5025.750120, total balance -1183.700131\n",
|
||||
"day 42, sell 5 units at price 5245.750120, investment 3.021467 %, total balance 4062.049989,\n",
|
||||
"day 43, sell 5 units at price 5320.949705, investment 2.725007 %, total balance 9382.999694,\n",
|
||||
"day 44, sell 2 units at price 2154.280030, investment -57.348168 %, total balance 11537.279724,\n",
|
||||
"day 45: buy 1 units at price 1070.680054, total balance 10466.599670\n",
|
||||
"day 48: buy 1 units at price 1060.119995, total balance 9406.479675\n",
|
||||
"day 51: buy 5 units at price 5240.700075, total balance 4165.779600\n",
|
||||
"day 52: buy 5 units at price 5232.000120, total balance -1066.220520\n",
|
||||
"day 56, sell 5 units at price 5511.149900, investment 9.658255 %, total balance 4444.929380,\n",
|
||||
"day 57, sell 5 units at price 5534.699705, investment 416.933110 %, total balance 9979.629085,\n",
|
||||
"day 58, sell 2 units at price 2212.520020, investment 108.704678 %, total balance 12192.149105,\n",
|
||||
"day 59: buy 5 units at price 5513.049925, total balance 6679.099180\n",
|
||||
"day 60: buy 5 units at price 5527.600100, total balance 1151.499080\n",
|
||||
"day 62: buy 5 units at price 5608.800050, total balance -4457.300970\n",
|
||||
"day 69, sell 5 units at price 5851.849975, investment 11.661608 %, total balance 1394.549005,\n",
|
||||
"day 70, sell 5 units at price 5879.199830, investment 12.370025 %, total balance 7273.748835,\n",
|
||||
"day 71, sell 5 units at price 5877.899780, investment 6.617931 %, total balance 13151.648615,\n",
|
||||
"day 72: buy 5 units at price 5818.449705, total balance 7333.198910\n",
|
||||
"day 73, sell 5 units at price 5849.699705, investment 5.827115 %, total balance 13182.898615,\n",
|
||||
"day 78: buy 5 units at price 5242.899780, total balance 7939.998835\n",
|
||||
"day 79: buy 5 units at price 5007.600100, total balance 2932.398735\n",
|
||||
"day 80: buy 5 units at price 5188.900145, total balance -2256.501410\n",
|
||||
"day 87, sell 5 units at price 5556.699830, investment -0.928901 %, total balance 3300.198420,\n",
|
||||
"day 89: buy 1 units at price 1126.790039, total balance 2173.408381\n",
|
||||
"day 90, sell 5 units at price 5718.750000, investment -1.713510 %, total balance 7892.158381,\n",
|
||||
"day 93: buy 5 units at price 5347.600100, total balance 2544.558281\n",
|
||||
"day 96: buy 5 units at price 5475.300295, total balance -2930.742014\n",
|
||||
"day 98, sell 5 units at price 5630.000000, investment 7.383323 %, total balance 2699.257986,\n",
|
||||
"day 99, sell 5 units at price 5800.200195, investment 15.827943 %, total balance 8499.458181,\n",
|
||||
"day 100, sell 5 units at price 5822.500000, investment 12.210677 %, total balance 14321.958181,\n",
|
||||
"day 101: buy 1 units at price 1138.170044, total balance 13183.788137\n",
|
||||
"day 102, sell 2 units at price 2298.979980, investment 104.029136 %, total balance 15482.768117,\n",
|
||||
"day 111: buy 5 units at price 5025.499880, total balance 10457.268237\n",
|
||||
"day 113: buy 5 units at price 5158.950195, total balance 5298.318042\n",
|
||||
"day 114: buy 5 units at price 5032.349855, total balance 265.968187\n",
|
||||
"day 116, sell 5 units at price 5125.700075, investment 1.993835 %, total balance 5391.668262,\n",
|
||||
"day 118: buy 1 units at price 1007.039978, total balance 4384.628284\n",
|
||||
"day 119: buy 5 units at price 5077.250060, total balance -692.621776\n",
|
||||
"day 126, sell 5 units at price 5360.399780, investment 3.904856 %, total balance 4667.778004,\n",
|
||||
"day 128, sell 5 units at price 5364.799805, investment 6.606257 %, total balance 10032.577809,\n",
|
||||
"day 129, sell 5 units at price 5337.249755, investment 429.993831 %, total balance 15369.827564,\n",
|
||||
"day 131, sell 1 units at price 1021.179993, investment -79.887144 %, total balance 16391.007557,\n",
|
||||
"day 132: buy 1 units at price 1040.040039, total balance 15350.967518\n",
|
||||
"day 135, sell 1 units at price 1037.310059, investment -0.262488 %, total balance 16388.277577,\n",
|
||||
"day 136: buy 5 units at price 5121.900025, total balance 11266.377552\n",
|
||||
"day 137: buy 1 units at price 1023.719971, total balance 10242.657581\n",
|
||||
"day 138: buy 5 units at price 5241.049805, total balance 5001.607776\n",
|
||||
"day 139: buy 5 units at price 5273.950195, total balance -272.342419\n",
|
||||
"day 141, sell 5 units at price 5413.800050, investment 5.699057 %, total balance 5141.457631,\n",
|
||||
"day 142, sell 5 units at price 5487.849730, investment 436.069422 %, total balance 10629.307361,\n",
|
||||
"day 144: buy 1 units at price 1100.199951, total balance 9529.107410\n",
|
||||
"day 147: buy 1 units at price 1078.589966, total balance 8450.517444\n",
|
||||
"day 148: buy 5 units at price 5331.799925, total balance 3118.717519\n",
|
||||
"day 150: buy 5 units at price 5348.649900, total balance -2229.932381\n",
|
||||
"day 159, sell 5 units at price 5698.300170, investment 8.724404 %, total balance 3468.367789,\n",
|
||||
"day 161, sell 5 units at price 5619.299925, investment 6.548218 %, total balance 9087.667714,\n",
|
||||
"day 162: buy 5 units at price 5604.349975, total balance 3483.317739\n",
|
||||
"day 168: buy 1 units at price 1173.459961, total balance 2309.857778\n",
|
||||
"day 170, sell 5 units at price 5849.199830, investment 431.648799 %, total balance 8159.057608,\n",
|
||||
"day 171, sell 5 units at price 5788.300170, investment 436.654368 %, total balance 13947.357778,\n",
|
||||
"day 172, sell 4 units at price 4621.919920, investment -13.314078 %, total balance 18569.277698,\n",
|
||||
"day 173: buy 5 units at price 5624.050295, total balance 12945.227403\n",
|
||||
"day 175: buy 5 units at price 5519.899900, total balance 7425.327503\n",
|
||||
"day 176: buy 5 units at price 5571.099855, total balance 1854.227648\n",
|
||||
"day 179: buy 5 units at price 5514.450075, total balance -3660.222427\n",
|
||||
"day 184, sell 5 units at price 5769.500120, investment 7.868345 %, total balance 2109.277693,\n",
|
||||
"day 187: buy 5 units at price 5919.299925, total balance -3810.022232\n",
|
||||
"day 194, sell 5 units at price 6318.499755, investment 12.742776 %, total balance 2508.477523,\n",
|
||||
"day 195, sell 5 units at price 6341.649780, investment 440.423192 %, total balance 8850.127303,\n",
|
||||
"day 196, sell 5 units at price 6192.500000, investment 10.107479 %, total balance 15042.627303,\n",
|
||||
"day 197, sell 5 units at price 6098.699950, investment 10.485698 %, total balance 21141.327253,\n",
|
||||
"day 204: buy 5 units at price 6228.049925, total balance 14913.277328\n",
|
||||
"day 205: buy 5 units at price 6245.499880, total balance 8667.777448\n",
|
||||
"day 206: buy 5 units at price 6188.049925, total balance 2479.727523\n",
|
||||
"day 207, sell 5 units at price 6175.050050, investment -0.850987 %, total balance 8654.777573,\n",
|
||||
"day 208, sell 5 units at price 6210.499880, investment -0.560404 %, total balance 14865.277453,\n",
|
||||
"day 209: buy 5 units at price 6071.900025, total balance 8793.377428\n",
|
||||
"day 210: buy 5 units at price 6032.449950, total balance 2760.927478\n",
|
||||
"day 211: buy 5 units at price 6004.799805, total balance -3243.872327\n",
|
||||
"day 219, sell 5 units at price 6246.500245, investment 0.944568 %, total balance 3002.627918,\n",
|
||||
"day 220, sell 5 units at price 6195.599975, investment 2.037253 %, total balance 9198.227893,\n",
|
||||
"day 221, sell 5 units at price 6090.949705, investment 0.969751 %, total balance 15289.177598,\n",
|
||||
"day 227: buy 1 units at price 1177.359985, total balance 14111.817613\n",
|
||||
"day 229: buy 5 units at price 5876.649780, total balance 8235.167833\n",
|
||||
"day 230: buy 5 units at price 5862.650145, total balance 2372.517688\n",
|
||||
"day 231: buy 1 units at price 1156.050049, total balance 1216.467639\n",
|
||||
"day 232: buy 1 units at price 1161.219971, total balance 55.247668\n",
|
||||
"day 233, sell 5 units at price 5855.449830, investment -2.487177 %, total balance 5910.697498,\n",
|
||||
"day 234, sell 5 units at price 5934.349975, investment 404.038701 %, total balance 11845.047473,\n",
|
||||
"day 235: buy 5 units at price 5830.449830, total balance 6014.597643\n",
|
||||
"day 238: buy 1 units at price 1180.489990, total balance 4834.107653\n",
|
||||
"day 242, sell 5 units at price 6000.549925, investment 2.108347 %, total balance 10834.657578,\n",
|
||||
"day 243, sell 5 units at price 6014.749755, investment 2.594383 %, total balance 16849.407333,\n",
|
||||
"day 245, sell 4 units at price 4629.399904, investment 300.449782 %, total balance 21478.807237,\n",
|
||||
"\n",
|
||||
"total gained 11478.807237, total investment 114.788072 %\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAABJIAAAJHCAYAAADLzny5AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvqOYd8AAAIABJREFUeJzs3Xl8lOW9///XPVu2yTZJSEDZlXGrgqIgorSenkNrW49Cj1IrirYu9bjUYu2v59Sebnaxek61HttTN1zK97QKerBaW9tqVRAVRdxgEIGAZJ2ZJJNkMpnM3Pfvj0kCkUAmyyxJ3s/Hg0eTa+655pqQGx/z7uf6XIZlWYiIiIiIiIiIiAzElukFiIiIiIiIiIjI6KAgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkqIgSUREREREREREkuLI9AKSkAOcCtQC8QyvRURERERERERkLLADE4HXgc5knzQagqRTgZcyvQgRERERERERkTHoTODlZC8eDUFSLUBTUzumaWV6LcNWVuYmEGjL9DJEsp7uFZHk6F4RSY7uFZHk6F4RGdhYuU9sNoPS0gLozl2SNRqCpDiAaVpjIkgCxsz7EEk13SsiydG9IpIc3SsiydG9IjKwMXafDKqNkJpti4iIiIiIiIhIUhQkiYiIiIiIiIhIUkbD1jYRERERERERGSPi8RhNTY3EYtFML2VIGhpsmKaZ6WUkzWazk5fnxu0uxjCMYc+nIElERERERERE0qapqZHc3HwKCqpGJNhIN4fDRiw2OoIky7KIx2O0tjbT1NSIxzNh2HNqa5uIiIiIiIiIpE0sFqWgoGhUhkijjWEYOBxOSkrKiEYjIzKngiQRERERERERSSuFSOllGDZgZE6aU5AkIiIiIiIiIiJJUZAkIiIiIiIiIuNSW1sb3/72yrS93q23fo9nnnkqba+XCgqSRERERERERGRcam0N8cEH2zO9jFFFp7aJiIiIiIiIyLj0i1/8HL+/kW9/+yauv/4brFx5HcXFJbhcOSxe/Fk2b36Df//37wFw7bVXc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mmaPPnkGvbs2d3bxLs/H/+522wGZWVugOnA7mRfTxVJIiIiIiIiWapo/gLCXRE+euxRCsMmTk8Z5UuWUjR/Ae7tjapIEhH+/d+/SX19HXfccXdaXk9BkoiIiIiISBYLnTCVB7vKuXb2V5nhmdU7Xuh0E4lH6DJjOG36aCcyFCefPLd3i9po9ZOf3JHW11OzbRERERERkSxW194AQFX+hD7jblcBAO1quC0iaaQgSUREREREJIvVhxtw2V2U5BT3GS90JoKkVm1vE5E0UpAkIiIiIiKSxeraG6jKr8AwjD7jbpcbgLautkwsS0TGKQVJIiIiIiIiWaw+3Ejlx7a1Abi7K5LUcFtE0klBkoiIiIiISJaKxDpp6mymqqCfIKm7R1KrKpJEssatt36PZ555Cr+/kZtuuv6w11533VXDfr377/8f7r//fwBYuDA9TcPV2l9ERERERCRLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1440x720 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"agent.buy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"inputHidden": false,
|
||||
"outputHidden": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernel_info": {
|
||||
"name": "python3"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
},
|
||||
"nteract": {
|
||||
"version": "0.11.9"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-05-23,12.930000,13.180000,12.900000,13.100000,13.100000,44388300
|
||||
2018-05-24,13.060000,13.430000,13.030000,13.410000,13.410000,47785700
|
||||
2018-05-25,13.400000,13.720000,13.360000,13.540000,13.540000,43850100
|
||||
2018-05-29,13.450000,13.630000,13.260000,13.360000,13.360000,39578500
|
||||
2018-05-30,13.480000,13.950000,13.480000,13.820000,13.820000,58186400
|
||||
2018-05-31,13.740000,13.930000,13.690000,13.730000,13.730000,46797700
|
||||
2018-06-01,13.980000,14.400000,13.920000,14.400000,14.400000,71677900
|
||||
2018-06-04,14.760000,14.980000,14.520000,14.850000,14.850000,74546000
|
||||
2018-06-05,14.850000,14.920000,14.630000,14.850000,14.850000,56122700
|
||||
2018-06-06,15.070000,15.740000,15.040000,15.670000,15.670000,97089000
|
||||
2018-06-07,15.830000,15.970000,14.850000,14.890000,14.890000,99860300
|
||||
2018-06-08,14.520000,15.330000,14.310000,15.250000,15.250000,81930500
|
||||
2018-06-11,15.210000,15.890000,15.010000,15.730000,15.730000,80737600
|
||||
2018-06-12,15.840000,15.950000,15.430000,15.850000,15.850000,67002600
|
||||
2018-06-13,15.810000,16.520000,15.780000,16.320000,16.320000,90227300
|
||||
2018-06-14,16.620001,16.790001,15.580000,16.250000,16.250000,113048600
|
||||
2018-06-15,16.059999,16.520000,15.820000,16.340000,16.340000,77612200
|
||||
2018-06-18,16.180000,17.340000,16.129999,17.110001,17.110001,104317400
|
||||
2018-06-19,16.850000,17.290001,16.309999,16.690001,16.690001,92542900
|
||||
2018-06-20,16.830000,17.129999,16.370001,16.520000,16.520000,76280600
|
||||
2018-06-21,16.650000,16.870001,15.460000,15.650000,15.650000,95638400
|
||||
2018-06-22,15.780000,15.910000,15.560000,15.800000,15.800000,59257100
|
||||
2018-06-25,15.640000,15.740000,14.540000,15.110000,15.110000,94418400
|
||||
2018-06-26,15.320000,15.600000,15.100000,15.500000,15.500000,54213500
|
||||
2018-06-27,15.650000,15.760000,14.960000,14.970000,14.970000,56014300
|
||||
2018-06-28,14.850000,15.360000,14.750000,15.310000,15.310000,48716800
|
||||
2018-06-29,15.410000,15.490000,14.980000,14.990000,14.990000,41527800
|
||||
2018-07-02,14.800000,15.180000,14.740000,15.160000,15.160000,43398800
|
||||
2018-07-03,15.210000,15.340000,14.960000,15.000000,15.000000,32094000
|
||||
2018-07-05,15.130000,15.500000,15.020000,15.500000,15.500000,40703300
|
||||
2018-07-06,15.520000,16.389999,15.480000,16.360001,16.360001,65101700
|
||||
2018-07-09,16.730000,16.840000,16.170000,16.610001,16.610001,58525500
|
||||
2018-07-10,16.590000,16.650000,16.309999,16.549999,16.549999,37093000
|
||||
2018-07-11,16.150000,16.530001,16.020000,16.270000,16.270000,42544100
|
||||
2018-07-12,16.410000,16.790001,16.379999,16.559999,16.559999,44188100
|
||||
2018-07-13,16.680000,16.690001,16.219999,16.270000,16.270000,40614100
|
||||
2018-07-16,16.420000,17.000000,16.410000,16.580000,16.580000,65275300
|
||||
2018-07-17,16.500000,16.879999,16.480000,16.870001,16.870001,42313500
|
||||
2018-07-18,16.940001,16.990000,16.549999,16.850000,16.850000,40881500
|
||||
2018-07-19,16.709999,16.879999,16.549999,16.709999,16.709999,41267800
|
||||
2018-07-20,16.660000,16.879999,16.440001,16.500000,16.500000,42879800
|
||||
2018-07-23,16.469999,16.680000,15.900000,16.660000,16.660000,44940800
|
||||
2018-07-24,16.750000,16.860001,16.110001,16.190001,16.190001,58201500
|
||||
2018-07-25,16.299999,16.389999,15.720000,16.049999,16.049999,82604900
|
||||
2018-07-26,17.160000,18.450001,16.830000,18.350000,18.350000,192661100
|
||||
2018-07-27,19.070000,19.879999,18.309999,18.940001,18.940001,161903800
|
||||
2018-07-30,19.400000,20.180000,19.309999,19.420000,19.420000,160823400
|
||||
2018-07-31,19.350000,19.500000,18.270000,18.330000,18.330000,118403400
|
||||
2018-08-01,18.340000,18.950001,18.320000,18.480000,18.480000,75495200
|
||||
2018-08-02,18.170000,18.830000,18.000000,18.790001,18.790001,52867100
|
||||
2018-08-03,18.940001,19.059999,18.370001,18.490000,18.490000,53232100
|
||||
2018-08-06,18.889999,19.440001,18.459999,19.430000,19.430000,83579700
|
||||
2018-08-07,19.530001,19.709999,19.080000,19.559999,19.559999,72822600
|
||||
2018-08-08,19.459999,19.770000,19.260000,19.580000,19.580000,52081400
|
||||
2018-08-09,19.580000,19.709999,19.080000,19.100000,19.100000,46536400
|
||||
2018-08-10,19.090000,19.480000,18.850000,19.059999,19.059999,65821100
|
||||
2018-08-13,19.160000,19.930000,19.120001,19.730000,19.730000,81262200
|
||||
2018-08-14,19.969999,20.280001,19.629999,20.020000,20.020000,89195500
|
||||
2018-08-15,19.860001,20.100000,19.200001,19.700001,19.700001,86355700
|
||||
2018-08-16,19.860001,20.070000,19.250000,19.330000,19.330000,69733700
|
||||
2018-08-17,19.120001,19.820000,18.730000,19.770000,19.770000,60616600
|
||||
2018-08-20,19.790001,20.080000,19.350000,19.980000,19.980000,62983200
|
||||
2018-08-21,19.980000,20.420000,19.860001,20.400000,20.400000,55629000
|
||||
2018-08-22,20.280001,20.920000,20.209999,20.900000,20.900000,62002700
|
||||
2018-08-23,21.190001,22.320000,21.139999,22.290001,22.290001,113444100
|
||||
2018-08-24,22.910000,24.000000,22.670000,23.980000,23.980000,164328200
|
||||
2018-08-27,24.940001,27.299999,24.629999,25.260000,25.260000,325058400
|
||||
2018-08-28,25.510000,26.180000,24.040001,25.049999,25.049999,215771200
|
||||
2018-08-29,24.360001,25.410000,24.010000,25.200001,25.200001,143223200
|
||||
2018-08-30,25.290001,25.670000,24.760000,24.889999,24.889999,103607300
|
||||
2018-08-31,24.889999,25.240000,24.719999,25.170000,25.170000,65206400
|
||||
2018-09-04,25.620001,28.110001,25.570000,28.059999,28.059999,192541300
|
||||
2018-09-05,29.410000,29.940001,26.840000,28.510000,28.510000,257349000
|
||||
2018-09-06,28.120001,28.580000,27.190001,27.840000,27.840000,143942900
|
||||
2018-09-07,26.959999,28.270000,26.799999,27.379999,27.379999,123348700
|
||||
2018-09-10,28.150000,29.930000,27.840000,29.889999,29.889999,162253800
|
||||
2018-09-11,30.020000,30.590000,29.370001,30.100000,30.100000,159902500
|
||||
2018-09-12,29.910000,32.290001,29.450001,32.209999,32.209999,197889600
|
||||
2018-09-13,33.160000,34.139999,29.870001,30.480000,30.480000,304147100
|
||||
2018-09-14,31.430000,33.090000,30.540001,32.720001,32.720001,217762800
|
||||
2018-09-17,31.750000,33.230000,31.600000,32.430000,32.430000,180410600
|
||||
2018-09-18,32.990002,33.369999,31.200001,31.930000,31.930000,176673200
|
||||
2018-09-19,31.520000,31.830000,30.510000,31.209999,31.209999,124287000
|
||||
2018-09-20,32.099998,32.200001,30.639999,31.180000,31.180000,123116500
|
||||
2018-09-21,31.190001,32.419998,30.910000,31.020000,31.020000,129792900
|
||||
2018-09-24,31.129999,32.650002,30.910000,32.610001,32.610001,118332600
|
||||
2018-09-25,33.180000,33.599998,32.189999,32.570000,32.570000,118570200
|
||||
2018-09-26,32.400002,32.599998,31.719999,32.189999,32.189999,79347300
|
||||
2018-09-27,31.860001,32.630001,31.389999,32.590000,32.590000,87934400
|
||||
2018-09-28,32.240002,32.779999,29.980000,30.889999,30.889999,165453500
|
||||
2018-10-01,30.690001,31.910000,30.250000,31.420000,31.420000,94742900
|
||||
2018-10-02,30.730000,30.820000,28.650000,29.020000,29.020000,145276500
|
||||
2018-10-03,29.040001,29.219999,26.540001,28.430000,28.430000,190137200
|
||||
2018-10-04,27.990000,28.830000,27.370001,27.780001,27.780001,95831200
|
||||
2018-10-05,28.070000,28.469999,26.930000,27.350000,27.350000,88008500
|
||||
2018-10-08,26.730000,27.540001,25.959999,26.459999,26.459999,103789500
|
||||
2018-10-09,26.150000,27.709999,26.000000,27.240000,27.240000,105461800
|
||||
2018-10-10,27.379999,27.400000,24.910000,25.000000,25.000000,147682900
|
||||
2018-10-11,24.740000,26.200001,24.549999,25.299999,25.299999,147013800
|
||||
2018-10-12,26.770000,26.969999,25.670000,26.340000,26.340000,111059400
|
||||
2018-10-15,26.379999,26.770000,25.750000,26.260000,26.260000,70523500
|
||||
2018-10-16,26.629999,28.230000,26.170000,28.180000,28.180000,92529000
|
||||
2018-10-17,28.410000,28.530001,26.920000,27.299999,27.299999,89466900
|
||||
2018-10-18,27.080000,27.750000,26.400000,26.620001,26.620001,79623700
|
||||
2018-10-19,27.030001,27.100000,23.600000,23.660000,23.660000,130799900
|
||||
2018-10-22,24.459999,25.639999,24.090000,25.030001,25.030001,114158900
|
||||
2018-10-23,24.180000,25.260000,23.850000,25.090000,25.090000,101763000
|
||||
2018-10-24,25.040001,25.100000,22.750000,22.790001,22.790001,134489100
|
||||
2018-10-25,17.920000,20.150000,17.719999,19.270000,19.270000,189173700
|
||||
2018-10-26,18.490000,18.780001,17.049999,17.629999,17.629999,119689000
|
||||
2018-10-29,18.209999,18.230000,16.270000,16.850000,16.850000,94479600
|
||||
2018-10-30,16.379999,17.240000,16.170000,17.200001,17.200001,99049400
|
||||
2018-10-31,17.870001,18.340000,17.120001,18.209999,18.209999,110463700
|
||||
2018-11-01,18.410000,20.330000,18.080000,20.219999,20.219999,136896500
|
||||
2018-11-02,20.590000,21.059999,19.469999,20.230000,20.230000,123788000
|
||||
2018-11-05,20.120001,20.180000,18.879999,19.900000,19.900000,108016700
|
||||
2018-11-06,19.500000,21.650000,19.480000,20.680000,20.680000,144995700
|
||||
2018-11-07,21.420000,22.219999,21.070000,21.840000,21.840000,121115800
|
||||
2018-11-08,21.770000,22.080000,20.969999,21.200001,21.200001,92387600
|
||||
2018-11-09,20.770000,21.190001,20.110001,21.030001,21.030001,85900700
|
||||
2018-11-12,20.680000,20.850000,18.799999,19.030001,19.030001,95948200
|
||||
2018-11-13,19.280001,20.020000,18.969999,19.610001,19.610001,76126000
|
||||
2018-11-14,20.180000,21.110001,19.760000,20.809999,20.809999,106344300
|
||||
2018-11-15,20.719999,21.770000,20.420000,21.490000,21.490000,97715500
|
||||
2018-11-16,19.870001,20.969999,19.719999,20.660000,20.660000,112376600
|
||||
2018-11-19,20.400000,20.590000,19.090000,19.110001,19.110001,93578200
|
||||
2018-11-20,17.400000,19.580000,17.180000,19.209999,19.209999,109869400
|
||||
2018-11-21,20.049999,20.309999,18.500000,18.730000,18.730000,81585600
|
||||
2018-11-23,18.610001,19.830000,18.559999,19.379999,19.379999,54611300
|
||||
2018-11-26,19.959999,20.190001,19.110001,20.080000,20.080000,83211000
|
||||
2018-11-27,19.770000,21.450001,19.730000,21.049999,21.049999,119230100
|
||||
2018-11-28,21.820000,21.879999,20.180000,21.340000,21.340000,134425300
|
||||
2018-11-29,21.190001,21.610001,20.730000,21.430000,21.430000,79853700
|
||||
2018-11-30,21.299999,21.360001,20.520000,21.299999,21.299999,82370700
|
||||
2018-12-03,22.480000,23.750000,22.370001,23.709999,23.709999,139607400
|
||||
2018-12-04,23.350000,23.420000,21.070000,21.120001,21.120001,127392900
|
||||
2018-12-06,20.219999,21.410000,20.059999,21.299999,21.299999,103434700
|
||||
2018-12-07,21.299999,21.379999,19.170000,19.459999,19.459999,105764500
|
||||
2018-12-10,19.350000,20.129999,19.270000,19.990000,19.990000,77984500
|
||||
2018-12-11,20.709999,21.139999,19.690001,19.980000,19.980000,88027400
|
||||
2018-12-12,20.320000,21.020000,19.709999,20.480000,20.480000,100340700
|
||||
2018-12-13,20.629999,20.870001,19.760000,19.860001,19.860001,88108300
|
||||
2018-12-14,19.580000,20.700001,19.520000,19.900000,19.900000,84713600
|
||||
2018-12-17,20.010000,20.020000,18.639999,18.830000,18.830000,115437900
|
||||
2018-12-18,19.150000,19.840000,18.879999,19.500000,19.500000,101512900
|
||||
2018-12-19,19.440001,19.719999,18.000000,18.160000,18.160000,120644500
|
||||
2018-12-20,18.110001,18.860001,17.340000,17.940001,17.940001,119394500
|
||||
2018-12-21,18.120001,18.340000,16.760000,16.930000,16.930000,132246000
|
||||
2018-12-24,16.520000,17.219999,16.370001,16.650000,16.650000,62933100
|
||||
2018-12-26,16.879999,17.910000,16.030001,17.900000,17.900000,108811800
|
||||
2018-12-27,17.430000,17.740000,16.440001,17.490000,17.490000,111373000
|
||||
2018-12-28,17.530001,18.309999,17.139999,17.820000,17.820000,109214400
|
||||
2018-12-31,18.150000,18.510000,17.850000,18.459999,18.459999,84732200
|
||||
2019-01-02,18.010000,19.000000,17.980000,18.830000,18.830000,87148700
|
||||
2019-01-03,18.420000,18.680000,16.940001,17.049999,17.049999,117073000
|
||||
2019-01-04,17.549999,19.070000,17.430000,19.000000,19.000000,111878600
|
||||
2019-01-07,19.440001,20.680000,19.000000,20.570000,20.570000,107157000
|
||||
2019-01-08,21.190001,21.200001,19.680000,20.750000,20.750000,121271000
|
||||
2019-01-09,20.889999,21.440001,20.070000,20.190001,20.190001,163944100
|
||||
2019-01-10,19.760000,19.830000,18.900000,19.740000,19.740000,115629400
|
||||
2019-01-11,19.469999,20.350000,19.190001,20.270000,20.270000,85110800
|
||||
2019-01-14,19.959999,20.620001,19.750000,20.230000,20.230000,71350200
|
||||
2019-01-15,20.440001,20.680000,20.260000,20.379999,20.379999,62785800
|
||||
2019-01-16,20.400000,20.540001,19.709999,19.730000,19.730000,70849300
|
||||
2019-01-17,19.490000,20.510000,19.020000,20.250000,20.250000,85018400
|
||||
2019-01-18,20.370001,21.049999,20.020000,20.770000,20.770000,88131000
|
||||
2019-01-22,20.480000,20.920000,19.700001,19.760000,19.760000,78513700
|
||||
2019-01-23,20.030001,20.480000,19.549999,19.799999,19.799999,77811300
|
||||
2019-01-24,20.059999,21.010000,20.040001,20.850000,20.850000,97433400
|
||||
2019-01-25,20.990000,22.030001,20.790001,21.930000,21.930000,110239500
|
||||
2019-01-28,20.320000,21.010000,20.020000,20.180000,20.180000,135164100
|
||||
2019-01-29,20.260000,20.389999,19.049999,19.250000,19.250000,131202500
|
||||
2019-01-30,21.490000,23.129999,21.370001,23.090000,23.090000,211421200
|
||||
2019-01-31,23.020000,25.139999,22.830000,24.410000,24.410000,182575600
|
||||
2019-02-01,24.610001,24.840000,24.070000,24.510000,24.510000,105356200
|
||||
2019-02-04,24.430000,24.660000,24.070000,24.129999,24.129999,70843800
|
||||
2019-02-05,23.420000,23.860001,22.980000,23.309999,23.309999,122226000
|
||||
2019-02-06,23.629999,24.139999,23.219999,23.260000,23.260000,78684300
|
||||
2019-02-07,22.990000,23.219999,22.320000,22.670000,22.670000,86723900
|
||||
2019-02-08,22.330000,23.280001,22.270000,23.049999,23.049999,78129300
|
||||
2019-02-11,23.049999,23.280001,22.660000,22.959999,22.959999,60578700
|
||||
2019-02-12,23.430000,23.559999,22.750000,22.820000,22.820000,67595400
|
||||
2019-02-13,22.980000,23.240000,22.709999,22.850000,22.850000,57544200
|
||||
2019-02-14,22.740000,23.370001,22.590000,23.129999,23.129999,64441200
|
||||
2019-02-15,23.580000,24.049999,23.200001,23.680000,23.680000,78644100
|
||||
2019-02-19,23.629999,24.410000,23.610001,23.950001,23.950001,57517900
|
||||
2019-02-20,24.139999,24.370001,23.900000,23.950001,23.950001,57091600
|
||||
2019-02-21,24.040001,24.330000,23.850000,23.920000,23.920000,49608200
|
||||
2019-02-22,24.049999,24.360001,23.879999,24.360001,24.360001,52650700
|
||||
2019-02-25,25.010000,25.520000,24.680000,24.709999,24.709999,63221000
|
||||
2019-02-26,24.650000,24.719999,24.150000,24.209999,24.209999,48470100
|
||||
2019-02-27,24.110001,24.230000,23.209999,23.480000,23.480000,62649300
|
||||
2019-02-28,23.209999,23.670000,23.110001,23.530001,23.530001,39384900
|
||||
2019-03-01,23.969999,24.190001,23.450001,23.680000,23.680000,48084000
|
||||
2019-03-04,23.889999,24.129999,23.010000,23.370001,23.370001,48147700
|
||||
2019-03-05,23.340000,23.680000,23.010000,23.500000,23.500000,35462600
|
||||
2019-03-06,23.469999,23.530001,22.400000,22.410000,22.410000,60479400
|
||||
2019-03-07,22.330000,22.410000,21.730000,22.080000,22.080000,52087400
|
||||
2019-03-08,21.350000,22.090000,21.040001,22.010000,22.010000,49967700
|
||||
2019-03-11,22.150000,23.080000,21.980000,22.959999,22.959999,54420200
|
||||
2019-03-12,23.100000,23.799999,22.780001,23.490000,23.490000,56410600
|
||||
2019-03-13,23.660000,24.150000,23.350000,23.379999,23.379999,56705800
|
||||
2019-03-14,23.370001,23.490000,22.799999,22.820000,22.820000,42818600
|
||||
2019-03-15,23.100000,23.650000,23.010000,23.290001,23.290001,46519900
|
||||
2019-03-18,23.299999,23.620001,23.040001,23.250000,23.250000,34731800
|
||||
2019-03-19,23.600000,26.080000,23.590000,26.000000,26.000000,156052200
|
||||
2019-03-20,26.490000,26.879999,25.309999,25.700001,25.700001,151292100
|
||||
2019-03-21,25.780001,28.110001,25.709999,27.889999,27.889999,129610300
|
||||
2019-03-22,27.540001,27.750000,26.330000,26.370001,26.370001,115323300
|
||||
2019-03-25,26.290001,26.990000,25.540001,25.969999,25.969999,78438200
|
||||
2019-03-26,26.690001,26.980000,25.459999,25.690001,25.690001,75754100
|
||||
2019-03-27,25.700001,25.879999,24.549999,24.889999,24.889999,88585300
|
||||
2019-03-28,25.100000,25.559999,24.650000,25.059999,25.059999,64667500
|
||||
2019-03-29,25.580000,25.730000,25.250000,25.520000,25.520000,53502800
|
||||
2019-04-01,26.420000,26.559999,25.830000,26.360001,26.360001,63000300
|
||||
2019-04-02,26.510000,26.799999,26.090000,26.750000,26.750000,53358800
|
||||
2019-04-03,28.020000,29.950001,27.879999,29.020000,29.020000,197650500
|
||||
2019-04-04,28.879999,29.389999,28.610001,29.090000,29.090000,82191100
|
||||
2019-04-05,29.639999,29.690001,28.799999,28.980000,28.980000,65662700
|
||||
2019-04-08,28.690001,28.950001,28.180000,28.530001,28.530001,58002500
|
||||
2019-04-09,28.240000,28.379999,27.190001,27.240000,27.240000,75539800
|
||||
2019-04-10,27.459999,28.120001,27.320000,27.830000,27.830000,64368100
|
||||
2019-04-11,27.809999,28.049999,27.459999,27.790001,27.790001,44801200
|
||||
2019-04-12,28.209999,28.379999,27.660000,27.850000,27.850000,41048800
|
||||
2019-04-15,27.799999,27.840000,26.959999,27.330000,27.330000,40812500
|
||||
2019-04-16,27.719999,28.180000,27.490000,27.930000,27.930000,47340100
|
||||
2019-04-17,28.209999,28.270000,27.219999,27.490000,27.490000,48240800
|
||||
2019-04-18,27.600000,27.879999,27.340000,27.680000,27.680000,39880900
|
||||
2019-04-22,27.620001,28.230000,27.389999,28.180000,28.180000,36477300
|
||||
2019-04-23,28.180000,28.490000,27.790001,27.969999,27.969999,41777500
|
||||
2019-04-24,28.100000,28.850000,27.930000,28.459999,28.459999,51784700
|
||||
2019-04-25,28.670000,28.860001,27.360001,27.660000,27.660000,57329700
|
||||
2019-04-26,27.660000,27.900000,27.049999,27.879999,27.879999,48827900
|
||||
2019-04-29,27.900000,28.139999,27.500000,27.690001,27.690001,44532700
|
||||
2019-04-30,27.590000,27.799999,26.940001,27.629999,27.629999,73165900
|
||||
2019-05-01,28.950001,29.150000,26.780001,26.809999,26.809999,136066900
|
||||
2019-05-02,26.940001,28.639999,26.610001,28.290001,28.290001,100514800
|
||||
2019-05-03,28.299999,28.420000,27.660000,28.219999,28.219999,55503100
|
||||
2019-05-06,26.719999,27.500000,26.450001,27.420000,27.420000,70344100
|
||||
2019-05-07,27.200001,27.350000,26.209999,26.660000,26.660000,75868800
|
||||
2019-05-08,26.410000,27.709999,26.270000,27.090000,27.090000,65967500
|
||||
2019-05-09,26.700001,27.379999,26.030001,27.209999,27.209999,73150900
|
||||
2019-05-10,27.030001,28.100000,26.930000,27.959999,27.959999,82930100
|
||||
2019-05-13,26.980000,27.230000,26.100000,26.240000,26.240000,99017900
|
||||
2019-05-14,26.530001,27.480000,26.150000,27.320000,27.320000,82980400
|
||||
2019-05-15,26.870001,27.790001,26.730000,27.580000,27.580000,55689900
|
||||
2019-05-16,27.370001,28.370001,27.270000,28.010000,28.010000,67330100
|
||||
2019-05-17,27.690001,28.459999,27.400000,27.500000,27.500000,65385400
|
||||
2019-05-20,26.980000,27.240000,26.490000,26.680000,26.680000,69757400
|
||||
2019-05-21,27.180000,27.370001,26.930000,27.350000,27.350000,46079200
|
||||
2019-05-22,27.120001,27.590000,27.070000,27.410000,27.410000,39957400
|
||||
2019-05-23,26.990000,27.100000,26.030001,26.309999,26.309999,63165410
|
||||
|
@@ -0,0 +1,340 @@
|
||||
timestamp,close,positive,negative
|
||||
2019-08-09T23:00:00,11860.074544270834,0.672895649396392,0.32710435060360804
|
||||
2019-08-09T23:20:00,11872.02587890625,0.5951002465690001,0.404899753431
|
||||
2019-08-09T23:40:00,11880.504557291666,0.5967015715902333,0.4032984284097667
|
||||
2019-08-10T00:00:00,11918.873480902777,0.577972446366791,0.4220275536332089
|
||||
2019-08-10T00:20:00,11937.581271701389,0.5853419523485952,0.41465804765140474
|
||||
2019-08-10T00:40:00,11910.677734375,0.5242306480132202,0.47576935198677994
|
||||
2019-08-10T01:00:00,11904.2208984375,0.6591064249015663,0.34089357509843377
|
||||
2019-08-10T01:20:00,11888.665581597223,0.6301647610692034,0.3698352389307965
|
||||
2019-08-10T01:40:00,11896.321180555555,0.5076873369870158,0.49231266301298415
|
||||
2019-08-10T02:00:00,11840.427625868055,0.44154417133540674,0.5584558286645933
|
||||
2019-08-10T02:20:00,11824.354383680555,0.6921190683962035,0.3078809316037964
|
||||
2019-08-10T02:40:00,11811.5078125,0.5520865149257126,0.44791348507428747
|
||||
2019-08-10T03:00:00,11814.022135416666,0.6486436760445364,0.3513563239554635
|
||||
2019-08-10T03:20:00,11814.034505208334,0.5938786908786935,0.4061213091213065
|
||||
2019-08-10T03:40:00,11800.455620659723,0.46793348737298585,0.5320665126270142
|
||||
2019-08-10T04:00:00,11817.322265625,0.6528729511052616,0.34712704889473833
|
||||
2019-08-10T04:20:00,11810.06220703125,0.6927590475558578,0.30724095244414223
|
||||
2019-08-10T04:40:00,11808.473415798611,0.6221166226882595,0.3778833773117405
|
||||
2019-08-10T05:00:00,11804.455512152777,0.6626195416594638,0.3373804583405362
|
||||
2019-08-10T05:20:00,11832.654513888889,0.6552284018875003,0.34477159811249974
|
||||
2019-08-10T05:40:00,11862.870008680555,0.6782426737680571,0.321757326231943
|
||||
2019-08-10T06:00:00,11842.598046875,0.5877573939130807,0.41224260608691937
|
||||
2019-08-10T06:20:00,11830.985568576389,0.6435594124509156,0.35644058754908453
|
||||
2019-08-10T06:40:00,11819.560004340277,0.650339727903031,0.3496602720969691
|
||||
2019-08-10T07:00:00,11821.251302083334,0.6423833371368259,0.35761666286317406
|
||||
2019-08-10T07:20:00,11837.832248263889,0.6769147271440639,0.3230852728559362
|
||||
2019-08-10T07:40:00,11831.2291015625,0.6523671453973767,0.34763285460262333
|
||||
2019-08-10T08:00:00,11847.092339409723,0.6242978657705356,0.3757021342294644
|
||||
2019-08-10T08:20:00,11860.499891493055,0.6816604139992111,0.31833958600078904
|
||||
2019-08-10T08:40:00,11853.944227430555,0.6886029775775693,0.3113970224224307
|
||||
2019-08-10T09:00:00,11854.416666666666,0.6619221403320443,0.3380778596679557
|
||||
2019-08-10T09:20:00,11866.56708984375,0.49276923484657936,0.5072307651534207
|
||||
2019-08-10T09:40:00,11878.548936631945,0.6324551456153391,0.367544854384661
|
||||
2019-08-10T10:00:00,11881.569878472223,0.6745155013265913,0.3254844986734087
|
||||
2019-08-10T10:20:00,11863.433268229166,0.6729489449466852,0.3270510550533148
|
||||
2019-08-10T10:40:00,11864.763346354166,0.6646037057680267,0.33539629423197315
|
||||
2019-08-10T11:00:00,11855.1240234375,0.652120254814023,0.3478797451859769
|
||||
2019-08-10T11:20:00,11548.468858506945,0.6449511698587633,0.35504883014123667
|
||||
2019-08-10T11:40:00,11404.045681423611,0.19822288491372791,0.8017771150862723
|
||||
2019-08-10T12:00:00,11379.092122395834,0.6106330443094115,0.3893669556905886
|
||||
2019-08-10T12:20:00,11372.661024305555,0.684582318142607,0.31541768185739316
|
||||
2019-08-10T12:40:00,11414.57509765625,0.6739784203158583,0.3260215796841417
|
||||
2019-08-10T13:00:00,11476.397786458334,0.5122779635805852,0.4877220364194148
|
||||
2019-08-10T13:20:00,11460.401150173611,0.5995944235746562,0.4004055764253437
|
||||
2019-08-10T13:40:00,11451.494466145834,0.40688145562970296,0.5931185443702972
|
||||
2019-08-10T14:00:00,11456.026801215277,0.6781337542255548,0.32186624577444517
|
||||
2019-08-10T14:20:00,11426.1630859375,0.631335461936906,0.368664538063094
|
||||
2019-08-10T14:40:00,11413.129014756945,0.6214467965284589,0.378553203471541
|
||||
2019-08-10T15:00:00,11416.041015625,0.6508698520753338,0.34913014792466635
|
||||
2019-08-10T15:20:00,11432.755750868055,0.5618785826253185,0.43812141737468147
|
||||
2019-08-10T15:40:00,11410.909939236111,0.5524372826728199,0.4475627173271801
|
||||
2019-08-10T16:00:00,11401.2259765625,0.5376810695041971,0.46231893049580275
|
||||
2019-08-10T16:20:00,11392.866536458334,0.6380488083153308,0.36195119168466916
|
||||
2019-08-10T16:40:00,11368.657769097223,0.6491787688948025,0.35082123110519753
|
||||
2019-08-10T17:00:00,11347.328884548611,0.6123678328389881,0.3876321671610119
|
||||
2019-08-10T17:20:00,11398.531032986111,0.6743216891030781,0.32567831089692195
|
||||
2019-08-10T17:40:00,11401.97197265625,0.6711219061934897,0.32887809380651023
|
||||
2019-08-10T18:00:00,11385.302191840277,0.667910332241187,0.33208966775881293
|
||||
2019-08-10T18:20:00,11368.152235243055,0.6877028110752175,0.31229718892478264
|
||||
2019-08-10T18:40:00,11374.289930555555,0.6421495151175716,0.3578504848824283
|
||||
2019-08-10T19:00:00,11361.828776041666,0.6651905829219482,0.33480941707805184
|
||||
2019-08-10T19:20:00,11349.28291015625,0.6353849057818903,0.36461509421810967
|
||||
2019-08-10T19:40:00,11344.813368055555,0.5802551243959988,0.4197448756040013
|
||||
2019-08-10T20:00:00,11367.511176215277,0.6598421413837092,0.34015785861629083
|
||||
2019-08-10T20:20:00,11376.683485243055,0.6901700620910418,0.30982993790895824
|
||||
2019-08-10T20:40:00,11356.072374131945,0.6277910109477822,0.37220898905221783
|
||||
2019-08-10T21:00:00,11332.16474609375,0.5748772842933609,0.42512271570663906
|
||||
2019-08-10T21:20:00,11335.863389756945,0.6097175320856112,0.39028246791438886
|
||||
2019-08-10T21:40:00,11332.8466796875,0.41341007586462647,0.5865899241353736
|
||||
2019-08-10T22:00:00,11319.971137152777,0.6791769782431056,0.3208230217568943
|
||||
2019-08-10T22:20:00,11298.448893229166,0.5369720459965348,0.463027954003465
|
||||
2019-08-10T22:40:00,11315.787890625,0.6491861766678833,0.3508138233321167
|
||||
2019-08-10T23:00:00,11345.858832465277,0.6605692536158401,0.33943074638416
|
||||
2019-08-10T23:20:00,11343.282335069445,0.6610958589703089,0.33890414102969113
|
||||
2019-08-10T23:40:00,11332.678927951389,0.6769499006582065,0.3230500993417934
|
||||
2019-08-11T00:00:00,11310.861219618055,0.6757194457447895,0.3242805542552107
|
||||
2019-08-11T00:20:00,11366.58095703125,0.5495295542377412,0.45047044576225886
|
||||
2019-08-11T00:40:00,11425.075412326389,0.5495721618384806,0.4504278381615194
|
||||
2019-08-11T01:00:00,11392.177625868055,0.6569360850440795,0.3430639149559205
|
||||
2019-08-11T01:20:00,11391.202256944445,0.6387944193483925,0.3612055806516075
|
||||
2019-08-11T01:40:00,11376.443250868055,0.5342728930646066,0.46572710693539354
|
||||
2019-08-11T02:00:00,11382.28896484375,0.654112780824424,0.34588721917557586
|
||||
2019-08-11T02:20:00,11386.936740451389,0.6347371785576235,0.3652628214423765
|
||||
2019-08-11T02:40:00,11376.695638020834,0.6007389236995619,0.3992610763004379
|
||||
2019-08-11T03:00:00,11387.851236979166,0.652357996303953,0.34764200369604703
|
||||
2019-08-11T03:20:00,11404.256727430555,0.6584472946206366,0.34155270537936333
|
||||
2019-08-11T03:40:00,11397.7041015625,0.6361597150913694,0.36384028490863063
|
||||
2019-08-11T04:00:00,11407.205620659723,0.6434419807577798,0.3565580192422201
|
||||
2019-08-11T04:20:00,11393.106770833334,0.6786527307775161,0.321347269222484
|
||||
2019-08-11T04:40:00,11387.253363715277,0.47659807067248267,0.5234019293275174
|
||||
2019-08-11T05:00:00,11376.687717013889,0.6075992572428865,0.39240074275711356
|
||||
2019-08-11T05:20:00,11371.49091796875,0.6667653723922398,0.33323462760776035
|
||||
2019-08-11T05:40:00,11377.342230902777,0.6510149226393858,0.3489850773606141
|
||||
2019-08-11T06:00:00,11377.576714409723,0.5900247192571966,0.4099752807428035
|
||||
2019-08-11T06:20:00,11384.317816840277,0.584004544651997,0.4159954553480031
|
||||
2019-08-11T06:40:00,11355.543402777777,0.6099239410937645,0.39007605890623553
|
||||
2019-08-11T07:00:00,11355.77001953125,0.6247484369695742,0.3752515630304257
|
||||
2019-08-11T07:20:00,11353.465494791666,0.6070929228730317,0.39290707712696826
|
||||
2019-08-11T07:40:00,11365.033420138889,0.5465993992486684,0.4534006007513317
|
||||
2019-08-11T08:00:00,11377.826714409723,0.5562781576822704,0.4437218423177296
|
||||
2019-08-11T08:20:00,11361.535481770834,0.4722174599328615,0.5277825400671385
|
||||
2019-08-11T08:40:00,11331.27197265625,0.626879640620249,0.3731203593797511
|
||||
2019-08-11T09:00:00,11251.542209201389,0.5745919248762765,0.4254080751237235
|
||||
2019-08-11T09:20:00,11242.855577256945,0.6338213893038831,0.3661786106961168
|
||||
2019-08-11T09:40:00,11293.3134765625,0.5675677468658742,0.4324322531341258
|
||||
2019-08-11T10:00:00,11375.678927951389,0.662068755690373,0.3379312443096269
|
||||
2019-08-11T10:20:00,11370.65,0.6528751953484073,0.34712480465159257
|
||||
2019-08-11T10:40:00,11347.428927951389,0.599597773208383,0.40040222679161697
|
||||
2019-08-11T11:00:00,11367.512261284723,0.38706937873298136,0.6129306212670186
|
||||
2019-08-11T11:20:00,11374.599934895834,0.6438946415019184,0.35610535849808156
|
||||
2019-08-11T11:40:00,11408.058919270834,0.16081816740881932,0.8391818325911806
|
||||
2019-08-11T12:00:00,11449.3798828125,0.6823639956542296,0.3176360043457704
|
||||
2019-08-11T12:20:00,11436.747721354166,0.6770972889952676,0.3229027110047324
|
||||
2019-08-11T12:40:00,11446.329969618055,0.6925934466871058,0.30740655331289424
|
||||
2019-08-11T13:00:00,11438.517795138889,0.654652478815902,0.34534752118409795
|
||||
2019-08-11T13:20:00,11422.4033203125,0.6327927182404469,0.36720728175955303
|
||||
2019-08-11T13:40:00,11401.083984375,0.6798617417015522,0.3201382582984478
|
||||
2019-08-11T14:00:00,11389.884331597223,0.6052985343987611,0.3947014656012389
|
||||
2019-08-11T14:20:00,11401.158854166666,0.570166067688104,0.4298339323118961
|
||||
2019-08-11T14:40:00,11399.070963541666,0.5136693050712073,0.4863306949287925
|
||||
2019-08-11T15:00:00,11402.946614583334,0.5796801649711361,0.42031983502886394
|
||||
2019-08-11T15:20:00,11377.36298828125,0.5919218614141928,0.4080781385858073
|
||||
2019-08-11T15:40:00,11371.596462673611,0.6310810207062723,0.3689189792937278
|
||||
2019-08-11T16:00:00,11395.904513888889,0.6217371805101322,0.3782628194898679
|
||||
2019-08-11T16:20:00,11419.927734375,0.6461206955537799,0.35387930444622007
|
||||
2019-08-11T16:40:00,11420.288736979166,0.5300978440776618,0.4699021559223382
|
||||
2019-08-11T17:00:00,11403.67509765625,0.6306025833631896,0.36939741663681025
|
||||
2019-08-11T17:20:00,11396.848958333334,0.547456589821129,0.45254341017887106
|
||||
2019-08-11T17:40:00,11388.457790798611,0.6536498289468671,0.3463501710531329
|
||||
2019-08-11T18:00:00,11382.766710069445,0.6082083185010192,0.3917916814989807
|
||||
2019-08-11T18:20:00,11402.794379340277,0.5628988213178285,0.43710117868217163
|
||||
2019-08-11T18:40:00,11396.54404296875,0.43049449803380485,0.5695055019661952
|
||||
2019-08-11T19:00:00,11395.010091145834,0.639117670357487,0.360882329642513
|
||||
2019-08-11T19:20:00,11388.391167534723,0.6560616186341286,0.3439383813658712
|
||||
2019-08-11T19:40:00,11386.055555555555,0.6757055424160328,0.3242944575839673
|
||||
2019-08-11T20:00:00,11378.362196180555,0.5539835390589898,0.4460164609410101
|
||||
2019-08-11T20:20:00,11421.919140625,0.6092714409461888,0.3907285590538111
|
||||
2019-08-11T20:40:00,11421.776584201389,0.6781988311663134,0.32180116883368665
|
||||
2019-08-11T21:00:00,11434.21875,0.6626951060952113,0.3373048939047888
|
||||
2019-08-11T21:20:00,11476.744466145834,0.6350809690036068,0.3649190309963932
|
||||
2019-08-11T21:40:00,11524.327690972223,0.3247190105646936,0.6752809894353065
|
||||
2019-08-11T22:00:00,11511.52587890625,0.6511572743872109,0.3488427256127891
|
||||
2019-08-11T22:20:00,11502.041015625,0.6762538431724938,0.3237461568275062
|
||||
2019-08-11T22:40:00,11514.976671006945,0.5141931297456488,0.4858068702543512
|
||||
2019-08-11T23:00:00,11531.945638020834,0.6313772789942997,0.36862272100570015
|
||||
2019-08-11T23:20:00,11532.624565972223,0.6016062596191318,0.39839374038086817
|
||||
2019-08-11T23:40:00,11551.14814453125,0.6236360480763794,0.3763639519236207
|
||||
2019-08-12T00:00:00,11521.941080729166,0.6253381699666754,0.37466183003332465
|
||||
2019-08-12T00:20:00,11502.863064236111,0.5719546518362717,0.4280453481637282
|
||||
2019-08-12T00:40:00,11485.684353298611,0.6294134488120073,0.37058655118799255
|
||||
2019-08-12T01:00:00,11488.705620659723,0.6794083484801265,0.32059165151987346
|
||||
2019-08-12T01:20:00,11516.49697265625,0.5208981787238789,0.47910182127612094
|
||||
2019-08-12T01:40:00,11486.148871527777,0.4094999078005231,0.590500092199477
|
||||
2019-08-12T02:00:00,11488.198893229166,0.671080283782491,0.32891971621750915
|
||||
2019-08-12T02:20:00,11471.236653645834,0.6679527549970672,0.332047245002933
|
||||
2019-08-12T02:40:00,11461.664605034723,0.6869545472611236,0.31304545273887624
|
||||
2019-08-12T03:00:00,11447.75390625,0.6403190453591587,0.3596809546408413
|
||||
2019-08-12T03:20:00,11438.854383680555,0.5849811299997134,0.4150188700002866
|
||||
2019-08-12T03:40:00,11452.5,0.6118184704602951,0.38818152953970486
|
||||
2019-08-12T04:00:00,11453.569878472223,0.6297212525725702,0.37027874742742983
|
||||
2019-08-12T04:20:00,11463.232421875,0.6813426637109852,0.3186573362890149
|
||||
2019-08-12T04:40:00,11446.39384765625,0.6536365546454181,0.3463634453545818
|
||||
2019-08-12T05:00:00,11440.851128472223,0.5398910600898461,0.4601089399101538
|
||||
2019-08-12T05:20:00,11400.792100694445,0.5035652314928276,0.49643476850717244
|
||||
2019-08-12T05:40:00,11403.041232638889,0.6071596274052669,0.3928403725947331
|
||||
2019-08-12T06:00:00,11391.78896484375,0.6440912520510085,0.3559087479489917
|
||||
2019-08-12T06:20:00,11387.051106770834,0.49408216522863796,0.5059178347713621
|
||||
2019-08-12T06:40:00,11382.095486111111,0.6456520913335915,0.35434790866640853
|
||||
2019-08-12T07:00:00,11364.397786458334,0.5844129665591042,0.4155870334408958
|
||||
2019-08-12T07:20:00,11369.844618055555,0.6361426617503474,0.3638573382496526
|
||||
2019-08-12T07:40:00,11398.74208984375,0.6590629697838333,0.34093703021616667
|
||||
2019-08-12T08:00:00,11386.335503472223,0.6477999791225999,0.3522000208774
|
||||
2019-08-12T08:20:00,11348.097873263889,0.6431414167709211,0.3568585832290789
|
||||
2019-08-12T08:40:00,11353.737847222223,0.6615011707187288,0.3384988292812711
|
||||
2019-08-12T09:00:00,11362.335503472223,0.6104913483678767,0.3895086516321233
|
||||
2019-08-12T09:20:00,11378.998828125,0.6854220220481311,0.31457797795186887
|
||||
2019-08-12T09:40:00,11376.811306423611,0.6715225557051585,0.3284774442948415
|
||||
2019-08-12T10:00:00,11362.680989583334,0.6622198218374931,0.3377801781625069
|
||||
2019-08-12T10:20:00,11380.706705729166,0.6414232069686598,0.35857679303134016
|
||||
2019-08-12T10:40:00,11388.443467881945,0.6835843520518221,0.3164156479481779
|
||||
2019-08-12T11:00:00,11367.816796875,0.6472069155613542,0.3527930844386457
|
||||
2019-08-12T11:20:00,11343.722113715277,0.6590942729628322,0.3409057270371679
|
||||
2019-08-12T11:40:00,11327.515625,0.6684264626591627,0.3315735373408374
|
||||
2019-08-12T12:00:00,11362.027669270834,0.5877699982840787,0.4122300017159214
|
||||
2019-08-12T12:20:00,11384.478841145834,0.6810240234120695,0.31897597658793064
|
||||
2019-08-12T12:40:00,11398.3560546875,0.3431108147000487,0.6568891852999512
|
||||
2019-08-12T13:00:00,11375.647786458334,0.4894574283406821,0.5105425716593177
|
||||
2019-08-12T13:20:00,11369.760959201389,0.6570530608418301,0.34294693915816993
|
||||
2019-08-12T13:40:00,11394.930013020834,0.6641049678741577,0.3358950321258421
|
||||
2019-08-12T14:00:00,11380.068901909723,0.5628184209920828,0.43718157900791715
|
||||
2019-08-12T14:20:00,11393.87607421875,0.48251707929705906,0.5174829207029409
|
||||
2019-08-12T14:40:00,11385.921115451389,0.6079831361496497,0.3920168638503503
|
||||
2019-08-12T15:00:00,11375.189887152777,0.6563552519079952,0.3436447480920048
|
||||
2019-08-12T15:20:00,11375.322157118055,0.6331616932927264,0.36683830670727346
|
||||
2019-08-12T15:40:00,11391.156792534723,0.5950923093252443,0.4049076906747557
|
||||
2019-08-12T16:00:00,11399.801953125,0.633309515004878,0.366690484995122
|
||||
2019-08-12T16:20:00,11395.595486111111,0.6457060433135906,0.35429395668640945
|
||||
2019-08-12T16:40:00,11409.202256944445,0.5591611494061302,0.44083885059386996
|
||||
2019-08-12T17:00:00,11424.571072048611,0.6197593405228927,0.3802406594771073
|
||||
2019-08-12T17:20:00,11426.198676215277,0.6763167812618478,0.32368321873815226
|
||||
2019-08-12T17:40:00,11406.51796875,0.6286722346068885,0.3713277653931115
|
||||
2019-08-12T18:00:00,11404.352322048611,0.5115319976972041,0.48846800230279586
|
||||
2019-08-12T18:20:00,11398.163519965277,0.6257554813739197,0.3742445186260802
|
||||
2019-08-12T18:40:00,11413.278971354166,0.6378656952486648,0.3621343047513351
|
||||
2019-08-12T19:00:00,11421.8134765625,0.5973654004046227,0.4026345995953772
|
||||
2019-08-12T19:20:00,11426.74892578125,0.6115391612886458,0.3884608387113542
|
||||
2019-08-12T19:40:00,11423.771158854166,0.6031121766413208,0.3968878233586792
|
||||
2019-08-12T20:00:00,11452.396809895834,0.5385366074687161,0.4614633925312838
|
||||
2019-08-12T20:20:00,11449.376736111111,0.4542962781325796,0.5457037218674204
|
||||
2019-08-12T20:40:00,11440.247829861111,0.4990482904635068,0.5009517095364933
|
||||
2019-08-12T21:00:00,11442.9650390625,0.6153896418548124,0.3846103581451876
|
||||
2019-08-12T21:20:00,11424.958767361111,0.5218757491422692,0.4781242508577307
|
||||
2019-08-12T21:40:00,11403.863606770834,0.6258322688382039,0.374167731161796
|
||||
2019-08-12T22:00:00,11389.853298611111,0.6385406263433715,0.36145937365662845
|
||||
2019-08-12T22:20:00,11420.267903645834,0.3822032629780549,0.617796737021945
|
||||
2019-08-12T22:40:00,11421.151953125,0.6706924267223235,0.3293075732776765
|
||||
2019-08-12T23:00:00,11405.87890625,0.5321807162637315,0.4678192837362684
|
||||
2019-08-12T23:20:00,11394.993489583334,0.6515322129983447,0.34846778700165526
|
||||
2019-08-12T23:40:00,11396.562174479166,0.6702444184949312,0.3297555815050688
|
||||
2019-08-13T00:00:00,11386.675564236111,0.6376087190686965,0.36239128093130335
|
||||
2019-08-13T00:20:00,11390.31396484375,0.656433236317971,0.3435667636820291
|
||||
2019-08-13T00:40:00,11418.509006076389,0.6300882756647801,0.3699117243352198
|
||||
2019-08-13T01:00:00,11425.298719618055,0.5936846794796631,0.4063153205203369
|
||||
2019-08-13T01:20:00,11417.285373263889,0.6344587910902493,0.3655412089097507
|
||||
2019-08-13T01:40:00,11414.825629340277,0.5885973876024824,0.41140261239751774
|
||||
2019-08-13T02:00:00,11415.9611328125,0.6232328912938021,0.3767671087061978
|
||||
2019-08-13T02:20:00,11414.3388671875,0.5080158921362159,0.491984107863784
|
||||
2019-08-13T02:40:00,11397.362196180555,0.6054939302573688,0.39450606974263125
|
||||
2019-08-13T03:00:00,11381.907660590277,0.5981995907128752,0.4018004092871248
|
||||
2019-08-13T03:20:00,11381.373372395834,0.6041410039449,0.39585899605509994
|
||||
2019-08-13T03:40:00,11381.59794921875,0.6227639641432441,0.3772360358567557
|
||||
2019-08-13T04:00:00,11386.341037326389,0.5673000788531883,0.43269992114681166
|
||||
2019-08-13T04:20:00,11372.684353298611,0.6154754580024837,0.3845245419975163
|
||||
2019-08-13T04:40:00,11361.396484375,0.6555001794226949,0.3444998205773051
|
||||
2019-08-13T05:00:00,11363.478841145834,0.6170534964633667,0.3829465035366333
|
||||
2019-08-13T05:20:00,11369.023220486111,0.5584422192899314,0.44155778071006857
|
||||
2019-08-13T05:40:00,11380.88798828125,0.66378368455584,0.3362163154441599
|
||||
2019-08-13T06:00:00,11381.498806423611,0.6208091778422817,0.3791908221577183
|
||||
2019-08-13T06:20:00,11375.743272569445,0.6362667243295514,0.3637332756704484
|
||||
2019-08-13T06:40:00,11368.918836805555,0.6088606977056898,0.39113930229431026
|
||||
2019-08-13T07:00:00,11360.1048828125,0.5781304175876052,0.42186958241239486
|
||||
2019-08-13T07:20:00,11349.404513888889,0.5725347705496153,0.4274652294503847
|
||||
2019-08-13T07:40:00,11372.202256944445,0.6690789812687885,0.3309210187312115
|
||||
2019-08-13T08:00:00,11349.807725694445,0.5506707559522843,0.44932924404771557
|
||||
2019-08-13T08:20:00,11334.747938368055,0.5475636768966059,0.452436323103394
|
||||
2019-08-13T08:40:00,11339.99189453125,0.5888062805659351,0.4111937194340649
|
||||
2019-08-13T09:00:00,11327.682183159723,0.6284692330424246,0.3715307669575753
|
||||
2019-08-13T09:20:00,11299.879014756945,0.6480465537434921,0.3519534462565079
|
||||
2019-08-13T09:40:00,11292.828993055555,0.6433289130369744,0.35667108696302563
|
||||
2019-08-13T10:00:00,11295.584309895834,0.6116558223862982,0.3883441776137017
|
||||
2019-08-13T10:20:00,11259.08193359375,0.6343773803276949,0.365622619672305
|
||||
2019-08-13T10:40:00,11264.911241319445,0.6045441092614918,0.3954558907385082
|
||||
2019-08-13T11:00:00,11271.285481770834,0.5917488903795041,0.4082511096204959
|
||||
2019-08-13T11:20:00,11291.047743055555,0.6270154308360245,0.3729845691639755
|
||||
2019-08-13T11:40:00,11285.030056423611,0.6614001471975195,0.3385998528024806
|
||||
2019-08-13T12:00:00,11267.033984375,0.5997020296936002,0.40029797030639985
|
||||
2019-08-13T12:20:00,11288.711046006945,0.6217492788226657,0.37825072117733416
|
||||
2019-08-13T12:40:00,11274.071180555555,0.5852572858745833,0.4147427141254166
|
||||
2019-08-13T13:00:00,11244.406792534723,0.5823822205562896,0.41761777944371037
|
||||
2019-08-13T13:20:00,11205.04287109375,0.6250742403126983,0.3749257596873016
|
||||
2019-08-13T13:40:00,11185.309027777777,0.5697763800243181,0.43022361997568204
|
||||
2019-08-13T14:00:00,11089.408962673611,0.5875652613700966,0.4124347386299035
|
||||
2019-08-13T14:20:00,11013.327690972223,0.650181183560554,0.349818816439446
|
||||
2019-08-13T14:40:00,11004.953342013889,0.5419771494825585,0.4580228505174415
|
||||
2019-08-13T15:00:00,10989.151953125,0.5910518061020651,0.408948193897935
|
||||
2019-08-13T15:20:00,10985.026692708334,0.6630092475208966,0.3369907524791034
|
||||
2019-08-13T15:40:00,10960.876736111111,0.6704077571858567,0.3295922428141435
|
||||
2019-08-13T16:00:00,10975.355577256945,0.6596598515532603,0.34034014844673977
|
||||
2019-08-13T16:20:00,10956.637912326389,0.6592742279339825,0.3407257720660174
|
||||
2019-08-13T16:40:00,10892.023328993055,0.633758612209417,0.36624138779058313
|
||||
2019-08-13T17:00:00,10849.48798828125,0.46963732733007474,0.5303626726699252
|
||||
2019-08-13T17:20:00,10899.003363715277,0.4638991110236021,0.536100888976398
|
||||
2019-08-13T17:40:00,10901.8955078125,0.5231334403701868,0.4768665596298132
|
||||
2019-08-13T18:00:00,10896.616753472223,0.5060679081774209,0.49393209182257897
|
||||
2019-08-13T18:20:00,10919.703450520834,0.6023811846973985,0.3976188153026014
|
||||
2019-08-13T18:40:00,10942.492296006945,0.6613861882805943,0.33861381171940585
|
||||
2019-08-13T19:00:00,10931.265625,0.6583488260660886,0.3416511739339113
|
||||
2019-08-13T19:20:00,10938.12509765625,0.6777627073334098,0.3222372926665902
|
||||
2019-08-13T19:40:00,10926.246744791666,0.6670229261643958,0.33297707383560404
|
||||
2019-08-13T20:00:00,10957.206705729166,0.6592474272156832,0.3407525727843167
|
||||
2019-08-13T20:20:00,10956.663194444445,0.4701140271025393,0.5298859728974608
|
||||
2019-08-13T20:40:00,10942.324327256945,0.6088136690822006,0.3911863309177995
|
||||
2019-08-13T21:00:00,10896.081271701389,0.6526086259159338,0.3473913740840662
|
||||
2019-08-13T21:20:00,10878.505078125,0.5984884864141065,0.40151151358589343
|
||||
2019-08-13T21:40:00,10894.5244140625,0.5858468110353678,0.41415318896463216
|
||||
2019-08-13T22:00:00,10911.148871527777,0.6517440545616155,0.34825594543838445
|
||||
2019-08-13T22:20:00,10875.351236979166,0.6408948807456714,0.3591051192543285
|
||||
2019-08-13T22:40:00,10906.046657986111,0.6070251027841336,0.3929748972158663
|
||||
2019-08-13T23:00:00,10918.7451171875,0.6641092373104258,0.3358907626895741
|
||||
2019-08-13T23:20:00,10914.104383680555,0.6424595231161692,0.35754047688383084
|
||||
2019-08-13T23:40:00,10898.773328993055,0.6538924739421303,0.34610752605786965
|
||||
2019-08-14T00:00:00,10873.996636284723,0.6132870407444451,0.38671295925555504
|
||||
2019-08-14T00:20:00,10839.976671006945,0.679415714675889,0.3205842853241109
|
||||
2019-08-14T00:40:00,10811.24296875,0.6352162178387528,0.364783782161247
|
||||
2019-08-14T01:00:00,10787.704427083334,0.6667575705678144,0.3332424294321857
|
||||
2019-08-14T01:20:00,10669.369900173611,0.6772618370689663,0.3227381629310338
|
||||
2019-08-14T01:40:00,10626.171006944445,0.663499853984217,0.3365001460157831
|
||||
2019-08-14T02:00:00,10598.291015625,0.5839783775146445,0.4160216224853554
|
||||
2019-08-14T02:20:00,10641.641015625,0.4963626271331116,0.5036373728668885
|
||||
2019-08-14T02:40:00,10678.185763888889,0.6085323000086991,0.39146769999130077
|
||||
2019-08-14T03:00:00,10664.284505208334,0.5850178563335919,0.414982143666408
|
||||
2019-08-14T03:20:00,10652.567816840277,0.5786693343289557,0.42133066567104416
|
||||
2019-08-14T03:40:00,10633.665364583334,0.5445778667121564,0.45542213328784353
|
||||
2019-08-14T04:00:00,10648.1958984375,0.6380466950418269,0.3619533049581733
|
||||
2019-08-14T04:20:00,10624.257921006945,0.503431721914356,0.496568278085644
|
||||
2019-08-14T04:40:00,10603.869900173611,0.5316118327541259,0.46838816724587407
|
||||
2019-08-14T05:00:00,10573.132052951389,0.572482802175557,0.4275171978244431
|
||||
2019-08-14T05:20:00,10614.732313368055,0.6447251208433402,0.35527487915665973
|
||||
2019-08-14T05:40:00,10622.78212890625,0.5339827512839717,0.46601724871602834
|
||||
2019-08-14T06:00:00,10646.657769097223,0.6020114847659737,0.39798851523402645
|
||||
2019-08-14T06:20:00,10646.678819444445,0.5045179979003855,0.49548200209961446
|
||||
2019-08-14T06:40:00,10629.991102430555,0.6474735752909355,0.35252642470906453
|
||||
2019-08-14T07:00:00,10639.841145833334,0.5615304233768255,0.43846957662317443
|
||||
2019-08-14T07:20:00,10633.24521484375,0.5408644363829478,0.45913556361705227
|
||||
2019-08-14T07:40:00,10607.752170138889,0.5771468309385042,0.42285316906149584
|
||||
2019-08-14T08:00:00,10611.884440104166,0.5180131894689965,0.48198681053100334
|
||||
2019-08-14T08:20:00,10578.325629340277,0.6507972358307423,0.34920276416925766
|
||||
2019-08-14T08:40:00,10502.283311631945,0.40341333464791446,0.5965866653520855
|
||||
2019-08-14T09:00:00,10474.62294921875,0.5144603350389285,0.4855396649610715
|
||||
2019-08-14T09:20:00,10481.101236979166,0.6212257377445534,0.3787742622554465
|
||||
2019-08-14T09:40:00,10513.264431423611,0.3472157087280547,0.6527842912719453
|
||||
2019-08-14T10:00:00,10540.198784722223,0.6376548157062898,0.3623451842937102
|
||||
2019-08-14T10:20:00,10572.214409722223,0.6300339382826192,0.3699660617173808
|
||||
2019-08-14T10:40:00,10543.14501953125,0.5123722983833987,0.4876277016166012
|
||||
2019-08-14T11:00:00,10514.2333984375,0.5105536952733631,0.48944630472663686
|
||||
2019-08-14T11:20:00,10502.462239583334,0.6768515922736023,0.3231484077263977
|
||||
2019-08-14T11:40:00,10544.894422743055,0.6404339243363617,0.3595660756636382
|
||||
2019-08-14T12:00:00,10584.820963541666,0.5108466761339676,0.4891533238660325
|
||||
2019-08-14T12:20:00,10653.50693359375,0.41516060336877764,0.5848393966312222
|
||||
2019-08-14T12:40:00,10636.584309895834,0.52252995555138,0.47747004444862007
|
||||
2019-08-14T13:00:00,10630.9609375,0.5960856891153969,0.403914310884603
|
||||
2019-08-14T13:20:00,10636.311089409723,0.49714385228560004,0.5028561477144
|
||||
2019-08-14T13:40:00,10606.923285590277,0.549213639938779,0.45078636006122086
|
||||
2019-08-14T14:00:00,10555.8548828125,0.6202144785481931,0.37978552145180694
|
||||
2019-08-14T14:20:00,10529.615559895834,0.5541000645435712,0.4458999354564288
|
||||
2019-08-14T14:40:00,10502.307725694445,0.6038164189536659,0.39618358104633417
|
||||
2019-08-14T15:00:00,10532.066840277777,0.6324756224418648,0.3675243775581353
|
||||
2019-08-14T15:20:00,10515.2619140625,0.5939761793482323,0.4060238206517677
|
||||
2019-08-14T15:40:00,10513.206705729166,0.4425083712449534,0.5574916287550467
|
||||
|
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-05-23,182.500000,186.910004,182.179993,186.899994,186.899994,16628100
|
||||
2018-05-24,185.880005,186.800003,185.029999,185.929993,185.929993,12354700
|
||||
2018-05-25,186.020004,186.330002,184.449997,184.919998,184.919998,10965100
|
||||
2018-05-29,184.339996,186.809998,183.710007,185.740005,185.740005,16398900
|
||||
2018-05-30,186.539993,188.000000,185.250000,187.669998,187.669998,13736900
|
||||
2018-05-31,187.869995,192.720001,187.479996,191.779999,191.779999,30782600
|
||||
2018-06-01,193.070007,194.550003,192.070007,193.990005,193.990005,17307200
|
||||
2018-06-04,191.839996,193.979996,191.470001,193.279999,193.279999,18939800
|
||||
2018-06-05,194.300003,195.000000,192.619995,192.940002,192.940002,15544300
|
||||
2018-06-06,191.029999,192.529999,189.110001,191.339996,191.339996,22558900
|
||||
2018-06-07,190.750000,190.970001,186.770004,188.179993,188.179993,21503200
|
||||
2018-06-08,187.529999,189.479996,186.429993,189.100006,189.100006,12677100
|
||||
2018-06-11,188.809998,192.600006,188.800003,191.539993,191.539993,12928900
|
||||
2018-06-12,192.169998,193.279999,191.559998,192.399994,192.399994,11562700
|
||||
2018-06-13,192.740005,194.500000,191.910004,192.410004,192.410004,15853800
|
||||
2018-06-14,193.100006,197.279999,192.910004,196.809998,196.809998,19120900
|
||||
2018-06-15,195.789993,197.070007,194.639999,195.850006,195.850006,21860900
|
||||
2018-06-18,194.800003,199.580002,194.130005,198.309998,198.309998,16826000
|
||||
2018-06-19,196.240005,197.960007,193.789993,197.490005,197.490005,19994000
|
||||
2018-06-20,199.100006,203.550003,198.809998,202.000000,202.000000,28230900
|
||||
2018-06-21,202.759995,203.389999,200.089996,201.500000,201.500000,19045700
|
||||
2018-06-22,201.160004,202.240005,199.309998,201.740005,201.740005,17420200
|
||||
2018-06-25,200.000000,200.000000,193.110001,196.350006,196.350006,25275100
|
||||
2018-06-26,197.600006,199.100006,196.229996,199.000000,199.000000,17897600
|
||||
2018-06-27,199.179993,200.750000,195.800003,195.839996,195.839996,18734400
|
||||
2018-06-28,195.179993,197.339996,193.259995,196.229996,196.229996,18172400
|
||||
2018-06-29,197.320007,197.600006,193.960007,194.320007,194.320007,15811600
|
||||
2018-07-02,193.369995,197.449997,192.220001,197.360001,197.360001,13961600
|
||||
2018-07-03,194.550003,195.399994,192.520004,192.729996,192.729996,13489500
|
||||
2018-07-05,194.740005,198.649994,194.029999,198.449997,198.449997,19684200
|
||||
2018-07-06,198.449997,203.639999,197.699997,203.229996,203.229996,19740100
|
||||
2018-07-09,204.929993,205.800003,202.119995,204.740005,204.740005,18149400
|
||||
2018-07-10,204.500000,204.910004,202.259995,203.539993,203.539993,13190100
|
||||
2018-07-11,202.220001,204.500000,201.750000,202.539993,202.539993,12927400
|
||||
2018-07-12,203.429993,207.080002,203.190002,206.919998,206.919998,15454700
|
||||
2018-07-13,207.809998,208.429993,206.449997,207.320007,207.320007,11486800
|
||||
2018-07-16,207.500000,208.720001,206.839996,207.229996,207.229996,11078200
|
||||
2018-07-17,204.899994,210.460007,204.839996,209.990005,209.990005,15349900
|
||||
2018-07-18,209.820007,210.990005,208.440002,209.360001,209.360001,15334900
|
||||
2018-07-19,208.770004,209.990005,207.759995,208.089996,208.089996,11350400
|
||||
2018-07-20,208.850006,211.500000,208.500000,209.940002,209.940002,16163900
|
||||
2018-07-23,210.580002,211.619995,208.800003,210.910004,210.910004,16732000
|
||||
2018-07-24,215.110001,216.199997,212.600006,214.669998,214.669998,28468700
|
||||
2018-07-25,215.720001,218.619995,214.270004,217.500000,217.500000,58954200
|
||||
2018-07-26,174.889999,180.130005,173.750000,176.259995,176.259995,169803700
|
||||
2018-07-27,179.869995,179.929993,173.000000,174.889999,174.889999,60073700
|
||||
2018-07-30,175.300003,175.300003,166.559998,171.059998,171.059998,65280800
|
||||
2018-07-31,170.669998,174.240005,170.000000,172.580002,172.580002,40356500
|
||||
2018-08-01,173.929993,175.080002,170.899994,171.649994,171.649994,34042100
|
||||
2018-08-02,170.679993,176.789993,170.270004,176.369995,176.369995,32400000
|
||||
2018-08-03,177.690002,178.850006,176.149994,177.779999,177.779999,24763400
|
||||
2018-08-06,178.970001,185.789993,178.380005,185.690002,185.690002,49716200
|
||||
2018-08-07,186.500000,188.300003,183.720001,183.809998,183.809998,33398600
|
||||
2018-08-08,184.750000,186.850006,183.759995,185.179993,185.179993,22205200
|
||||
2018-08-09,185.850006,186.570007,182.479996,183.089996,183.089996,19696700
|
||||
2018-08-10,182.039993,182.100006,179.419998,180.259995,180.259995,21500400
|
||||
2018-08-13,180.100006,182.610001,178.899994,180.050003,180.050003,17369400
|
||||
2018-08-14,180.710007,181.990005,178.619995,181.110001,181.110001,19102000
|
||||
2018-08-15,179.339996,180.869995,174.779999,179.529999,179.529999,33020200
|
||||
2018-08-16,180.419998,180.500000,174.009995,174.699997,174.699997,31351800
|
||||
2018-08-17,174.500000,176.220001,172.039993,173.800003,173.800003,24893200
|
||||
2018-08-20,174.039993,174.570007,170.910004,172.500000,172.500000,21518000
|
||||
2018-08-21,172.809998,174.169998,171.389999,172.619995,172.619995,19578500
|
||||
2018-08-22,172.210007,174.240005,172.130005,173.639999,173.639999,16894100
|
||||
2018-08-23,173.089996,175.550003,172.830002,172.899994,172.899994,18053600
|
||||
2018-08-24,173.699997,174.820007,172.919998,174.649994,174.649994,14631600
|
||||
2018-08-27,175.990005,178.669998,175.789993,177.460007,177.460007,17921900
|
||||
2018-08-28,178.100006,178.240005,175.830002,176.259995,176.259995,15910700
|
||||
2018-08-29,176.300003,176.789993,174.750000,175.899994,175.899994,18494100
|
||||
2018-08-30,175.899994,179.789993,175.699997,177.639999,177.639999,24216500
|
||||
2018-08-31,177.149994,177.619995,174.979996,175.729996,175.729996,18065200
|
||||
2018-09-04,173.500000,173.889999,168.800003,171.160004,171.160004,29809000
|
||||
2018-09-05,169.490005,171.130005,166.669998,167.179993,167.179993,31226700
|
||||
2018-09-06,166.979996,166.979996,160.000000,162.529999,162.529999,41514800
|
||||
2018-09-07,160.309998,164.630005,160.160004,163.039993,163.039993,24300600
|
||||
2018-09-10,163.509995,165.009995,162.160004,164.179993,164.179993,20197700
|
||||
2018-09-11,163.940002,167.190002,163.720001,165.940002,165.940002,20457100
|
||||
2018-09-12,163.250000,164.490005,161.800003,162.000000,162.000000,24078100
|
||||
2018-09-13,162.000000,163.320007,160.860001,161.360001,161.360001,25453800
|
||||
2018-09-14,161.720001,162.839996,160.339996,162.320007,162.320007,21770400
|
||||
2018-09-17,161.919998,162.059998,159.770004,160.580002,160.580002,21005300
|
||||
2018-09-18,159.389999,161.759995,158.869995,160.300003,160.300003,22465200
|
||||
2018-09-19,160.080002,163.440002,159.479996,163.059998,163.059998,19629000
|
||||
2018-09-20,164.500000,166.449997,164.470001,166.020004,166.020004,18824200
|
||||
2018-09-21,166.639999,167.250000,162.809998,162.929993,162.929993,45994800
|
||||
2018-09-24,161.029999,165.699997,160.880005,165.410004,165.410004,19222800
|
||||
2018-09-25,161.990005,165.589996,161.149994,164.910004,164.910004,27622800
|
||||
2018-09-26,164.300003,169.300003,164.210007,166.949997,166.949997,25252200
|
||||
2018-09-27,167.550003,171.770004,167.210007,168.839996,168.839996,27266900
|
||||
2018-09-28,168.330002,168.789993,162.559998,164.460007,164.460007,34265600
|
||||
2018-10-01,163.029999,165.880005,161.259995,162.440002,162.440002,26407700
|
||||
2018-10-02,161.580002,162.279999,158.669998,159.330002,159.330002,36031000
|
||||
2018-10-03,160.000000,163.660004,159.529999,162.429993,162.429993,23109500
|
||||
2018-10-04,161.460007,161.460007,157.350006,158.850006,158.850006,25739600
|
||||
2018-10-05,159.210007,160.899994,156.199997,157.330002,157.330002,25744000
|
||||
2018-10-08,155.539993,158.339996,154.389999,157.250000,157.250000,24046000
|
||||
2018-10-09,157.690002,160.589996,157.419998,157.899994,157.899994,18844400
|
||||
2018-10-10,156.820007,157.690002,151.309998,151.380005,151.380005,30610000
|
||||
2018-10-11,150.130005,154.809998,149.160004,153.350006,153.350006,35338900
|
||||
2018-10-12,156.729996,156.889999,151.300003,153.740005,153.740005,25293500
|
||||
2018-10-15,153.320007,155.570007,152.550003,153.520004,153.520004,15433500
|
||||
2018-10-16,155.399994,159.460007,155.009995,158.779999,158.779999,19180100
|
||||
2018-10-17,159.559998,160.490005,157.949997,159.419998,159.419998,17592000
|
||||
2018-10-18,158.509995,158.660004,153.279999,154.919998,154.919998,21675100
|
||||
2018-10-19,155.860001,157.350006,153.550003,154.050003,154.050003,19761300
|
||||
2018-10-22,154.759995,157.339996,154.460007,154.779999,154.779999,15424700
|
||||
2018-10-23,151.220001,154.770004,150.850006,154.389999,154.389999,19095000
|
||||
2018-10-24,154.279999,154.649994,145.600006,146.039993,146.039993,27744600
|
||||
2018-10-25,147.729996,152.210007,147.000000,150.949997,150.949997,22105700
|
||||
2018-10-26,145.820007,149.000000,143.800003,145.369995,145.369995,31303300
|
||||
2018-10-29,148.500000,148.830002,139.029999,142.089996,142.089996,31336800
|
||||
2018-10-30,139.940002,146.639999,139.740005,146.220001,146.220001,50528300
|
||||
2018-10-31,155.000000,156.399994,148.960007,151.789993,151.789993,60101300
|
||||
2018-11-01,151.520004,152.750000,149.350006,151.750000,151.750000,25640800
|
||||
2018-11-02,151.800003,154.130005,148.960007,150.350006,150.350006,24708700
|
||||
2018-11-05,150.100006,150.190002,147.440002,148.679993,148.679993,15971200
|
||||
2018-11-06,149.309998,150.970001,148.000000,149.940002,149.940002,16667100
|
||||
2018-11-07,151.570007,153.009995,149.830002,151.529999,151.529999,21877400
|
||||
2018-11-08,150.490005,150.940002,146.740005,147.869995,147.869995,24145800
|
||||
2018-11-09,146.750000,147.759995,144.070007,144.960007,144.960007,17326900
|
||||
2018-11-12,144.479996,145.039993,140.490005,141.550003,141.550003,18542100
|
||||
2018-11-13,142.000000,144.880005,141.619995,142.160004,142.160004,15141700
|
||||
2018-11-14,143.699997,145.580002,141.550003,144.220001,144.220001,22068400
|
||||
2018-11-15,142.330002,144.839996,140.830002,143.850006,143.850006,30320300
|
||||
2018-11-16,141.070007,141.770004,137.770004,139.529999,139.529999,37250600
|
||||
2018-11-19,137.610001,137.750000,131.210007,131.550003,131.550003,44362700
|
||||
2018-11-20,127.029999,134.160004,126.849998,132.429993,132.429993,41939500
|
||||
2018-11-21,134.399994,137.190002,134.130005,134.820007,134.820007,25469700
|
||||
2018-11-23,133.649994,134.500000,131.259995,131.729996,131.729996,11886100
|
||||
2018-11-26,133.000000,137.000000,132.779999,136.380005,136.380005,24263600
|
||||
2018-11-27,135.750000,136.610001,133.710007,135.000000,135.000000,20750300
|
||||
2018-11-28,136.279999,136.789993,131.850006,136.759995,136.759995,29847500
|
||||
2018-11-29,135.919998,139.990005,135.660004,138.679993,138.679993,24238700
|
||||
2018-11-30,138.259995,140.970001,137.360001,140.610001,140.610001,25732600
|
||||
2018-12-03,143.000000,143.679993,140.759995,141.089996,141.089996,24819200
|
||||
2018-12-04,140.729996,143.389999,137.160004,137.929993,137.929993,30307400
|
||||
2018-12-06,133.820007,139.699997,133.669998,139.630005,139.630005,28218100
|
||||
2018-12-07,139.250000,140.869995,136.660004,137.419998,137.419998,21195500
|
||||
2018-12-10,139.600006,143.050003,139.009995,141.850006,141.850006,26422200
|
||||
2018-12-11,143.880005,143.880005,141.100006,142.080002,142.080002,20300300
|
||||
2018-12-12,143.080002,147.190002,142.509995,144.500000,144.500000,23696900
|
||||
2018-12-13,145.570007,145.850006,143.190002,145.009995,145.009995,18148600
|
||||
2018-12-14,143.339996,146.009995,142.509995,144.059998,144.059998,21785800
|
||||
2018-12-17,143.080002,144.919998,138.419998,140.190002,140.190002,24334000
|
||||
2018-12-18,141.080002,145.929993,139.830002,143.660004,143.660004,24709100
|
||||
2018-12-19,141.210007,144.910004,132.500000,133.240005,133.240005,57404900
|
||||
2018-12-20,130.699997,135.570007,130.000000,133.399994,133.399994,40297900
|
||||
2018-12-21,133.389999,134.899994,123.419998,124.949997,124.949997,56901500
|
||||
2018-12-24,123.099998,129.740005,123.019997,124.059998,124.059998,22066000
|
||||
2018-12-26,126.000000,134.240005,125.889999,134.179993,134.179993,39723400
|
||||
2018-12-27,132.440002,134.990005,129.669998,134.520004,134.520004,31202500
|
||||
2018-12-28,135.339996,135.919998,132.199997,133.199997,133.199997,22627600
|
||||
2018-12-31,134.449997,134.639999,129.949997,131.089996,131.089996,24625300
|
||||
2019-01-02,128.990005,137.509995,128.559998,135.679993,135.679993,28146200
|
||||
2019-01-03,134.690002,137.169998,131.119995,131.740005,131.740005,22700800
|
||||
2019-01-04,134.009995,138.000000,133.750000,137.949997,137.949997,29002100
|
||||
2019-01-07,137.559998,138.869995,135.910004,138.050003,138.050003,20089300
|
||||
2019-01-08,139.889999,143.139999,139.539993,142.529999,142.529999,26263800
|
||||
2019-01-09,142.949997,144.699997,141.270004,144.229996,144.229996,22205900
|
||||
2019-01-10,143.080002,144.559998,140.839996,144.199997,144.199997,16125000
|
||||
2019-01-11,143.149994,145.360001,142.570007,143.800003,143.800003,12908000
|
||||
2019-01-14,142.000000,146.570007,141.270004,145.389999,145.389999,20520300
|
||||
2019-01-15,146.009995,150.679993,145.990005,148.949997,148.949997,24069000
|
||||
2019-01-16,149.000000,149.649994,147.000000,147.539993,147.539993,18025700
|
||||
2019-01-17,146.949997,149.000000,146.500000,148.300003,148.300003,15787900
|
||||
2019-01-18,149.750000,152.429993,148.550003,150.039993,150.039993,31029600
|
||||
2019-01-22,149.199997,151.529999,146.369995,147.570007,147.570007,22378700
|
||||
2019-01-23,148.279999,148.800003,143.059998,144.300003,144.300003,20098400
|
||||
2019-01-24,144.639999,146.440002,142.520004,145.830002,145.830002,20955500
|
||||
2019-01-25,147.479996,149.830002,146.539993,149.009995,149.009995,22237200
|
||||
2019-01-28,148.050003,148.960007,146.210007,147.470001,147.470001,15508500
|
||||
2019-01-29,148.089996,148.100006,143.429993,144.190002,144.190002,17632100
|
||||
2019-01-30,146.220001,150.949997,145.699997,150.419998,150.419998,44613200
|
||||
2019-01-31,165.600006,171.679993,165.000000,166.690002,166.690002,77233600
|
||||
2019-02-01,165.839996,169.100006,165.660004,165.710007,165.710007,30806500
|
||||
2019-02-04,165.699997,169.300003,163.619995,169.250000,169.250000,20036000
|
||||
2019-02-05,169.149994,171.979996,168.690002,171.160004,171.160004,22557000
|
||||
2019-02-06,171.199997,172.470001,169.270004,170.490005,170.490005,13281200
|
||||
2019-02-07,168.199997,169.240005,165.250000,166.380005,166.380005,17517600
|
||||
2019-02-08,164.470001,167.369995,164.210007,167.330002,167.330002,12561400
|
||||
2019-02-11,167.899994,168.300003,165.080002,165.789993,165.789993,12811200
|
||||
2019-02-12,166.860001,168.339996,164.500000,165.039993,165.039993,16292300
|
||||
2019-02-13,165.380005,166.220001,163.729996,164.070007,164.070007,14205100
|
||||
2019-02-14,163.190002,164.869995,162.250000,163.949997,163.949997,12755200
|
||||
2019-02-15,164.509995,164.699997,160.860001,162.500000,162.500000,15504400
|
||||
2019-02-19,160.500000,164.149994,160.330002,162.289993,162.289993,14345400
|
||||
2019-02-20,162.250000,163.720001,161.250000,162.559998,162.559998,11770700
|
||||
2019-02-21,161.929993,162.240005,159.589996,160.039993,160.039993,15607800
|
||||
2019-02-22,160.580002,162.410004,160.309998,161.889999,161.889999,15858500
|
||||
2019-02-25,163.070007,166.070007,162.899994,164.619995,164.619995,18737100
|
||||
2019-02-26,164.339996,166.240005,163.800003,164.130005,164.130005,13784100
|
||||
2019-02-27,162.899994,163.929993,160.410004,162.809998,162.809998,12697500
|
||||
2019-02-28,162.369995,163.500000,160.860001,161.449997,161.449997,11114200
|
||||
2019-03-01,162.600006,163.130005,161.690002,162.279999,162.279999,11097800
|
||||
2019-03-04,163.899994,167.500000,163.830002,167.369995,167.369995,18894700
|
||||
2019-03-05,167.369995,171.880005,166.550003,171.259995,171.259995,28187900
|
||||
2019-03-06,172.899994,173.570007,171.270004,172.509995,172.509995,21531700
|
||||
2019-03-07,171.500000,171.740005,167.610001,169.130005,169.130005,18205400
|
||||
2019-03-08,166.199997,169.619995,165.970001,169.600006,169.600006,13184800
|
||||
2019-03-11,171.600006,174.300003,171.580002,172.070007,172.070007,18884000
|
||||
2019-03-12,172.089996,173.800003,171.220001,171.919998,171.919998,12155300
|
||||
2019-03-13,172.320007,174.029999,172.119995,173.369995,173.369995,11973300
|
||||
2019-03-14,169.759995,171.149994,168.160004,170.169998,170.169998,18037400
|
||||
2019-03-15,167.160004,167.580002,162.509995,165.979996,165.979996,37135400
|
||||
2019-03-18,163.570007,163.899994,159.279999,160.470001,160.470001,37524200
|
||||
2019-03-19,161.479996,163.820007,160.820007,161.570007,161.570007,25611500
|
||||
2019-03-20,161.500000,166.119995,161.240005,165.440002,165.440002,20211500
|
||||
2019-03-21,164.889999,166.389999,163.750000,166.080002,166.080002,16223000
|
||||
2019-03-22,165.649994,167.419998,164.089996,164.339996,164.339996,16389200
|
||||
2019-03-25,163.000000,166.539993,162.000000,166.289993,166.289993,12631200
|
||||
2019-03-26,167.350006,169.449997,166.350006,167.679993,167.679993,15437900
|
||||
2019-03-27,167.850006,168.940002,164.789993,165.869995,165.869995,10620300
|
||||
2019-03-28,164.570007,166.720001,163.330002,165.550003,165.550003,10689200
|
||||
2019-03-29,166.389999,167.190002,164.809998,166.690002,166.690002,13455500
|
||||
2019-04-01,167.830002,168.899994,167.279999,168.699997,168.699997,10381500
|
||||
2019-04-02,170.139999,174.899994,169.550003,174.199997,174.199997,23946500
|
||||
2019-04-03,174.500000,177.960007,172.949997,173.539993,173.539993,27590100
|
||||
2019-04-04,176.020004,178.000000,175.529999,176.020004,176.020004,17847700
|
||||
2019-04-05,176.880005,177.000000,175.100006,175.720001,175.720001,9594100
|
||||
2019-04-08,175.210007,175.500000,174.229996,174.929993,174.929993,7297400
|
||||
2019-04-09,175.619995,179.190002,175.550003,177.580002,177.580002,19751000
|
||||
2019-04-10,178.179993,178.789993,176.539993,177.820007,177.820007,11701500
|
||||
2019-04-11,178.240005,178.399994,177.000000,177.509995,177.509995,8071000
|
||||
2019-04-12,178.000000,179.630005,177.949997,179.100006,179.100006,12329800
|
||||
2019-04-15,178.500000,180.500000,176.869995,179.649994,179.649994,10834800
|
||||
2019-04-16,179.000000,180.169998,178.300003,178.869995,178.869995,11215200
|
||||
2019-04-17,179.600006,180.740005,178.360001,178.779999,178.779999,9973700
|
||||
2019-04-18,178.800003,178.880005,177.339996,178.279999,178.279999,11655600
|
||||
2019-04-22,178.250000,181.669998,178.250000,181.440002,181.440002,13389900
|
||||
2019-04-23,182.740005,184.220001,181.479996,183.779999,183.779999,19954800
|
||||
2019-04-24,184.490005,185.139999,181.649994,182.580002,182.580002,37289900
|
||||
2019-04-25,196.979996,198.479996,192.119995,193.259995,193.259995,54148800
|
||||
2019-04-26,192.500000,192.899994,189.089996,191.490005,191.490005,22075000
|
||||
2019-04-29,190.949997,195.410004,190.649994,194.779999,194.779999,19641300
|
||||
2019-04-30,194.190002,197.389999,192.279999,193.399994,193.399994,23494700
|
||||
2019-05-01,194.779999,196.179993,193.009995,193.029999,193.029999,15996600
|
||||
2019-05-02,193.000000,194.000000,189.750000,192.529999,192.529999,13209500
|
||||
2019-05-03,194.380005,196.160004,193.710007,195.470001,195.470001,14575400
|
||||
2019-05-06,191.240005,194.279999,190.550003,193.880005,193.880005,13994900
|
||||
2019-05-07,192.539993,192.899994,187.850006,189.770004,189.770004,16253000
|
||||
2019-05-08,189.389999,190.720001,188.550003,189.539993,189.539993,12505700
|
||||
2019-05-09,187.199997,189.770004,186.259995,188.649994,188.649994,12967000
|
||||
2019-05-10,188.250000,190.000000,184.589996,188.339996,188.339996,12578500
|
||||
2019-05-13,183.500000,185.429993,180.839996,181.539993,181.539993,16833300
|
||||
2019-05-14,182.520004,183.490005,178.100006,180.729996,180.729996,17628100
|
||||
2019-05-15,180.419998,187.279999,180.020004,186.270004,186.270004,16746900
|
||||
2019-05-16,185.050003,188.580002,185.050003,186.990005,186.990005,12953100
|
||||
2019-05-17,184.839996,187.580002,184.279999,185.300003,185.300003,10485400
|
||||
2019-05-20,181.880005,184.229996,181.369995,182.720001,182.720001,10352000
|
||||
2019-05-21,184.570007,185.699997,183.889999,184.820007,184.820007,7502800
|
||||
2019-05-22,184.729996,186.740005,183.610001,185.320007,185.320007,9203300
|
||||
2019-05-23,182.419998,183.899994,179.669998,180.460007,180.460007,10396877
|
||||
|
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-05-23,71.050003,71.910004,71.050003,71.620003,71.106422,13000
|
||||
2018-05-24,71.870003,71.919998,71.300003,71.449997,70.937630,28000
|
||||
2018-05-25,71.760002,71.889999,71.528000,71.879997,71.364548,11900
|
||||
2018-05-29,71.510002,71.510002,70.610001,70.800003,70.292305,17400
|
||||
2018-05-30,71.419998,71.419998,70.480003,70.720001,70.212875,48300
|
||||
2018-05-31,70.730003,70.860001,70.300003,70.389999,69.885231,13700
|
||||
2018-06-01,70.489998,70.730003,70.050003,70.430000,69.924950,17700
|
||||
2018-06-04,70.500000,70.739998,70.459999,70.639999,70.133446,8400
|
||||
2018-06-05,70.919998,71.489998,70.629997,70.980003,70.471008,10800
|
||||
2018-06-06,71.120003,72.169998,71.120003,71.790001,71.275200,12000
|
||||
2018-06-07,72.250000,72.250000,71.662003,72.120003,71.602829,14900
|
||||
2018-06-08,72.349998,72.519997,71.955002,72.419998,71.900681,11200
|
||||
2018-06-11,72.620003,73.610001,72.269997,73.449997,72.923286,12500
|
||||
2018-06-12,73.323997,73.680000,72.949997,73.510002,72.982864,11300
|
||||
2018-06-13,74.290001,74.290001,73.550003,74.160004,73.628197,36400
|
||||
2018-06-14,73.919998,74.709999,73.629997,74.250000,73.717560,11100
|
||||
2018-06-15,74.370003,74.470001,73.690002,73.739998,73.211212,13000
|
||||
2018-06-18,73.440002,74.098999,73.080002,73.980003,73.449493,10000
|
||||
2018-06-19,73.220001,73.660004,73.089996,73.309998,72.784302,13000
|
||||
2018-06-20,72.930000,74.250000,72.930000,74.089996,73.558701,29200
|
||||
2018-06-21,73.739998,74.349998,73.739998,74.099998,73.568634,16900
|
||||
2018-06-22,73.720001,74.599998,73.720001,74.269997,73.737419,15300
|
||||
2018-06-25,73.879997,74.220001,73.815002,74.029999,73.499138,13500
|
||||
2018-06-26,73.540001,74.540001,73.540001,74.339996,73.806908,20500
|
||||
2018-06-27,73.959999,74.389999,73.650002,73.849998,73.320427,13900
|
||||
2018-06-28,74.629997,75.300003,74.379997,75.059998,74.658226,11700
|
||||
2018-06-29,75.050003,76.320000,75.050003,76.040001,75.632980,25700
|
||||
2018-07-02,75.000000,77.930000,75.000000,77.930000,77.512863,10500
|
||||
2018-07-03,77.129997,77.849998,75.830002,76.160004,75.752335,80400
|
||||
2018-07-05,76.129997,77.620003,75.959999,77.050003,76.637581,48400
|
||||
2018-07-06,77.680000,77.970001,77.300003,77.559998,77.144836,22800
|
||||
2018-07-09,77.550003,78.730003,77.462997,78.370003,77.950508,53800
|
||||
2018-07-10,78.665001,79.269997,78.260002,78.309998,77.890823,67100
|
||||
2018-07-11,78.599998,78.980003,78.110001,78.629997,78.209114,67700
|
||||
2018-07-12,78.809998,80.790001,78.489998,80.680000,80.248138,74400
|
||||
2018-07-13,80.570000,80.570000,78.559998,78.820000,78.398102,20300
|
||||
2018-07-16,78.849998,78.849998,77.959000,78.250000,77.831154,16000
|
||||
2018-07-17,78.349998,78.629997,77.800003,77.940002,77.522812,21700
|
||||
2018-07-18,77.290001,78.099998,77.279999,77.940002,77.522812,15200
|
||||
2018-07-19,77.870003,78.690002,77.830002,78.055000,77.637192,13500
|
||||
2018-07-20,78.070000,79.209999,77.639999,78.959999,78.537346,17800
|
||||
2018-07-23,78.949997,78.949997,78.044998,78.519997,78.099701,10700
|
||||
2018-07-24,78.550003,78.559998,77.790001,78.480003,78.059921,21600
|
||||
2018-07-25,79.190002,85.269997,79.150002,83.959999,83.510582,44800
|
||||
2018-07-26,84.949997,86.050003,83.250000,85.099998,84.644478,22700
|
||||
2018-07-27,85.320000,85.320000,83.010002,84.050003,83.600105,20200
|
||||
2018-07-30,83.040001,83.430000,81.940002,82.269997,81.829620,20900
|
||||
2018-07-31,82.459999,83.620003,82.260002,83.180000,82.734756,23700
|
||||
2018-08-01,82.540001,83.739998,81.800003,82.889999,82.446312,17800
|
||||
2018-08-02,82.879997,83.190002,81.900002,83.120003,82.675087,43100
|
||||
2018-08-03,83.379997,83.489998,81.849998,82.190002,81.750053,16700
|
||||
2018-08-06,83.800003,83.800003,82.040001,83.010002,82.565674,7200
|
||||
2018-08-07,82.849998,82.849998,81.070000,81.330002,80.894661,16600
|
||||
2018-08-08,80.930000,82.809998,80.930000,82.570000,82.128021,18100
|
||||
2018-08-09,83.040001,83.139999,82.160004,82.209999,81.769951,18500
|
||||
2018-08-10,82.199997,83.160004,81.510002,82.440002,81.998726,42100
|
||||
2018-08-13,82.449997,84.389999,80.820000,81.720001,81.282578,17100
|
||||
2018-08-14,81.540001,82.790001,81.440002,82.269997,81.829620,13000
|
||||
2018-08-15,82.260002,82.389999,81.580002,82.139999,81.700325,16500
|
||||
2018-08-16,82.199997,82.980003,82.199997,82.480003,82.038513,30500
|
||||
2018-08-17,82.629997,86.440002,82.629997,85.370003,84.913040,67100
|
||||
2018-08-20,86.059998,86.059998,84.620003,85.000000,84.545013,25800
|
||||
2018-08-21,85.690002,86.129997,85.129997,86.040001,85.579445,16300
|
||||
2018-08-22,86.029999,86.305000,85.620003,86.059998,85.599342,20500
|
||||
2018-08-23,86.050003,86.224998,85.550003,85.629997,85.171638,18100
|
||||
2018-08-24,86.209999,86.889999,85.750000,86.489998,86.027039,25000
|
||||
2018-08-27,87.239998,87.989998,86.269997,87.720001,87.250458,31100
|
||||
2018-08-28,88.139999,88.430000,87.230003,87.540001,87.071419,24500
|
||||
2018-08-29,87.550003,90.205002,86.860001,87.290001,86.822762,32300
|
||||
2018-08-30,87.169998,87.309998,86.169998,86.430000,85.967361,15100
|
||||
2018-08-31,86.199997,86.199997,85.250000,85.860001,85.400414,23600
|
||||
2018-09-04,86.089996,86.089996,83.879997,85.750000,85.291008,28200
|
||||
2018-09-05,85.120003,86.010002,85.120003,85.680000,85.221382,20600
|
||||
2018-09-06,86.510002,87.190002,85.709999,87.190002,86.723305,17500
|
||||
2018-09-07,86.610001,87.540001,86.430000,86.690002,86.225975,7300
|
||||
2018-09-10,87.059998,87.565002,86.674004,87.279999,86.812813,22700
|
||||
2018-09-11,87.290001,87.440002,85.440002,85.940002,85.479988,18600
|
||||
2018-09-12,86.860001,86.860001,85.680000,86.250000,85.788330,24400
|
||||
2018-09-13,86.570000,86.739998,85.279999,85.570000,85.111969,14600
|
||||
2018-09-14,85.199997,86.070000,85.183998,85.400002,84.942871,17800
|
||||
2018-09-17,84.599998,85.769997,84.580002,85.430000,84.972717,9100
|
||||
2018-09-18,85.699997,86.620003,85.570000,86.169998,85.708755,21900
|
||||
2018-09-19,86.430000,86.430000,84.739998,84.790001,84.336143,17400
|
||||
2018-09-20,85.180000,85.910004,84.860001,85.139999,84.684265,10900
|
||||
2018-09-21,85.379997,85.379997,84.339996,84.889999,84.435608,16500
|
||||
2018-09-24,84.860001,85.199997,84.629997,84.690002,84.236679,19500
|
||||
2018-09-25,84.750000,85.000000,84.538002,84.900002,84.445549,32800
|
||||
2018-09-26,84.820000,84.970001,82.639999,83.120003,82.675087,25900
|
||||
2018-09-27,83.110001,83.698997,82.699997,83.589996,83.277817,21600
|
||||
2018-09-28,83.820000,85.260002,83.820000,84.660004,84.343834,38200
|
||||
2018-10-01,85.199997,86.080002,83.709999,84.150002,83.835732,20700
|
||||
2018-10-02,84.110001,84.120003,82.855003,83.139999,82.829498,15000
|
||||
2018-10-03,83.150002,83.550003,82.580002,83.000000,82.690025,15200
|
||||
2018-10-04,83.454002,83.454002,82.010002,82.769997,82.460884,23900
|
||||
2018-10-05,82.599998,83.250000,82.010002,82.470001,82.162010,12200
|
||||
2018-10-08,82.139999,82.139999,80.879997,80.879997,80.577942,15100
|
||||
2018-10-09,80.669998,81.720001,80.620003,80.839996,80.538086,56000
|
||||
2018-10-10,80.849998,80.849998,78.449997,78.449997,78.157021,31100
|
||||
2018-10-11,78.430000,79.389999,78.184998,78.620003,78.326385,21600
|
||||
2018-10-12,79.449997,79.860001,78.315002,78.680000,78.386162,16300
|
||||
2018-10-15,78.639999,79.089996,78.000000,78.320000,78.027504,20300
|
||||
2018-10-16,79.180000,80.230003,79.000000,79.830002,79.531868,88500
|
||||
2018-10-17,79.639999,80.080002,79.135002,79.589996,79.292755,98200
|
||||
2018-10-18,78.980003,80.349998,78.980003,79.629997,79.332603,32900
|
||||
2018-10-19,80.290001,81.949997,80.269997,81.430000,81.125893,23300
|
||||
2018-10-22,81.940002,82.510002,81.370003,81.559998,81.255402,27400
|
||||
2018-10-23,81.080002,81.080002,78.089996,78.470001,78.176941,24100
|
||||
2018-10-24,77.720001,78.739998,74.750000,75.820000,75.536835,78400
|
||||
2018-10-25,76.540001,76.540001,73.989998,74.440002,74.161995,30400
|
||||
2018-10-26,73.279999,73.639999,72.059998,72.330002,72.059875,33400
|
||||
2018-10-29,72.320000,72.779999,70.970001,71.190002,70.924133,31700
|
||||
2018-10-30,70.750000,71.669998,70.750000,71.500000,71.232971,29100
|
||||
2018-10-31,72.489998,74.010002,72.099998,73.400002,73.125885,91300
|
||||
2018-11-01,74.199997,75.339996,72.790001,75.010002,74.729866,133600
|
||||
2018-11-02,75.500000,75.760002,74.720001,75.339996,75.058632,138300
|
||||
2018-11-05,75.330002,75.660004,73.900002,74.300003,74.022522,47400
|
||||
2018-11-06,74.389999,74.889999,73.949997,74.699997,74.421021,22800
|
||||
2018-11-07,75.169998,75.360001,74.540001,74.970001,74.690018,38900
|
||||
2018-11-08,75.540001,75.570000,74.949997,75.260002,74.978935,11300
|
||||
2018-11-09,75.239998,75.239998,73.894997,74.500000,74.221764,17000
|
||||
2018-11-12,74.455002,74.629997,73.964996,74.349998,74.072327,81200
|
||||
2018-11-13,74.339996,75.290001,73.760002,74.320000,74.042442,278800
|
||||
2018-11-14,74.360001,74.900002,74.360001,74.540001,74.261620,30100
|
||||
2018-11-15,74.274002,74.300003,72.379997,72.559998,72.289017,72200
|
||||
2018-11-16,72.430000,73.410004,72.040001,73.290001,73.016296,48300
|
||||
2018-11-19,73.589996,73.650002,72.849998,73.250000,72.976440,67000
|
||||
2018-11-20,72.209999,73.190002,71.910004,72.790001,72.518158,75900
|
||||
2018-11-21,72.800003,73.970001,72.419998,73.900002,73.624008,102500
|
||||
2018-11-23,73.440002,73.720001,72.000000,73.160004,72.886780,14100
|
||||
2018-11-26,73.209999,74.660004,73.209999,74.199997,73.922882,33800
|
||||
2018-11-27,74.040001,74.290001,73.610001,74.070000,73.793373,35100
|
||||
2018-11-28,74.080002,74.650002,73.709999,74.410004,74.132111,27500
|
||||
2018-11-29,74.690002,75.385002,73.860001,74.870003,74.590393,28600
|
||||
2018-11-30,74.879997,75.849998,74.669998,75.639999,75.357513,24900
|
||||
2018-12-03,76.860001,77.820000,75.639999,77.650002,77.360008,34700
|
||||
2018-12-04,77.639999,78.279999,75.764999,76.269997,75.985153,69500
|
||||
2018-12-06,75.809998,75.930000,73.790001,75.930000,75.646423,49100
|
||||
2018-12-07,75.919998,76.650002,74.209999,74.820000,74.540573,21500
|
||||
2018-12-10,74.970001,74.989998,72.800003,73.089996,72.817032,28800
|
||||
2018-12-11,73.459999,73.480003,72.599998,72.650002,72.378685,20600
|
||||
2018-12-12,73.519997,73.760002,72.800003,72.919998,72.647667,18200
|
||||
2018-12-13,72.540001,72.919998,71.529999,71.639999,71.372452,19200
|
||||
2018-12-14,71.419998,71.419998,69.870003,70.550003,70.286522,37900
|
||||
2018-12-17,69.989998,70.139999,66.930000,67.139999,66.889259,45600
|
||||
2018-12-18,67.489998,69.160004,67.400002,68.440002,68.184402,78500
|
||||
2018-12-19,68.430000,70.050003,68.080002,68.320000,68.064850,42600
|
||||
2018-12-20,68.639999,68.639999,65.339996,65.769997,65.524368,40600
|
||||
2018-12-21,65.730003,67.199997,65.029999,65.959999,65.713661,78100
|
||||
2018-12-24,65.775002,66.610001,65.529999,66.410004,66.161987,39200
|
||||
2018-12-26,67.019997,67.190002,65.279999,66.550003,66.301460,50800
|
||||
2018-12-27,65.910004,67.239998,64.870003,67.169998,66.919144,33100
|
||||
2018-12-28,67.629997,68.779999,67.589996,68.139999,68.022232,47700
|
||||
2018-12-31,68.199997,68.860001,67.849998,68.480003,68.361649,44200
|
||||
2019-01-02,68.250000,68.339996,66.834999,67.230003,67.113808,35100
|
||||
2019-01-03,67.250000,67.599998,65.550003,66.080002,65.965797,29900
|
||||
2019-01-04,66.320000,69.290001,66.320000,69.290001,69.170250,48800
|
||||
2019-01-07,69.610001,71.559998,69.344002,71.199997,71.076942,26500
|
||||
2019-01-08,71.269997,72.230003,71.269997,71.870003,71.745789,17200
|
||||
2019-01-09,71.860001,72.720001,71.230003,72.660004,72.534424,21600
|
||||
2019-01-10,73.000000,73.400002,72.349998,73.029999,72.903778,15700
|
||||
2019-01-11,73.260002,73.279999,72.199997,72.860001,72.734077,70700
|
||||
2019-01-14,72.470001,73.419998,72.139999,73.349998,73.223228,182300
|
||||
2019-01-15,74.430000,74.430000,73.239998,73.790001,73.662468,27400
|
||||
2019-01-16,74.250000,74.480003,73.622002,74.239998,74.111687,18200
|
||||
2019-01-17,74.599998,75.680000,74.485001,75.430000,75.299637,34700
|
||||
2019-01-18,75.510002,76.769997,75.360001,76.379997,76.247993,24100
|
||||
2019-01-22,75.330002,76.790001,74.370003,76.790001,76.657288,60400
|
||||
2019-01-23,77.239998,77.970001,77.190002,77.739998,77.605637,35000
|
||||
2019-01-24,77.940002,78.760002,77.940002,78.589996,78.454170,11100
|
||||
2019-01-25,78.839996,80.720001,78.839996,80.459999,80.320938,37300
|
||||
2019-01-28,80.360001,81.190002,79.834999,80.589996,80.450714,29600
|
||||
2019-01-29,80.580002,81.830002,80.360001,80.769997,80.630402,25600
|
||||
2019-01-30,81.139999,81.209999,80.004997,80.940002,80.800117,21300
|
||||
2019-01-31,80.980003,81.949997,80.779999,81.279999,81.139526,37200
|
||||
2019-02-01,81.279999,82.029999,80.980003,81.709999,81.568779,64700
|
||||
2019-02-04,81.720001,82.330002,81.430000,82.190002,82.047951,49500
|
||||
2019-02-05,81.889999,83.769997,81.889999,83.480003,83.335724,23100
|
||||
2019-02-06,83.750000,84.760002,83.019997,83.779999,83.635201,57900
|
||||
2019-02-07,83.400002,84.849998,83.000000,83.889999,83.745010,46100
|
||||
2019-02-08,84.160004,84.870003,83.809998,84.709999,84.563599,57000
|
||||
2019-02-11,85.059998,85.300003,84.419998,84.769997,84.623489,57500
|
||||
2019-02-12,85.440002,87.440002,85.254997,87.129997,86.979408,44700
|
||||
2019-02-13,87.610001,89.169998,87.610001,88.629997,88.476822,34300
|
||||
2019-02-14,88.239998,88.389999,87.019997,87.839996,87.688179,28100
|
||||
2019-02-15,87.910004,87.910004,87.110001,87.260002,87.109192,43200
|
||||
2019-02-19,87.650002,87.650002,86.040001,86.800003,86.649986,47400
|
||||
2019-02-20,86.410004,87.839996,86.220001,87.620003,87.468567,34100
|
||||
2019-02-21,87.779999,87.900002,87.150002,87.580002,87.428635,23300
|
||||
2019-02-22,88.029999,88.400002,87.459999,88.290001,88.137413,29600
|
||||
2019-02-25,88.519997,88.930000,87.059998,87.110001,86.959450,19700
|
||||
2019-02-26,87.120003,87.910004,86.720001,87.639999,87.488533,29600
|
||||
2019-02-27,87.589996,87.589996,86.584999,87.199997,87.049286,18400
|
||||
2019-02-28,86.949997,87.059998,86.379997,86.839996,86.689911,32800
|
||||
2019-03-01,87.150002,87.150002,85.660004,86.500000,86.350502,38600
|
||||
2019-03-04,86.410004,87.510002,86.160004,87.019997,86.869598,32900
|
||||
2019-03-05,86.900002,88.070000,86.820000,87.940002,87.788017,23600
|
||||
2019-03-06,87.489998,87.489998,86.639999,87.059998,86.909531,25500
|
||||
2019-03-07,87.410004,87.410004,86.040001,86.779999,86.630020,34000
|
||||
2019-03-08,86.540001,86.750000,85.199997,85.570000,85.422112,24000
|
||||
2019-03-11,85.949997,86.980003,85.010002,86.739998,86.590088,24100
|
||||
2019-03-12,86.889999,86.889999,85.900002,86.650002,86.500244,13500
|
||||
2019-03-13,86.720001,87.099998,83.680000,84.260002,84.114372,62500
|
||||
2019-03-14,84.320000,85.510002,83.959999,85.269997,85.122627,51900
|
||||
2019-03-15,85.150002,85.550003,83.910004,84.400002,84.254135,34300
|
||||
2019-03-18,84.879997,85.230003,83.699997,84.260002,84.114372,32700
|
||||
2019-03-19,84.849998,85.059998,83.980003,83.980003,83.834862,21700
|
||||
2019-03-20,83.690002,84.330002,83.220001,83.970001,83.824875,43400
|
||||
2019-03-21,84.050003,85.440002,83.940002,85.370003,85.222458,26700
|
||||
2019-03-22,84.910004,85.150002,84.019997,84.019997,83.874786,30500
|
||||
2019-03-25,83.900002,85.269997,83.400002,85.220001,85.072716,34400
|
||||
2019-03-26,85.820000,86.019997,84.849998,85.809998,85.661690,31200
|
||||
2019-03-27,85.029999,86.970001,85.029999,86.790001,86.639999,28800
|
||||
2019-03-28,86.800003,88.084999,86.650002,87.699997,87.699997,22400
|
||||
2019-03-29,88.419998,89.669998,87.699997,89.339996,89.339996,40500
|
||||
2019-04-01,89.360001,89.949997,88.199997,89.620003,89.620003,42500
|
||||
2019-04-02,89.919998,89.919998,88.300003,88.720001,88.720001,27600
|
||||
2019-04-03,89.320000,89.320000,88.260002,88.449997,88.449997,25100
|
||||
2019-04-04,88.889999,88.889999,87.930000,88.190002,88.190002,29400
|
||||
2019-04-05,88.285004,88.430000,87.629997,88.070000,88.070000,23700
|
||||
2019-04-08,88.070000,88.495003,87.199997,87.739998,87.739998,18700
|
||||
2019-04-09,87.930000,88.360001,87.830002,88.070000,88.070000,24700
|
||||
2019-04-10,88.430000,89.660004,88.120003,89.209999,89.209999,20900
|
||||
2019-04-11,89.529999,89.529999,88.139999,88.300003,88.300003,21000
|
||||
2019-04-12,88.139999,89.639999,88.092003,89.589996,89.589996,18500
|
||||
2019-04-15,89.129997,89.779999,89.129997,89.389999,89.389999,20700
|
||||
2019-04-16,89.639999,89.879997,88.139999,88.209999,88.209999,17200
|
||||
2019-04-17,88.830002,88.830002,85.410004,86.480003,86.480003,53800
|
||||
2019-04-18,86.470001,87.470001,85.800003,87.050003,87.050003,17400
|
||||
2019-04-22,86.589996,87.849998,86.589996,87.419998,87.419998,14500
|
||||
2019-04-23,86.830002,88.110001,86.639999,88.110001,88.110001,21200
|
||||
2019-04-24,83.769997,87.589996,83.769997,87.269997,87.269997,34800
|
||||
2019-04-25,87.070000,87.839996,86.925003,87.669998,87.669998,18500
|
||||
2019-04-26,87.650002,87.769997,86.699997,86.970001,86.970001,22500
|
||||
2019-04-29,86.550003,86.669998,83.019997,86.360001,86.360001,22800
|
||||
2019-04-30,87.014000,87.349998,86.410004,87.220001,87.220001,33700
|
||||
2019-05-01,88.141998,88.141998,86.985001,87.129997,87.129997,43100
|
||||
2019-05-02,87.180000,87.474998,86.610001,87.379997,87.379997,26600
|
||||
2019-05-03,87.449997,88.120003,87.449997,87.940002,87.940002,35600
|
||||
2019-05-06,86.970001,87.800003,86.970001,87.620003,87.620003,28600
|
||||
2019-05-07,86.419998,86.699997,85.970001,86.599998,86.599998,29300
|
||||
2019-05-08,87.010002,87.010002,85.930000,86.379997,86.379997,50800
|
||||
2019-05-09,85.919998,86.150002,85.476997,85.790001,85.790001,37400
|
||||
2019-05-10,86.070000,86.889999,85.510002,86.889999,86.889999,63800
|
||||
2019-05-13,86.309998,86.455002,85.169998,86.040001,86.040001,24400
|
||||
2019-05-14,85.800003,86.370003,85.610001,86.070000,86.070000,34400
|
||||
2019-05-15,86.489998,87.680000,85.690002,86.970001,86.970001,46700
|
||||
2019-05-16,87.730003,89.209999,87.540001,89.010002,89.010002,51300
|
||||
2019-05-17,89.029999,89.519997,87.110001,87.790001,87.790001,29900
|
||||
2019-05-20,85.930000,89.010002,85.930000,86.620003,86.620003,19000
|
||||
2019-05-21,87.959999,88.010002,86.089996,86.290001,86.290001,51600
|
||||
2019-05-22,86.269997,86.480003,85.260002,86.129997,86.129997,49400
|
||||
2019-05-23,88.639999,93.860001,88.014999,93.500000,93.500000,115185
|
||||
|
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2016-11-02,778.200012,781.650024,763.450012,768.700012,768.700012,1872400
|
||||
2016-11-03,767.250000,769.950012,759.030029,762.130005,762.130005,1943200
|
||||
2016-11-04,750.659973,770.359985,750.560974,762.020020,762.020020,2134800
|
||||
2016-11-07,774.500000,785.190002,772.549988,782.520020,782.520020,1585100
|
||||
2016-11-08,783.400024,795.632996,780.190002,790.510010,790.510010,1350800
|
||||
2016-11-09,779.940002,791.226990,771.669983,785.309998,785.309998,2607100
|
||||
2016-11-10,791.169983,791.169983,752.179993,762.559998,762.559998,4745200
|
||||
2016-11-11,756.539978,760.780029,750.380005,754.020020,754.020020,2431800
|
||||
2016-11-14,755.599976,757.849976,727.539978,736.080017,736.080017,3631700
|
||||
2016-11-15,746.969971,764.416016,746.969971,758.489990,758.489990,2384000
|
||||
2016-11-16,755.200012,766.359985,750.510010,764.479980,764.479980,1465200
|
||||
2016-11-17,766.919983,772.700012,764.229980,771.229980,771.229980,1304000
|
||||
2016-11-18,771.369995,775.000000,760.000000,760.539978,760.539978,1547100
|
||||
2016-11-21,762.609985,769.700012,760.599976,769.200012,769.200012,1330600
|
||||
2016-11-22,772.630005,776.960022,767.000000,768.270020,768.270020,1593100
|
||||
2016-11-23,767.729980,768.283020,755.250000,760.989990,760.989990,1477400
|
||||
2016-11-25,764.260010,765.000000,760.520020,761.679993,761.679993,587400
|
||||
2016-11-28,760.000000,779.530029,759.799988,768.239990,768.239990,2188200
|
||||
2016-11-29,771.530029,778.500000,768.239990,770.840027,770.840027,1616600
|
||||
2016-11-30,770.070007,772.989990,754.830017,758.039978,758.039978,2392900
|
||||
2016-12-01,757.440002,759.849976,737.025024,747.919983,747.919983,3017900
|
||||
2016-12-02,744.590027,754.000000,743.099976,750.500000,750.500000,1452500
|
||||
2016-12-05,757.710022,763.900024,752.900024,762.520020,762.520020,1394200
|
||||
2016-12-06,764.729980,768.830017,757.340027,759.109985,759.109985,1690700
|
||||
2016-12-07,761.000000,771.359985,755.799988,771.190002,771.190002,1761000
|
||||
2016-12-08,772.479980,778.179993,767.229980,776.419983,776.419983,1488100
|
||||
2016-12-09,780.000000,789.429993,779.020996,789.289978,789.289978,1821900
|
||||
2016-12-12,785.039978,791.250000,784.354980,789.270020,789.270020,2104100
|
||||
2016-12-13,793.900024,804.380005,793.340027,796.099976,796.099976,2145200
|
||||
2016-12-14,797.400024,804.000000,794.010010,797.070007,797.070007,1704200
|
||||
2016-12-15,797.340027,803.000000,792.919983,797.849976,797.849976,1626500
|
||||
2016-12-16,800.400024,800.856018,790.289978,790.799988,790.799988,2428300
|
||||
2016-12-19,790.219971,797.659973,786.270020,794.200012,794.200012,1232100
|
||||
2016-12-20,796.760010,798.650024,793.270020,796.419983,796.419983,951000
|
||||
2016-12-21,795.840027,796.676025,787.099976,794.559998,794.559998,1211300
|
||||
2016-12-22,792.359985,793.320007,788.580017,791.260010,791.260010,972200
|
||||
2016-12-23,790.900024,792.739990,787.280029,789.909973,789.909973,623400
|
||||
2016-12-27,790.679993,797.859985,787.656982,791.549988,791.549988,789100
|
||||
2016-12-28,793.700012,794.229980,783.200012,785.049988,785.049988,1153800
|
||||
2016-12-29,783.330017,785.929993,778.919983,782.789978,782.789978,742200
|
||||
2016-12-30,782.750000,782.780029,770.409973,771.820007,771.820007,1770000
|
||||
2017-01-03,778.809998,789.630005,775.799988,786.140015,786.140015,1657300
|
||||
2017-01-04,788.359985,791.340027,783.159973,786.900024,786.900024,1073000
|
||||
2017-01-05,786.080017,794.479980,785.020020,794.020020,794.020020,1335200
|
||||
2017-01-06,795.260010,807.900024,792.203979,806.150024,806.150024,1640200
|
||||
2017-01-09,806.400024,809.966003,802.830017,806.650024,806.650024,1272400
|
||||
2017-01-10,807.859985,809.130005,803.510010,804.789978,804.789978,1176800
|
||||
2017-01-11,805.000000,808.150024,801.369995,807.909973,807.909973,1065900
|
||||
2017-01-12,807.140015,807.390015,799.169983,806.359985,806.359985,1353100
|
||||
2017-01-13,807.479980,811.223999,806.690002,807.880005,807.880005,1099200
|
||||
2017-01-17,807.080017,807.140015,800.369995,804.609985,804.609985,1355800
|
||||
2017-01-18,805.809998,806.205017,800.989990,806.070007,806.070007,1294400
|
||||
2017-01-19,805.119995,809.479980,801.799988,802.174988,802.174988,919300
|
||||
2017-01-20,806.909973,806.909973,801.690002,805.020020,805.020020,1670000
|
||||
2017-01-23,807.250000,820.869995,803.739990,819.309998,819.309998,1963600
|
||||
2017-01-24,822.299988,825.900024,817.820984,823.869995,823.869995,1474000
|
||||
2017-01-25,829.619995,835.770020,825.059998,835.669983,835.669983,1494500
|
||||
2017-01-26,837.809998,838.000000,827.010010,832.150024,832.150024,2973900
|
||||
2017-01-27,834.710022,841.950012,820.440002,823.309998,823.309998,2965800
|
||||
2017-01-30,814.659973,815.840027,799.799988,802.320007,802.320007,3246600
|
||||
2017-01-31,796.859985,801.250000,790.520020,796.789978,796.789978,2160600
|
||||
2017-02-01,799.679993,801.190002,791.190002,795.695007,795.695007,2029700
|
||||
2017-02-02,793.799988,802.700012,792.000000,798.530029,798.530029,1532100
|
||||
2017-02-03,802.989990,806.000000,800.369995,801.489990,801.489990,1463400
|
||||
2017-02-06,799.700012,801.669983,795.250000,801.340027,801.340027,1184500
|
||||
2017-02-07,803.989990,810.500000,801.780029,806.969971,806.969971,1241200
|
||||
2017-02-08,807.000000,811.840027,803.190002,808.380005,808.380005,1155300
|
||||
2017-02-09,809.510010,810.659973,804.539978,809.559998,809.559998,989700
|
||||
2017-02-10,811.700012,815.250000,809.780029,813.669983,813.669983,1135000
|
||||
2017-02-13,816.000000,820.958984,815.489990,819.239990,819.239990,1213300
|
||||
2017-02-14,819.000000,823.000000,816.000000,820.450012,820.450012,1053600
|
||||
2017-02-15,819.359985,823.000000,818.469971,818.979980,818.979980,1313600
|
||||
2017-02-16,819.929993,824.400024,818.979980,824.159973,824.159973,1287600
|
||||
2017-02-17,823.020020,828.070007,821.655029,828.070007,828.070007,1611000
|
||||
2017-02-21,828.659973,833.450012,828.349976,831.659973,831.659973,1262300
|
||||
2017-02-22,828.659973,833.250000,828.640015,830.760010,830.760010,982900
|
||||
2017-02-23,830.119995,832.460022,822.880005,831.330017,831.330017,1472800
|
||||
2017-02-24,827.729980,829.000000,824.200012,828.640015,828.640015,1392200
|
||||
2017-02-27,824.549988,830.500000,824.000000,829.280029,829.280029,1101500
|
||||
2017-02-28,825.609985,828.539978,820.200012,823.210022,823.210022,2260800
|
||||
2017-03-01,828.849976,836.255005,827.260010,835.239990,835.239990,1496500
|
||||
2017-03-02,833.849976,834.510010,829.640015,830.630005,830.630005,942500
|
||||
2017-03-03,830.559998,831.359985,825.750977,829.080017,829.080017,896400
|
||||
2017-03-06,826.950012,828.880005,822.400024,827.780029,827.780029,1109000
|
||||
2017-03-07,827.400024,833.409973,826.520020,831.909973,831.909973,1037600
|
||||
2017-03-08,833.510010,838.150024,831.789978,835.369995,835.369995,989800
|
||||
2017-03-09,836.000000,842.000000,834.210022,838.679993,838.679993,1261500
|
||||
2017-03-10,843.280029,844.909973,839.500000,843.250000,843.250000,1704000
|
||||
2017-03-13,844.000000,848.684998,843.250000,845.539978,845.539978,1223600
|
||||
2017-03-14,843.640015,847.239990,840.799988,845.619995,845.619995,779900
|
||||
2017-03-15,847.590027,848.630005,840.770020,847.200012,847.200012,1381500
|
||||
2017-03-16,849.030029,850.849976,846.130005,848.780029,848.780029,977600
|
||||
2017-03-17,851.609985,853.400024,847.109985,852.119995,852.119995,1712300
|
||||
2017-03-20,850.010010,850.219971,845.150024,848.400024,848.400024,1231500
|
||||
2017-03-21,851.400024,853.500000,829.020020,830.460022,830.460022,2463500
|
||||
2017-03-22,831.909973,835.549988,827.179993,829.590027,829.590027,1401500
|
||||
2017-03-23,821.000000,822.570007,812.257019,817.580017,817.580017,3487100
|
||||
2017-03-24,820.080017,821.929993,808.890015,814.429993,814.429993,1981000
|
||||
2017-03-27,806.950012,821.630005,803.369995,819.510010,819.510010,1894300
|
||||
2017-03-28,820.409973,825.989990,814.026978,820.919983,820.919983,1620500
|
||||
2017-03-29,825.000000,832.765015,822.380005,831.409973,831.409973,1786300
|
||||
2017-03-30,833.500000,833.679993,829.000000,831.500000,831.500000,1055300
|
||||
2017-03-31,828.969971,831.640015,827.390015,829.559998,829.559998,1401900
|
||||
2017-04-03,829.219971,840.849976,829.219971,838.549988,838.549988,1671500
|
||||
2017-04-04,831.359985,835.179993,829.036011,834.570007,834.570007,1045400
|
||||
2017-04-05,835.510010,842.450012,830.719971,831.409973,831.409973,1555300
|
||||
2017-04-06,832.400024,836.390015,826.460022,827.880005,827.880005,1254400
|
||||
2017-04-07,827.960022,828.484985,820.513000,824.669983,824.669983,1057300
|
||||
2017-04-10,825.390015,829.349976,823.770020,824.729980,824.729980,978900
|
||||
2017-04-11,824.710022,827.427002,817.020020,823.349976,823.349976,1079700
|
||||
2017-04-12,821.929993,826.659973,821.020020,824.320007,824.320007,900500
|
||||
2017-04-13,822.140015,826.380005,821.440002,823.559998,823.559998,1122400
|
||||
2017-04-17,825.010010,837.750000,824.469971,837.169983,837.169983,895000
|
||||
2017-04-18,834.219971,838.929993,832.710022,836.820007,836.820007,836700
|
||||
2017-04-19,839.789978,842.219971,836.289978,838.210022,838.210022,954200
|
||||
2017-04-20,841.440002,845.200012,839.320007,841.650024,841.650024,959000
|
||||
2017-04-21,842.880005,843.880005,840.599976,843.190002,843.190002,1323600
|
||||
2017-04-24,851.200012,863.450012,849.859985,862.760010,862.760010,1372500
|
||||
2017-04-25,865.000000,875.000000,862.809998,872.299988,872.299988,1672000
|
||||
2017-04-26,874.229980,876.049988,867.747986,871.729980,871.729980,1237200
|
||||
2017-04-27,873.599976,875.400024,870.380005,874.250000,874.250000,2026800
|
||||
2017-04-28,910.659973,916.849976,905.770020,905.960022,905.960022,3219500
|
||||
2017-05-01,901.940002,915.679993,901.450012,912.570007,912.570007,2116000
|
||||
2017-05-02,909.619995,920.770020,909.453003,916.440002,916.440002,1587200
|
||||
2017-05-03,914.859985,928.099976,912.543030,927.039978,927.039978,1499500
|
||||
2017-05-04,926.070007,935.929993,924.590027,931.659973,931.659973,1422100
|
||||
2017-05-05,933.539978,934.900024,925.200012,927.130005,927.130005,1911300
|
||||
2017-05-08,926.119995,936.924988,925.260010,934.299988,934.299988,1329800
|
||||
2017-05-09,936.950012,937.500000,929.530029,932.169983,932.169983,1581800
|
||||
2017-05-10,931.979980,932.000000,925.159973,928.780029,928.780029,1173900
|
||||
2017-05-11,925.320007,932.530029,923.030029,930.599976,930.599976,835000
|
||||
2017-05-12,931.530029,933.440002,927.849976,932.219971,932.219971,1050600
|
||||
2017-05-15,932.950012,938.250000,929.340027,937.080017,937.080017,1108100
|
||||
2017-05-16,940.000000,943.109985,937.580017,943.000000,943.000000,969500
|
||||
2017-05-17,935.669983,939.333008,918.140015,919.619995,919.619995,2362100
|
||||
2017-05-18,921.000000,933.169983,918.750000,930.239990,930.239990,1596900
|
||||
2017-05-19,931.469971,937.755005,931.000000,934.010010,934.010010,1393000
|
||||
2017-05-22,935.000000,941.882996,935.000000,941.859985,941.859985,1120400
|
||||
2017-05-23,947.919983,951.466980,942.575012,948.820007,948.820007,1270800
|
||||
2017-05-24,952.979980,955.090027,949.500000,954.960022,954.960022,1024800
|
||||
2017-05-25,957.330017,972.629028,955.469971,969.539978,969.539978,1660500
|
||||
2017-05-26,969.700012,974.979980,965.030029,971.469971,971.469971,1252000
|
||||
2017-05-30,970.309998,976.200012,969.489990,975.880005,975.880005,1466700
|
||||
2017-05-31,975.020020,979.270020,960.179993,964.859985,964.859985,2448100
|
||||
2017-06-01,968.950012,971.500000,960.010010,966.950012,966.950012,1410500
|
||||
2017-06-02,969.460022,975.880005,966.000000,975.599976,975.599976,1751000
|
||||
2017-06-05,976.549988,986.909973,975.099976,983.679993,983.679993,1252100
|
||||
2017-06-06,983.159973,988.250000,975.140015,976.570007,976.570007,1814600
|
||||
2017-06-07,980.000000,983.979980,975.940002,980.940002,980.940002,1453900
|
||||
2017-06-08,982.349976,984.570007,977.200012,983.409973,983.409973,1471500
|
||||
2017-06-09,984.500000,984.500000,935.630005,949.830017,949.830017,3309400
|
||||
2017-06-12,939.559998,949.354980,915.232971,942.900024,942.900024,3763500
|
||||
2017-06-13,951.909973,959.979980,944.090027,953.400024,953.400024,2013300
|
||||
2017-06-14,959.919983,961.150024,942.250000,950.760010,950.760010,1489700
|
||||
2017-06-15,933.969971,943.338989,924.440002,942.309998,942.309998,2133100
|
||||
2017-06-16,940.000000,942.039978,931.594971,939.780029,939.780029,3094700
|
||||
2017-06-19,949.960022,959.989990,949.049988,957.369995,957.369995,1533300
|
||||
2017-06-20,957.520020,961.619995,950.010010,950.630005,950.630005,1126000
|
||||
2017-06-21,953.640015,960.099976,950.760010,959.450012,959.450012,1202200
|
||||
2017-06-22,958.700012,960.719971,954.549988,957.090027,957.090027,941400
|
||||
2017-06-23,956.830017,966.000000,954.200012,965.590027,965.590027,1527900
|
||||
2017-06-26,969.900024,973.309998,950.789978,952.270020,952.270020,1598400
|
||||
2017-06-27,942.460022,948.289978,926.849976,927.330017,927.330017,2579900
|
||||
2017-06-28,929.000000,942.750000,916.000000,940.489990,940.489990,2721400
|
||||
2017-06-29,929.919983,931.260010,910.619995,917.789978,917.789978,3299200
|
||||
2017-06-30,926.049988,926.049988,908.309998,908.729980,908.729980,2090200
|
||||
2017-07-03,912.179993,913.940002,894.789978,898.700012,898.700012,1709800
|
||||
2017-07-05,901.760010,914.510010,898.500000,911.710022,911.710022,1813900
|
||||
2017-07-06,904.119995,914.943970,899.700012,906.690002,906.690002,1424500
|
||||
2017-07-07,908.849976,921.539978,908.849976,918.590027,918.590027,1637800
|
||||
2017-07-10,921.770020,930.380005,919.590027,928.799988,928.799988,1192800
|
||||
2017-07-11,929.539978,931.429993,922.000000,930.090027,930.090027,1113200
|
||||
2017-07-12,938.679993,946.299988,934.469971,943.830017,943.830017,1532100
|
||||
2017-07-13,946.289978,954.450012,943.010010,947.159973,947.159973,1294700
|
||||
2017-07-14,952.000000,956.909973,948.005005,955.989990,955.989990,1053800
|
||||
2017-07-17,957.000000,960.739990,949.241028,953.419983,953.419983,1165500
|
||||
2017-07-18,953.000000,968.039978,950.599976,965.400024,965.400024,1154000
|
||||
2017-07-19,967.840027,973.039978,964.030029,970.890015,970.890015,1224500
|
||||
2017-07-20,975.000000,975.900024,961.510010,968.150024,968.150024,1624500
|
||||
2017-07-21,962.250000,973.229980,960.150024,972.919983,972.919983,1711000
|
||||
2017-07-24,972.219971,986.200012,970.770020,980.340027,980.340027,3248300
|
||||
2017-07-25,953.809998,959.700012,945.400024,950.700012,950.700012,4661000
|
||||
2017-07-26,954.679993,955.000000,942.278992,947.799988,947.799988,2088300
|
||||
2017-07-27,951.780029,951.780029,920.000000,934.090027,934.090027,3213000
|
||||
2017-07-28,929.400024,943.830017,927.500000,941.530029,941.530029,1846400
|
||||
2017-07-31,941.890015,943.590027,926.039978,930.500000,930.500000,1970100
|
||||
2017-08-01,932.380005,937.447021,929.260010,930.830017,930.830017,1277700
|
||||
2017-08-02,928.609985,932.599976,916.679993,930.390015,930.390015,1824400
|
||||
2017-08-03,930.340027,932.239990,922.239990,923.650024,923.650024,1202500
|
||||
2017-08-04,926.750000,930.307007,923.030029,927.960022,927.960022,1082300
|
||||
2017-08-07,929.059998,931.700012,926.500000,929.359985,929.359985,1032200
|
||||
2017-08-08,927.090027,935.814026,925.609985,926.789978,926.789978,1061600
|
||||
2017-08-09,920.609985,925.979980,917.250000,922.900024,922.900024,1192100
|
||||
2017-08-10,917.549988,919.260010,906.130005,907.239990,907.239990,1824000
|
||||
2017-08-11,907.969971,917.780029,905.580017,914.390015,914.390015,1206800
|
||||
2017-08-14,922.530029,924.668030,918.190002,922.669983,922.669983,1064500
|
||||
2017-08-15,924.229980,926.549988,919.820007,922.219971,922.219971,883400
|
||||
2017-08-16,925.289978,932.700012,923.445007,926.960022,926.960022,1006700
|
||||
2017-08-17,925.780029,926.859985,910.979980,910.979980,910.979980,1277200
|
||||
2017-08-18,910.309998,915.275024,907.153992,910.669983,910.669983,1342700
|
||||
2017-08-21,910.000000,913.000000,903.400024,906.659973,906.659973,943400
|
||||
2017-08-22,912.719971,925.859985,911.474976,924.690002,924.690002,1166700
|
||||
2017-08-23,921.929993,929.929993,919.359985,927.000000,927.000000,1090200
|
||||
2017-08-24,928.659973,930.840027,915.500000,921.280029,921.280029,1270300
|
||||
2017-08-25,923.489990,925.554993,915.500000,915.890015,915.890015,1053400
|
||||
2017-08-28,916.000000,919.244995,911.869995,913.809998,913.809998,1086500
|
||||
2017-08-29,905.099976,923.330017,905.000000,921.289978,921.289978,1185600
|
||||
2017-08-30,920.049988,930.818970,919.650024,929.570007,929.570007,1301200
|
||||
2017-08-31,931.760010,941.979980,931.760010,939.330017,939.330017,1582600
|
||||
2017-09-01,941.130005,942.479980,935.150024,937.340027,937.340027,947400
|
||||
2017-09-05,933.080017,937.000000,921.960022,928.450012,928.450012,1326400
|
||||
2017-09-06,930.150024,930.914978,919.270020,927.809998,927.809998,1527700
|
||||
2017-09-07,931.729980,936.409973,923.619995,935.950012,935.950012,1212700
|
||||
2017-09-08,936.489990,936.989990,924.880005,926.500000,926.500000,1011500
|
||||
2017-09-11,934.250000,938.380005,926.919983,929.080017,929.080017,1267000
|
||||
2017-09-12,932.590027,933.479980,923.861023,932.070007,932.070007,1134400
|
||||
2017-09-13,930.659973,937.250000,929.859985,935.090027,935.090027,1102600
|
||||
2017-09-14,931.250000,932.770020,924.000000,925.109985,925.109985,1397600
|
||||
2017-09-15,924.659973,926.489990,916.359985,920.289978,920.289978,2505400
|
||||
2017-09-18,920.010010,922.080017,910.599976,915.000000,915.000000,1306900
|
||||
2017-09-19,917.419983,922.419983,912.549988,921.809998,921.809998,936700
|
||||
2017-09-20,922.979980,933.880005,922.000000,931.580017,931.580017,1669800
|
||||
2017-09-21,933.000000,936.530029,923.830017,932.450012,932.450012,1290600
|
||||
2017-09-22,927.750000,934.729980,926.479980,928.530029,928.530029,1052700
|
||||
2017-09-25,925.450012,926.400024,909.700012,920.969971,920.969971,1856800
|
||||
2017-09-26,923.719971,930.820007,921.140015,924.859985,924.859985,1666900
|
||||
2017-09-27,927.739990,949.900024,927.739990,944.489990,944.489990,2239400
|
||||
2017-09-28,941.359985,950.690002,940.549988,949.500000,949.500000,1020300
|
||||
2017-09-29,952.000000,959.786011,951.510010,959.109985,959.109985,1581000
|
||||
2017-10-02,959.979980,962.539978,947.840027,953.270020,953.270020,1283400
|
||||
2017-10-03,954.000000,958.000000,949.140015,957.789978,957.789978,888300
|
||||
2017-10-04,957.000000,960.390015,950.690002,951.679993,951.679993,952400
|
||||
2017-10-05,955.489990,970.909973,955.179993,969.960022,969.960022,1213800
|
||||
2017-10-06,966.700012,979.460022,963.359985,978.890015,978.890015,1173900
|
||||
2017-10-09,980.000000,985.424988,976.109985,977.000000,977.000000,891400
|
||||
2017-10-10,980.000000,981.570007,966.080017,972.599976,972.599976,968400
|
||||
2017-10-11,973.719971,990.710022,972.250000,989.250000,989.250000,1693300
|
||||
2017-10-12,987.450012,994.119995,985.000000,987.830017,987.830017,1262400
|
||||
2017-10-13,992.000000,997.210022,989.000000,989.679993,989.679993,1169800
|
||||
2017-10-16,992.099976,993.906982,984.000000,992.000000,992.000000,910500
|
||||
2017-10-17,990.289978,996.440002,988.590027,992.179993,992.179993,1290200
|
||||
2017-10-18,991.770020,996.719971,986.974976,992.809998,992.809998,1057600
|
||||
2017-10-19,986.000000,988.880005,978.390015,984.450012,984.450012,1313600
|
||||
2017-10-20,989.440002,991.000000,984.580017,988.200012,988.200012,1183200
|
||||
2017-10-23,989.520020,989.520020,966.119995,968.450012,968.450012,1478400
|
||||
2017-10-24,970.000000,972.229980,961.000000,970.539978,970.539978,1212200
|
||||
2017-10-25,968.369995,976.090027,960.520020,973.330017,973.330017,1211300
|
||||
2017-10-26,980.000000,987.599976,972.200012,972.559998,972.559998,2042100
|
||||
2017-10-27,1009.190002,1048.390015,1008.200012,1019.270020,1019.270020,5167700
|
||||
2017-10-30,1014.000000,1024.969971,1007.500000,1017.109985,1017.109985,2085100
|
||||
2017-10-31,1015.219971,1024.000000,1010.419983,1016.640015,1016.640015,1330700
|
||||
2017-11-01,1017.210022,1029.670044,1016.950012,1025.500000,1025.500000,1373444
|
||||
|
@@ -0,0 +1,24 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2017-10-02,959.979980,962.539978,947.840027,953.270020,953.270020,1283400
|
||||
2017-10-03,954.000000,958.000000,949.140015,957.789978,957.789978,888300
|
||||
2017-10-04,957.000000,960.390015,950.690002,951.679993,951.679993,952400
|
||||
2017-10-05,955.489990,970.909973,955.179993,969.960022,969.960022,1213800
|
||||
2017-10-06,966.700012,979.460022,963.359985,978.890015,978.890015,1173900
|
||||
2017-10-09,980.000000,985.424988,976.109985,977.000000,977.000000,891400
|
||||
2017-10-10,980.000000,981.570007,966.080017,972.599976,972.599976,968400
|
||||
2017-10-11,973.719971,990.710022,972.250000,989.250000,989.250000,1693300
|
||||
2017-10-12,987.450012,994.119995,985.000000,987.830017,987.830017,1262400
|
||||
2017-10-13,992.000000,997.210022,989.000000,989.679993,989.679993,1169800
|
||||
2017-10-16,992.099976,993.906982,984.000000,992.000000,992.000000,910500
|
||||
2017-10-17,990.289978,996.440002,988.590027,992.179993,992.179993,1290200
|
||||
2017-10-18,991.770020,996.719971,986.974976,992.809998,992.809998,1057600
|
||||
2017-10-19,986.000000,988.880005,978.390015,984.450012,984.450012,1313600
|
||||
2017-10-20,989.440002,991.000000,984.580017,988.200012,988.200012,1183200
|
||||
2017-10-23,989.520020,989.520020,966.119995,968.450012,968.450012,1478400
|
||||
2017-10-24,970.000000,972.229980,961.000000,970.539978,970.539978,1212200
|
||||
2017-10-25,968.369995,976.090027,960.520020,973.330017,973.330017,1211300
|
||||
2017-10-26,980.000000,987.599976,972.200012,972.559998,972.559998,2042100
|
||||
2017-10-27,1009.190002,1048.390015,1008.200012,1019.270020,1019.270020,5167700
|
||||
2017-10-30,1014.000000,1024.969971,1007.500000,1017.109985,1017.109985,2085100
|
||||
2017-10-31,1015.219971,1024.000000,1010.419983,1016.640015,1016.640015,1330700
|
||||
2017-11-01,1017.210022,1029.670044,1016.950012,1025.500000,1025.500000,1373444
|
||||
|
@@ -0,0 +1,252 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-07-13,9.560000,9.815000,9.550000,9.710000,9.423450,27224000
|
||||
2018-07-16,9.865000,9.885000,9.770000,9.800000,9.510794,14771800
|
||||
2018-07-17,9.785000,9.970000,9.760000,9.950000,9.656367,12135400
|
||||
2018-07-18,9.880000,9.910000,9.830000,9.840000,9.549613,5833800
|
||||
2018-07-19,9.720000,9.905000,9.720000,9.855000,9.564170,14995000
|
||||
2018-07-20,9.950000,10.075000,9.945000,10.005000,9.709743,10394600
|
||||
2018-07-23,10.025000,10.090000,9.995000,10.075000,9.777678,6873400
|
||||
2018-07-24,10.175000,10.210000,10.130000,10.195000,9.894136,8553200
|
||||
2018-07-25,10.220000,10.340000,10.205000,10.290000,9.986332,7539200
|
||||
2018-07-26,10.255000,10.300000,10.220000,10.270000,9.966924,9587400
|
||||
2018-07-27,10.300000,10.315000,10.115000,10.170000,9.869875,8674800
|
||||
2018-07-30,10.100000,10.150000,9.940000,9.970000,9.675777,14123200
|
||||
2018-07-31,10.055000,10.105000,10.035000,10.090000,9.792234,16554000
|
||||
2018-08-01,10.125000,10.180000,10.100000,10.175000,9.874727,9094000
|
||||
2018-08-02,10.090000,10.220000,10.060000,10.190000,9.889283,14119600
|
||||
2018-08-03,10.175000,10.275000,10.130000,10.270000,9.966924,8424400
|
||||
2018-08-06,10.215000,10.305000,10.160000,10.295000,9.991185,6577600
|
||||
2018-08-07,10.325000,10.330000,10.150000,10.300000,9.996037,9157800
|
||||
2018-08-08,10.300000,10.430000,10.265000,10.375000,10.068825,7491200
|
||||
2018-08-09,10.425000,10.490000,10.410000,10.450000,10.141611,5629600
|
||||
2018-08-10,10.355000,10.420000,10.335000,10.400000,10.093086,4435400
|
||||
2018-08-13,10.435000,10.475000,10.395000,10.410000,10.102792,4827600
|
||||
2018-08-14,10.405000,10.475000,10.385000,10.450000,10.141611,5472800
|
||||
2018-08-15,10.390000,10.450000,10.300000,10.435000,10.127054,6000200
|
||||
2018-08-16,10.500000,10.520000,10.455000,10.495000,10.185283,6288200
|
||||
2018-08-17,10.490000,10.570000,10.465000,10.540000,10.228956,7209600
|
||||
2018-08-20,10.250000,10.390000,10.145000,10.250000,9.947514,12304800
|
||||
2018-08-21,10.295000,10.375000,10.225000,10.245000,9.942661,8231200
|
||||
2018-08-22,10.245000,10.285000,10.205000,10.250000,9.947514,10869000
|
||||
2018-08-23,10.300000,10.365000,10.180000,10.190000,9.889283,11189200
|
||||
2018-08-24,10.215000,10.215000,10.090000,10.150000,9.850465,7687000
|
||||
2018-08-27,10.325000,10.390000,10.260000,10.270000,9.966924,7388600
|
||||
2018-08-28,10.350000,10.355000,10.305000,10.325000,10.020301,7254000
|
||||
2018-08-29,10.220000,10.265000,10.160000,10.175000,9.874727,12943200
|
||||
2018-08-30,10.180000,10.255000,10.135000,10.230000,9.928103,9159000
|
||||
2018-08-31,10.310000,10.400000,10.300000,10.385000,10.078529,6992200
|
||||
2018-09-04,10.430000,10.460000,10.280000,10.380000,10.073677,12184400
|
||||
2018-09-05,10.190000,10.380000,10.170000,10.355000,10.049415,12774400
|
||||
2018-09-06,10.295000,10.405000,10.285000,10.400000,10.093086,7225000
|
||||
2018-09-07,10.390000,10.535000,10.365000,10.425000,10.117350,10373600
|
||||
2018-09-10,10.380000,10.480000,10.300000,10.350000,10.044563,8940600
|
||||
2018-09-11,10.360000,10.555000,10.350000,10.485000,10.175577,8671800
|
||||
2018-09-12,10.570000,10.650000,10.530000,10.550000,10.238660,4392500
|
||||
2018-09-13,10.600000,10.640000,10.470000,10.610000,10.296889,9522300
|
||||
2018-09-14,10.520000,10.540000,10.270000,10.320000,10.015448,10702000
|
||||
2018-09-17,10.210000,10.290000,10.200000,10.250000,9.947514,7260000
|
||||
2018-09-18,10.180000,10.180000,10.080000,10.140000,9.840760,6657600
|
||||
2018-09-19,10.200000,10.230000,9.990000,10.010000,9.714596,11042600
|
||||
2018-09-20,10.080000,10.100000,9.970000,10.070000,9.772825,9811500
|
||||
2018-09-21,10.040000,10.040000,9.890000,9.930000,9.636957,7935600
|
||||
2018-09-24,10.040000,10.070000,9.930000,9.950000,9.656367,12798900
|
||||
2018-09-25,10.060000,10.190000,10.030000,10.150000,9.850465,10763500
|
||||
2018-09-26,10.060000,10.200000,10.030000,10.060000,9.763121,6388300
|
||||
2018-09-27,10.100000,10.200000,10.070000,10.100000,9.801940,8822100
|
||||
2018-09-28,10.220000,10.250000,10.120000,10.170000,9.869875,9329800
|
||||
2018-10-01,10.340000,10.390000,10.300000,10.370000,10.063972,7440100
|
||||
2018-10-02,10.320000,10.320000,10.200000,10.270000,9.966924,9053800
|
||||
2018-10-03,10.120000,10.150000,10.010000,10.040000,9.743711,10073600
|
||||
2018-10-04,9.810000,10.020000,9.800000,10.000000,9.704891,14905600
|
||||
2018-10-05,10.020000,10.190000,10.010000,10.170000,9.869875,8296800
|
||||
2018-10-08,9.970000,10.070000,9.920000,10.030000,9.734005,9091600
|
||||
2018-10-09,9.970000,10.040000,9.930000,10.020000,9.724301,8825600
|
||||
2018-10-10,9.810000,9.840000,9.630000,9.740000,9.452564,23251500
|
||||
2018-10-11,9.530000,9.680000,9.460000,9.520000,9.239058,18729300
|
||||
2018-10-12,9.650000,9.760000,9.570000,9.740000,9.452564,11524500
|
||||
2018-10-15,9.850000,9.960000,9.790000,9.910000,9.617547,17032800
|
||||
2018-10-16,9.950000,10.520000,9.910000,10.220000,9.918399,27500700
|
||||
2018-10-17,10.060000,10.060000,9.740000,9.800000,9.510794,20694700
|
||||
2018-10-18,9.780000,9.780000,9.350000,9.540000,9.258466,21337000
|
||||
2018-10-19,9.600000,9.760000,9.540000,9.580000,9.297286,8651900
|
||||
2018-10-22,9.570000,9.580000,9.400000,9.480000,9.200236,9586300
|
||||
2018-10-23,9.240000,9.420000,9.220000,9.400000,9.122598,10275200
|
||||
2018-10-24,9.300000,9.370000,9.050000,9.060000,8.792632,11702500
|
||||
2018-10-25,9.080000,9.200000,9.050000,9.110000,8.934843,9126200
|
||||
2018-10-26,8.990000,9.050000,8.850000,9.020000,8.846575,8647400
|
||||
2018-10-29,9.080000,9.100000,8.880000,8.970000,8.797536,9618300
|
||||
2018-10-30,9.160000,9.320000,9.100000,9.190000,9.013305,13429500
|
||||
2018-10-31,9.450000,9.540000,9.410000,9.470000,9.287922,9089200
|
||||
2018-11-01,9.390000,9.400000,9.210000,9.300000,9.121191,12112500
|
||||
2018-11-02,9.340000,9.410000,9.230000,9.250000,9.072152,8302600
|
||||
2018-11-05,9.290000,9.470000,9.290000,9.400000,9.219268,9240100
|
||||
2018-11-06,9.440000,9.550000,9.390000,9.480000,9.297729,6901300
|
||||
2018-11-07,9.620000,9.690000,9.580000,9.610000,9.425230,6291500
|
||||
2018-11-08,9.610000,9.650000,9.460000,9.510000,9.327153,5057100
|
||||
2018-11-09,9.410000,9.500000,9.370000,9.490000,9.307537,5318600
|
||||
2018-11-12,9.420000,9.470000,9.270000,9.340000,9.160421,7691300
|
||||
2018-11-13,9.370000,9.460000,9.330000,9.400000,9.219268,5937800
|
||||
2018-11-14,9.410000,9.440000,9.160000,9.250000,9.072152,5406400
|
||||
2018-11-15,9.270000,9.370000,9.160000,9.330000,9.150614,7020900
|
||||
2018-11-16,9.280000,9.370000,9.250000,9.330000,9.150614,4709700
|
||||
2018-11-19,9.330000,9.330000,9.210000,9.290000,9.111383,6507000
|
||||
2018-11-20,9.180000,9.190000,9.050000,9.090000,8.915228,7108500
|
||||
2018-11-21,8.990000,9.100000,8.950000,9.070000,8.895612,7539300
|
||||
2018-11-23,9.050000,9.160000,9.050000,9.090000,8.915228,3158400
|
||||
2018-11-26,9.070000,9.210000,9.060000,9.180000,9.003497,6051900
|
||||
2018-11-27,9.250000,9.310000,9.190000,9.270000,9.091768,5458600
|
||||
2018-11-28,9.630000,9.890000,9.560000,9.860000,9.670423,13928900
|
||||
2018-11-29,9.790000,9.800000,9.660000,9.680000,9.493885,12415800
|
||||
2018-11-30,9.800000,9.860000,9.750000,9.860000,9.670423,10463200
|
||||
2018-12-03,9.910000,9.940000,9.860000,9.900000,9.709654,13191200
|
||||
2018-12-04,9.940000,10.050000,9.830000,9.850000,9.660616,9518900
|
||||
2018-12-06,9.770000,9.860000,9.710000,9.830000,9.641000,17177400
|
||||
2018-12-07,9.830000,9.970000,9.650000,9.690000,9.503692,11353200
|
||||
2018-12-10,9.550000,9.590000,9.430000,9.570000,9.386000,7731000
|
||||
2018-12-11,9.650000,9.770000,9.620000,9.690000,9.503692,7952700
|
||||
2018-12-12,9.770000,9.820000,9.660000,9.660000,9.474269,5453300
|
||||
2018-12-13,9.930000,9.980000,9.850000,9.870000,9.680231,7971700
|
||||
2018-12-14,9.990000,10.010000,9.870000,9.890000,9.699846,7799600
|
||||
2018-12-17,9.820000,9.830000,9.540000,9.600000,9.415423,10520400
|
||||
2018-12-18,9.680000,9.720000,9.500000,9.540000,9.356576,9742500
|
||||
2018-12-19,9.490000,9.580000,9.380000,9.430000,9.248692,8175400
|
||||
2018-12-20,9.510000,9.540000,9.140000,9.280000,9.101575,23548800
|
||||
2018-12-21,9.210000,9.310000,9.100000,9.100000,8.925036,15286200
|
||||
2018-12-24,9.190000,9.240000,9.070000,9.080000,8.905420,8590700
|
||||
2018-12-26,9.150000,9.380000,9.120000,9.380000,9.199653,9004200
|
||||
2018-12-27,9.300000,9.450000,9.280000,9.450000,9.268306,9856500
|
||||
2018-12-28,9.480000,9.500000,9.380000,9.430000,9.248692,6818500
|
||||
2018-12-31,9.470000,9.530000,9.390000,9.520000,9.336961,7229400
|
||||
2019-01-02,9.500000,9.730000,9.470000,9.610000,9.425230,9818900
|
||||
2019-01-03,9.550000,9.590000,9.470000,9.470000,9.287922,9404900
|
||||
2019-01-04,9.520000,9.720000,9.500000,9.630000,9.444846,7119000
|
||||
2019-01-07,9.700000,9.810000,9.660000,9.710000,9.523308,7732700
|
||||
2019-01-08,9.820000,9.850000,9.720000,9.750000,9.562538,9391600
|
||||
2019-01-09,9.760000,9.890000,9.740000,9.870000,9.680231,9634300
|
||||
2019-01-10,9.780000,9.890000,9.700000,9.880000,9.690039,13956500
|
||||
2019-01-11,10.180000,10.680000,10.120000,10.410000,10.209848,40526400
|
||||
2019-01-14,10.180000,10.410000,10.130000,10.370000,10.170618,34162200
|
||||
2019-01-15,10.570000,10.580000,10.460000,10.490000,10.288310,12153400
|
||||
2019-01-16,10.570000,10.640000,10.550000,10.610000,10.406003,12048400
|
||||
2019-01-17,10.540000,10.630000,10.500000,10.580000,10.376580,9581000
|
||||
2019-01-18,10.600000,10.690000,10.580000,10.670000,10.464850,10168700
|
||||
2019-01-22,10.640000,10.680000,10.470000,10.530000,10.327541,10437700
|
||||
2019-01-23,10.560000,10.560000,10.430000,10.530000,10.327541,8108100
|
||||
2019-01-24,10.530000,10.550000,10.430000,10.430000,10.285139,6627700
|
||||
2019-01-25,10.510000,10.580000,10.410000,10.550000,10.403472,8839100
|
||||
2019-01-28,10.490000,10.570000,10.440000,10.520000,10.373889,5192200
|
||||
2019-01-29,10.490000,10.530000,10.420000,10.480000,10.334444,8271100
|
||||
2019-01-30,10.460000,10.630000,10.420000,10.590000,10.442917,7026000
|
||||
2019-01-31,10.680000,10.820000,10.680000,10.800000,10.650001,10354900
|
||||
2019-02-01,10.850000,10.930000,10.810000,10.900000,10.748610,6651300
|
||||
2019-02-04,10.830000,10.880000,10.790000,10.870000,10.719028,7449000
|
||||
2019-02-05,10.850000,10.920000,10.830000,10.840000,10.689445,6926300
|
||||
2019-02-06,10.910000,10.940000,10.850000,10.900000,10.748610,5478400
|
||||
2019-02-07,10.860000,10.930000,10.800000,10.850000,10.699306,7116200
|
||||
2019-02-08,10.830000,10.890000,10.810000,10.860000,10.709167,4103300
|
||||
2019-02-11,10.860000,10.890000,10.790000,10.820000,10.669722,5929600
|
||||
2019-02-12,10.850000,10.860000,10.760000,10.770000,10.620417,6300600
|
||||
2019-02-13,10.850000,10.870000,10.770000,10.800000,10.650001,9532300
|
||||
2019-02-14,10.650000,10.760000,10.620000,10.760000,10.610556,8443400
|
||||
2019-02-15,10.680000,10.780000,10.640000,10.760000,10.610556,9269100
|
||||
2019-02-19,10.470000,10.570000,10.380000,10.550000,10.403472,13443300
|
||||
2019-02-20,10.620000,10.730000,10.620000,10.700000,10.551389,6693600
|
||||
2019-02-21,10.600000,10.620000,10.490000,10.550000,10.403472,6913200
|
||||
2019-02-22,10.560000,10.680000,10.550000,10.630000,10.482361,4644600
|
||||
2019-02-25,10.830000,10.920000,10.780000,10.810000,10.659862,7781200
|
||||
2019-02-26,10.700000,10.760000,10.620000,10.730000,10.580972,6343700
|
||||
2019-02-27,10.610000,10.730000,10.570000,10.700000,10.551389,6812900
|
||||
2019-02-28,10.690000,10.770000,10.650000,10.720000,10.571112,6815000
|
||||
2019-03-01,10.790000,10.870000,10.740000,10.840000,10.689445,8012100
|
||||
2019-03-04,10.850000,10.870000,10.660000,10.720000,10.571112,5650900
|
||||
2019-03-05,10.720000,10.800000,10.650000,10.770000,10.620417,5576000
|
||||
2019-03-06,10.700000,10.750000,10.680000,10.710000,10.561250,6026600
|
||||
2019-03-07,10.650000,10.710000,10.480000,10.490000,10.344305,8304200
|
||||
2019-03-08,10.410000,10.480000,10.360000,10.470000,10.324583,6145300
|
||||
2019-03-11,10.480000,10.570000,10.460000,10.550000,10.403472,5497300
|
||||
2019-03-12,10.530000,10.550000,10.460000,10.520000,10.373889,9129600
|
||||
2019-03-13,10.530000,10.600000,10.480000,10.550000,10.403472,10809900
|
||||
2019-03-14,10.520000,10.610000,10.480000,10.600000,10.452778,5964500
|
||||
2019-03-15,10.690000,10.770000,10.690000,10.700000,10.551389,7108200
|
||||
2019-03-18,10.700000,10.740000,10.680000,10.720000,10.571112,5828700
|
||||
2019-03-19,10.800000,10.910000,10.790000,10.900000,10.748610,7204100
|
||||
2019-03-20,10.970000,11.040000,10.880000,10.940000,10.788055,7616300
|
||||
2019-03-21,10.940000,11.080000,10.940000,11.060000,10.906389,5108000
|
||||
2019-03-22,11.050000,11.120000,10.860000,10.880000,10.728889,8525300
|
||||
2019-03-25,10.860000,10.880000,10.770000,10.820000,10.669722,7322400
|
||||
2019-03-26,10.860000,10.880000,10.770000,10.820000,10.669722,4427200
|
||||
2019-03-27,10.790000,10.830000,10.670000,10.730000,10.580972,5644900
|
||||
2019-03-28,10.910000,10.940000,10.830000,10.880000,10.728889,5429900
|
||||
2019-03-29,10.930000,11.030000,10.920000,10.930000,10.778194,5862300
|
||||
2019-04-01,11.070000,11.160000,11.030000,11.090000,10.935972,5765400
|
||||
2019-04-02,11.160000,11.260000,11.100000,11.180000,11.024722,7661600
|
||||
2019-04-03,11.200000,11.280000,11.160000,11.200000,11.044444,7516300
|
||||
2019-04-04,11.090000,11.150000,11.000000,11.070000,10.916249,5658700
|
||||
2019-04-05,11.170000,11.360000,11.150000,11.320000,11.162777,9160300
|
||||
2019-04-08,11.280000,11.380000,11.270000,11.310000,11.152917,4759200
|
||||
2019-04-09,11.230000,11.260000,11.140000,11.150000,10.995138,15865500
|
||||
2019-04-10,11.120000,11.170000,11.040000,11.080000,10.926111,15151400
|
||||
2019-04-11,11.050000,11.070000,10.900000,10.970000,10.817639,19697600
|
||||
2019-04-12,10.600000,10.710000,10.450000,10.550000,10.403472,27590500
|
||||
2019-04-15,10.610000,10.610000,10.500000,10.570000,10.423194,15949200
|
||||
2019-04-16,10.560000,10.570000,10.360000,10.370000,10.225972,21431800
|
||||
2019-04-17,10.420000,10.460000,10.370000,10.410000,10.265416,10317700
|
||||
2019-04-18,10.450000,10.490000,10.340000,10.390000,10.245695,18307000
|
||||
2019-04-22,10.440000,10.470000,10.390000,10.450000,10.304861,7392500
|
||||
2019-04-23,10.480000,10.640000,10.480000,10.530000,10.383750,13314800
|
||||
2019-04-24,10.620000,10.700000,10.560000,10.580000,10.433056,12134700
|
||||
2019-04-25,10.570000,10.570000,10.400000,10.520000,10.373889,7030200
|
||||
2019-04-26,10.640000,10.680000,10.560000,10.650000,10.502083,5366900
|
||||
2019-04-29,10.690000,10.780000,10.660000,10.750000,10.600695,6956900
|
||||
2019-04-30,10.820000,10.880000,10.750000,10.760000,10.610556,8432300
|
||||
2019-05-01,10.810000,10.880000,10.760000,10.770000,10.620417,9212300
|
||||
2019-05-02,10.640000,10.720000,10.470000,10.570000,10.423194,12516500
|
||||
2019-05-03,10.560000,10.560000,10.070000,10.330000,10.186527,30580400
|
||||
2019-05-06,10.310000,10.520000,10.310000,10.450000,10.304861,13121800
|
||||
2019-05-07,10.440000,10.460000,10.290000,10.340000,10.196389,13262100
|
||||
2019-05-08,10.420000,10.430000,10.260000,10.390000,10.245695,11854500
|
||||
2019-05-09,10.340000,10.360000,10.190000,10.200000,10.058333,18123600
|
||||
2019-05-10,10.230000,10.270000,10.110000,10.260000,10.117500,19016300
|
||||
2019-05-13,10.170000,10.180000,10.040000,10.110000,9.969583,13309000
|
||||
2019-05-14,10.150000,10.290000,10.130000,10.210000,10.068194,5885600
|
||||
2019-05-15,10.230000,10.390000,10.190000,10.390000,10.245695,11611400
|
||||
2019-05-16,10.480000,10.560000,10.470000,10.510000,10.364028,8502800
|
||||
2019-05-17,10.400000,10.440000,10.150000,10.170000,10.028750,12427600
|
||||
2019-05-20,10.310000,10.360000,10.150000,10.200000,10.058333,17155700
|
||||
2019-05-21,10.200000,10.210000,10.120000,10.180000,10.038611,13841200
|
||||
2019-05-22,10.190000,10.320000,10.180000,10.220000,10.078055,8639200
|
||||
2019-05-23,10.080000,10.200000,10.010000,10.160000,10.018888,6115600
|
||||
2019-05-24,10.270000,10.350000,10.230000,10.230000,10.087916,10594200
|
||||
2019-05-28,10.400000,10.450000,10.300000,10.300000,10.156944,13101000
|
||||
2019-05-29,10.330000,10.340000,10.230000,10.270000,10.127361,9885000
|
||||
2019-05-30,10.380000,10.600000,10.370000,10.570000,10.423194,9560600
|
||||
2019-05-31,10.500000,10.530000,10.410000,10.470000,10.324583,8501000
|
||||
2019-06-03,10.560000,10.630000,10.410000,10.450000,10.304861,16517600
|
||||
2019-06-04,10.500000,10.600000,10.420000,10.580000,10.433056,8956200
|
||||
2019-06-05,10.560000,10.570000,10.430000,10.440000,10.294999,7705300
|
||||
2019-06-06,10.460000,10.580000,10.460000,10.530000,10.383750,5068500
|
||||
2019-06-07,10.520000,10.680000,10.520000,10.630000,10.482361,4587400
|
||||
2019-06-10,10.760000,10.810000,10.730000,10.780000,10.630278,6059100
|
||||
2019-06-11,10.840000,10.850000,10.720000,10.840000,10.689445,6582600
|
||||
2019-06-12,10.820000,10.900000,10.770000,10.800000,10.650001,7234800
|
||||
2019-06-13,10.630000,10.700000,10.540000,10.620000,10.620000,11253600
|
||||
2019-06-14,10.590000,10.670000,10.570000,10.600000,10.600000,4910900
|
||||
2019-06-17,10.520000,10.650000,10.470000,10.600000,10.600000,5351000
|
||||
2019-06-18,10.790000,10.830000,10.740000,10.750000,10.750000,9545700
|
||||
2019-06-19,10.780000,10.800000,10.590000,10.600000,10.600000,17172400
|
||||
2019-06-20,10.790000,10.800000,10.680000,10.770000,10.770000,14996300
|
||||
2019-06-21,10.720000,10.790000,10.680000,10.720000,10.720000,8959000
|
||||
2019-06-24,10.720000,10.770000,10.650000,10.680000,10.680000,6824800
|
||||
2019-06-25,10.700000,10.700000,10.610000,10.650000,10.650000,8018600
|
||||
2019-06-26,10.660000,10.680000,10.600000,10.660000,10.660000,4425400
|
||||
2019-06-27,10.590000,10.670000,10.520000,10.620000,10.620000,7636900
|
||||
2019-06-28,10.640000,10.710000,10.630000,10.700000,10.700000,6315300
|
||||
2019-07-01,10.700000,10.700000,10.620000,10.700000,10.700000,8902200
|
||||
2019-07-02,10.730000,10.790000,10.720000,10.760000,10.760000,7007200
|
||||
2019-07-03,10.690000,10.770000,10.670000,10.740000,10.740000,6879500
|
||||
2019-07-05,10.570000,10.700000,10.540000,10.690000,10.690000,18833300
|
||||
2019-07-08,10.600000,10.620000,10.550000,10.560000,10.560000,13564300
|
||||
2019-07-09,10.540000,10.550000,10.370000,10.420000,10.420000,23515700
|
||||
2019-07-10,10.550000,10.610000,10.440000,10.480000,10.480000,14954300
|
||||
2019-07-11,10.570000,10.720000,10.530000,10.720000,10.720000,15798500
|
||||
2019-07-12,11.340000,11.560000,11.270000,11.400000,11.400000,41385700
|
||||
|
@@ -0,0 +1,252 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-07-13,37.910000,38.630001,37.910000,37.910000,37.629288,1768500
|
||||
2018-07-16,39.060001,39.139999,35.810001,36.360001,36.090759,6443500
|
||||
2018-07-17,36.459999,36.860001,35.970001,36.279999,36.011356,3448800
|
||||
2018-07-18,36.610001,37.580002,36.259998,37.500000,37.222321,3257200
|
||||
2018-07-19,37.200001,38.130001,37.180000,37.990002,37.708694,2428500
|
||||
2018-07-20,37.980000,38.200001,37.639999,37.980000,37.698772,2091800
|
||||
2018-07-23,37.820000,38.259998,37.619999,38.240002,37.956844,1848700
|
||||
2018-07-24,38.950001,39.119999,34.369999,34.840000,34.582024,6131100
|
||||
2018-07-25,35.540001,35.900002,34.810001,35.810001,35.544838,4187200
|
||||
2018-07-26,31.049999,32.290001,29.000000,32.250000,32.011204,20546000
|
||||
2018-07-27,31.980000,32.299999,31.469999,32.090000,31.852381,5144500
|
||||
2018-07-30,31.990000,32.759998,31.799999,32.209999,31.971493,2951100
|
||||
2018-07-31,32.240002,33.090000,32.060001,32.549999,32.308971,3225200
|
||||
2018-08-01,32.549999,32.630001,31.959999,32.090000,31.852381,3427700
|
||||
2018-08-02,32.209999,33.380001,32.150002,33.270000,33.023647,4617000
|
||||
2018-08-03,33.279999,33.279999,32.209999,32.419998,32.179935,3282000
|
||||
2018-08-06,32.330002,32.860001,31.920000,32.700001,32.457867,2558700
|
||||
2018-08-07,32.779999,33.459999,32.529999,33.439999,33.192387,1876600
|
||||
2018-08-08,33.270000,33.400002,32.849998,32.980000,32.735790,1581300
|
||||
2018-08-09,32.840000,33.400002,32.720001,32.790001,32.547199,1818700
|
||||
2018-08-10,32.720001,33.380001,32.270000,33.220001,32.974014,1738800
|
||||
2018-08-13,33.330002,33.480000,32.410000,32.580002,32.338757,2865000
|
||||
2018-08-14,32.720001,33.270000,32.689999,32.770000,32.527348,1705600
|
||||
2018-08-15,32.650002,32.700001,31.570000,32.380001,32.140240,1734900
|
||||
2018-08-16,32.700001,32.900002,32.369999,32.700001,32.457867,1700900
|
||||
2018-08-17,32.730000,33.020000,32.360001,32.900002,32.656387,1677500
|
||||
2018-08-20,32.970001,33.580002,32.560001,33.540001,33.291645,2835500
|
||||
2018-08-21,33.680000,34.080002,33.439999,33.509998,33.261868,2393400
|
||||
2018-08-22,33.450001,33.500000,32.580002,32.770000,32.527348,1920800
|
||||
2018-08-23,32.830002,33.049999,32.360001,32.700001,32.457867,1559300
|
||||
2018-08-24,32.810001,33.160000,32.680000,32.810001,32.567055,888700
|
||||
2018-08-27,33.099998,33.779999,33.000000,33.740002,33.490166,1934500
|
||||
2018-08-28,33.869999,33.880001,33.389999,33.580002,33.331356,2569800
|
||||
2018-08-29,33.599998,34.279999,33.410000,34.209999,33.956684,2601400
|
||||
2018-08-30,34.169998,34.680000,33.689999,33.709999,33.519176,2322400
|
||||
2018-08-31,33.680000,34.340000,33.509998,34.130001,33.936794,2122500
|
||||
2018-09-04,34.000000,34.410000,33.619999,34.410000,34.215214,3660600
|
||||
2018-09-05,34.259998,35.290001,34.029999,35.250000,35.050457,2425000
|
||||
2018-09-06,35.099998,35.240002,34.799999,34.930000,34.732269,4373600
|
||||
2018-09-07,34.799999,35.689999,34.799999,35.270000,35.070347,2026000
|
||||
2018-09-10,35.580002,37.509998,35.389999,36.840000,36.631454,4462200
|
||||
2018-09-11,36.770000,37.029999,36.070000,36.330002,36.124344,4345300
|
||||
2018-09-12,36.230000,36.340000,35.730000,36.070000,35.865818,2350000
|
||||
2018-09-13,36.320000,36.549999,35.910000,36.040001,35.835987,2045000
|
||||
2018-09-14,36.060001,36.560001,35.939999,36.380001,36.174061,1378600
|
||||
2018-09-17,36.480000,36.480000,35.459999,35.630001,35.428307,1849300
|
||||
2018-09-18,35.919998,36.660000,35.529999,36.590000,36.382870,1803000
|
||||
2018-09-19,36.580002,36.790001,36.349998,36.500000,36.293385,1788600
|
||||
2018-09-20,36.630001,36.910000,36.139999,36.180000,35.975193,2553500
|
||||
2018-09-21,36.230000,36.240002,35.000000,35.090000,34.891365,3324000
|
||||
2018-09-24,34.900002,35.000000,34.220001,34.740002,34.543346,2124200
|
||||
2018-09-25,34.810001,34.959999,34.250000,34.910000,34.712383,1525800
|
||||
2018-09-26,35.000000,35.860001,34.700001,35.240002,35.040516,1816500
|
||||
2018-09-27,35.419998,35.630001,34.840000,34.919998,34.722324,1952500
|
||||
2018-09-28,34.509998,34.990002,34.320000,34.480000,34.284817,1874100
|
||||
2018-10-01,34.869999,35.189999,34.599998,34.889999,34.692497,1822400
|
||||
2018-10-02,35.099998,35.099998,33.150002,33.189999,33.002117,2314000
|
||||
2018-10-03,33.240002,33.799999,32.869999,33.500000,33.310364,2390900
|
||||
2018-10-04,33.509998,33.740002,32.639999,32.830002,32.644157,1615500
|
||||
2018-10-05,32.840000,32.860001,31.629999,32.110001,31.928234,3305300
|
||||
2018-10-08,32.070000,32.259998,31.639999,32.160000,31.977951,1675500
|
||||
2018-10-09,32.110001,32.990002,32.009998,32.540001,32.355801,2296400
|
||||
2018-10-10,32.410000,32.770000,31.670000,31.690001,31.510611,2352000
|
||||
2018-10-11,31.680000,32.279999,30.680000,30.719999,30.546101,3726900
|
||||
2018-10-12,31.070000,31.520000,30.770000,30.969999,30.794683,3017100
|
||||
2018-10-15,30.920000,31.410000,30.639999,31.070000,30.894119,2133400
|
||||
2018-10-16,31.469999,32.320000,31.180000,32.310001,32.127106,1960300
|
||||
2018-10-17,32.410000,32.560001,30.990000,31.190001,31.013441,2147800
|
||||
2018-10-18,31.049999,31.240000,29.690001,29.750000,29.581593,3346600
|
||||
2018-10-19,30.139999,31.070000,29.920000,30.410000,30.237854,2827200
|
||||
2018-10-22,30.100000,30.889999,30.000000,30.549999,30.377062,2048900
|
||||
2018-10-23,30.070000,30.520000,29.670000,30.200001,30.029047,2316300
|
||||
2018-10-24,33.990002,33.990002,30.480000,30.719999,30.546101,6106100
|
||||
2018-10-25,31.450001,33.549999,31.190001,33.389999,33.200985,4700800
|
||||
2018-10-26,32.880001,33.259998,31.809999,32.009998,31.828798,3329600
|
||||
2018-10-29,32.529999,32.759998,30.969999,31.389999,31.212307,2003600
|
||||
2018-10-30,31.360001,32.590000,31.110001,32.560001,32.375690,1857600
|
||||
2018-10-31,32.950001,33.270000,31.920000,32.000000,31.818855,2221000
|
||||
2018-11-01,32.130001,33.709999,31.549999,33.650002,33.459515,3066700
|
||||
2018-11-02,33.900002,34.000000,32.900002,33.650002,33.459515,2108100
|
||||
2018-11-05,33.779999,34.090000,33.020000,33.930000,33.737930,1931000
|
||||
2018-11-06,33.759998,34.320000,33.490002,33.889999,33.698158,1680400
|
||||
2018-11-07,34.029999,34.340000,33.099998,33.590000,33.399857,2010800
|
||||
2018-11-08,34.310001,34.759998,33.480000,34.320000,34.125721,3230600
|
||||
2018-11-09,34.189999,34.279999,33.230000,33.480000,33.290478,2107300
|
||||
2018-11-12,33.450001,33.619999,32.820000,32.869999,32.683929,1396300
|
||||
2018-11-13,32.880001,34.209999,32.869999,33.720001,33.529118,2119600
|
||||
2018-11-14,34.049999,34.500000,33.279999,33.570000,33.379967,1175900
|
||||
2018-11-15,33.340000,33.520000,32.200001,32.619999,32.435345,2460300
|
||||
2018-11-16,32.320000,32.610001,31.280001,31.730000,31.550385,2587500
|
||||
2018-11-19,31.490000,32.560001,31.490000,32.220001,32.037613,1954600
|
||||
2018-11-20,32.279999,33.130001,31.360001,31.730000,31.550385,2181600
|
||||
2018-11-21,31.930000,32.869999,31.639999,32.820000,32.634212,1755500
|
||||
2018-11-23,32.500000,32.970001,32.189999,32.650002,32.465172,494100
|
||||
2018-11-26,32.990002,34.060001,32.959999,34.060001,33.867195,1819200
|
||||
2018-11-27,33.830002,34.180000,33.310001,33.360001,33.171158,1302400
|
||||
2018-11-28,33.470001,34.840000,32.950001,34.820000,34.622894,1720100
|
||||
2018-11-29,34.900002,35.490002,34.169998,34.349998,34.155548,1516900
|
||||
2018-11-30,34.209999,35.060001,34.209999,34.660000,34.524101,1975700
|
||||
2018-12-03,35.419998,35.500000,33.889999,33.959999,33.826847,1499300
|
||||
2018-12-04,33.189999,33.450001,30.360001,30.610001,30.489981,4417700
|
||||
2018-12-06,29.940001,30.740000,29.730000,30.709999,30.589588,3405500
|
||||
2018-12-07,30.709999,31.190001,28.680000,28.820000,28.707001,4329900
|
||||
2018-12-10,28.600000,28.850000,28.080000,28.650000,28.537666,2573400
|
||||
2018-12-11,29.059999,29.450001,28.350000,28.540001,28.428099,2111100
|
||||
2018-12-12,29.030001,29.240000,28.430000,28.490000,28.378292,2508900
|
||||
2018-12-13,28.500000,28.580000,27.190001,27.320000,27.212881,3105500
|
||||
2018-12-14,27.020000,27.400000,26.670000,26.879999,26.774605,3853900
|
||||
2018-12-17,26.850000,26.950001,26.309999,26.780001,26.674999,2630100
|
||||
2018-12-18,26.920000,27.620001,26.850000,27.110001,27.003704,3329000
|
||||
2018-12-19,26.889999,27.639999,25.980000,26.059999,25.957821,2541100
|
||||
2018-12-20,25.879999,26.110001,25.100000,25.260000,25.160959,3548900
|
||||
2018-12-21,25.190001,25.450001,23.780001,23.980000,23.885977,6748200
|
||||
2018-12-24,23.500000,24.430000,23.270000,24.030001,23.935781,1289900
|
||||
2018-12-26,24.040001,25.030001,23.629999,25.010000,24.911938,2031900
|
||||
2018-12-27,24.639999,25.100000,24.219999,25.100000,25.001585,1502800
|
||||
2018-12-28,25.129999,25.400000,24.730000,25.070000,24.971703,1568500
|
||||
2018-12-31,25.219999,25.330000,24.700001,25.070000,24.971703,1517200
|
||||
2019-01-02,24.719999,26.250000,24.650000,25.840000,25.738684,2191700
|
||||
2019-01-03,25.670000,25.940001,25.030001,25.209999,25.111153,1971300
|
||||
2019-01-04,25.740000,26.600000,25.639999,26.540001,26.435940,2149300
|
||||
2019-01-07,26.580000,27.660000,26.500000,27.379999,27.272646,2917300
|
||||
2019-01-08,27.799999,28.639999,27.680000,28.629999,28.517742,2525300
|
||||
2019-01-09,28.650000,29.730000,28.549999,29.580000,29.464020,3723300
|
||||
2019-01-10,29.530001,29.530001,28.530001,28.840000,28.726921,2391000
|
||||
2019-01-11,28.830000,29.110001,28.350000,28.650000,28.537666,1608000
|
||||
2019-01-14,28.100000,29.469999,28.100000,29.230000,29.115391,2348400
|
||||
2019-01-15,29.280001,29.400000,28.629999,28.840000,28.726921,1840400
|
||||
2019-01-16,29.059999,29.540001,28.959999,29.170000,29.055626,1168600
|
||||
2019-01-17,31.049999,32.090000,30.290001,31.700001,31.575708,6283500
|
||||
2019-01-18,32.410000,33.139999,31.900000,32.639999,32.512020,4627600
|
||||
2019-01-22,32.110001,32.299999,30.900000,30.969999,30.848568,3325000
|
||||
2019-01-23,31.299999,31.510000,30.080000,30.440001,30.320648,2273200
|
||||
2019-01-24,30.420000,30.629999,29.760000,29.809999,29.693117,2008700
|
||||
2019-01-25,30.209999,31.020000,29.940001,30.780001,30.659315,2170800
|
||||
2019-01-28,30.450001,30.480000,29.900000,30.080000,29.962059,2141300
|
||||
2019-01-29,30.370001,32.200001,29.340000,32.110001,31.984100,3769300
|
||||
2019-01-30,32.209999,33.419998,31.400000,33.369999,33.239159,4151600
|
||||
2019-01-31,33.400002,33.400002,31.559999,31.750000,31.625511,3374600
|
||||
2019-02-01,32.090000,32.590000,31.520000,32.230000,32.103630,1933400
|
||||
2019-02-04,32.049999,32.270000,31.680000,32.250000,32.123554,1355500
|
||||
2019-02-05,32.349998,32.889999,31.799999,31.879999,31.755001,1719200
|
||||
2019-02-06,31.950001,32.320000,31.420000,31.469999,31.346609,1355300
|
||||
2019-02-07,31.430000,32.150002,31.379999,31.700001,31.575708,1395400
|
||||
2019-02-08,31.299999,31.760000,30.780001,31.100000,30.978060,2103700
|
||||
2019-02-11,31.280001,31.440001,30.690001,31.420000,31.296806,1575100
|
||||
2019-02-12,31.620001,32.700001,31.450001,32.360001,32.233120,1847700
|
||||
2019-02-13,32.490002,33.680000,32.490002,33.490002,33.358692,2413800
|
||||
2019-02-14,33.310001,34.299999,33.099998,34.009998,33.876648,2926300
|
||||
2019-02-15,34.090000,34.130001,33.320000,33.529999,33.398533,3065800
|
||||
2019-02-19,33.450001,34.480000,33.340000,34.200001,34.065907,2861900
|
||||
2019-02-20,34.330002,35.299999,33.970001,34.720001,34.583866,3875700
|
||||
2019-02-21,34.759998,34.770000,33.820000,34.189999,34.055943,1396000
|
||||
2019-02-22,34.310001,34.459999,33.400002,33.599998,33.468254,1446800
|
||||
2019-02-25,33.869999,34.630001,33.720001,33.930000,33.796963,1427000
|
||||
2019-02-26,33.840000,34.220001,33.529999,33.680000,33.547947,819100
|
||||
2019-02-27,33.720001,34.049999,33.400002,33.869999,33.737198,842400
|
||||
2019-02-28,33.810001,33.950001,33.310001,33.630001,33.498142,999200
|
||||
2019-03-01,34.000000,34.230000,33.470001,33.680000,33.607906,1096100
|
||||
2019-03-04,33.919998,34.310001,33.320000,33.669998,33.597923,1364500
|
||||
2019-03-05,33.700001,33.700001,32.830002,32.860001,32.789661,1108300
|
||||
2019-03-06,32.950001,33.480000,32.770000,33.180000,33.108974,1586700
|
||||
2019-03-07,32.980000,33.130001,32.619999,32.889999,32.819595,1775600
|
||||
2019-03-08,32.430000,32.560001,31.750000,32.459999,32.390514,1760500
|
||||
2019-03-11,32.360001,33.889999,32.340000,33.869999,33.797497,1961200
|
||||
2019-03-12,33.860001,33.980000,33.180000,33.799999,33.727646,1483700
|
||||
2019-03-13,34.049999,34.830002,33.770000,34.459999,34.386234,1529600
|
||||
2019-03-14,34.400002,34.900002,34.130001,34.770000,34.695572,2083200
|
||||
2019-03-15,34.820000,34.970001,33.580002,33.939999,33.867348,3955300
|
||||
2019-03-18,34.189999,34.700001,34.009998,34.590000,34.515957,1105800
|
||||
2019-03-19,34.380001,34.380001,32.680000,32.849998,32.779682,2526600
|
||||
2019-03-20,32.799999,32.950001,30.990000,31.870001,31.801781,4073000
|
||||
2019-03-21,31.840000,32.259998,31.580000,32.230000,32.161007,2844400
|
||||
2019-03-22,32.060001,32.130001,31.160000,31.370001,31.302851,1802800
|
||||
2019-03-25,31.330000,32.049999,31.100000,31.320000,31.252956,1843000
|
||||
2019-03-26,31.620001,32.160000,31.559999,32.060001,31.991375,1897800
|
||||
2019-03-27,32.130001,32.259998,31.299999,31.379999,31.312828,2092600
|
||||
2019-03-28,31.379999,32.750000,31.290001,32.450001,32.380539,3897800
|
||||
2019-03-29,32.610001,32.910000,32.279999,32.680000,32.610046,1914300
|
||||
2019-04-01,32.990002,33.439999,32.799999,33.119999,33.049103,2025600
|
||||
2019-04-02,32.990002,33.290001,32.349998,32.520000,32.450390,1398800
|
||||
2019-04-03,32.540001,33.119999,32.500000,32.689999,32.620022,1430900
|
||||
2019-04-04,32.549999,33.470001,32.520000,33.299999,33.228718,1670600
|
||||
2019-04-05,33.290001,33.799999,33.189999,33.320000,33.248676,1189900
|
||||
2019-04-08,32.930000,33.500000,32.750000,33.480000,33.408333,1133000
|
||||
2019-04-09,33.250000,33.320000,32.840000,32.919998,32.849529,1556800
|
||||
2019-04-10,32.959999,33.200001,32.669998,33.090000,33.019169,648400
|
||||
2019-04-11,33.090000,33.820000,32.990002,33.700001,33.627865,1161600
|
||||
2019-04-12,34.020000,34.389999,33.779999,34.080002,34.007050,1042800
|
||||
2019-04-15,33.930000,34.029999,33.259998,33.529999,33.458225,1235200
|
||||
2019-04-16,32.840000,33.720001,32.369999,33.580002,33.508121,2495800
|
||||
2019-04-17,34.000000,34.730000,33.740002,34.549999,34.476044,2568000
|
||||
2019-04-18,34.650002,35.349998,34.500000,34.720001,34.645679,2501900
|
||||
2019-04-22,34.549999,34.910000,34.270000,34.700001,34.625721,1616100
|
||||
2019-04-23,34.650002,35.049999,34.349998,35.000000,34.925079,3498300
|
||||
2019-04-24,34.990002,36.810001,34.299999,36.150002,36.072620,4686400
|
||||
2019-04-25,35.880001,36.119999,34.549999,34.720001,34.645679,2598800
|
||||
2019-04-26,34.599998,35.610001,34.430000,35.240002,35.164566,1047500
|
||||
2019-04-29,35.290001,35.520000,34.060001,34.110001,34.036983,1933700
|
||||
2019-04-30,34.119999,34.150002,33.029999,33.349998,33.278610,2524500
|
||||
2019-05-01,33.250000,33.250000,31.299999,31.360001,31.292871,3751300
|
||||
2019-05-02,31.360001,32.290001,31.010000,32.259998,32.190945,1907500
|
||||
2019-05-03,32.419998,33.529999,32.180000,33.299999,33.228718,1793600
|
||||
2019-05-06,32.410000,33.450001,32.259998,33.279999,33.208759,2361000
|
||||
2019-05-07,32.779999,33.090000,31.860001,32.040001,31.971416,1001000
|
||||
2019-05-08,32.040001,32.490002,31.209999,31.270000,31.203064,1396400
|
||||
2019-05-09,31.020000,31.510000,30.590000,31.340000,31.272915,1854700
|
||||
2019-05-10,31.190001,31.740000,30.780001,31.480000,31.412615,944300
|
||||
2019-05-13,30.500000,30.549999,29.170000,29.340000,29.277195,2738400
|
||||
2019-05-14,29.459999,30.879999,29.430000,30.549999,30.484604,1437000
|
||||
2019-05-15,30.360001,31.080000,30.219999,30.990000,30.923664,1587700
|
||||
2019-05-16,31.139999,31.760000,31.100000,31.639999,31.572271,1783000
|
||||
2019-05-17,31.420000,31.760000,30.850000,30.930000,30.863792,1598000
|
||||
2019-05-20,30.400000,31.459999,30.150000,31.059999,30.993513,1070300
|
||||
2019-05-21,31.500000,32.000000,31.120001,31.440001,31.372700,1590100
|
||||
2019-05-22,31.219999,31.639999,30.600000,30.840000,30.773985,1191600
|
||||
2019-05-23,30.270000,30.870001,29.930000,30.400000,30.334927,1657700
|
||||
2019-05-24,30.590000,30.590000,29.469999,30.000000,29.935783,2216800
|
||||
2019-05-28,29.830000,30.059999,29.360001,29.660000,29.596510,1983100
|
||||
2019-05-29,29.320000,29.850000,28.969999,29.200001,29.137495,1853500
|
||||
2019-05-30,29.219999,29.309999,27.980000,28.030001,27.969999,2948600
|
||||
2019-05-31,27.430000,28.070000,27.030001,27.639999,27.639999,2361800
|
||||
2019-06-03,27.540001,28.549999,27.530001,28.090000,28.090000,3066200
|
||||
2019-06-04,28.600000,29.709999,28.600000,29.670000,29.670000,2052200
|
||||
2019-06-05,30.030001,30.330000,29.530001,30.120001,30.120001,1936400
|
||||
2019-06-06,29.940001,29.950001,29.020000,29.889999,29.889999,1473900
|
||||
2019-06-07,30.000000,30.430000,29.889999,30.170000,30.170000,1272700
|
||||
2019-06-10,30.420000,31.610001,30.370001,31.299999,31.299999,1975300
|
||||
2019-06-11,31.740000,31.740000,30.980000,31.059999,31.059999,1323700
|
||||
2019-06-12,30.910000,31.219999,30.559999,31.090000,31.090000,1730700
|
||||
2019-06-13,31.160000,32.689999,31.129999,32.660000,32.660000,2759100
|
||||
2019-06-14,32.520000,32.610001,31.750000,32.220001,32.220001,1644300
|
||||
2019-06-17,32.130001,32.230000,31.370001,31.549999,31.549999,1717700
|
||||
2019-06-18,31.600000,32.490002,31.420000,31.700001,31.700001,1292400
|
||||
2019-06-19,31.600000,32.029999,31.500000,31.870001,31.870001,1250600
|
||||
2019-06-20,32.340000,32.730000,32.029999,32.610001,32.610001,1395300
|
||||
2019-06-21,32.450001,32.720001,31.820000,31.889999,31.889999,1669400
|
||||
2019-06-24,31.889999,31.950001,29.900000,30.049999,30.049999,2932200
|
||||
2019-06-25,29.990000,30.590000,29.440001,29.549999,29.549999,3074100
|
||||
2019-06-26,29.790001,31.040001,29.610001,30.959999,30.959999,1668100
|
||||
2019-06-27,31.059999,31.680000,30.760000,31.440001,31.440001,1168800
|
||||
2019-06-28,31.680000,32.880001,31.670000,32.840000,32.840000,2573800
|
||||
2019-07-01,33.320000,33.759998,32.650002,32.939999,32.939999,1576200
|
||||
2019-07-02,32.860001,33.540001,32.439999,32.700001,32.700001,1624700
|
||||
2019-07-03,32.799999,33.240002,32.680000,33.220001,33.220001,750300
|
||||
2019-07-05,33.000000,33.680000,32.820000,33.590000,33.590000,897400
|
||||
2019-07-08,33.250000,33.669998,33.070000,33.369999,33.369999,1442200
|
||||
2019-07-09,33.060001,33.430000,32.959999,33.049999,33.049999,927500
|
||||
2019-07-10,33.230000,33.330002,31.570000,31.670000,31.670000,1873800
|
||||
2019-07-11,31.520000,32.230000,30.680000,31.490000,31.490000,1958800
|
||||
2019-07-12,31.490000,33.580002,31.420000,33.529999,33.529999,2295600
|
||||
|
@@ -0,0 +1,252 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-07-13,56.889999,56.889999,56.889999,56.889999,54.963757,0
|
||||
2018-07-16,56.639999,56.639999,56.639999,56.639999,54.722221,320
|
||||
2018-07-17,57.730000,57.730000,57.730000,57.730000,55.775318,538
|
||||
2018-07-18,57.810001,57.810001,57.810001,57.810001,55.852608,522
|
||||
2018-07-19,56.700001,57.279999,52.380001,52.380001,50.606461,2047
|
||||
2018-07-20,52.380001,52.380001,52.380001,52.380001,50.606461,0
|
||||
2018-07-23,52.380001,52.380001,52.380001,52.380001,50.606461,0
|
||||
2018-07-24,57.349998,57.349998,57.290001,57.290001,55.350212,349
|
||||
2018-07-25,57.290001,57.290001,57.290001,57.290001,55.350212,0
|
||||
2018-07-26,57.310001,57.509998,53.080002,57.509998,55.562763,728
|
||||
2018-07-27,57.590000,57.590000,57.590000,57.590000,55.640057,357
|
||||
2018-07-30,57.869999,57.869999,57.669998,57.669998,55.717346,294
|
||||
2018-07-31,58.549999,58.549999,58.549999,58.549999,56.567551,398
|
||||
2018-08-01,58.299999,58.590000,58.299999,58.590000,56.606197,451
|
||||
2018-08-02,57.189999,57.189999,57.189999,57.189999,55.253601,294
|
||||
2018-08-03,58.496101,58.496101,58.496101,58.496101,56.515476,663
|
||||
2018-08-06,58.496101,58.496101,58.496101,58.496101,56.515476,0
|
||||
2018-08-07,60.619999,60.619999,60.490002,60.490002,58.441868,638
|
||||
2018-08-08,58.000000,58.000000,58.000000,58.000000,56.036175,302
|
||||
2018-08-09,60.270000,60.470001,60.270000,60.470001,58.422546,1523
|
||||
2018-08-10,58.110001,58.400002,58.110001,58.400002,56.422630,766
|
||||
2018-08-13,57.770000,57.770000,56.000000,56.000000,54.103893,457
|
||||
2018-08-14,56.950001,57.330002,56.950001,57.330002,55.388863,844
|
||||
2018-08-15,57.330002,57.330002,57.330002,57.330002,55.388863,0
|
||||
2018-08-16,56.020000,56.049999,55.900002,55.900002,54.007278,839
|
||||
2018-08-17,56.439999,56.439999,56.439999,56.439999,54.528992,183
|
||||
2018-08-20,56.439999,56.439999,56.439999,56.439999,54.528992,0
|
||||
2018-08-21,56.439999,56.439999,56.439999,56.439999,54.528992,0
|
||||
2018-08-22,58.380001,58.380001,58.380001,58.380001,56.403309,430
|
||||
2018-08-23,57.410000,57.410000,57.410000,57.410000,55.942810,369
|
||||
2018-08-24,57.810001,58.009998,55.750000,55.750000,54.325233,2546
|
||||
2018-08-27,59.209999,59.209999,55.290001,55.290001,53.876991,657
|
||||
2018-08-28,58.720001,58.720001,58.720001,58.720001,57.219334,970
|
||||
2018-08-29,59.000000,59.000000,59.000000,59.000000,57.492176,1111
|
||||
2018-08-30,57.049999,57.049999,55.709999,55.709999,54.286255,594
|
||||
2018-08-31,55.689999,57.970001,55.689999,57.970001,56.488503,823
|
||||
2018-09-04,57.869999,58.070000,57.869999,58.070000,56.585945,541
|
||||
2018-09-05,58.070000,58.070000,58.070000,58.070000,56.585945,0
|
||||
2018-09-06,57.320000,57.320000,57.320000,57.320000,55.855110,1033
|
||||
2018-09-07,56.650002,56.650002,53.849998,53.849998,52.473789,411
|
||||
2018-09-10,53.849998,53.849998,53.849998,53.849998,52.473789,0
|
||||
2018-09-11,54.090000,54.090000,54.090000,54.090000,52.707657,361
|
||||
2018-09-12,56.450001,56.650002,54.000000,54.000000,52.619957,915
|
||||
2018-09-13,54.759998,57.250000,54.759998,57.250000,55.786900,530
|
||||
2018-09-14,56.162498,58.360001,56.162498,58.360001,56.868534,541
|
||||
2018-09-17,58.389999,58.590000,58.389999,58.590000,57.092655,422
|
||||
2018-09-18,57.919998,58.119999,57.919998,58.119999,56.634666,534
|
||||
2018-09-19,58.349998,58.349998,58.349998,58.349998,56.858788,155
|
||||
2018-09-20,58.869999,59.070000,58.869999,59.070000,57.560387,355
|
||||
2018-09-21,59.119999,59.320000,59.119999,59.320000,57.803997,552
|
||||
2018-09-24,59.320000,59.320000,59.320000,59.320000,57.803997,0
|
||||
2018-09-25,59.320000,59.320000,59.320000,59.320000,57.803997,0
|
||||
2018-09-26,58.689999,58.889999,58.689999,58.889999,57.384987,607
|
||||
2018-09-27,58.540001,58.540001,58.540001,58.540001,57.043934,393
|
||||
2018-09-28,58.540001,58.540001,58.540001,58.540001,57.043934,0
|
||||
2018-10-01,57.830002,57.849998,57.650002,57.650002,56.176678,610
|
||||
2018-10-02,57.330002,57.330002,57.330002,57.330002,55.864857,191
|
||||
2018-10-03,57.230000,57.230000,57.230000,57.230000,55.767410,628
|
||||
2018-10-04,54.630001,54.630001,54.630001,54.630001,53.233860,181
|
||||
2018-10-05,54.660000,54.860001,54.660000,54.860001,53.457981,280
|
||||
2018-10-08,51.759998,54.430000,51.560001,54.430000,53.038971,884
|
||||
2018-10-09,54.430000,54.430000,54.430000,54.430000,53.038971,0
|
||||
2018-10-10,47.779999,49.470001,47.779999,47.779999,46.558918,503
|
||||
2018-10-11,47.529999,49.630001,46.470001,49.630001,48.361641,3604
|
||||
2018-10-12,46.130001,46.130001,46.130001,46.130001,44.951088,2170
|
||||
2018-10-15,48.000000,48.349998,48.000000,48.349998,47.114349,934
|
||||
2018-10-16,49.500000,49.500000,49.500000,49.500000,48.234962,213
|
||||
2018-10-17,49.500000,49.500000,48.790001,48.790001,47.543106,489
|
||||
2018-10-18,47.372002,47.372002,47.372002,47.372002,46.161346,275
|
||||
2018-10-19,48.990002,48.990002,48.990002,48.990002,47.737999,352
|
||||
2018-10-22,46.090000,48.730000,46.090000,48.730000,47.484638,723
|
||||
2018-10-23,47.480000,47.480000,45.310001,45.310001,44.152042,341
|
||||
2018-10-24,45.310001,45.310001,45.310001,45.310001,44.152042,0
|
||||
2018-10-25,47.500000,47.500000,45.910000,45.910000,44.736710,506
|
||||
2018-10-26,45.910000,45.910000,45.910000,45.910000,44.736710,0
|
||||
2018-10-29,45.910000,45.910000,45.910000,45.910000,44.736710,0
|
||||
2018-10-30,45.509998,45.509998,45.509998,45.509998,44.346931,497
|
||||
2018-10-31,47.049999,47.049999,46.790001,46.790001,45.594219,591
|
||||
2018-11-01,47.049999,47.049999,47.049999,47.049999,45.847572,349
|
||||
2018-11-02,47.490002,47.500000,47.490002,47.494999,46.281200,1008
|
||||
2018-11-05,48.959999,48.959999,48.959999,48.959999,47.708763,455
|
||||
2018-11-06,50.000000,50.000000,47.540001,47.540001,46.325054,963
|
||||
2018-11-07,47.790001,47.790001,47.790001,47.790001,46.568665,288
|
||||
2018-11-08,47.790001,47.790001,47.790001,47.790001,46.568665,0
|
||||
2018-11-09,47.360001,47.360001,47.360001,47.360001,46.149654,268
|
||||
2018-11-12,44.029999,46.230000,43.880001,43.880001,42.758591,1785
|
||||
2018-11-13,46.689999,46.689999,46.689999,46.689999,45.496773,1146
|
||||
2018-11-14,47.779999,47.779999,47.779999,47.779999,46.558918,2250
|
||||
2018-11-15,47.700001,47.700001,47.700001,47.700001,46.480965,626
|
||||
2018-11-16,47.700001,47.700001,47.700001,47.700001,46.480965,0
|
||||
2018-11-19,47.700001,47.700001,47.700001,47.700001,46.480965,0
|
||||
2018-11-20,47.700001,47.700001,47.700001,47.700001,46.480965,0
|
||||
2018-11-21,45.430000,47.950001,45.430000,47.950001,46.724575,590
|
||||
2018-11-23,47.950001,47.950001,47.950001,47.950001,46.724575,0
|
||||
2018-11-26,46.470001,46.470001,46.470001,46.470001,45.282398,427
|
||||
2018-11-27,44.721401,45.910000,44.721401,45.910000,44.736710,934
|
||||
2018-11-28,43.810001,43.810001,43.810001,43.810001,42.690380,1041
|
||||
2018-11-29,46.049999,46.049999,46.049999,46.049999,44.873131,640
|
||||
2018-11-30,44.520000,44.520000,42.740002,42.740002,41.647724,658
|
||||
2018-12-03,46.330002,46.330002,44.650002,46.259998,45.077763,1756
|
||||
2018-12-04,44.080002,44.080002,44.080002,44.080002,42.953480,802
|
||||
2018-12-06,41.750000,43.169998,41.750000,43.169998,42.066730,1655
|
||||
2018-12-07,41.950001,42.450001,41.110001,41.110001,40.059380,2218
|
||||
2018-12-10,40.830002,42.189999,39.970001,39.970001,38.948513,1240
|
||||
2018-12-11,41.340000,42.549999,40.650002,42.549999,41.462578,22122
|
||||
2018-12-12,43.980000,43.990002,43.980000,43.990002,42.865780,681
|
||||
2018-12-13,42.230000,42.230000,42.230000,42.230000,41.150757,487
|
||||
2018-12-14,42.060001,42.060001,40.150002,40.150002,39.123917,653
|
||||
2018-12-17,39.599998,40.709999,39.490002,40.709999,39.669601,12561
|
||||
2018-12-18,39.570000,39.570000,39.570000,39.570000,38.558735,659
|
||||
2018-12-19,42.330002,42.330002,42.130001,42.130001,41.053314,833
|
||||
2018-12-20,39.639999,42.369999,39.639999,42.369999,41.287178,634
|
||||
2018-12-21,41.840000,41.840000,41.840000,41.840000,40.770725,682
|
||||
2018-12-24,42.380001,42.380001,42.380001,42.380001,41.296925,433
|
||||
2018-12-26,42.380001,42.380001,42.380001,42.380001,41.296925,0
|
||||
2018-12-27,41.340000,41.340000,41.340000,41.340000,40.283501,437
|
||||
2018-12-28,41.340000,41.340000,41.340000,41.340000,40.283501,0
|
||||
2018-12-31,44.299999,44.310001,44.189999,44.189999,43.060665,1769
|
||||
2019-01-02,43.169998,43.630001,43.169998,43.630001,42.514980,662
|
||||
2019-01-03,43.130001,43.130001,43.130001,43.130001,42.027756,303
|
||||
2019-01-04,43.584702,43.584702,43.584702,43.584702,42.470837,13482
|
||||
2019-01-07,45.169998,45.169998,45.160000,45.160000,44.005875,768
|
||||
2019-01-08,45.410000,45.410000,45.410000,45.410000,44.249489,500
|
||||
2019-01-09,45.410000,45.410000,45.410000,45.410000,44.249489,0
|
||||
2019-01-10,45.410000,45.410000,45.410000,45.410000,44.249489,0
|
||||
2019-01-11,46.500000,46.799999,46.500000,46.599998,45.409073,22858
|
||||
2019-01-14,46.599998,46.799999,46.599998,46.799999,45.603962,2155
|
||||
2019-01-15,46.799999,46.799999,46.799999,46.799999,45.603962,0
|
||||
2019-01-16,46.799999,46.799999,46.799999,46.799999,45.603962,269
|
||||
2019-01-17,49.349998,49.349998,49.349998,49.349998,48.088795,793
|
||||
2019-01-18,49.509998,49.549999,49.509998,49.549999,48.283684,506
|
||||
2019-01-22,47.119999,49.980000,47.119999,49.980000,48.702694,5183
|
||||
2019-01-23,49.950001,50.099998,47.279999,50.090000,48.809883,2687
|
||||
2019-01-24,49.360001,49.570000,49.360001,49.570000,48.303173,7566
|
||||
2019-01-25,48.540001,51.139999,48.540001,51.139999,49.833050,668
|
||||
2019-01-28,51.000000,51.000000,51.000000,51.000000,49.696629,1144
|
||||
2019-01-29,51.660000,51.660000,51.459999,51.660000,50.339760,562
|
||||
2019-01-30,52.110001,52.310001,51.750000,51.750000,50.427460,1681
|
||||
2019-01-31,50.669998,50.900002,50.400002,50.900002,49.599186,1109
|
||||
2019-02-01,49.794998,49.794998,49.794998,49.794998,48.522423,352
|
||||
2019-02-04,51.049999,51.259998,51.049999,51.259998,49.949982,772
|
||||
2019-02-05,51.250000,51.250000,51.250000,51.250000,49.940239,5765
|
||||
2019-02-06,51.250000,51.250000,51.250000,51.250000,49.940239,0
|
||||
2019-02-07,46.560001,46.560001,46.560001,46.560001,45.370098,543
|
||||
2019-02-08,49.259998,49.650002,49.259998,49.650002,48.381130,741
|
||||
2019-02-11,49.650002,49.650002,49.650002,49.650002,48.381130,0
|
||||
2019-02-12,50.029999,50.029999,50.029999,50.029999,48.751415,261
|
||||
2019-02-13,50.720001,50.919998,50.720001,50.919998,49.618671,551
|
||||
2019-02-14,50.919998,50.919998,50.919998,50.919998,49.618671,0
|
||||
2019-02-15,50.919998,50.919998,50.919998,50.919998,49.618671,0
|
||||
2019-02-19,50.919998,50.919998,50.919998,50.919998,49.618671,0
|
||||
2019-02-20,49.930000,49.930000,49.930000,49.930000,48.653973,339
|
||||
2019-02-21,51.230000,51.230000,51.230000,51.230000,49.920750,200
|
||||
2019-02-22,51.650002,51.919998,50.404999,51.919998,50.593113,756
|
||||
2019-02-25,51.919998,51.919998,51.919998,51.919998,50.593113,0
|
||||
2019-02-26,51.209999,52.520000,51.209999,52.520000,51.177784,614
|
||||
2019-02-27,50.220001,50.220001,50.220001,50.220001,48.936562,151
|
||||
2019-02-28,46.790001,48.320000,46.790001,48.320000,47.085117,460
|
||||
2019-03-01,48.320000,48.320000,48.320000,48.320000,47.085117,0
|
||||
2019-03-04,48.650002,48.650002,47.430000,47.430000,46.217865,639
|
||||
2019-03-05,47.430000,47.430000,47.430000,47.430000,46.217865,0
|
||||
2019-03-06,47.430000,47.430000,47.430000,47.430000,46.217865,0
|
||||
2019-03-07,49.330002,49.400002,48.029999,49.400002,48.137520,1703
|
||||
2019-03-08,49.400002,49.400002,49.400002,49.400002,48.137520,0
|
||||
2019-03-11,49.400002,49.400002,49.400002,49.400002,48.137520,0
|
||||
2019-03-12,49.400002,49.400002,49.400002,49.400002,48.137520,0
|
||||
2019-03-13,49.400002,49.400002,49.400002,49.400002,48.137520,0
|
||||
2019-03-14,48.619999,48.619999,48.619999,48.619999,47.377449,238
|
||||
2019-03-15,46.810001,48.970001,46.810001,48.970001,47.718510,21435
|
||||
2019-03-18,48.639999,49.060001,48.639999,49.060001,47.806210,3681
|
||||
2019-03-19,48.395000,49.830002,48.060001,48.060001,46.831764,3475
|
||||
2019-03-20,48.139999,49.820000,48.139999,48.750000,47.504128,2702
|
||||
2019-03-21,48.549999,48.750000,47.500000,48.750000,47.504128,1842
|
||||
2019-03-22,46.450001,46.450001,46.450001,46.450001,45.262909,7506
|
||||
2019-03-25,46.910000,46.910000,43.840000,46.759998,45.564983,1482
|
||||
2019-03-26,44.930000,46.060001,44.930000,46.060001,44.882877,284
|
||||
2019-03-27,46.060001,46.060001,46.060001,46.060001,44.882877,0
|
||||
2019-03-28,46.529999,46.529999,46.529999,46.529999,45.340862,369
|
||||
2019-03-29,46.529999,46.529999,46.529999,46.529999,45.340862,0
|
||||
2019-04-01,48.150002,48.150002,46.880001,46.880001,45.681919,2118
|
||||
2019-04-02,46.880001,46.880001,46.880001,46.880001,45.681919,0
|
||||
2019-04-03,48.310001,48.310001,47.139999,47.139999,45.935276,841
|
||||
2019-04-04,46.549999,46.599998,46.400002,46.400002,45.214188,18054
|
||||
2019-04-05,47.755001,48.970001,47.755001,48.970001,47.718510,923
|
||||
2019-04-08,48.025002,48.025002,48.025002,48.025002,46.797661,229
|
||||
2019-04-09,49.160000,49.160000,48.689999,48.689999,47.445660,407
|
||||
2019-04-10,47.200001,47.200001,47.200001,47.200001,45.993744,664
|
||||
2019-04-11,45.820000,45.820000,45.820000,45.820000,45.820000,352
|
||||
2019-04-12,45.820000,45.820000,45.820000,45.820000,45.820000,0
|
||||
2019-04-15,49.500000,49.680000,48.000000,49.680000,49.680000,1399
|
||||
2019-04-16,46.689999,46.689999,46.689999,46.689999,46.689999,363
|
||||
2019-04-17,45.389999,45.709999,44.827202,45.709999,45.709999,10278
|
||||
2019-04-18,47.099998,47.099998,45.904999,45.950001,45.950001,764
|
||||
2019-04-22,44.480000,44.480000,44.480000,44.480000,44.480000,299
|
||||
2019-04-23,47.209999,47.209999,47.209999,47.209999,47.209999,518
|
||||
2019-04-24,44.935001,44.935001,44.935001,44.935001,44.935001,448
|
||||
2019-04-25,46.180000,46.180000,46.180000,46.180000,46.180000,383
|
||||
2019-04-26,43.700001,45.990002,43.700001,45.990002,45.990002,337
|
||||
2019-04-29,45.119999,45.119999,45.119999,45.119999,45.119999,325
|
||||
2019-04-30,44.834999,44.834999,44.834999,44.834999,44.834999,371
|
||||
2019-05-01,44.500000,44.500000,44.500000,44.500000,44.500000,837
|
||||
2019-05-02,45.500000,45.500000,45.500000,45.500000,45.500000,512
|
||||
2019-05-03,45.500000,45.500000,45.500000,45.500000,45.500000,336
|
||||
2019-05-06,45.500000,45.500000,45.500000,45.500000,45.500000,377
|
||||
2019-05-07,44.110001,44.110001,43.500000,43.500000,43.500000,1538
|
||||
2019-05-08,42.299999,42.299999,42.299999,42.299999,42.299999,555
|
||||
2019-05-09,44.900002,44.900002,43.980000,43.980000,43.980000,557
|
||||
2019-05-10,42.779999,45.259998,42.779999,45.259998,45.259998,410
|
||||
2019-05-13,44.700001,44.700001,44.700001,44.700001,44.700001,523
|
||||
2019-05-14,42.299999,42.299999,42.299999,42.299999,42.299999,485
|
||||
2019-05-15,42.259998,42.259998,42.259998,42.259998,42.259998,487
|
||||
2019-05-16,42.259998,42.259998,42.259998,42.259998,42.259998,0
|
||||
2019-05-17,45.900002,45.900002,45.900002,45.900002,45.900002,210
|
||||
2019-05-20,43.500000,44.540001,43.500000,44.540001,44.540001,625
|
||||
2019-05-21,44.500000,44.500000,44.500000,44.500000,44.500000,381
|
||||
2019-05-22,43.435001,43.930000,43.435001,43.930000,43.930000,325
|
||||
2019-05-23,44.500000,44.500000,44.430000,44.430000,44.430000,969
|
||||
2019-05-24,43.470001,43.470001,43.299999,43.299999,43.299999,1168
|
||||
2019-05-28,43.270000,43.270000,43.270000,43.270000,43.270000,368
|
||||
2019-05-29,42.189999,42.189999,42.189999,42.189999,42.189999,490
|
||||
2019-05-30,43.669998,43.669998,42.959999,42.959999,42.959999,392
|
||||
2019-05-31,42.959999,42.959999,42.959999,42.959999,42.959999,0
|
||||
2019-06-03,42.959999,42.959999,42.959999,42.959999,42.959999,0
|
||||
2019-06-04,44.070000,44.070000,41.860001,41.860001,41.860001,1808
|
||||
2019-06-05,44.000000,44.340000,44.000000,44.340000,44.340000,779
|
||||
2019-06-06,41.500000,41.500000,41.500000,41.500000,41.500000,502
|
||||
2019-06-07,44.000000,44.500000,44.000000,44.250000,44.250000,4369
|
||||
2019-06-10,44.259998,44.259998,44.259998,44.259998,44.259998,2461
|
||||
2019-06-11,44.000000,44.099998,44.000000,44.099998,44.099998,3424
|
||||
2019-06-12,44.500000,44.500000,44.250000,44.500000,44.500000,1681
|
||||
2019-06-13,46.000000,46.000000,45.000000,45.750000,45.750000,1551
|
||||
2019-06-14,43.460999,45.189999,43.460999,45.189999,45.189999,6990
|
||||
2019-06-17,45.369999,45.369999,44.000000,44.750000,44.750000,1353
|
||||
2019-06-18,45.130001,45.130001,44.250000,44.250000,44.250000,734
|
||||
2019-06-19,45.750000,46.000000,44.450001,46.000000,46.000000,811
|
||||
2019-06-20,45.849998,45.849998,45.849998,45.849998,45.849998,553
|
||||
2019-06-21,45.750000,45.799999,45.750000,45.799999,45.799999,2475
|
||||
2019-06-24,46.000000,46.000000,46.000000,46.000000,46.000000,719
|
||||
2019-06-25,46.369999,46.369999,46.299999,46.299999,46.299999,985
|
||||
2019-06-26,46.369999,46.369999,45.759998,45.759998,45.759998,1021
|
||||
2019-06-27,45.759998,45.759998,45.759998,45.759998,45.759998,0
|
||||
2019-06-28,46.509998,46.509998,46.509998,46.509998,46.509998,6919
|
||||
2019-07-01,46.509998,46.509998,46.509998,46.509998,46.509998,0
|
||||
2019-07-02,46.310001,46.310001,44.860001,45.584999,45.584999,517
|
||||
2019-07-03,46.400002,46.400002,46.400002,46.400002,46.400002,1750
|
||||
2019-07-05,46.349998,46.400002,46.349998,46.400002,46.400002,422
|
||||
2019-07-08,46.400002,46.400002,46.400002,46.400002,46.400002,0
|
||||
2019-07-09,43.779999,43.779999,43.779999,43.779999,43.779999,610
|
||||
2019-07-10,44.775002,45.770000,44.700001,45.770000,45.770000,2156
|
||||
2019-07-11,42.860001,42.860001,42.860001,42.860001,42.860001,306
|
||||
2019-07-12,44.984402,46.509998,44.984402,45.450001,45.450001,492057
|
||||
|
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-05-23,31.610001,32.000000,30.870001,31.059999,31.059999,1499700
|
||||
2018-05-24,30.510000,30.879999,29.770000,29.790001,29.790001,1831300
|
||||
2018-05-25,28.820000,29.100000,28.200001,28.670000,28.670000,2599900
|
||||
2018-05-29,28.370001,28.879999,28.020000,28.480000,28.480000,1868700
|
||||
2018-05-30,28.790001,29.260000,28.580000,29.059999,29.059999,1941900
|
||||
2018-05-31,28.760000,29.309999,28.040001,28.070000,28.070000,2092000
|
||||
2018-06-01,28.070000,28.270000,25.570000,26.400000,26.400000,3859100
|
||||
2018-06-04,26.450001,26.650000,25.530001,25.780001,25.780001,2426600
|
||||
2018-06-05,25.570000,26.120001,25.469999,25.930000,25.930000,1845400
|
||||
2018-06-06,26.200001,26.270000,25.650000,25.910000,25.910000,1948100
|
||||
2018-06-07,26.040001,27.129999,26.040001,26.930000,26.930000,1887700
|
||||
2018-06-08,26.930000,27.150000,25.719999,25.969999,25.969999,2083700
|
||||
2018-06-11,25.879999,26.309999,25.670000,25.910000,25.910000,1346700
|
||||
2018-06-12,25.910000,26.760000,25.709999,26.629999,26.629999,2097000
|
||||
2018-06-13,26.530001,26.990000,26.370001,26.610001,26.610001,1487000
|
||||
2018-06-14,27.030001,27.080000,26.270000,26.430000,26.430000,1385400
|
||||
2018-06-15,26.209999,26.410000,25.799999,26.129999,26.129999,3438400
|
||||
2018-06-18,26.190001,26.850000,26.180000,26.299999,26.299999,1562200
|
||||
2018-06-19,25.799999,27.750000,25.790001,27.690001,27.690001,2623000
|
||||
2018-06-20,27.950001,28.580000,27.700001,28.440001,28.440001,2600100
|
||||
2018-06-21,28.070000,28.430000,26.870001,27.020000,27.020000,2158200
|
||||
2018-06-22,28.389999,28.600000,27.620001,27.860001,27.860001,2175900
|
||||
2018-06-25,27.900000,28.030001,27.219999,27.400000,27.400000,1367100
|
||||
2018-06-26,27.520000,28.920000,27.290001,28.809999,28.809999,2166000
|
||||
2018-06-27,29.190001,30.299999,29.120001,30.059999,30.059999,2816500
|
||||
2018-06-28,30.139999,30.379999,29.680000,30.139999,30.139999,2197500
|
||||
2018-06-29,30.480000,31.410000,30.000000,30.049999,30.049999,2761400
|
||||
2018-07-02,29.760000,29.770000,29.000000,29.209999,29.209999,1384700
|
||||
2018-07-03,30.049999,30.299999,29.280001,29.650000,29.650000,1132100
|
||||
2018-07-05,29.830000,30.020000,29.190001,29.879999,29.879999,1599100
|
||||
2018-07-06,29.629999,31.389999,29.580000,30.940001,30.940001,2125700
|
||||
2018-07-09,31.350000,32.619999,31.270000,32.520000,32.520000,2248800
|
||||
2018-07-10,32.919998,33.430000,32.700001,32.990002,32.990002,1932700
|
||||
2018-07-11,32.369999,33.180000,31.620001,31.730000,31.730000,1928400
|
||||
2018-07-12,31.879999,32.259998,31.209999,31.930000,31.930000,1773300
|
||||
2018-07-13,31.870001,32.599998,31.709999,31.809999,31.809999,943400
|
||||
2018-07-16,31.049999,31.770000,30.910000,31.670000,31.670000,1013800
|
||||
2018-07-17,31.520000,32.259998,31.340000,32.020000,32.020000,693200
|
||||
2018-07-18,31.709999,32.040001,31.030001,31.740000,31.740000,1187700
|
||||
2018-07-19,31.440001,32.400002,31.420000,32.330002,32.330002,1222300
|
||||
2018-07-20,32.410000,32.720001,31.809999,31.980000,31.980000,1304300
|
||||
2018-07-23,31.990000,32.299999,31.709999,31.940001,31.940001,1221200
|
||||
2018-07-24,32.480000,32.869999,32.209999,32.349998,32.349998,1098400
|
||||
2018-07-25,32.330002,32.910000,31.920000,32.830002,32.830002,1171200
|
||||
2018-07-26,32.970001,33.349998,32.759998,32.980000,32.980000,978700
|
||||
2018-07-27,32.770000,33.599998,32.700001,32.820000,32.820000,1240500
|
||||
2018-07-30,33.349998,33.860001,33.099998,33.529999,33.529999,1815300
|
||||
2018-07-31,33.470001,33.720001,32.820000,33.500000,33.500000,1345700
|
||||
2018-08-01,33.049999,33.410000,32.580002,33.189999,33.189999,1568000
|
||||
2018-08-02,32.150002,34.439999,31.700001,32.779999,32.779999,3111200
|
||||
2018-08-03,32.669998,33.080002,31.090000,31.139999,31.139999,2989500
|
||||
2018-08-06,31.290001,31.860001,31.000000,31.330000,31.330000,1450000
|
||||
2018-08-07,31.590000,32.529999,31.420000,31.980000,31.980000,1902600
|
||||
2018-08-08,31.750000,31.980000,31.090000,31.570000,31.570000,1681000
|
||||
2018-08-09,31.600000,32.410000,31.549999,32.009998,32.009998,1537200
|
||||
2018-08-10,31.950001,32.849998,31.889999,32.720001,32.720001,1065600
|
||||
2018-08-13,32.650002,32.840000,31.400000,31.420000,31.420000,1409000
|
||||
2018-08-14,31.820000,32.330002,31.400000,31.650000,31.650000,1176200
|
||||
2018-08-15,30.900000,31.370001,29.370001,29.790001,29.790001,1948800
|
||||
2018-08-16,30.030001,30.450001,29.870001,30.090000,30.090000,991800
|
||||
2018-08-17,30.219999,31.480000,30.219999,30.590000,30.590000,1029300
|
||||
2018-08-20,30.559999,31.020000,30.389999,30.520000,30.520000,700900
|
||||
2018-08-21,31.000000,31.940001,30.910000,31.670000,31.670000,1416600
|
||||
2018-08-22,32.000000,32.669998,31.889999,32.560001,32.560001,858000
|
||||
2018-08-23,32.310001,32.520000,31.990000,32.200001,32.200001,899500
|
||||
2018-08-24,32.590000,33.009998,32.400002,32.610001,32.610001,654200
|
||||
2018-08-27,32.650002,32.939999,32.560001,32.580002,32.580002,828000
|
||||
2018-08-28,32.630001,32.900002,32.009998,32.230000,32.230000,722500
|
||||
2018-08-29,32.459999,32.970001,32.259998,32.830002,32.830002,945200
|
||||
2018-08-30,32.849998,33.669998,32.820000,33.520000,33.520000,1031900
|
||||
2018-08-31,33.299999,33.430000,32.570000,32.740002,32.740002,991900
|
||||
2018-09-04,32.900002,33.080002,31.889999,31.980000,31.980000,972100
|
||||
2018-09-05,31.750000,31.850000,31.080000,31.809999,31.809999,1168600
|
||||
2018-09-06,31.860001,31.910000,31.150000,31.290001,31.290001,1264200
|
||||
2018-09-07,30.969999,31.200001,30.530001,30.820000,30.820000,1103400
|
||||
2018-09-10,31.059999,31.360001,30.740000,30.850000,30.850000,824500
|
||||
2018-09-11,30.690001,32.410000,30.690001,32.080002,32.080002,1062500
|
||||
2018-09-12,32.599998,33.570000,32.470001,33.459999,33.459999,1332900
|
||||
2018-09-13,32.470001,32.939999,30.809999,31.129999,31.129999,3468900
|
||||
2018-09-14,30.990000,31.459999,30.730000,31.049999,31.049999,1830600
|
||||
2018-09-17,31.150000,31.690001,30.629999,30.879999,30.879999,1261000
|
||||
2018-09-18,31.230000,31.870001,31.129999,31.740000,31.740000,1311600
|
||||
2018-09-19,31.850000,32.669998,31.820000,32.419998,32.419998,1039000
|
||||
2018-09-20,32.639999,32.820000,31.850000,32.110001,32.110001,813900
|
||||
2018-09-21,32.150002,32.669998,31.870001,32.520000,32.520000,2323100
|
||||
2018-09-24,33.209999,33.680000,32.389999,33.189999,33.189999,1279300
|
||||
2018-09-25,32.650002,33.509998,32.430000,32.860001,32.860001,1635200
|
||||
2018-09-26,32.480000,33.099998,32.020000,32.060001,32.060001,1021200
|
||||
2018-09-27,32.570000,33.240002,32.209999,33.009998,33.009998,1269000
|
||||
2018-09-28,32.880001,33.549999,32.880001,33.049999,33.049999,1029800
|
||||
2018-10-01,33.279999,33.450001,32.680000,33.000000,33.000000,1028200
|
||||
2018-10-02,33.099998,33.720001,32.939999,33.110001,33.110001,972900
|
||||
2018-10-03,33.279999,34.240002,33.029999,34.220001,34.220001,968900
|
||||
2018-10-04,34.040001,34.910000,33.849998,33.910000,33.910000,1554300
|
||||
2018-10-05,33.860001,34.220001,33.080002,33.509998,33.509998,1034600
|
||||
2018-10-08,33.130001,33.330002,32.509998,32.849998,32.849998,903400
|
||||
2018-10-09,33.049999,34.090000,32.950001,33.630001,33.630001,969200
|
||||
2018-10-10,33.660000,33.750000,31.790001,32.160000,32.160000,1684600
|
||||
2018-10-11,31.750000,31.790001,30.270000,30.299999,30.299999,2048400
|
||||
2018-10-12,30.980000,31.190001,30.129999,31.100000,31.100000,1460000
|
||||
2018-10-15,31.360001,31.770000,30.629999,31.559999,31.559999,850500
|
||||
2018-10-16,31.770000,32.220001,31.459999,32.070000,32.070000,959300
|
||||
2018-10-17,31.809999,32.130001,31.309999,31.980000,31.980000,1143400
|
||||
2018-10-18,31.500000,32.160000,31.260000,31.690001,31.690001,1279900
|
||||
2018-10-19,31.840000,32.529999,31.389999,31.740000,31.740000,1265600
|
||||
2018-10-22,31.620001,31.730000,30.930000,31.510000,31.510000,858400
|
||||
2018-10-23,30.820000,30.879999,29.440001,29.790001,29.790001,1893500
|
||||
2018-10-24,30.059999,30.370001,27.870001,27.879999,27.879999,1762400
|
||||
2018-10-25,28.330000,29.059999,27.820000,28.850000,28.850000,1260400
|
||||
2018-10-26,28.280001,29.370001,27.719999,28.840000,28.840000,1089000
|
||||
2018-10-29,28.940001,29.110001,27.379999,27.879999,27.879999,1834600
|
||||
2018-10-30,27.530001,28.889999,27.219999,28.840000,28.840000,1399200
|
||||
2018-10-31,29.299999,29.610001,28.750000,28.840000,28.840000,2103100
|
||||
2018-11-01,29.350000,30.000000,28.090000,28.920000,28.920000,3087600
|
||||
2018-11-02,28.969999,29.020000,27.230000,27.379999,27.379999,3000500
|
||||
2018-11-05,28.389999,28.389999,27.520000,28.129999,28.129999,1918200
|
||||
2018-11-06,28.170000,28.340000,27.410000,27.549999,27.549999,1018700
|
||||
2018-11-07,28.299999,28.790001,27.990000,28.690001,28.690001,1377400
|
||||
2018-11-08,28.610001,28.760000,26.700001,26.990000,26.990000,1534200
|
||||
2018-11-09,26.410000,26.750000,25.660000,26.559999,26.559999,1969100
|
||||
2018-11-12,26.900000,26.940001,24.760000,24.790001,24.790001,1675800
|
||||
2018-11-13,24.650000,25.080000,23.740000,23.830000,23.830000,2076100
|
||||
2018-11-14,24.400000,24.910000,23.840000,23.900000,23.900000,1851800
|
||||
2018-11-15,23.680000,24.850000,23.660000,24.680000,24.680000,1370900
|
||||
2018-11-16,24.530001,25.480000,24.450001,24.709999,24.709999,2441700
|
||||
2018-11-19,24.000000,24.750000,23.910000,24.450001,24.450001,1645000
|
||||
2018-11-20,23.879999,23.889999,22.600000,22.990000,22.990000,2149300
|
||||
2018-11-21,23.580000,23.790001,23.180000,23.389999,23.389999,1063900
|
||||
2018-11-23,22.230000,23.040001,22.200001,22.299999,22.299999,738200
|
||||
2018-11-26,22.799999,23.379999,22.530001,22.740000,22.740000,1646100
|
||||
2018-11-27,22.639999,22.799999,21.889999,21.940001,21.940001,1653900
|
||||
2018-11-28,22.139999,22.730000,21.430000,22.730000,22.730000,1846800
|
||||
2018-11-29,22.820000,23.600000,22.750000,23.340000,23.340000,1959500
|
||||
2018-11-30,22.969999,23.200001,22.379999,22.799999,22.799999,2318500
|
||||
2018-12-03,23.930000,24.660000,23.760000,24.360001,24.360001,2709400
|
||||
2018-12-04,24.360001,24.430000,23.340000,23.370001,23.370001,2100600
|
||||
2018-12-06,22.799999,22.799999,21.340000,21.740000,21.740000,2257900
|
||||
2018-12-07,22.200001,22.400000,21.250000,21.290001,21.290001,2365500
|
||||
2018-12-10,20.879999,21.410000,19.430000,19.690001,19.690001,2600900
|
||||
2018-12-11,19.760000,20.150000,18.980000,19.080000,19.080000,3237900
|
||||
2018-12-12,19.370001,19.900000,18.980000,19.090000,19.090000,3078400
|
||||
2018-12-13,18.940001,19.280001,18.340000,18.570000,18.570000,2852600
|
||||
2018-12-14,18.309999,18.430000,17.219999,17.370001,17.370001,3092700
|
||||
2018-12-17,17.200001,17.510000,16.670000,16.780001,16.780001,2342600
|
||||
2018-12-18,16.870001,17.250000,16.370001,16.490000,16.490000,2793400
|
||||
2018-12-19,16.520000,17.010000,15.930000,16.139999,16.139999,2146800
|
||||
2018-12-20,15.710000,16.270000,15.610000,15.640000,15.640000,2391400
|
||||
2018-12-21,15.560000,15.560000,14.570000,14.760000,14.760000,5661400
|
||||
2018-12-24,14.470000,14.750000,14.000000,14.120000,14.120000,1167100
|
||||
2018-12-26,14.340000,15.940000,13.970000,15.890000,15.890000,3433800
|
||||
2018-12-27,15.500000,15.840000,15.070000,15.830000,15.830000,2380700
|
||||
2018-12-28,15.920000,16.020000,15.440000,15.490000,15.490000,1809500
|
||||
2018-12-31,15.650000,15.830000,15.240000,15.530000,15.530000,1730900
|
||||
2019-01-02,15.080000,16.110001,14.800000,16.070000,16.070000,2266300
|
||||
2019-01-03,16.090000,16.430000,15.480000,15.930000,15.930000,2089900
|
||||
2019-01-04,16.370001,17.160000,16.100000,17.030001,17.030001,3369500
|
||||
2019-01-07,17.049999,18.450001,16.780001,18.260000,18.260000,3888100
|
||||
2019-01-08,18.650000,19.010000,18.219999,18.900000,18.900000,3145300
|
||||
2019-01-09,19.270000,19.799999,18.780001,19.760000,19.760000,2722500
|
||||
2019-01-10,19.410000,20.030001,19.230000,19.780001,19.780001,2355100
|
||||
2019-01-11,19.389999,19.480000,18.980000,19.340000,19.340000,2266100
|
||||
2019-01-14,18.950001,19.280001,18.469999,18.950001,18.950001,1899000
|
||||
2019-01-15,19.170000,19.620001,18.980000,19.430000,19.430000,1913900
|
||||
2019-01-16,19.219999,19.820000,19.219999,19.510000,19.510000,1581100
|
||||
2019-01-17,19.270000,19.469999,18.920000,19.410000,19.410000,1785900
|
||||
2019-01-18,19.700001,19.940001,19.299999,19.920000,19.920000,1552000
|
||||
2019-01-22,19.600000,19.600000,18.730000,18.809999,18.809999,2052300
|
||||
2019-01-23,19.030001,19.129999,18.320000,18.520000,18.520000,1795000
|
||||
2019-01-24,18.459999,18.799999,18.320000,18.719999,18.719999,1353000
|
||||
2019-01-25,18.830000,19.549999,18.799999,19.290001,19.290001,1976600
|
||||
2019-01-28,19.040001,19.040001,18.410000,18.889999,18.889999,1834900
|
||||
2019-01-29,19.080000,19.410000,18.730000,19.240000,19.240000,2004100
|
||||
2019-01-30,19.309999,19.900000,19.020000,19.860001,19.860001,1984900
|
||||
2019-01-31,20.070000,20.180000,19.280001,19.500000,19.500000,2414600
|
||||
2019-02-01,19.570000,19.820000,19.350000,19.660000,19.660000,1611400
|
||||
2019-02-04,19.400000,19.590000,19.209999,19.580000,19.580000,927100
|
||||
2019-02-05,19.530001,19.629999,18.840000,18.870001,18.870001,1731900
|
||||
2019-02-06,18.690001,18.980000,18.500000,18.590000,18.590000,1553500
|
||||
2019-02-07,18.389999,18.430000,17.200001,17.540001,17.540001,2794500
|
||||
2019-02-08,17.450001,17.680000,16.740000,17.260000,17.260000,2808200
|
||||
2019-02-11,17.100000,17.459999,16.840000,17.410000,17.410000,1507800
|
||||
2019-02-12,17.780001,18.049999,17.360001,17.639999,17.639999,1761500
|
||||
2019-02-13,17.790001,18.230000,17.660000,17.870001,17.870001,2321200
|
||||
2019-02-14,17.830000,18.420000,17.650000,18.219999,18.219999,1484400
|
||||
2019-02-15,18.500000,19.320000,18.389999,19.309999,19.309999,2972500
|
||||
2019-02-19,19.350000,19.540001,18.650000,18.700001,18.700001,1733800
|
||||
2019-02-20,18.700001,19.020000,18.459999,18.940001,18.940001,1868600
|
||||
2019-02-21,18.889999,18.990000,18.000000,18.059999,18.059999,2098300
|
||||
2019-02-22,18.280001,18.820000,18.209999,18.700001,18.700001,2014500
|
||||
2019-02-25,18.520000,18.959999,18.420000,18.850000,18.850000,2277300
|
||||
2019-02-26,18.930000,19.320000,17.809999,17.910000,17.910000,4242400
|
||||
2019-02-27,19.340000,19.879999,18.299999,18.990000,18.990000,5300300
|
||||
2019-02-28,19.059999,19.209999,18.570000,18.600000,18.600000,3536000
|
||||
2019-03-01,18.780001,19.160000,17.870001,18.030001,18.030001,3544400
|
||||
2019-03-04,18.219999,18.320000,17.629999,17.990000,17.990000,2658600
|
||||
2019-03-05,18.040001,18.350000,17.600000,18.320000,18.320000,2244000
|
||||
2019-03-06,18.180000,18.180000,17.320000,17.469999,17.469999,2802900
|
||||
2019-03-07,17.510000,17.629999,17.110001,17.180000,17.180000,1525100
|
||||
2019-03-08,17.000000,17.000000,16.379999,16.700001,16.700001,1805000
|
||||
2019-03-11,16.930000,17.340000,16.709999,17.170000,17.170000,1623100
|
||||
2019-03-12,17.320000,18.250000,17.209999,18.190001,18.190001,3135000
|
||||
2019-03-13,18.510000,19.059999,18.299999,18.600000,18.600000,3540400
|
||||
2019-03-14,18.540001,18.830000,18.480000,18.530001,18.530001,1879000
|
||||
2019-03-15,18.219999,18.860001,18.180000,18.690001,18.690001,3377500
|
||||
2019-03-18,18.690001,19.200001,18.639999,19.129999,19.129999,3175600
|
||||
2019-03-19,19.320000,19.480000,18.959999,19.059999,19.059999,3023300
|
||||
2019-03-20,18.959999,20.080000,18.900000,19.650000,19.650000,2775600
|
||||
2019-03-21,19.590000,20.260000,19.520000,20.000000,20.000000,2815100
|
||||
2019-03-22,19.629999,19.690001,18.590000,18.910000,18.910000,2727300
|
||||
2019-03-25,18.799999,18.940001,18.260000,18.840000,18.840000,1872500
|
||||
2019-03-26,19.280001,19.980000,19.170000,19.510000,19.510000,2142600
|
||||
2019-03-27,19.549999,19.959999,19.350000,19.620001,19.620001,2107500
|
||||
2019-03-28,19.440001,20.030001,19.400000,19.690001,19.690001,3751900
|
||||
2019-03-29,20.049999,20.129999,19.139999,19.330000,19.330000,2271100
|
||||
2019-04-01,19.590000,19.740000,19.320000,19.600000,19.600000,1413200
|
||||
2019-04-02,19.610001,19.930000,19.290001,19.459999,19.459999,2247900
|
||||
2019-04-03,19.520000,19.660000,17.930000,17.969999,17.969999,3014600
|
||||
2019-04-04,17.920000,18.660000,17.910000,18.480000,18.480000,3069100
|
||||
2019-04-05,18.540001,19.770000,18.540001,19.540001,19.540001,2691000
|
||||
2019-04-08,19.639999,20.379999,19.639999,19.940001,19.940001,2536900
|
||||
2019-04-09,19.920000,20.320000,19.610001,19.950001,19.950001,3005600
|
||||
2019-04-10,20.219999,20.350000,19.850000,20.280001,20.280001,1947400
|
||||
2019-04-11,20.070000,20.180000,18.750000,19.420000,19.420000,3460600
|
||||
2019-04-12,20.500000,21.000000,20.410000,20.950001,20.950001,3636400
|
||||
2019-04-15,20.780001,21.059999,20.500000,20.820000,20.820000,2207400
|
||||
2019-04-16,20.950001,21.200001,20.590000,21.049999,21.049999,1503000
|
||||
2019-04-17,21.250000,21.299999,20.760000,20.930000,20.930000,1458200
|
||||
2019-04-18,20.850000,21.070000,20.700001,20.820000,20.820000,1523100
|
||||
2019-04-22,21.200001,21.930000,20.930000,21.889999,21.889999,2105100
|
||||
2019-04-23,21.780001,22.250000,21.389999,21.799999,21.799999,1741000
|
||||
2019-04-24,22.000000,22.010000,21.190001,21.200001,21.200001,1594600
|
||||
2019-04-25,21.200001,21.370001,20.719999,20.730000,20.730000,1418400
|
||||
2019-04-26,20.400000,20.450001,19.450001,19.719999,19.719999,2245600
|
||||
2019-04-29,19.620001,19.930000,19.350000,19.780001,19.780001,1470700
|
||||
2019-04-30,20.049999,20.100000,19.299999,19.690001,19.690001,1680800
|
||||
2019-05-01,19.750000,19.900000,18.740000,18.740000,18.740000,3363700
|
||||
2019-05-02,18.549999,18.799999,17.760000,18.440001,18.440001,3526200
|
||||
2019-05-03,18.709999,19.230000,18.389999,19.150000,19.150000,1924400
|
||||
2019-05-06,18.570000,19.620001,18.450001,19.480000,19.480000,1713700
|
||||
2019-05-07,19.059999,19.180000,18.590000,19.020000,19.020000,1733000
|
||||
2019-05-08,19.090000,19.860001,19.080000,19.650000,19.650000,2025300
|
||||
2019-05-09,19.430000,20.090000,19.240000,19.930000,19.930000,1972500
|
||||
2019-05-10,19.750000,20.020000,19.230000,19.920000,19.920000,1598300
|
||||
2019-05-13,19.680000,19.959999,19.270000,19.330000,19.330000,2206800
|
||||
2019-05-14,19.490000,20.280001,19.370001,20.010000,20.010000,2359500
|
||||
2019-05-15,19.719999,20.820000,19.620001,20.620001,20.620001,1477800
|
||||
2019-05-16,20.780001,21.190001,20.540001,20.799999,20.799999,1627700
|
||||
2019-05-17,20.440001,20.590000,19.990000,20.000000,20.000000,1246600
|
||||
2019-05-20,19.990000,20.260000,19.850000,20.010000,20.010000,1166200
|
||||
2019-05-21,20.170000,20.750000,19.990000,20.709999,20.709999,1096500
|
||||
2019-05-22,20.549999,20.750000,19.629999,19.660000,19.660000,1844400
|
||||
2019-05-23,19.110001,19.110001,17.730000,17.799999,17.799999,2256823
|
||||
|
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-05-23,89.540001,92.650002,89.339996,90.220001,90.220001,846200
|
||||
2018-05-24,91.160004,92.150002,90.120003,90.620003,90.620003,819100
|
||||
2018-05-25,91.150002,91.690002,90.709999,90.949997,90.949997,550900
|
||||
2018-05-29,90.389999,91.870003,88.589996,89.989998,89.989998,1271300
|
||||
2018-05-30,90.300003,90.730003,89.620003,89.970001,89.970001,883600
|
||||
2018-05-31,90.330002,91.389999,90.150002,90.820000,90.820000,1300400
|
||||
2018-06-01,91.129997,92.690002,90.820000,92.239998,92.239998,713700
|
||||
2018-06-04,92.510002,93.660004,92.510002,93.330002,93.330002,843900
|
||||
2018-06-05,93.779999,94.620003,93.339996,94.430000,94.430000,1115800
|
||||
2018-06-06,94.760002,94.769997,91.900002,92.389999,92.389999,924100
|
||||
2018-06-07,92.870003,93.629997,91.750000,93.209999,93.209999,604400
|
||||
2018-06-08,92.720001,95.440002,92.360001,94.980003,94.980003,927800
|
||||
2018-06-11,95.260002,95.790001,92.529999,93.099998,93.099998,781900
|
||||
2018-06-12,93.709999,95.430000,93.709999,94.150002,94.150002,710900
|
||||
2018-06-13,94.790001,95.459999,94.190002,94.639999,94.639999,628300
|
||||
2018-06-14,94.570000,96.709999,93.269997,96.260002,96.260002,568300
|
||||
2018-06-15,95.790001,95.790001,94.360001,94.580002,94.580002,658400
|
||||
2018-06-18,93.669998,93.669998,91.809998,92.150002,92.150002,805700
|
||||
2018-06-19,90.040001,90.470001,88.309998,89.139999,89.139999,1323600
|
||||
2018-06-20,89.989998,90.580002,89.110001,90.019997,90.019997,799500
|
||||
2018-06-21,89.779999,89.779999,87.839996,88.000000,88.000000,1123000
|
||||
2018-06-22,89.279999,89.290001,87.300003,88.809998,88.809998,998400
|
||||
2018-06-25,87.230003,87.489998,85.029999,85.309998,85.309998,1093400
|
||||
2018-06-26,85.629997,86.019997,84.529999,85.330002,85.330002,942900
|
||||
2018-06-27,85.449997,85.970001,81.449997,81.580002,81.580002,1168700
|
||||
2018-06-28,80.500000,83.779999,79.690002,83.129997,83.129997,1271100
|
||||
2018-06-29,83.809998,85.269997,83.500000,84.690002,84.690002,1035500
|
||||
2018-07-02,83.150002,84.180000,82.160004,83.989998,83.989998,799400
|
||||
2018-07-03,85.209999,85.290001,83.459999,83.790001,83.790001,470200
|
||||
2018-07-05,83.790001,84.290001,81.919998,83.059998,83.059998,918300
|
||||
2018-07-06,83.000000,84.470001,82.169998,84.300003,84.300003,626200
|
||||
2018-07-09,85.029999,85.860001,83.720001,84.919998,84.919998,401900
|
||||
2018-07-10,85.220001,85.220001,84.199997,84.680000,84.680000,656700
|
||||
2018-07-11,83.599998,84.900002,83.180000,83.570000,83.570000,467800
|
||||
2018-07-12,84.500000,84.839996,83.750000,84.500000,84.500000,497400
|
||||
2018-07-13,84.279999,84.940002,83.580002,84.070000,84.070000,285500
|
||||
2018-07-16,83.919998,84.300003,83.230003,83.949997,83.949997,632300
|
||||
2018-07-17,83.360001,85.339996,83.220001,84.870003,84.870003,477300
|
||||
2018-07-18,84.889999,85.440002,83.470001,83.989998,83.989998,387600
|
||||
2018-07-19,83.570000,84.209999,82.589996,82.940002,82.940002,488100
|
||||
2018-07-20,83.599998,84.480003,82.980003,83.260002,83.260002,392100
|
||||
2018-07-23,83.260002,83.589996,82.459999,83.110001,83.110001,510800
|
||||
2018-07-24,84.110001,85.400002,82.769997,83.110001,83.110001,701100
|
||||
2018-07-25,83.500000,84.669998,83.150002,84.660004,84.660004,786000
|
||||
2018-07-26,83.629997,84.989998,83.400002,84.230003,84.230003,524300
|
||||
2018-07-27,84.120003,84.489998,82.930000,83.510002,83.510002,1004600
|
||||
2018-07-30,83.220001,83.220001,80.000000,80.690002,80.690002,1320800
|
||||
2018-07-31,80.559998,80.709999,79.620003,80.480003,80.480003,1054000
|
||||
2018-08-01,79.510002,81.900002,79.510002,80.260002,80.260002,635400
|
||||
2018-08-02,79.320000,80.070000,78.709999,79.900002,79.900002,474000
|
||||
2018-08-03,80.050003,80.370003,79.309998,79.620003,79.620003,382400
|
||||
2018-08-06,79.110001,80.459999,79.080002,79.989998,79.989998,585600
|
||||
2018-08-07,80.809998,81.199997,79.239998,80.529999,80.529999,1024700
|
||||
2018-08-08,85.000000,85.050003,74.360001,74.879997,74.879997,3154700
|
||||
2018-08-09,75.489998,76.209999,74.790001,75.059998,75.059998,1381600
|
||||
2018-08-10,75.489998,75.489998,73.529999,74.730003,74.730003,1671000
|
||||
2018-08-13,74.919998,75.279999,71.029999,71.300003,71.300003,1363900
|
||||
2018-08-14,71.070000,71.839996,69.599998,70.190002,70.190002,945500
|
||||
2018-08-15,68.000000,69.150002,67.470001,68.709999,68.709999,1542200
|
||||
2018-08-16,69.470001,69.839996,68.379997,68.430000,68.430000,989200
|
||||
2018-08-17,68.209999,69.230003,67.760002,69.010002,69.010002,756600
|
||||
2018-08-20,69.500000,70.790001,69.500000,70.389999,70.389999,566200
|
||||
2018-08-21,70.919998,71.669998,69.919998,70.290001,70.290001,775700
|
||||
2018-08-22,70.489998,71.400002,70.080002,70.889999,70.889999,587700
|
||||
2018-08-23,71.260002,71.930000,69.339996,69.510002,69.510002,909800
|
||||
2018-08-24,70.260002,70.489998,69.570000,69.849998,69.849998,302000
|
||||
2018-08-27,70.709999,72.360001,70.709999,71.589996,71.589996,465600
|
||||
2018-08-28,72.339996,72.940002,71.010002,72.070000,72.070000,711800
|
||||
2018-08-29,72.410004,72.870003,71.070000,72.000000,72.000000,579200
|
||||
2018-08-30,71.330002,71.610001,69.160004,69.650002,69.650002,646800
|
||||
2018-08-31,69.660004,71.500000,69.500000,70.959999,70.959999,570800
|
||||
2018-09-04,70.250000,70.570000,69.120003,70.370003,70.370003,754200
|
||||
2018-09-05,69.559998,69.690002,66.120003,66.779999,66.779999,1126100
|
||||
2018-09-06,67.000000,67.800003,66.110001,66.720001,66.720001,584800
|
||||
2018-09-07,66.000000,68.169998,65.580002,65.769997,65.769997,823900
|
||||
2018-09-10,65.870003,65.959999,64.309998,65.139999,65.139999,758500
|
||||
2018-09-11,64.050003,65.879997,63.220001,64.519997,64.519997,801400
|
||||
2018-09-12,64.500000,65.930000,63.200001,65.360001,65.360001,656500
|
||||
2018-09-13,66.209999,67.790001,66.209999,66.809998,66.809998,808000
|
||||
2018-09-14,67.309998,67.480003,65.540001,66.110001,66.110001,490700
|
||||
2018-09-17,65.360001,66.580002,64.639999,64.809998,64.809998,519900
|
||||
2018-09-18,64.919998,66.220001,64.220001,65.720001,65.720001,593200
|
||||
2018-09-19,66.139999,69.029999,66.089996,68.239998,68.239998,814700
|
||||
2018-09-20,68.940002,70.959999,68.800003,70.599998,70.599998,945700
|
||||
2018-09-21,72.000000,72.379997,70.180000,70.360001,70.360001,650400
|
||||
2018-09-24,69.220001,69.400002,68.000000,68.540001,68.540001,557100
|
||||
2018-09-25,68.339996,69.300003,68.209999,68.889999,68.889999,350800
|
||||
2018-09-26,68.910004,71.150002,68.910004,70.059998,70.059998,627200
|
||||
2018-09-27,70.510002,70.519997,68.739998,69.720001,69.720001,394200
|
||||
2018-09-28,69.239998,70.489998,68.750000,69.480003,69.480003,716300
|
||||
2018-10-01,69.559998,71.029999,69.559998,70.129997,70.129997,469300
|
||||
2018-10-02,69.129997,69.519997,66.610001,67.180000,67.180000,817800
|
||||
2018-10-03,67.690002,68.040001,67.279999,67.559998,67.559998,462100
|
||||
2018-10-04,67.220001,67.220001,64.099998,65.040001,65.040001,951900
|
||||
2018-10-05,65.120003,65.120003,62.610001,64.250000,64.250000,1339100
|
||||
2018-10-08,62.500000,65.050003,61.810001,64.150002,64.150002,1159800
|
||||
2018-10-09,63.939999,64.360001,62.270000,62.639999,62.639999,1440000
|
||||
2018-10-10,62.099998,62.130001,59.110001,59.180000,59.180000,1613600
|
||||
2018-10-11,58.400002,59.799999,57.849998,59.009998,59.009998,1689400
|
||||
2018-10-12,60.660000,62.689999,60.520000,62.500000,62.500000,909700
|
||||
2018-10-15,61.029999,63.279999,61.029999,62.419998,62.419998,617900
|
||||
2018-10-16,62.500000,63.900002,62.299999,63.840000,63.840000,529100
|
||||
2018-10-17,63.910000,64.410004,62.119999,63.000000,63.000000,721300
|
||||
2018-10-18,62.439999,62.439999,60.560001,60.709999,60.709999,811400
|
||||
2018-10-19,62.320000,62.910000,59.540001,59.849998,59.849998,462200
|
||||
2018-10-22,61.709999,63.639999,61.070000,62.270000,62.270000,794700
|
||||
2018-10-23,60.090000,62.220001,58.990002,61.299999,61.299999,661100
|
||||
2018-10-24,61.049999,61.509998,58.189999,58.209999,58.209999,900200
|
||||
2018-10-25,58.840000,60.200001,57.639999,59.650002,59.650002,612000
|
||||
2018-10-26,57.959999,61.950001,57.689999,61.369999,61.369999,626000
|
||||
2018-10-29,61.910000,61.910000,58.189999,59.049999,59.049999,713700
|
||||
2018-10-30,58.650002,59.470001,56.669998,58.799999,58.799999,873300
|
||||
2018-10-31,59.660000,63.439999,58.779999,63.310001,63.310001,829500
|
||||
2018-11-01,63.330002,68.059998,62.419998,67.360001,67.360001,1081700
|
||||
2018-11-02,67.760002,68.959999,65.870003,66.230003,66.230003,956100
|
||||
2018-11-05,64.919998,65.760002,64.010002,65.120003,65.120003,1091800
|
||||
2018-11-06,64.839996,67.870003,64.309998,65.750000,65.750000,1109700
|
||||
2018-11-07,65.750000,66.580002,64.489998,65.410004,65.410004,396700
|
||||
2018-11-08,64.620003,64.870003,63.009998,64.230003,64.230003,699100
|
||||
2018-11-09,63.250000,63.250000,60.689999,61.820000,61.820000,618200
|
||||
2018-11-12,61.680000,62.009998,60.110001,61.099998,61.099998,524500
|
||||
2018-11-13,61.750000,62.900002,60.840000,61.669998,61.669998,573800
|
||||
2018-11-14,62.400002,64.330002,61.299999,62.220001,62.220001,798800
|
||||
2018-11-15,62.910000,65.900002,62.299999,64.949997,64.949997,762900
|
||||
2018-11-16,64.209999,65.230003,62.790001,64.959999,64.959999,455400
|
||||
2018-11-19,63.549999,63.939999,60.500000,60.840000,60.840000,888700
|
||||
2018-11-20,59.020000,60.840000,57.840000,59.939999,59.939999,728200
|
||||
2018-11-21,61.419998,63.119999,61.360001,62.369999,62.369999,1008800
|
||||
2018-11-23,61.299999,62.080002,60.549999,60.740002,60.740002,280800
|
||||
2018-11-26,61.549999,63.250000,61.250000,62.450001,62.450001,908600
|
||||
2018-11-27,62.049999,62.450001,61.000000,61.900002,61.900002,749100
|
||||
2018-11-28,64.849998,66.800003,60.299999,63.009998,63.009998,1872200
|
||||
2018-11-29,62.279999,63.320000,61.529999,62.599998,62.599998,813000
|
||||
2018-11-30,62.380001,65.330002,61.799999,64.769997,64.769997,652600
|
||||
2018-12-03,67.720001,68.610001,66.949997,67.000000,67.000000,630800
|
||||
2018-12-04,67.309998,68.199997,65.250000,65.489998,65.489998,854000
|
||||
2018-12-06,63.209999,64.940002,62.630001,64.320000,64.320000,745800
|
||||
2018-12-07,64.099998,65.419998,63.040001,63.220001,63.220001,463800
|
||||
2018-12-10,62.900002,64.320000,62.150002,62.820000,62.820000,521200
|
||||
2018-12-11,63.889999,65.430000,62.720001,62.770000,62.770000,618900
|
||||
2018-12-12,64.169998,65.470001,63.279999,64.440002,64.440002,604000
|
||||
2018-12-13,65.050003,65.500000,61.939999,61.990002,61.990002,543100
|
||||
2018-12-14,61.070000,63.779999,60.470001,62.130001,62.130001,975800
|
||||
2018-12-17,62.139999,62.139999,59.340000,59.619999,59.619999,827500
|
||||
2018-12-18,60.099998,60.820000,58.770000,58.810001,58.810001,535000
|
||||
2018-12-19,58.770000,59.759998,55.189999,55.439999,55.439999,784400
|
||||
2018-12-20,55.340000,56.270000,53.560001,54.310001,54.310001,878500
|
||||
2018-12-21,54.209999,55.720001,53.000000,53.270000,53.270000,869000
|
||||
2018-12-24,52.889999,54.599998,52.169998,53.660000,53.660000,391800
|
||||
2018-12-26,54.040001,55.139999,52.450001,55.080002,55.080002,631300
|
||||
2018-12-27,53.869999,54.590000,52.639999,54.169998,54.169998,1189400
|
||||
2018-12-28,54.450001,55.869999,53.990002,54.580002,54.580002,580900
|
||||
2018-12-31,55.430000,55.970001,53.240002,53.639999,53.639999,496800
|
||||
2019-01-02,52.759998,55.139999,51.759998,54.750000,54.750000,474800
|
||||
2019-01-03,53.930000,54.189999,52.700001,53.299999,53.299999,499700
|
||||
2019-01-04,54.810001,58.410000,54.419998,57.680000,57.680000,568200
|
||||
2019-01-07,58.099998,59.919998,58.020000,59.639999,59.639999,559400
|
||||
2019-01-08,59.150002,60.169998,56.660000,57.849998,57.849998,1180700
|
||||
2019-01-09,59.680000,60.290001,57.930000,60.139999,60.139999,804600
|
||||
2019-01-10,59.889999,60.020000,58.419998,59.959999,59.959999,567000
|
||||
2019-01-11,59.669998,59.720001,58.070000,58.700001,58.700001,570000
|
||||
2019-01-14,57.889999,57.889999,56.259998,56.410000,56.410000,764000
|
||||
2019-01-15,56.830002,58.509998,56.770000,57.000000,57.000000,627500
|
||||
2019-01-16,57.299999,59.430000,57.299999,59.060001,59.060001,584200
|
||||
2019-01-17,58.400002,59.880001,57.889999,59.299999,59.299999,595800
|
||||
2019-01-18,59.840000,62.340000,59.840000,61.720001,61.720001,580800
|
||||
2019-01-22,60.549999,61.200001,55.150002,55.900002,55.900002,1130200
|
||||
2019-01-23,56.720001,57.029999,54.200001,54.509998,54.509998,861900
|
||||
2019-01-24,54.500000,55.450001,54.029999,54.610001,54.610001,1283300
|
||||
2019-01-25,55.750000,59.700001,55.630001,58.930000,58.930000,1052400
|
||||
2019-01-28,58.000000,58.970001,57.029999,58.939999,58.939999,681000
|
||||
2019-01-29,59.230000,59.660000,58.070000,58.590000,58.590000,693700
|
||||
2019-01-30,59.459999,59.459999,56.840000,58.060001,58.060001,647300
|
||||
2019-01-31,58.549999,61.639999,58.439999,61.419998,61.419998,971600
|
||||
2019-02-01,60.689999,61.389999,59.500000,60.740002,60.740002,434200
|
||||
2019-02-04,60.490002,60.950001,59.730000,59.959999,59.959999,312000
|
||||
2019-02-05,60.380001,60.580002,59.630001,60.029999,60.029999,448700
|
||||
2019-02-06,60.139999,61.040001,59.650002,59.900002,59.900002,540900
|
||||
2019-02-07,59.500000,59.660000,56.930000,57.369999,57.369999,690600
|
||||
2019-02-08,57.619999,58.939999,57.040001,58.779999,58.779999,553300
|
||||
2019-02-11,59.830002,60.450001,58.700001,59.820000,59.820000,688900
|
||||
2019-02-12,60.169998,62.029999,60.060001,61.639999,61.639999,787000
|
||||
2019-02-13,62.000000,63.959999,62.000000,62.230000,62.230000,1028300
|
||||
2019-02-14,62.070000,62.200001,60.099998,61.040001,61.040001,991700
|
||||
2019-02-15,61.070000,61.490002,59.889999,60.400002,60.400002,512000
|
||||
2019-02-19,60.299999,61.840000,59.520000,61.340000,61.340000,657200
|
||||
2019-02-20,61.610001,64.459999,61.450001,62.950001,62.950001,647400
|
||||
2019-02-21,63.040001,63.500000,61.840000,62.970001,62.970001,575000
|
||||
2019-02-22,63.500000,65.800003,63.139999,65.610001,65.610001,828600
|
||||
2019-02-25,68.519997,70.830002,67.800003,68.989998,68.989998,1331900
|
||||
2019-02-26,66.800003,68.900002,66.800003,68.830002,68.830002,830800
|
||||
2019-02-27,68.150002,69.080002,67.410004,67.750000,67.750000,356300
|
||||
2019-02-28,67.669998,68.860001,66.830002,67.370003,67.370003,550800
|
||||
2019-03-01,68.160004,68.879997,66.730003,67.349998,67.349998,694500
|
||||
2019-03-04,68.120003,69.489998,65.779999,67.239998,67.239998,802900
|
||||
2019-03-05,63.500000,65.660004,62.330002,64.510002,64.510002,1492600
|
||||
2019-03-06,64.610001,65.139999,61.910000,62.639999,62.639999,1366300
|
||||
2019-03-07,62.490002,62.500000,58.459999,58.770000,58.770000,1180700
|
||||
2019-03-08,56.160000,57.959999,55.270000,56.959999,56.959999,1617600
|
||||
2019-03-11,58.169998,58.730000,57.599998,58.570000,58.570000,864100
|
||||
2019-03-12,59.200001,59.200001,57.669998,58.200001,58.200001,553300
|
||||
2019-03-13,58.279999,58.680000,57.669998,58.139999,58.139999,678700
|
||||
2019-03-14,57.939999,58.049999,56.970001,57.700001,57.700001,607700
|
||||
2019-03-15,58.049999,59.169998,57.950001,58.090000,58.090000,659900
|
||||
2019-03-18,58.200001,58.980000,57.730000,58.209999,58.209999,522400
|
||||
2019-03-19,58.599998,58.959999,58.150002,58.540001,58.540001,530400
|
||||
2019-03-20,58.160000,58.939999,57.200001,58.439999,58.439999,424500
|
||||
2019-03-21,57.939999,59.150002,57.900002,59.139999,59.139999,362400
|
||||
2019-03-22,58.220001,58.630001,57.580002,57.759998,57.759998,938300
|
||||
2019-03-25,57.700001,58.869999,57.080002,58.580002,58.580002,396600
|
||||
2019-03-26,58.790001,59.459999,57.900002,58.549999,58.549999,1102400
|
||||
2019-03-27,58.590000,58.990002,57.910000,57.910000,57.910000,1711700
|
||||
2019-03-28,57.580002,58.049999,56.549999,56.820000,56.820000,746900
|
||||
2019-03-29,57.930000,59.470001,57.599998,59.240002,59.240002,765000
|
||||
2019-04-01,60.000000,62.500000,59.770000,61.680000,61.680000,1182600
|
||||
2019-04-02,61.529999,62.369999,61.119999,61.700001,61.700001,1089000
|
||||
2019-04-03,63.259998,64.360001,62.770000,63.160000,63.160000,1098700
|
||||
2019-04-04,62.869999,64.870003,62.520000,64.839996,64.839996,989500
|
||||
2019-04-05,64.839996,66.660004,64.699997,66.320000,66.320000,866800
|
||||
2019-04-08,65.430000,66.489998,65.430000,66.480003,66.480003,465500
|
||||
2019-04-09,66.300003,66.449997,64.360001,64.589996,64.589996,899600
|
||||
2019-04-10,64.650002,64.970001,63.189999,64.459999,64.459999,584000
|
||||
2019-04-11,64.080002,64.720001,63.430000,63.919998,63.919998,545700
|
||||
2019-04-12,64.690002,65.250000,63.880001,64.430000,64.430000,425600
|
||||
2019-04-15,63.820000,64.360001,62.880001,63.779999,63.779999,658000
|
||||
2019-04-16,64.309998,64.889999,63.360001,63.730000,63.730000,747900
|
||||
2019-04-17,61.200001,65.449997,61.060001,64.739998,64.739998,1032300
|
||||
2019-04-18,64.300003,65.180000,63.700001,65.110001,65.110001,522400
|
||||
2019-04-22,64.570000,65.260002,64.169998,64.820000,64.820000,301400
|
||||
2019-04-23,65.139999,65.680000,64.470001,64.820000,64.820000,358800
|
||||
2019-04-24,64.699997,64.849998,63.599998,64.419998,64.419998,612600
|
||||
2019-04-25,63.869999,64.559998,62.939999,63.009998,63.009998,600300
|
||||
2019-04-26,63.570000,63.570000,62.310001,62.660000,62.660000,803900
|
||||
2019-04-29,62.099998,63.549999,62.099998,63.279999,63.279999,434100
|
||||
2019-04-30,62.889999,63.910000,62.759998,62.939999,62.939999,846100
|
||||
2019-05-01,63.220001,63.950001,62.910000,63.349998,63.349998,303500
|
||||
2019-05-02,63.049999,63.169998,62.000000,62.070000,62.070000,1138900
|
||||
2019-05-03,62.590000,63.340000,62.139999,62.900002,62.900002,804900
|
||||
2019-05-06,60.080002,60.560001,58.700001,59.919998,59.919998,1831200
|
||||
2019-05-07,60.110001,60.189999,57.150002,57.980000,57.980000,989800
|
||||
2019-05-08,58.529999,58.810001,56.830002,57.590000,57.590000,2106500
|
||||
2019-05-09,56.500000,57.340000,55.250000,57.000000,57.000000,1249000
|
||||
2019-05-10,56.779999,57.650002,55.009998,55.730000,55.730000,964700
|
||||
2019-05-13,53.889999,54.660000,53.110001,54.439999,54.439999,836500
|
||||
2019-05-14,56.020000,56.349998,53.930000,53.990002,53.990002,1041400
|
||||
2019-05-15,53.730000,54.299999,53.090000,53.930000,53.930000,657200
|
||||
2019-05-16,54.000000,54.549999,53.209999,53.480000,53.480000,812700
|
||||
2019-05-17,52.000000,52.009998,48.799999,49.099998,49.099998,1734400
|
||||
2019-05-20,47.810001,48.279999,47.000000,47.099998,47.099998,1009100
|
||||
2019-05-21,47.709999,48.860001,47.259998,48.779999,48.779999,1218800
|
||||
2019-05-22,48.680000,48.709999,47.080002,47.330002,47.330002,947700
|
||||
2019-05-23,42.000000,42.974998,40.349998,42.360001,42.360001,2667771
|
||||
|
@@ -0,0 +1,252 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-07-13,61.240002,61.810001,60.970001,61.680000,61.680000,3079300
|
||||
2018-07-16,61.509998,62.090000,61.509998,61.630001,61.630001,1695100
|
||||
2018-07-17,61.439999,61.990002,61.099998,61.209999,61.209999,1779700
|
||||
2018-07-18,61.419998,61.490002,60.490002,60.520000,60.520000,1916800
|
||||
2018-07-19,60.259998,60.380001,59.230000,59.410000,59.410000,2738600
|
||||
2018-07-20,59.410000,59.610001,59.070000,59.250000,59.250000,2102600
|
||||
2018-07-23,59.130001,59.230000,58.320000,58.360001,58.360001,2462100
|
||||
2018-07-24,58.480000,59.099998,57.889999,58.779999,58.779999,2519700
|
||||
2018-07-25,58.779999,59.320000,58.509998,58.830002,58.830002,2194900
|
||||
2018-07-26,59.099998,59.799999,58.889999,59.369999,59.369999,2548000
|
||||
2018-07-27,59.470001,60.669998,59.290001,59.610001,59.610001,2325100
|
||||
2018-07-30,59.500000,60.740002,59.500000,59.820000,59.820000,2324200
|
||||
2018-07-31,59.869999,60.200001,59.480000,60.000000,60.000000,2495700
|
||||
2018-08-01,59.950001,60.169998,59.139999,59.360001,59.360001,4099300
|
||||
2018-08-02,61.110001,62.240002,60.000000,62.180000,62.180000,6327000
|
||||
2018-08-03,61.110001,62.259998,61.110001,61.549999,61.549999,2603300
|
||||
2018-08-06,61.590000,66.400002,61.580002,66.300003,66.300003,14881000
|
||||
2018-08-07,66.309998,66.339996,65.129997,65.330002,65.330002,4777600
|
||||
2018-08-08,65.309998,65.650002,64.459999,65.500000,65.500000,3106500
|
||||
2018-08-09,65.519997,65.739998,64.839996,64.970001,64.970001,2295400
|
||||
2018-08-10,64.639999,65.199997,64.059998,64.980003,64.980003,2241600
|
||||
2018-08-13,64.940002,65.800003,64.529999,65.699997,65.699997,3222200
|
||||
2018-08-14,65.779999,66.440002,65.660004,65.959999,65.959999,3311900
|
||||
2018-08-15,65.519997,65.900002,65.040001,65.410004,65.410004,2408500
|
||||
2018-08-16,65.680000,66.209999,65.550003,65.980003,65.980003,2549400
|
||||
2018-08-17,65.870003,66.059998,65.550003,65.900002,65.900002,3088400
|
||||
2018-08-20,65.739998,66.239998,65.500000,65.839996,65.839996,2621400
|
||||
2018-08-21,65.769997,66.639999,65.769997,66.510002,66.510002,1973900
|
||||
2018-08-22,66.279999,66.410004,65.519997,65.809998,65.809998,1750200
|
||||
2018-08-23,65.500000,65.900002,65.180000,65.209999,65.209999,1564500
|
||||
2018-08-24,65.449997,65.930000,65.169998,65.410004,65.410004,1945600
|
||||
2018-08-27,65.199997,65.940002,65.199997,65.800003,65.800003,1629300
|
||||
2018-08-28,65.849998,66.089996,65.410004,65.589996,65.589996,1697800
|
||||
2018-08-29,65.739998,66.489998,65.510002,65.980003,65.980003,2411700
|
||||
2018-08-30,65.570000,66.360001,65.330002,66.010002,66.010002,1965700
|
||||
2018-08-31,66.129997,66.379997,65.699997,66.040001,66.040001,2375200
|
||||
2018-09-04,65.739998,66.300003,65.529999,65.769997,65.769997,2103900
|
||||
2018-09-05,65.629997,66.029999,65.430000,65.709999,65.709999,3141100
|
||||
2018-09-06,65.500000,65.959999,65.370003,65.519997,65.519997,2277400
|
||||
2018-09-07,65.330002,65.639999,64.070000,64.389999,64.389999,2843100
|
||||
2018-09-10,64.980003,65.800003,64.589996,65.559998,65.559998,2171600
|
||||
2018-09-11,65.559998,66.019997,65.239998,65.910004,65.910004,1375400
|
||||
2018-09-12,66.000000,68.320000,65.620003,67.919998,67.919998,5019900
|
||||
2018-09-13,68.000000,68.709999,67.370003,68.480003,68.480003,3797300
|
||||
2018-09-14,68.580002,68.650002,67.900002,68.250000,68.250000,3161700
|
||||
2018-09-17,68.029999,68.180000,67.320000,67.419998,67.419998,2757300
|
||||
2018-09-18,67.430000,68.580002,67.080002,68.489998,68.489998,4113100
|
||||
2018-09-19,68.610001,68.870003,67.720001,68.349998,68.349998,2847100
|
||||
2018-09-20,68.779999,69.150002,68.339996,69.129997,69.129997,1875200
|
||||
2018-09-21,69.339996,69.769997,68.889999,69.070000,69.070000,4046100
|
||||
2018-09-24,68.699997,69.339996,68.500000,68.919998,68.919998,2749300
|
||||
2018-09-25,69.510002,69.550003,68.559998,69.449997,69.449997,2610800
|
||||
2018-09-26,69.470001,69.489998,68.690002,68.790001,68.790001,2577700
|
||||
2018-09-27,69.080002,70.629997,68.870003,70.489998,70.489998,3661000
|
||||
2018-09-28,70.449997,70.879997,69.980003,70.180000,70.180000,3703800
|
||||
2018-10-01,70.709999,70.940002,69.970001,70.250000,70.250000,2684400
|
||||
2018-10-02,70.160004,70.580002,69.570000,69.720001,69.720001,2272300
|
||||
2018-10-03,69.820000,69.980003,68.750000,68.980003,68.980003,5562000
|
||||
2018-10-04,68.529999,68.989998,68.260002,68.949997,68.949997,2890600
|
||||
2018-10-05,68.949997,69.730003,68.120003,68.589996,68.589996,2933600
|
||||
2018-10-08,68.589996,69.239998,68.029999,68.820000,68.820000,3656400
|
||||
2018-10-09,68.769997,69.349998,68.239998,68.459999,68.459999,3237200
|
||||
2018-10-10,68.540001,69.050003,66.550003,66.629997,66.629997,6295500
|
||||
2018-10-11,66.750000,67.709999,66.010002,66.209999,66.209999,5098300
|
||||
2018-10-12,67.269997,68.599998,66.839996,68.379997,68.379997,3870600
|
||||
2018-10-15,68.120003,68.660004,67.580002,67.620003,67.620003,3014000
|
||||
2018-10-16,67.680000,69.129997,67.559998,68.940002,68.940002,2422300
|
||||
2018-10-17,68.959999,69.660004,68.580002,69.080002,69.080002,2662700
|
||||
2018-10-18,69.230003,69.690002,68.330002,68.769997,68.769997,2954400
|
||||
2018-10-19,69.529999,70.139999,69.209999,69.750000,69.750000,4744300
|
||||
2018-10-22,69.849998,70.209999,69.419998,69.510002,69.510002,2647300
|
||||
2018-10-23,69.080002,69.769997,67.790001,69.279999,69.279999,3113000
|
||||
2018-10-24,69.050003,69.370003,65.589996,65.709999,65.709999,5758800
|
||||
2018-10-25,66.339996,67.000000,65.099998,66.320000,66.320000,6795200
|
||||
2018-10-26,64.550003,65.559998,64.279999,65.110001,65.110001,7111300
|
||||
2018-10-29,66.139999,66.660004,63.480000,64.910004,64.910004,4654800
|
||||
2018-10-30,64.529999,65.489998,63.610001,63.919998,63.919998,6077300
|
||||
2018-10-31,67.370003,69.599998,67.250000,68.550003,68.550003,9212400
|
||||
2018-11-01,68.959999,69.599998,68.610001,68.879997,68.879997,4194700
|
||||
2018-11-02,69.389999,69.449997,67.550003,68.510002,68.510002,2606400
|
||||
2018-11-05,68.959999,69.669998,68.570000,68.889999,68.889999,2743100
|
||||
2018-11-06,68.849998,69.279999,68.269997,68.769997,68.769997,3891400
|
||||
2018-11-07,69.239998,70.779999,69.050003,70.339996,70.339996,3639600
|
||||
2018-11-08,70.070000,70.750000,69.519997,69.750000,69.750000,2763700
|
||||
2018-11-09,69.419998,69.699997,67.980003,68.680000,68.680000,2263600
|
||||
2018-11-12,68.690002,69.160004,67.779999,67.889999,67.889999,2471500
|
||||
2018-11-13,68.540001,69.410004,68.260002,68.410004,68.410004,4005100
|
||||
2018-11-14,68.660004,69.220001,67.550003,68.120003,68.120003,3665500
|
||||
2018-11-15,67.860001,68.360001,67.550003,68.080002,68.080002,2826800
|
||||
2018-11-16,67.230003,69.699997,67.000000,69.139999,69.139999,5257200
|
||||
2018-11-19,68.529999,69.599998,67.919998,68.000000,68.000000,3387500
|
||||
2018-11-20,67.739998,67.779999,66.209999,66.570000,66.570000,5081100
|
||||
2018-11-21,66.790001,67.510002,66.629997,66.779999,66.779999,3274200
|
||||
2018-11-23,66.389999,67.459999,66.080002,67.070000,67.070000,1215200
|
||||
2018-11-26,67.760002,67.889999,67.180000,67.489998,67.489998,3285100
|
||||
2018-11-27,67.239998,68.120003,66.940002,67.489998,67.489998,2393800
|
||||
2018-11-28,67.750000,68.980003,67.510002,68.919998,68.919998,3272000
|
||||
2018-11-29,68.400002,68.940002,68.080002,68.680000,68.680000,2814600
|
||||
2018-11-30,68.879997,69.209999,68.080002,68.449997,68.449997,3470700
|
||||
2018-12-03,68.500000,69.019997,66.970001,67.800003,67.800003,3641200
|
||||
2018-12-04,67.459999,68.540001,65.470001,66.570000,66.570000,5329700
|
||||
2018-12-06,65.680000,67.550003,65.019997,67.300003,67.300003,4899100
|
||||
2018-12-07,66.680000,68.470001,65.370003,65.690002,65.690002,3340800
|
||||
2018-12-10,65.669998,65.959999,64.339996,65.730003,65.730003,3012500
|
||||
2018-12-11,66.620003,67.339996,65.440002,65.529999,65.529999,2475300
|
||||
2018-12-12,66.769997,67.080002,66.059998,66.099998,66.099998,3279600
|
||||
2018-12-13,66.209999,66.849998,65.470001,66.360001,66.360001,3209600
|
||||
2018-12-14,65.830002,66.690002,65.389999,65.650002,65.650002,2872700
|
||||
2018-12-17,65.730003,65.940002,64.279999,64.809998,64.809998,3999300
|
||||
2018-12-18,64.809998,65.629997,63.950001,64.779999,64.779999,4068400
|
||||
2018-12-19,64.639999,66.570000,64.480003,64.870003,64.870003,4204100
|
||||
2018-12-20,64.620003,65.309998,63.410000,64.260002,64.260002,5126900
|
||||
2018-12-21,64.580002,65.370003,61.700001,61.930000,61.930000,7669100
|
||||
2018-12-24,61.509998,61.720001,59.959999,60.799999,60.799999,2581800
|
||||
2018-12-26,60.919998,63.029999,59.959999,63.009998,63.009998,5088100
|
||||
2018-12-27,62.349998,62.660000,60.889999,62.650002,62.650002,4353500
|
||||
2018-12-28,62.700001,63.750000,62.610001,63.240002,63.240002,4098300
|
||||
2018-12-31,63.320000,63.650002,62.410000,63.610001,63.610001,3112500
|
||||
2019-01-02,62.869999,65.330002,62.549999,65.260002,65.260002,4418600
|
||||
2019-01-03,64.550003,66.239998,64.110001,65.019997,65.019997,3390700
|
||||
2019-01-04,65.879997,67.559998,65.550003,67.489998,67.489998,5490200
|
||||
2019-01-07,67.370003,68.610001,67.010002,68.440002,68.440002,4786900
|
||||
2019-01-08,68.669998,68.680000,67.370003,67.769997,67.769997,6052700
|
||||
2019-01-09,68.650002,68.669998,67.120003,67.720001,67.720001,4498600
|
||||
2019-01-10,67.440002,68.860001,67.220001,67.959999,67.959999,3230800
|
||||
2019-01-11,67.830002,69.059998,67.400002,69.000000,69.000000,4177300
|
||||
2019-01-14,68.410004,68.519997,67.419998,67.910004,67.910004,2691000
|
||||
2019-01-15,67.809998,68.559998,66.699997,67.139999,67.139999,5366700
|
||||
2019-01-16,67.510002,67.610001,66.379997,66.690002,66.690002,3261000
|
||||
2019-01-17,66.720001,66.750000,65.639999,66.250000,66.250000,3357400
|
||||
2019-01-18,67.110001,68.050003,66.370003,66.959999,66.959999,4766500
|
||||
2019-01-22,66.750000,67.190002,66.199997,66.830002,66.830002,2971200
|
||||
2019-01-23,66.800003,67.849998,66.570000,67.800003,67.800003,1979500
|
||||
2019-01-24,67.720001,68.660004,67.220001,68.660004,68.660004,2849800
|
||||
2019-01-25,68.930000,69.269997,68.199997,68.480003,68.480003,4940400
|
||||
2019-01-28,67.870003,68.489998,67.059998,67.620003,67.620003,2958100
|
||||
2019-01-29,67.660004,67.940002,66.629997,67.779999,67.779999,2340100
|
||||
2019-01-30,67.739998,68.690002,67.089996,68.320000,68.320000,2318300
|
||||
2019-01-31,68.410004,70.120003,68.029999,69.620003,69.620003,5816100
|
||||
2019-02-01,69.459999,69.779999,69.080002,69.629997,69.629997,3117900
|
||||
2019-02-04,69.410004,69.629997,68.879997,69.410004,69.410004,1926200
|
||||
2019-02-05,69.699997,69.699997,66.800003,66.849998,66.849998,5001100
|
||||
2019-02-06,66.800003,67.300003,66.540001,66.940002,66.940002,4334500
|
||||
2019-02-07,68.000000,68.459999,65.559998,68.279999,68.279999,7554600
|
||||
2019-02-08,67.639999,68.470001,66.949997,68.379997,68.379997,4129500
|
||||
2019-02-11,68.169998,69.180000,68.000000,68.440002,68.440002,2644100
|
||||
2019-02-12,69.000000,69.669998,68.500000,69.599998,69.599998,2630700
|
||||
2019-02-13,69.610001,69.959999,68.860001,69.070000,69.070000,4742400
|
||||
2019-02-14,69.099998,70.519997,69.010002,70.500000,70.500000,3200300
|
||||
2019-02-15,70.650002,72.070000,70.430000,72.050003,72.050003,5711300
|
||||
2019-02-19,72.010002,72.860001,71.760002,72.519997,72.519997,3672800
|
||||
2019-02-20,72.459999,73.080002,72.169998,72.750000,72.750000,4476900
|
||||
2019-02-21,72.570000,73.760002,72.389999,73.410004,73.410004,4561200
|
||||
2019-02-22,73.800003,74.059998,72.970001,73.199997,73.199997,2522000
|
||||
2019-02-25,73.370003,73.529999,72.620003,73.050003,73.050003,2400400
|
||||
2019-02-26,71.440002,72.970001,71.250000,72.529999,72.529999,2915400
|
||||
2019-02-27,72.129997,72.529999,71.769997,72.120003,72.120003,2742600
|
||||
2019-02-28,72.080002,72.699997,71.919998,72.209999,72.209999,3072600
|
||||
2019-03-01,72.389999,72.599998,71.779999,72.339996,72.339996,1883600
|
||||
2019-03-04,72.239998,72.339996,71.279999,71.650002,71.650002,2439400
|
||||
2019-03-05,71.750000,71.980003,71.279999,71.410004,71.410004,1725600
|
||||
2019-03-06,71.410004,71.599998,69.989998,70.779999,70.779999,4375400
|
||||
2019-03-07,70.620003,71.379997,70.519997,71.320000,71.320000,2309300
|
||||
2019-03-08,70.930000,71.250000,70.180000,70.650002,70.650002,3573400
|
||||
2019-03-11,70.830002,71.580002,70.410004,71.470001,71.470001,2493100
|
||||
2019-03-12,71.470001,72.129997,71.250000,71.699997,71.699997,2773200
|
||||
2019-03-13,72.120003,72.480003,71.680000,72.250000,72.250000,1920400
|
||||
2019-03-14,72.250000,72.440002,71.940002,72.339996,72.339996,1653300
|
||||
2019-03-15,72.760002,73.500000,72.099998,73.459999,73.459999,2806200
|
||||
2019-03-18,73.279999,73.910004,72.949997,73.680000,73.680000,2945300
|
||||
2019-03-19,74.000000,74.059998,72.410004,72.589996,72.589996,4188100
|
||||
2019-03-20,72.800003,72.930000,72.010002,72.059998,72.059998,3136200
|
||||
2019-03-21,72.080002,72.739998,71.989998,72.260002,72.260002,2329200
|
||||
2019-03-22,71.930000,72.470001,71.809998,71.900002,71.900002,3265900
|
||||
2019-03-25,71.849998,72.410004,71.639999,72.309998,72.309998,1972400
|
||||
2019-03-26,72.580002,73.360001,72.370003,73.339996,73.339996,2857200
|
||||
2019-03-27,73.389999,73.790001,71.959999,72.239998,72.239998,4989700
|
||||
2019-03-28,71.980003,72.250000,68.919998,69.150002,69.150002,5866500
|
||||
2019-03-29,69.330002,69.489998,68.160004,69.099998,69.099998,5051900
|
||||
2019-04-01,69.540001,70.040001,69.269997,69.970001,69.970001,3808300
|
||||
2019-04-02,69.809998,70.160004,69.220001,69.730003,69.730003,3913300
|
||||
2019-04-03,69.820000,70.019997,68.699997,69.000000,69.000000,3077500
|
||||
2019-04-04,69.080002,70.129997,69.000000,70.050003,70.050003,3037100
|
||||
2019-04-05,70.480003,70.750000,70.050003,70.349998,70.349998,3483100
|
||||
2019-04-08,70.139999,71.239998,69.769997,71.230003,71.230003,4035400
|
||||
2019-04-09,70.959999,71.660004,70.269997,71.419998,71.419998,2866800
|
||||
2019-04-10,71.550003,72.330002,71.190002,72.160004,72.160004,2629900
|
||||
2019-04-11,72.150002,73.010002,71.839996,73.000000,73.000000,2535700
|
||||
2019-04-12,73.190002,73.879997,72.870003,73.709999,73.709999,2858700
|
||||
2019-04-15,73.709999,73.860001,73.190002,73.790001,73.790001,2194500
|
||||
2019-04-16,74.000000,74.269997,73.519997,74.099998,74.099998,2834500
|
||||
2019-04-17,72.720001,74.309998,71.300003,72.459999,72.459999,7576000
|
||||
2019-04-18,73.220001,73.779999,72.570000,73.750000,73.750000,2746800
|
||||
2019-04-22,73.760002,73.860001,73.010002,73.720001,73.720001,1912400
|
||||
2019-04-23,73.559998,74.000000,73.050003,73.750000,73.750000,2180400
|
||||
2019-04-24,73.389999,73.870003,72.720001,73.330002,73.330002,2373500
|
||||
2019-04-25,72.699997,73.580002,72.510002,72.709999,72.709999,3365800
|
||||
2019-04-26,74.730003,74.730003,72.599998,72.919998,72.919998,3561900
|
||||
2019-04-29,72.940002,73.820000,72.500000,73.599998,73.599998,2631300
|
||||
2019-04-30,73.470001,73.739998,72.550003,72.989998,72.989998,3064900
|
||||
2019-05-01,73.139999,74.110001,73.040001,73.580002,73.580002,2941100
|
||||
2019-05-02,73.779999,73.949997,72.660004,73.610001,73.610001,2269300
|
||||
2019-05-03,73.709999,74.779999,73.400002,74.739998,74.739998,2588800
|
||||
2019-05-06,73.879997,74.510002,73.339996,74.459999,74.459999,1875700
|
||||
2019-05-07,73.800003,74.449997,73.540001,74.000000,74.000000,2594800
|
||||
2019-05-08,73.959999,73.959999,72.480003,72.629997,72.629997,2849200
|
||||
2019-05-09,72.300003,74.400002,72.250000,74.250000,74.250000,3319800
|
||||
2019-05-10,74.000000,75.320000,73.540001,75.230003,75.230003,3360500
|
||||
2019-05-13,74.379997,74.739998,73.250000,73.419998,73.419998,3476800
|
||||
2019-05-14,73.419998,73.870003,73.040001,73.599998,73.599998,2987100
|
||||
2019-05-15,73.370003,74.779999,73.050003,74.629997,74.629997,3798200
|
||||
2019-05-16,74.480003,77.129997,74.250000,75.379997,75.379997,4946400
|
||||
2019-05-17,75.389999,76.320000,75.019997,75.370003,75.370003,3619000
|
||||
2019-05-20,79.870003,80.930000,75.720001,78.290001,78.290001,20102700
|
||||
2019-05-21,78.160004,78.290001,76.779999,77.150002,77.150002,6339000
|
||||
2019-05-22,76.489998,77.129997,76.139999,76.349998,76.349998,3287300
|
||||
2019-05-23,76.260002,77.000000,75.250000,76.010002,76.010002,3887000
|
||||
2019-05-24,76.500000,77.449997,75.629997,77.269997,77.269997,3579500
|
||||
2019-05-28,77.550003,77.980003,75.889999,75.910004,75.910004,5391300
|
||||
2019-05-29,75.820000,76.650002,75.290001,76.129997,76.129997,3540700
|
||||
2019-05-30,76.040001,76.570000,75.019997,76.029999,76.029999,4111900
|
||||
2019-05-31,74.809998,75.250000,73.279999,73.440002,73.440002,5147800
|
||||
2019-06-03,73.620003,74.639999,73.370003,74.199997,74.199997,2941000
|
||||
2019-06-04,74.629997,75.739998,74.519997,75.690002,75.690002,3144700
|
||||
2019-06-05,75.940002,76.550003,75.669998,76.290001,76.290001,1970200
|
||||
2019-06-06,76.139999,76.440002,74.610001,75.949997,75.949997,2671000
|
||||
2019-06-07,76.550003,77.139999,76.309998,77.029999,77.029999,2256600
|
||||
2019-06-10,77.059998,77.379997,76.379997,76.669998,76.669998,3096000
|
||||
2019-06-11,77.129997,77.459999,74.629997,75.459999,75.459999,3822500
|
||||
2019-06-12,75.830002,75.900002,74.650002,75.379997,75.379997,3296200
|
||||
2019-06-13,75.169998,75.279999,74.010002,74.449997,74.449997,4279700
|
||||
2019-06-14,74.669998,78.000000,74.279999,74.900002,74.900002,4813600
|
||||
2019-06-17,75.080002,75.430000,74.540001,75.269997,75.269997,2929000
|
||||
2019-06-18,75.940002,77.129997,75.550003,76.440002,76.440002,4995900
|
||||
2019-06-19,76.339996,78.500000,76.339996,78.279999,78.279999,3377800
|
||||
2019-06-20,79.110001,79.260002,77.150002,77.930000,77.930000,3339500
|
||||
2019-06-21,77.709999,77.750000,75.199997,75.699997,75.699997,5223900
|
||||
2019-06-24,76.019997,76.550003,75.379997,75.540001,75.540001,2123000
|
||||
2019-06-25,75.470001,75.500000,74.449997,74.580002,74.580002,2593900
|
||||
2019-06-26,74.279999,74.400002,72.769997,73.040001,73.040001,4393300
|
||||
2019-06-27,73.449997,73.720001,72.919998,73.290001,73.290001,2319600
|
||||
2019-06-28,73.290001,74.489998,72.900002,74.139999,74.139999,4117800
|
||||
2019-07-01,74.669998,74.669998,73.919998,74.150002,74.150002,2226100
|
||||
2019-07-02,74.400002,75.779999,73.919998,75.480003,75.480003,2871700
|
||||
2019-07-03,75.889999,77.029999,75.599998,75.820000,75.820000,2080900
|
||||
2019-07-05,75.830002,76.430000,75.629997,76.230003,76.230003,1318500
|
||||
2019-07-08,76.370003,76.389999,75.430000,75.629997,75.629997,1667300
|
||||
2019-07-09,75.349998,75.830002,75.080002,75.150002,75.150002,3553000
|
||||
2019-07-10,77.959999,78.949997,77.570000,78.629997,78.629997,14441200
|
||||
2019-07-11,78.730003,78.879997,76.949997,78.260002,78.260002,10108700
|
||||
2019-07-12,78.199997,79.730003,78.010002,79.449997,79.449997,63065600
|
||||
|
@@ -0,0 +1,252 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-03-23,311.250000,311.250000,300.450012,301.540009,301.540009,6654900
|
||||
2018-03-26,307.339996,307.589996,291.359985,304.179993,304.179993,8375200
|
||||
2018-03-27,304.000000,304.269989,277.179993,279.179993,279.179993,13872000
|
||||
2018-03-28,264.579987,268.679993,252.100006,257.779999,257.779999,21001400
|
||||
2018-03-29,256.489990,270.959991,248.210007,266.130005,266.130005,15170700
|
||||
2018-04-02,256.260010,260.329987,244.589996,252.479996,252.479996,16114000
|
||||
2018-04-03,269.820007,273.350006,254.490005,267.529999,267.529999,18844400
|
||||
2018-04-04,252.779999,288.369995,252.000000,286.940002,286.940002,19896700
|
||||
2018-04-05,289.339996,306.260010,288.200012,305.720001,305.720001,19121100
|
||||
2018-04-06,301.000000,309.279999,295.500000,299.299988,299.299988,13520300
|
||||
2018-04-09,300.369995,309.500000,289.209991,289.660004,289.660004,10249800
|
||||
2018-04-10,298.970001,307.100006,293.679993,304.700012,304.700012,10989800
|
||||
2018-04-11,300.739990,308.980011,299.660004,300.929993,300.929993,7482900
|
||||
2018-04-12,302.320007,303.950012,293.679993,294.079987,294.079987,7608800
|
||||
2018-04-13,303.600006,303.950012,295.980011,300.339996,300.339996,7327200
|
||||
2018-04-16,299.000000,299.660004,289.010010,291.209991,291.209991,6338500
|
||||
2018-04-17,288.869995,292.170013,282.510010,287.690002,287.690002,7000000
|
||||
2018-04-18,291.079987,300.239990,288.160004,293.350006,293.350006,6557700
|
||||
2018-04-19,291.079987,301.010010,288.549988,300.079987,300.079987,6090600
|
||||
2018-04-20,295.170013,299.980011,289.750000,290.239990,290.239990,5627900
|
||||
2018-04-23,291.290009,291.619995,282.329987,283.369995,283.369995,4893400
|
||||
2018-04-24,285.000000,287.089996,278.459991,283.459991,283.459991,5685300
|
||||
2018-04-25,283.500000,285.160004,277.250000,280.690002,280.690002,4013600
|
||||
2018-04-26,278.750000,285.790009,276.500000,285.480011,285.480011,4356000
|
||||
2018-04-27,285.369995,294.470001,283.829987,294.079987,294.079987,4364600
|
||||
2018-04-30,293.609985,298.730011,292.500000,293.899994,293.899994,4228200
|
||||
2018-05-01,293.510010,300.820007,293.220001,299.920013,299.920013,4625600
|
||||
2018-05-02,298.570007,306.850006,297.779999,301.149994,301.149994,8970400
|
||||
2018-05-03,278.790009,288.040009,275.230011,284.450012,284.450012,17352100
|
||||
2018-05-04,283.000000,296.859985,279.519989,294.089996,294.089996,8569400
|
||||
2018-05-07,297.500000,305.959991,295.170013,302.769989,302.769989,8678200
|
||||
2018-05-08,300.799988,307.750000,299.000000,301.970001,301.970001,5930000
|
||||
2018-05-09,300.410004,307.010010,300.049988,306.850006,306.850006,5727400
|
||||
2018-05-10,307.500000,312.989990,304.109985,305.019989,305.019989,5651600
|
||||
2018-05-11,307.700012,308.880005,299.079987,301.059998,301.059998,4679600
|
||||
2018-05-14,303.320007,304.940002,291.619995,291.970001,291.970001,7286800
|
||||
2018-05-15,285.010010,286.959991,280.500000,284.179993,284.179993,9519200
|
||||
2018-05-16,283.829987,288.809998,281.559998,286.480011,286.480011,5674000
|
||||
2018-05-17,285.899994,289.190002,283.970001,284.540009,284.540009,4420600
|
||||
2018-05-18,284.649994,284.649994,274.000000,276.820007,276.820007,7251900
|
||||
2018-05-21,281.329987,291.489990,281.299988,284.489990,284.489990,9182600
|
||||
2018-05-22,287.760010,288.000000,273.420013,275.010010,275.010010,8945800
|
||||
2018-05-23,277.760010,279.910004,274.000000,279.070007,279.070007,5953100
|
||||
2018-05-24,278.399994,281.109985,274.890015,277.850006,277.850006,4176700
|
||||
2018-05-25,277.630005,279.640015,275.609985,278.850006,278.850006,3875100
|
||||
2018-05-29,278.510010,286.500000,276.149994,283.760010,283.760010,5666600
|
||||
2018-05-30,283.290009,295.010010,281.600006,291.720001,291.720001,7489700
|
||||
2018-05-31,287.209991,290.369995,282.929993,284.730011,284.730011,5919700
|
||||
2018-06-01,285.859985,291.950012,283.839996,291.820007,291.820007,5424400
|
||||
2018-06-04,294.339996,299.000000,293.549988,296.739990,296.739990,4797800
|
||||
2018-06-05,297.700012,297.799988,286.739990,291.130005,291.130005,5995200
|
||||
2018-06-06,300.500000,322.170013,297.480011,319.500000,319.500000,18767300
|
||||
2018-06-07,316.149994,330.000000,313.579987,316.089996,316.089996,14345300
|
||||
2018-06-08,319.000000,324.480011,317.149994,317.660004,317.660004,8205200
|
||||
2018-06-11,322.510010,334.660004,322.500000,332.100006,332.100006,13183500
|
||||
2018-06-12,344.700012,354.970001,338.000000,342.769989,342.769989,22347400
|
||||
2018-06-13,346.709991,347.200012,339.799988,344.779999,344.779999,9469800
|
||||
2018-06-14,347.630005,358.750000,346.600006,357.720001,357.720001,10981000
|
||||
2018-06-15,353.839996,364.670013,351.250000,358.170013,358.170013,10848300
|
||||
2018-06-18,355.399994,373.730011,354.500000,370.829987,370.829987,12073200
|
||||
2018-06-19,365.160004,370.000000,346.250000,352.549988,352.549988,12761900
|
||||
2018-06-20,358.040009,364.380005,352.000000,362.220001,362.220001,8383700
|
||||
2018-06-21,362.000000,366.209991,346.269989,347.510010,347.510010,7967100
|
||||
2018-06-22,351.540009,352.250000,332.000000,333.630005,333.630005,10266100
|
||||
2018-06-25,330.119995,338.470001,327.500000,333.010010,333.010010,6931300
|
||||
2018-06-26,336.049988,343.549988,325.799988,342.000000,342.000000,7452500
|
||||
2018-06-27,345.000000,350.790009,339.500000,344.500000,344.500000,8333700
|
||||
2018-06-28,348.660004,357.019989,346.109985,349.929993,349.929993,8398000
|
||||
2018-06-29,353.329987,353.859985,342.410004,342.950012,342.950012,6492400
|
||||
2018-07-02,360.070007,364.779999,329.850006,335.070007,335.070007,18759800
|
||||
2018-07-03,331.750000,332.489990,309.690002,310.859985,310.859985,12282600
|
||||
2018-07-05,313.760010,314.390015,296.220001,309.160004,309.160004,17476400
|
||||
2018-07-06,304.950012,312.070007,302.000000,308.899994,308.899994,8865500
|
||||
2018-07-09,311.989990,318.519989,308.000000,318.510010,318.510010,7596800
|
||||
2018-07-10,324.559998,327.679993,319.200012,322.470001,322.470001,9471500
|
||||
2018-07-11,315.799988,321.940002,315.070007,318.959991,318.959991,4884100
|
||||
2018-07-12,321.429993,323.230011,312.769989,316.709991,316.709991,5721200
|
||||
2018-07-13,315.579987,319.579987,309.250000,318.869995,318.869995,5869800
|
||||
2018-07-16,311.709991,315.160004,306.250000,310.100006,310.100006,7818700
|
||||
2018-07-17,308.809998,324.739990,308.500000,322.690002,322.690002,6996200
|
||||
2018-07-18,325.000000,325.500000,316.250000,323.850006,323.850006,5624200
|
||||
2018-07-19,316.329987,323.540009,314.010010,320.230011,320.230011,5915300
|
||||
2018-07-20,321.230011,323.239990,311.709991,313.579987,313.579987,5162200
|
||||
2018-07-23,301.839996,305.500000,292.859985,303.200012,303.200012,10992900
|
||||
2018-07-24,304.420013,307.720001,292.549988,297.429993,297.429993,9590800
|
||||
2018-07-25,296.739990,309.619995,294.500000,308.739990,308.739990,7075400
|
||||
2018-07-26,304.850006,310.700012,303.640015,306.649994,306.649994,4630500
|
||||
2018-07-27,307.250000,307.690002,295.339996,297.179993,297.179993,5703300
|
||||
2018-07-30,295.899994,296.100006,286.130005,290.170013,290.170013,6814100
|
||||
2018-07-31,292.250000,298.320007,289.070007,298.140015,298.140015,5076900
|
||||
2018-08-01,297.989990,303.000000,293.000000,300.839996,300.839996,10129400
|
||||
2018-08-02,328.440002,349.989990,323.160004,349.540009,349.540009,23215000
|
||||
2018-08-03,347.809998,355.000000,342.529999,348.170013,348.170013,13656500
|
||||
2018-08-06,345.459991,354.980011,341.820007,341.989990,341.989990,8564300
|
||||
2018-08-07,343.839996,387.459991,339.149994,379.570007,379.570007,30875800
|
||||
2018-08-08,369.089996,382.640015,367.119995,370.339996,370.339996,24571200
|
||||
2018-08-09,365.549988,367.010010,345.730011,352.450012,352.450012,17103700
|
||||
2018-08-10,354.000000,360.000000,346.000000,355.489990,355.489990,11552000
|
||||
2018-08-13,361.130005,363.190002,349.019989,356.410004,356.410004,10450200
|
||||
2018-08-14,358.450012,359.200012,347.100006,347.640015,347.640015,6986400
|
||||
2018-08-15,341.910004,344.489990,332.140015,338.690002,338.690002,9101300
|
||||
2018-08-16,339.910004,342.279999,333.820007,335.450012,335.450012,6064000
|
||||
2018-08-17,323.500000,326.769989,303.529999,305.500000,305.500000,18958600
|
||||
2018-08-20,291.700012,308.500000,288.200012,308.440002,308.440002,17402300
|
||||
2018-08-21,310.609985,324.790009,309.000000,321.899994,321.899994,13172200
|
||||
2018-08-22,320.869995,323.880005,314.670013,321.640015,321.640015,5946000
|
||||
2018-08-23,319.140015,327.320007,318.100006,320.100006,320.100006,5147300
|
||||
2018-08-24,320.700012,323.850006,319.399994,322.820007,322.820007,3602600
|
||||
2018-08-27,318.000000,322.440002,308.809998,319.269989,319.269989,13079300
|
||||
2018-08-28,318.410004,318.880005,311.190002,311.859985,311.859985,7649100
|
||||
2018-08-29,310.269989,311.850006,303.690002,305.010010,305.010010,7447400
|
||||
2018-08-30,302.260010,304.600006,297.720001,303.149994,303.149994,7216700
|
||||
2018-08-31,302.000000,305.309998,298.600006,301.660004,301.660004,5375100
|
||||
2018-09-04,296.940002,298.190002,288.000000,288.950012,288.950012,8350500
|
||||
2018-09-05,285.049988,286.779999,277.179993,280.739990,280.739990,7720800
|
||||
2018-09-06,284.799988,291.170013,278.880005,280.950012,280.950012,7480800
|
||||
2018-09-07,260.100006,268.350006,252.250000,263.239990,263.239990,22491900
|
||||
2018-09-10,273.260010,286.029999,271.000000,285.500000,285.500000,14283500
|
||||
2018-09-11,279.470001,282.000000,273.549988,279.440002,279.440002,9170000
|
||||
2018-09-12,281.440002,292.500000,278.649994,290.540009,290.540009,10015400
|
||||
2018-09-13,288.019989,295.000000,285.179993,289.459991,289.459991,6340300
|
||||
2018-09-14,288.760010,297.329987,286.519989,295.200012,295.200012,6765600
|
||||
2018-09-17,290.040009,300.869995,288.130005,294.839996,294.839996,6887600
|
||||
2018-09-18,296.690002,302.640015,275.500000,284.959991,284.959991,16547500
|
||||
2018-09-19,280.510010,300.000000,280.500000,299.019989,299.019989,8294900
|
||||
2018-09-20,303.559998,305.980011,293.329987,298.329987,298.329987,7349400
|
||||
2018-09-21,297.700012,300.579987,295.369995,299.100006,299.100006,5050500
|
||||
2018-09-24,298.480011,303.000000,293.579987,299.679993,299.679993,4843000
|
||||
2018-09-25,300.000000,304.600006,296.500000,300.989990,300.989990,4481700
|
||||
2018-09-26,301.910004,313.890015,301.109985,309.579987,309.579987,7843200
|
||||
2018-09-27,312.899994,314.959991,306.910004,307.519989,307.519989,8509100
|
||||
2018-09-28,270.260010,278.000000,260.559998,264.769989,264.769989,33649700
|
||||
2018-10-01,305.769989,311.440002,301.049988,310.700012,310.700012,21777600
|
||||
2018-10-02,313.950012,316.839996,299.149994,301.019989,301.019989,11743500
|
||||
2018-10-03,303.329987,304.600006,291.570007,294.799988,294.799988,7995000
|
||||
2018-10-04,293.950012,294.000000,277.670013,281.829987,281.829987,9814200
|
||||
2018-10-05,274.649994,274.880005,260.000000,261.950012,261.950012,17944500
|
||||
2018-10-08,264.519989,267.760010,249.000000,250.559998,250.559998,13472700
|
||||
2018-10-09,255.250000,266.769989,253.300003,262.799988,262.799988,12060600
|
||||
2018-10-10,264.609985,265.510010,247.770004,256.880005,256.880005,12815300
|
||||
2018-10-11,257.529999,262.250000,249.029999,252.229996,252.229996,8167700
|
||||
2018-10-12,261.000000,261.989990,252.009995,258.779999,258.779999,7201400
|
||||
2018-10-15,259.059998,263.279999,254.539993,259.589996,259.589996,6200000
|
||||
2018-10-16,265.700012,277.380005,262.239990,276.589996,276.589996,9526400
|
||||
2018-10-17,282.399994,282.700012,265.799988,271.779999,271.779999,8655500
|
||||
2018-10-18,269.290009,271.000000,263.000000,263.910004,263.910004,5421200
|
||||
2018-10-19,267.390015,269.660004,253.500000,260.000000,260.000000,9375500
|
||||
2018-10-22,260.679993,261.859985,252.589996,260.950012,260.950012,5600300
|
||||
2018-10-23,263.869995,297.929993,262.100006,294.140015,294.140015,19027800
|
||||
2018-10-24,301.049988,304.440002,285.730011,288.500000,288.500000,20058300
|
||||
2018-10-25,317.220001,321.000000,301.010010,314.859985,314.859985,20840700
|
||||
2018-10-26,308.250000,339.899994,306.649994,330.899994,330.899994,27425500
|
||||
2018-10-29,337.470001,347.160004,326.500000,334.850006,334.850006,14486000
|
||||
2018-10-30,328.390015,337.899994,322.260010,329.899994,329.899994,9126700
|
||||
2018-10-31,332.540009,342.000000,329.100006,337.320007,337.320007,7624300
|
||||
2018-11-01,338.260010,347.839996,334.730011,344.279999,344.279999,8000100
|
||||
2018-11-02,343.739990,349.200012,340.910004,346.410004,346.410004,7808000
|
||||
2018-11-05,340.500000,343.959991,330.140015,341.399994,341.399994,7831000
|
||||
2018-11-06,339.070007,348.799988,336.089996,341.059998,341.059998,6762900
|
||||
2018-11-07,343.339996,351.179993,340.799988,348.160004,348.160004,7374500
|
||||
2018-11-08,348.500000,357.579987,348.440002,351.399994,351.399994,7090700
|
||||
2018-11-09,349.000000,354.000000,345.230011,350.510010,350.510010,5098800
|
||||
2018-11-12,348.369995,349.779999,330.339996,331.279999,331.279999,6941500
|
||||
2018-11-13,333.160004,344.700012,332.200012,338.730011,338.730011,5448600
|
||||
2018-11-14,342.700012,347.109985,337.149994,344.000000,344.000000,5040300
|
||||
2018-11-15,342.329987,348.579987,339.040009,348.440002,348.440002,4625700
|
||||
2018-11-16,345.190002,355.700012,345.119995,354.309998,354.309998,7206200
|
||||
2018-11-19,356.339996,366.750000,352.880005,353.470001,353.470001,9708900
|
||||
2018-11-20,341.750000,349.799988,333.549988,347.489990,347.489990,8004700
|
||||
2018-11-21,352.000000,353.100006,337.399994,338.190002,338.190002,4686800
|
||||
2018-11-23,334.350006,337.500000,325.549988,325.829987,325.829987,4202600
|
||||
2018-11-26,325.000000,346.220001,325.000000,346.000000,346.000000,7992100
|
||||
2018-11-27,340.049988,346.959991,335.500000,343.920013,343.920013,6358300
|
||||
2018-11-28,345.989990,348.279999,342.209991,347.869995,347.869995,4127600
|
||||
2018-11-29,347.000000,347.500000,339.549988,341.170013,341.170013,3080700
|
||||
2018-11-30,341.829987,351.600006,338.260010,350.480011,350.480011,5629100
|
||||
2018-12-03,360.000000,366.000000,352.000000,358.489990,358.489990,8306500
|
||||
2018-12-04,356.049988,368.679993,352.000000,359.700012,359.700012,8461900
|
||||
2018-12-06,356.010010,367.380005,350.760010,363.059998,363.059998,7842500
|
||||
2018-12-07,369.000000,379.489990,357.649994,357.970001,357.970001,11511200
|
||||
2018-12-10,360.000000,365.980011,353.119995,365.149994,365.149994,6613500
|
||||
2018-12-11,369.910004,372.170013,360.230011,366.760010,366.760010,6308800
|
||||
2018-12-12,369.420013,371.910004,365.160004,366.600006,366.600006,5027000
|
||||
2018-12-13,370.149994,377.440002,366.750000,376.790009,376.790009,7365900
|
||||
2018-12-14,375.000000,377.869995,364.329987,365.709991,365.709991,6337600
|
||||
2018-12-17,362.000000,365.700012,343.880005,348.420013,348.420013,7674000
|
||||
2018-12-18,350.540009,351.549988,333.690002,337.029999,337.029999,7100000
|
||||
2018-12-19,337.600006,347.010010,329.739990,332.970001,332.970001,8274200
|
||||
2018-12-20,327.049988,330.290009,311.869995,315.380005,315.380005,9071900
|
||||
2018-12-21,317.399994,323.470001,312.440002,319.769989,319.769989,8016800
|
||||
2018-12-24,313.500000,314.500000,295.200012,295.390015,295.390015,5559900
|
||||
2018-12-26,300.000000,326.970001,294.089996,326.089996,326.089996,8163100
|
||||
2018-12-27,319.839996,322.170013,301.500000,316.130005,316.130005,8575100
|
||||
2018-12-28,323.100006,336.239990,318.410004,333.869995,333.869995,9939000
|
||||
2018-12-31,337.790009,339.209991,325.260010,332.799988,332.799988,6302300
|
||||
2019-01-02,306.100006,315.130005,298.799988,310.119995,310.119995,11658600
|
||||
2019-01-03,307.000000,309.399994,297.380005,300.359985,300.359985,6965200
|
||||
2019-01-04,306.000000,318.000000,302.730011,317.690002,317.690002,7394100
|
||||
2019-01-07,321.720001,336.739990,317.750000,334.959991,334.959991,7551200
|
||||
2019-01-08,341.959991,344.010010,327.019989,335.350006,335.350006,7008500
|
||||
2019-01-09,335.500000,343.500000,331.470001,338.529999,338.529999,5432900
|
||||
2019-01-10,334.399994,345.390015,331.790009,344.970001,344.970001,6056400
|
||||
2019-01-11,342.089996,348.410004,338.769989,347.260010,347.260010,5039100
|
||||
2019-01-14,342.380005,342.500000,334.000000,334.399994,334.399994,5247300
|
||||
2019-01-15,335.000000,348.799988,334.500000,344.429993,344.429993,6056600
|
||||
2019-01-16,344.779999,352.000000,343.500000,346.049988,346.049988,4691700
|
||||
2019-01-17,346.209991,351.500000,344.149994,347.309998,347.309998,3676700
|
||||
2019-01-18,323.000000,327.130005,299.730011,302.260010,302.260010,24150800
|
||||
2019-01-22,304.820007,308.000000,295.500000,298.920013,298.920013,12066700
|
||||
2019-01-23,292.500000,294.500000,281.690002,287.589996,287.589996,12530000
|
||||
2019-01-24,283.029999,293.679993,279.279999,291.510010,291.510010,8012200
|
||||
2019-01-25,294.390015,298.519989,289.549988,297.040009,297.040009,7249600
|
||||
2019-01-28,292.910004,297.459991,287.750000,296.380005,296.380005,6423300
|
||||
2019-01-29,295.269989,298.559998,291.799988,297.459991,297.459991,4621700
|
||||
2019-01-30,300.450012,309.000000,298.489990,308.769989,308.769989,11250300
|
||||
2019-01-31,301.000000,311.559998,294.000000,307.019989,307.019989,12569200
|
||||
2019-02-01,305.420013,316.100006,303.500000,312.209991,312.209991,7283400
|
||||
2019-02-04,312.980011,315.299988,301.880005,312.890015,312.890015,7352100
|
||||
2019-02-05,312.489990,322.440002,312.250000,321.350006,321.350006,6742800
|
||||
2019-02-06,319.589996,324.239990,315.619995,317.220001,317.220001,5038500
|
||||
2019-02-07,313.299988,314.700012,303.000000,307.510010,307.510010,6520600
|
||||
2019-02-08,306.829987,307.450012,298.500000,305.799988,305.799988,5844200
|
||||
2019-02-11,311.600006,318.600006,310.500000,312.839996,312.839996,7129700
|
||||
2019-02-12,316.200012,318.190002,309.619995,311.809998,311.809998,5517600
|
||||
2019-02-13,312.350006,312.750000,305.570007,308.170013,308.170013,5141600
|
||||
2019-02-14,303.380005,306.769989,301.000000,303.769989,303.769989,5200800
|
||||
2019-02-15,304.500000,308.000000,303.899994,307.880005,307.880005,3904900
|
||||
2019-02-19,306.559998,311.540009,305.470001,305.640015,305.640015,4168400
|
||||
2019-02-20,304.410004,306.299988,299.000000,302.559998,302.559998,7142100
|
||||
2019-02-21,301.809998,303.239990,290.500000,291.230011,291.230011,8909200
|
||||
2019-02-22,294.489990,296.500000,292.100006,294.709991,294.709991,5740600
|
||||
2019-02-25,297.910004,302.899994,297.000000,298.769989,298.769989,6626500
|
||||
2019-02-26,292.220001,302.010010,288.769989,297.859985,297.859985,8582500
|
||||
2019-02-27,301.779999,316.299988,300.549988,314.739990,314.739990,11183900
|
||||
2019-02-28,318.920013,320.000000,310.809998,319.880005,319.880005,10520700
|
||||
2019-03-01,306.940002,307.130005,291.899994,294.790009,294.790009,22911400
|
||||
2019-03-04,298.119995,299.000000,282.779999,285.359985,285.359985,17096800
|
||||
2019-03-05,282.000000,284.000000,270.100006,276.540009,276.540009,18764700
|
||||
2019-03-06,276.480011,281.510010,274.390015,276.239990,276.239990,10335500
|
||||
2019-03-07,278.839996,284.700012,274.250000,276.589996,276.589996,9442500
|
||||
2019-03-08,276.910004,285.589996,275.890015,284.140015,284.140015,8819600
|
||||
2019-03-11,283.519989,291.279999,280.500000,290.920013,290.920013,7392300
|
||||
2019-03-12,286.489990,288.070007,281.059998,283.359985,283.359985,7504100
|
||||
2019-03-13,283.899994,291.989990,282.700012,288.959991,288.959991,6844700
|
||||
2019-03-14,292.450012,295.390015,288.290009,289.959991,289.959991,7103400
|
||||
2019-03-15,283.510010,283.720001,274.399994,275.429993,275.429993,14785500
|
||||
2019-03-18,276.000000,278.049988,267.299988,269.489990,269.489990,10281000
|
||||
2019-03-19,267.500000,273.299988,263.459991,267.470001,267.470001,11800600
|
||||
2019-03-20,269.690002,274.970001,266.299988,273.600006,273.600006,6908200
|
||||
2019-03-21,272.600006,276.450012,268.450012,274.019989,274.019989,5947100
|
||||
2019-03-22,272.579987,272.799988,264.000000,264.529999,264.529999,8732600
|
||||
|
@@ -0,0 +1,253 @@
|
||||
Date,Open,High,Low,Close,Adj Close,Volume
|
||||
2018-05-23,32.700001,33.430000,32.599998,33.419998,33.419998,13407500
|
||||
2018-05-24,33.439999,33.759998,33.119999,33.520000,33.520000,14491900
|
||||
2018-05-25,33.540001,33.990002,33.310001,33.630001,33.630001,10424400
|
||||
2018-05-29,33.419998,34.830002,33.349998,34.040001,34.040001,22086700
|
||||
2018-05-30,34.200001,34.660000,34.080002,34.360001,34.360001,14588200
|
||||
2018-05-31,34.389999,34.970001,34.250000,34.700001,34.700001,14433200
|
||||
2018-06-01,35.139999,36.689999,35.090000,36.650002,36.650002,29583100
|
||||
2018-06-04,36.450001,37.980000,35.950001,37.880001,37.880001,32632800
|
||||
2018-06-05,39.529999,40.160000,39.189999,39.799999,39.799999,66122200
|
||||
2018-06-06,39.419998,40.230000,39.209999,40.099998,40.099998,147805700
|
||||
2018-06-07,40.139999,40.160000,38.639999,39.700001,39.700001,41573400
|
||||
2018-06-08,39.490002,41.259998,39.419998,41.209999,41.209999,34538900
|
||||
2018-06-11,41.419998,41.689999,40.660000,41.419998,41.419998,24605300
|
||||
2018-06-12,42.470001,44.330002,42.410000,43.490002,43.490002,51155000
|
||||
2018-06-13,44.240002,44.549999,43.419998,44.070000,44.070000,35169200
|
||||
2018-06-14,44.549999,46.799999,44.500000,46.759998,46.759998,50949100
|
||||
2018-06-15,46.619999,47.790001,45.639999,45.799999,45.799999,51489600
|
||||
2018-06-18,45.340000,46.259998,44.500000,46.000000,46.000000,26025600
|
||||
2018-06-19,45.189999,45.709999,43.570000,44.950001,44.950001,39252000
|
||||
2018-06-20,45.560001,46.919998,45.439999,46.130001,46.130001,31230200
|
||||
2018-06-21,46.360001,46.869999,44.200001,45.240002,45.240002,32200300
|
||||
2018-06-22,45.580002,46.009998,44.500000,45.880001,45.880001,28628900
|
||||
2018-06-25,45.470001,45.520000,43.330002,44.169998,44.169998,31379300
|
||||
2018-06-26,44.360001,45.320000,43.509998,44.840000,44.840000,20194100
|
||||
2018-06-27,45.500000,46.220001,43.680000,43.700001,43.700001,25875500
|
||||
2018-06-28,43.650002,44.840000,42.490002,44.790001,44.790001,18911200
|
||||
2018-06-29,45.049999,45.200001,43.560001,43.669998,43.669998,24392900
|
||||
2018-07-02,43.060001,45.000000,42.750000,44.980000,44.980000,16703600
|
||||
2018-07-03,45.360001,45.480000,43.799999,43.889999,43.889999,14237500
|
||||
2018-07-05,44.070000,45.110001,43.549999,45.060001,45.060001,16172000
|
||||
2018-07-06,44.910000,46.750000,44.610001,46.650002,46.650002,23740700
|
||||
2018-07-09,46.740002,46.900002,42.080002,44.139999,44.139999,107582400
|
||||
2018-07-10,44.200001,45.259998,43.630001,43.750000,43.750000,38467400
|
||||
2018-07-11,42.630001,44.099998,42.220001,43.869999,43.869999,35100100
|
||||
2018-07-12,44.799999,45.340000,44.360001,45.259998,45.259998,27078500
|
||||
2018-07-13,45.279999,45.320000,43.930000,44.490002,44.490002,16426700
|
||||
2018-07-16,44.299999,44.730000,43.910000,44.259998,44.259998,13012800
|
||||
2018-07-17,43.590000,45.259998,43.150002,44.709999,44.709999,20122300
|
||||
2018-07-18,44.189999,44.750000,42.740002,43.340000,43.340000,26536800
|
||||
2018-07-19,43.270000,43.869999,43.110001,43.439999,43.439999,13366800
|
||||
2018-07-20,43.500000,44.130001,43.230000,43.419998,43.419998,10437700
|
||||
2018-07-23,43.450001,43.849998,42.400002,43.310001,43.310001,15251200
|
||||
2018-07-24,43.770000,43.799999,41.590000,42.169998,42.169998,22433900
|
||||
2018-07-25,42.349998,44.389999,42.349998,44.220001,44.220001,25140700
|
||||
2018-07-26,42.869999,43.410000,42.139999,42.939999,42.939999,30018700
|
||||
2018-07-27,37.250000,37.470001,33.900002,34.119999,34.119999,122752800
|
||||
2018-07-30,34.169998,34.259998,31.070000,31.379999,31.379999,77852400
|
||||
2018-07-31,31.950001,32.480000,31.070000,31.870001,31.870001,64392200
|
||||
2018-08-01,32.250000,32.590000,31.459999,31.910000,31.910000,33231700
|
||||
2018-08-02,31.580000,32.869999,31.340000,32.820000,32.820000,27088000
|
||||
2018-08-03,32.580002,32.990002,31.799999,31.959999,31.959999,26317000
|
||||
2018-08-06,31.820000,33.040001,31.450001,32.980000,32.980000,27512400
|
||||
2018-08-07,33.099998,33.610001,32.549999,32.669998,32.669998,24635600
|
||||
2018-08-08,32.750000,32.779999,31.809999,31.840000,31.840000,22539400
|
||||
2018-08-09,31.850000,32.380001,31.610001,31.959999,31.959999,17637600
|
||||
2018-08-10,31.650000,32.250000,31.469999,32.009998,32.009998,16073900
|
||||
2018-08-13,32.040001,33.619999,32.020000,32.799999,32.799999,44134600
|
||||
2018-08-14,33.400002,33.430000,32.520000,33.189999,33.189999,26442600
|
||||
2018-08-15,32.810001,33.230000,31.950001,32.380001,32.380001,26433400
|
||||
2018-08-16,32.700001,33.150002,32.419998,32.830002,32.830002,20873200
|
||||
2018-08-17,32.740002,33.090000,32.340000,32.730000,32.730000,14874600
|
||||
2018-08-20,32.790001,32.939999,32.200001,32.599998,32.599998,16535700
|
||||
2018-08-21,32.750000,34.139999,32.599998,33.689999,33.689999,29575700
|
||||
2018-08-22,33.450001,34.169998,33.349998,33.810001,33.810001,18576300
|
||||
2018-08-23,33.900002,34.740002,33.720001,33.880001,33.880001,25746300
|
||||
2018-08-24,34.000000,34.490002,33.930000,34.279999,34.279999,15214000
|
||||
2018-08-27,34.660000,36.000000,34.480000,35.889999,35.889999,28306300
|
||||
2018-08-28,35.980000,36.040001,34.889999,35.490002,35.490002,22281600
|
||||
2018-08-29,35.410000,35.599998,34.810001,35.349998,35.349998,17697200
|
||||
2018-08-30,35.270000,36.150002,35.209999,35.639999,35.639999,19217200
|
||||
2018-08-31,35.570000,35.720001,34.590000,35.180000,35.180000,19073900
|
||||
2018-09-04,34.750000,35.130001,34.480000,34.840000,34.840000,13567600
|
||||
2018-09-05,34.650002,34.700001,32.509998,32.730000,32.730000,36051100
|
||||
2018-09-06,32.860001,32.950001,30.620001,30.809999,30.809999,36023600
|
||||
2018-09-07,30.309999,31.389999,29.820000,30.490000,30.490000,31484200
|
||||
2018-09-10,30.500000,30.600000,29.950001,30.540001,30.540001,17805800
|
||||
2018-09-11,30.440001,31.440001,30.350000,30.889999,30.889999,16000100
|
||||
2018-09-12,30.610001,30.830000,29.250000,29.750000,29.750000,29845200
|
||||
2018-09-13,30.100000,30.570000,29.860001,30.389999,30.389999,18522500
|
||||
2018-09-14,30.450001,30.770000,30.059999,30.120001,30.120001,13474700
|
||||
2018-09-17,29.049999,29.280001,28.430000,28.860001,28.860001,30592300
|
||||
2018-09-18,28.840000,29.629999,28.750000,29.219999,29.219999,15856800
|
||||
2018-09-19,29.150000,29.559999,28.820000,29.520000,29.520000,16023500
|
||||
2018-09-20,29.700001,30.020000,29.240000,29.850000,29.850000,15373600
|
||||
2018-09-21,29.860001,29.950001,28.490000,28.500000,28.500000,43122600
|
||||
2018-09-24,28.330000,29.120001,27.930000,28.600000,28.600000,20249000
|
||||
2018-09-25,28.750000,29.240000,28.440001,29.110001,29.110001,16130300
|
||||
2018-09-26,29.200001,29.450001,28.799999,29.010000,29.010000,12742100
|
||||
2018-09-27,29.059999,29.690001,28.879999,29.420000,29.420000,14830500
|
||||
2018-09-28,29.250000,29.280001,28.410000,28.459999,28.459999,22719600
|
||||
2018-10-01,28.510000,28.700001,28.000000,28.309999,28.309999,20538900
|
||||
2018-10-02,28.139999,28.620001,27.910000,28.190001,28.190001,17714400
|
||||
2018-10-03,28.379999,29.120001,28.250000,29.010000,29.010000,19358700
|
||||
2018-10-04,28.750000,28.760000,27.870001,28.230000,28.230000,21120400
|
||||
2018-10-05,28.340000,28.959999,27.969999,28.389999,28.389999,28996100
|
||||
2018-10-08,28.209999,28.940001,27.719999,28.450001,28.450001,22114400
|
||||
2018-10-09,28.700001,29.570000,28.340000,29.270000,29.270000,22749300
|
||||
2018-10-10,29.120001,29.120001,26.760000,26.790001,26.790001,40399400
|
||||
2018-10-11,26.350000,27.580000,26.190001,27.000000,27.000000,33065300
|
||||
2018-10-12,28.090000,28.170000,27.260000,27.990000,27.990000,27127500
|
||||
2018-10-15,27.850000,29.049999,27.590000,28.610001,28.610001,20225200
|
||||
2018-10-16,29.100000,29.889999,28.840000,29.870001,29.870001,18443000
|
||||
2018-10-17,29.950001,30.139999,28.959999,29.549999,29.549999,19379400
|
||||
2018-10-18,29.400000,30.240000,28.980000,29.290001,29.290001,24174300
|
||||
2018-10-19,29.330000,29.790001,28.680000,28.830000,28.830000,20112900
|
||||
2018-10-22,29.049999,29.280001,28.309999,29.180000,29.180000,21719400
|
||||
2018-10-23,28.480000,29.020000,28.070000,28.770000,28.770000,26503600
|
||||
2018-10-24,28.850000,29.770000,27.309999,27.540001,27.540001,37910200
|
||||
2018-10-25,31.320000,33.669998,30.760000,31.799999,31.799999,79251000
|
||||
2018-10-26,31.200001,33.139999,30.940001,32.360001,32.360001,47747000
|
||||
2018-10-29,32.459999,33.750000,31.620001,32.389999,32.389999,40899900
|
||||
2018-10-30,31.770000,34.549999,31.299999,33.860001,33.860001,43678200
|
||||
2018-10-31,34.369999,35.639999,34.349998,34.750000,34.750000,33063700
|
||||
2018-11-01,34.599998,34.910000,33.820000,34.619999,34.619999,27498000
|
||||
2018-11-02,34.869999,35.349998,33.849998,34.299999,34.299999,23994800
|
||||
2018-11-05,34.259998,34.279999,33.369999,34.020000,34.020000,18214300
|
||||
2018-11-06,33.959999,34.810001,33.840000,34.419998,34.419998,15508300
|
||||
2018-11-07,34.750000,35.119999,34.380001,34.990002,34.990002,16802100
|
||||
2018-11-08,34.880001,34.990002,33.869999,34.180000,34.180000,15146400
|
||||
2018-11-09,33.750000,34.419998,33.389999,34.080002,34.080002,16034700
|
||||
2018-11-12,34.000000,34.099998,31.780001,32.009998,32.009998,18147500
|
||||
2018-11-13,32.240002,32.849998,31.469999,32.490002,32.490002,17206300
|
||||
2018-11-14,32.889999,33.849998,32.750000,32.910000,32.910000,19448700
|
||||
2018-11-15,32.790001,33.360001,32.619999,33.150002,33.150002,16824800
|
||||
2018-11-16,32.830002,33.919998,32.599998,33.669998,33.669998,17904100
|
||||
2018-11-19,33.560001,33.599998,31.840000,31.980000,31.980000,15745000
|
||||
2018-11-20,29.969999,31.740000,29.940001,31.059999,31.059999,20927600
|
||||
2018-11-21,31.670000,32.080002,31.100000,31.610001,31.610001,16466900
|
||||
2018-11-23,31.299999,31.959999,31.110001,31.120001,31.120001,5813900
|
||||
2018-11-26,31.600000,32.869999,31.520000,32.820000,32.820000,17096000
|
||||
2018-11-27,32.439999,33.099998,32.360001,32.610001,32.610001,10727400
|
||||
2018-11-28,33.000000,33.000000,31.719999,32.730000,32.730000,19073300
|
||||
2018-11-29,32.459999,32.540001,29.870001,31.299999,31.299999,50505700
|
||||
2018-11-30,31.150000,31.549999,30.110001,31.450001,31.450001,25833200
|
||||
2018-12-03,32.240002,33.849998,32.209999,33.660000,33.660000,24027100
|
||||
2018-12-04,33.279999,34.160000,32.500000,32.560001,32.560001,22472000
|
||||
2018-12-06,32.459999,32.970001,31.110001,32.959999,32.959999,25922800
|
||||
2018-12-07,32.840000,34.369999,32.669998,32.830002,32.830002,29497100
|
||||
2018-12-10,32.730000,33.639999,32.259998,33.430000,33.430000,19971100
|
||||
2018-12-11,34.130001,35.750000,33.880001,34.450001,34.450001,30118600
|
||||
2018-12-12,34.970001,37.139999,34.849998,36.250000,36.250000,32608500
|
||||
2018-12-13,36.400002,36.490002,35.299999,35.889999,35.889999,22831600
|
||||
2018-12-14,35.250000,36.619999,35.049999,35.869999,35.869999,19528500
|
||||
2018-12-17,35.680000,35.700001,33.200001,33.430000,33.430000,23880900
|
||||
2018-12-18,33.630001,34.169998,33.080002,33.740002,33.740002,18885100
|
||||
2018-12-19,33.709999,34.700001,32.660000,32.930000,32.930000,24784300
|
||||
2018-12-20,32.590000,32.720001,28.510000,29.290001,29.290001,51983000
|
||||
2018-12-21,29.309999,29.760000,27.040001,27.309999,27.309999,38714100
|
||||
2018-12-24,26.549999,27.270000,26.260000,26.450001,26.450001,18208300
|
||||
2018-12-26,27.000000,28.700001,26.799999,28.660000,28.660000,35529600
|
||||
2018-12-27,28.139999,28.920000,27.260000,28.680000,28.680000,31987700
|
||||
2018-12-28,28.930000,29.139999,27.840000,28.430000,28.430000,21820500
|
||||
2018-12-31,28.600000,29.129999,28.340000,28.740000,28.740000,15975000
|
||||
2019-01-02,28.260000,28.990000,27.870001,28.809999,28.809999,15053700
|
||||
2019-01-03,28.379999,29.180000,27.940001,27.990000,27.990000,19031000
|
||||
2019-01-04,28.389999,30.100000,28.309999,29.950001,29.950001,23412600
|
||||
2019-01-07,30.200001,31.379999,29.770000,31.340000,31.340000,19917800
|
||||
2019-01-08,31.700001,32.049999,30.910000,31.799999,31.799999,18915200
|
||||
2019-01-09,31.799999,32.400002,31.540001,32.250000,32.250000,14554400
|
||||
2019-01-10,33.080002,33.500000,32.259998,33.090000,33.090000,30504500
|
||||
2019-01-11,32.849998,33.200001,32.430000,32.869999,32.869999,17732300
|
||||
2019-01-14,32.380001,32.750000,32.119999,32.369999,32.369999,9523000
|
||||
2019-01-15,32.509998,33.349998,32.450001,33.020000,33.020000,13548200
|
||||
2019-01-16,33.099998,33.299999,32.439999,32.470001,32.470001,10130200
|
||||
2019-01-17,32.470001,33.090000,32.389999,32.849998,32.849998,12059700
|
||||
2019-01-18,33.049999,33.889999,32.770000,33.270000,33.270000,16776800
|
||||
2019-01-22,32.970001,33.349998,31.930000,32.250000,32.250000,17780800
|
||||
2019-01-23,32.259998,32.450001,30.719999,30.969999,30.969999,21084400
|
||||
2019-01-24,30.940001,31.730000,30.910000,31.610001,31.610001,12470400
|
||||
2019-01-25,31.990000,33.619999,31.980000,32.900002,32.900002,22513700
|
||||
2019-01-28,32.650002,33.200001,32.119999,33.130001,33.130001,21750800
|
||||
2019-01-29,33.330002,33.549999,31.459999,31.639999,31.639999,18849800
|
||||
2019-01-30,32.040001,32.380001,31.420000,32.259998,32.259998,17142500
|
||||
2019-01-31,33.070000,33.689999,32.790001,33.560001,33.560001,21211300
|
||||
2019-02-01,33.560001,34.090000,32.959999,33.189999,33.189999,18816600
|
||||
2019-02-04,33.340000,34.180000,33.240002,33.939999,33.939999,14244100
|
||||
2019-02-05,34.290001,34.570000,33.919998,34.369999,34.369999,17610200
|
||||
2019-02-06,35.049999,35.250000,33.750000,34.160000,34.160000,34058000
|
||||
2019-02-07,31.170000,31.730000,30.309999,30.799999,30.799999,69764100
|
||||
2019-02-08,30.469999,30.740000,29.420000,30.010000,30.010000,40669800
|
||||
2019-02-11,30.170000,30.440001,29.660000,30.230000,30.230000,28838200
|
||||
2019-02-12,30.440001,30.799999,30.230000,30.389999,30.389999,20315300
|
||||
2019-02-13,30.570000,31.840000,30.549999,31.120001,31.120001,29683300
|
||||
2019-02-14,30.860001,31.280001,30.600000,30.959999,30.959999,15321100
|
||||
2019-02-15,31.200001,31.799999,30.969999,31.230000,31.230000,17591500
|
||||
2019-02-19,31.230000,32.110001,31.150000,31.650000,31.650000,14391700
|
||||
2019-02-20,31.709999,31.930000,31.209999,31.370001,31.370001,16871100
|
||||
2019-02-21,31.360001,31.480000,30.600000,30.760000,30.760000,13944900
|
||||
2019-02-22,30.809999,31.730000,30.809999,31.709999,31.709999,15413400
|
||||
2019-02-25,31.990000,32.709999,31.879999,31.990000,31.990000,15061300
|
||||
2019-02-26,31.889999,31.959999,30.990000,31.010000,31.010000,17519100
|
||||
2019-02-27,30.950001,31.000000,29.900000,30.410000,30.410000,24639100
|
||||
2019-02-28,30.250000,30.790001,30.010000,30.780001,30.780001,15242900
|
||||
2019-03-01,31.170000,31.190001,30.280001,30.620001,30.620001,12360700
|
||||
2019-03-04,30.780001,31.260000,30.070000,30.500000,30.500000,15920400
|
||||
2019-03-05,30.500000,31.230000,30.389999,31.030001,31.030001,13073500
|
||||
2019-03-06,30.940001,31.340000,30.590000,30.799999,30.799999,10938600
|
||||
2019-03-07,30.760000,30.840000,30.010000,30.120001,30.120001,15770300
|
||||
2019-03-08,29.639999,30.209999,29.410000,30.040001,30.040001,11964300
|
||||
2019-03-11,30.240000,30.910000,30.240000,30.870001,30.870001,16013200
|
||||
2019-03-12,31.150000,31.410000,30.889999,31.160000,31.160000,12324300
|
||||
2019-03-13,31.309999,31.480000,31.040001,31.299999,31.299999,10201300
|
||||
2019-03-14,31.280001,31.549999,30.940001,31.030001,31.030001,12090600
|
||||
2019-03-15,31.040001,31.410000,30.709999,31.219999,31.219999,17522700
|
||||
2019-03-18,31.250000,31.580000,30.840000,31.080000,31.080000,13172600
|
||||
2019-03-19,31.150000,31.500000,30.879999,31.270000,31.270000,15557400
|
||||
2019-03-20,31.240000,32.650002,31.160000,32.570000,32.570000,22373800
|
||||
2019-03-21,32.310001,32.689999,32.029999,32.610001,32.610001,13346900
|
||||
2019-03-22,32.500000,34.209999,32.340000,33.020000,33.020000,28034700
|
||||
2019-03-25,32.830002,33.299999,32.279999,32.590000,32.590000,15272300
|
||||
2019-03-26,32.980000,33.860001,32.919998,33.060001,33.060001,17252300
|
||||
2019-03-27,32.930000,33.450001,31.950001,32.279999,32.279999,13669400
|
||||
2019-03-28,32.290001,32.930000,31.730000,32.869999,32.869999,17750600
|
||||
2019-03-29,33.099998,33.240002,32.470001,32.880001,32.880001,13529300
|
||||
2019-04-01,33.160000,33.680000,32.700001,33.439999,33.439999,12499700
|
||||
2019-04-02,33.439999,33.889999,33.230000,33.750000,33.750000,11638000
|
||||
2019-04-03,34.000000,34.759998,33.810001,34.380001,34.380001,18041000
|
||||
2019-04-04,34.700001,35.139999,33.900002,34.419998,34.419998,14604100
|
||||
2019-04-05,34.549999,34.799999,34.369999,34.720001,34.720001,9571700
|
||||
2019-04-08,34.790001,35.060001,34.509998,34.860001,34.860001,10655000
|
||||
2019-04-09,34.840000,35.389999,34.810001,35.139999,35.139999,13889700
|
||||
2019-04-10,35.259998,35.270000,34.509998,34.750000,34.750000,11648800
|
||||
2019-04-11,34.750000,34.869999,34.410000,34.580002,34.580002,10982700
|
||||
2019-04-12,34.669998,34.830002,34.110001,34.369999,34.369999,12713800
|
||||
2019-04-15,34.380001,35.029999,34.340000,34.709999,34.709999,10248400
|
||||
2019-04-16,34.840000,34.990002,34.230000,34.459999,34.459999,9396300
|
||||
2019-04-17,34.730000,34.900002,34.200001,34.480000,34.480000,9023000
|
||||
2019-04-18,34.669998,34.860001,34.320000,34.400002,34.400002,9806100
|
||||
2019-04-22,34.400002,34.619999,33.820000,34.389999,34.389999,19704300
|
||||
2019-04-23,36.930000,40.529999,36.910000,39.770000,39.770000,104262500
|
||||
2019-04-24,39.860001,39.950001,38.799999,39.290001,39.290001,30266900
|
||||
2019-04-25,39.259998,40.130001,38.189999,38.480000,38.480000,26044800
|
||||
2019-04-26,38.590000,39.340000,38.180000,38.669998,38.669998,15270500
|
||||
2019-04-29,38.630001,39.970001,38.630001,39.779999,39.779999,19680000
|
||||
2019-04-30,39.790001,40.919998,39.650002,39.910000,39.910000,22912000
|
||||
2019-05-01,40.000000,40.070000,39.259998,39.290001,39.290001,14962600
|
||||
2019-05-02,39.240002,40.000000,38.840000,39.950001,39.950001,13419100
|
||||
2019-05-03,40.480000,40.820000,39.959999,40.799999,40.799999,15577100
|
||||
2019-05-06,39.689999,40.439999,39.450001,40.230000,40.230000,14517400
|
||||
2019-05-07,39.900002,40.150002,38.119999,38.619999,38.619999,19283100
|
||||
2019-05-08,38.450001,39.150002,38.330002,38.580002,38.580002,9168400
|
||||
2019-05-09,38.110001,39.020000,37.820000,38.790001,38.790001,10010700
|
||||
2019-05-10,38.680000,39.160000,37.860001,38.450001,38.450001,12259000
|
||||
2019-05-13,37.500000,37.639999,36.369999,36.590000,36.590000,16829700
|
||||
2019-05-14,37.040001,37.520000,36.599998,36.930000,36.930000,11125100
|
||||
2019-05-15,36.669998,38.139999,36.639999,37.900002,37.900002,11523100
|
||||
2019-05-16,38.110001,38.720001,38.049999,38.299999,38.299999,10104400
|
||||
2019-05-17,37.830002,38.130001,37.470001,37.500000,37.500000,9090300
|
||||
2019-05-20,37.119999,37.730000,36.919998,37.150002,37.150002,9411900
|
||||
2019-05-21,37.470001,37.860001,37.330002,37.470001,37.470001,8861400
|
||||
2019-05-22,37.410000,39.320000,37.240002,38.580002,38.580002,21093500
|
||||
2019-05-23,38.150002,38.290001,36.799999,37.189999,37.189999,18096372
|
||||
|
@@ -0,0 +1,31 @@
|
||||
Historical EUR to MYR Exchange Rates,Unnamed: 1
|
||||
02-11-17,4.926
|
||||
01-11-17,4.9232
|
||||
31-10-17,4.9255
|
||||
30-10-17,4.9239
|
||||
29-10-17,4.9251
|
||||
28-10-17,4.9251
|
||||
27-10-17,4.9325
|
||||
26-10-17,5.0033
|
||||
25-10-17,4.9782
|
||||
24-10-17,4.98
|
||||
23-10-17,4.9788
|
||||
22-10-17,4.9794
|
||||
21-10-17,4.9794
|
||||
20-10-17,4.9947
|
||||
19-10-17,4.9835
|
||||
18-10-17,4.9576
|
||||
17-10-17,4.9717
|
||||
16-10-17,4.9764
|
||||
15-10-17,4.9899
|
||||
14-10-17,4.9899
|
||||
13-10-17,4.987
|
||||
12-10-17,5.0071
|
||||
11-10-17,4.9793
|
||||
10-10-17,4.9729
|
||||
09-10-17,4.9701
|
||||
08-10-17,4.9701
|
||||
07-10-17,4.9701
|
||||
06-10-17,4.9613
|
||||
05-10-17,4.9747
|
||||
04-10-17,4.9776
|
||||
|
@@ -0,0 +1,27 @@
|
||||
"Date","Price","Open","High","Low","Vol.","Change %"
|
||||
"Nov 02, 2017","54.27","54.26","54.39","54.22","0","0.06"
|
||||
"Nov 01, 2017","54.24","54.59","55.22","53.89","0","-0.26"
|
||||
"Oct 31, 2017","54.38","54.08","54.85","53.93","497.30K","0.42"
|
||||
"Oct 30, 2017","54.15","54.16","54.46","53.75","565.04K","0.46"
|
||||
"Oct 27, 2017","53.90","52.80","54.20","52.25","730.92K","2.39"
|
||||
"Oct 26, 2017","52.64","52.19","52.86","51.91","594.65K","0.88"
|
||||
"Oct 25, 2017","52.18","52.56","52.57","51.89","681.74K","-0.55"
|
||||
"Oct 24, 2017","52.47","51.89","52.62","51.55","709.55K","1.10"
|
||||
"Oct 23, 2017","51.90","52.07","52.30","51.68","583.56K","0.84"
|
||||
"Oct 20, 2017","51.47","51.42","51.73","50.70","32.96K","0.35"
|
||||
"Oct 19, 2017","51.29","52.05","52.17","51.07","127.41K","-1.44"
|
||||
"Oct 18, 2017","52.04","51.94","52.33","51.69","152.62K","0.31"
|
||||
"Oct 17, 2017","51.88","51.93","52.25","51.21","471.61K","0.02"
|
||||
"Oct 16, 2017","51.87","51.43","52.37","51.35","520.73K","0.82"
|
||||
"Oct 13, 2017","51.45","50.73","51.72","50.70","667.50K","1.68"
|
||||
"Oct 12, 2017","50.60","51.00","51.13","50.15","729.27K","-1.36"
|
||||
"Oct 11, 2017","51.30","50.94","51.42","50.61","651.95K","0.75"
|
||||
"Oct 10, 2017","50.92","49.55","51.06","49.54","664.84K","2.70"
|
||||
"Oct 09, 2017","49.58","49.25","49.79","49.13","505.08K","0.59"
|
||||
"Oct 06, 2017","49.29","50.75","50.82","49.10","743.11K","-2.95"
|
||||
"Oct 05, 2017","50.79","49.88","51.22","49.85","654.39K","1.62"
|
||||
"Oct 04, 2017","49.98","50.16","50.67","49.76","598.84K","-0.87"
|
||||
"Oct 03, 2017","50.42","50.59","50.73","50.14","462.74K","-0.32"
|
||||
"Oct 02, 2017","50.58","51.64","51.71","50.07","600.93K","-2.11"
|
||||
"","","","","","",""
|
||||
"","Highest:55.22","Lowest:49.10","Difference:6.12","Average:51.82","Change %:5.03"
|
||||
|
@@ -0,0 +1,31 @@
|
||||
Historical USD to MYR Exchange Rates,Unnamed: 1
|
||||
02-11-17,4.226
|
||||
01-11-17,4.232
|
||||
31-10-17,4.231
|
||||
30-10-17,4.238
|
||||
29-10-17,4.241
|
||||
28-10-17,4.241
|
||||
27-10-17,4.24
|
||||
26-10-17,4.2283
|
||||
25-10-17,4.233
|
||||
24-10-17,4.2325
|
||||
23-10-17,4.23
|
||||
22-10-17,4.224
|
||||
21-10-17,4.224
|
||||
20-10-17,4.2245
|
||||
19-10-17,4.221
|
||||
18-10-17,4.21
|
||||
17-10-17,4.2195
|
||||
16-10-17,4.2155
|
||||
15-10-17,4.22
|
||||
14-10-17,4.22
|
||||
13-10-17,4.2095
|
||||
12-10-17,4.217
|
||||
11-10-17,4.211
|
||||
10-10-17,4.2215
|
||||
09-10-17,4.232
|
||||
08-10-17,4.236
|
||||
07-10-17,4.236
|
||||
06-10-17,4.2345
|
||||
05-10-17,4.2295
|
||||
04-10-17,4.2235
|
||||
|
@@ -0,0 +1,682 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0812 10:02:17.549519 140290267916096 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:12: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 10:02:17.551540 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f975091ada0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:02:17.552432 140290267916096 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0812 10:02:19.808033 140290267916096 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0812 10:02:19.816455 140290267916096 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:27: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0812 10:02:20.147778 140290267916096 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 10:02:20.154457 140290267916096 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:961: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 10:02:20.564182 140290267916096 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:29: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:10<00:00, 4.33it/s, acc=97.2, cost=0.00221]\n",
|
||||
"W0812 10:03:39.929984 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f975091add8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.33it/s, acc=97.4, cost=0.00193]\n",
|
||||
"W0812 10:04:50.024182 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f974694f240>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.34it/s, acc=97.2, cost=0.00212]\n",
|
||||
"W0812 10:05:59.904235 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f9746a5af28>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.30it/s, acc=97.3, cost=0.00195]\n",
|
||||
"W0812 10:07:10.197728 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f9704151390>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.31it/s, acc=97.2, cost=0.00208]\n",
|
||||
"W0812 10:08:20.024446 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f96b8051f98>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.31it/s, acc=97.1, cost=0.00224]\n",
|
||||
"W0812 10:09:30.567560 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f96a40a6fd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.30it/s, acc=97, cost=0.00229] \n",
|
||||
"W0812 10:10:40.653531 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f968d66ac88>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.23it/s, acc=97.5, cost=0.00168]\n",
|
||||
"W0812 10:11:50.874499 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f96941b8438>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:10<00:00, 4.32it/s, acc=97.3, cost=0.00193]\n",
|
||||
"W0812 10:13:01.677561 140290267916096 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f968b7442e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.28it/s, acc=97.8, cost=0.00115]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeXxNZ/7A8c9dElm5aUpJYontIGFQFYTQjqXVGV3H1JKKH6ZqidrKoBlB/RJLLKGoGa1S7TBTZSytXzviWqot0hL0pFKExJJGUomI696b3x/3JoIECcnN8n2/Xnm59zzPOef7nDySfO/znOdo8vLyEEIIIYQQQghR+WkdHYAQQgghhBBCiEdDEjwhhBBCCCGEqCIkwRNCCCGEEEKIKkISPCGEEEIIIYSoIiTBE0IIIYQQQogqQhI8IYQQQgghhKgiJMETQgghhBBCiCpC7+gAhBBCCFE6iqJogGnAG4AB2AH8RVXVq3fUewxQAVVV1a7FHOs1IBKoC9wAdgJj84+lKEr2Hbu4Au+pqjpWUZRBwKpCZVp7eQdVVQ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WCwYDF5ERMwutv64cRN5772lhIUNQKPR4OTkTHj4RHx8fImImE1MTDShof3RanX06tWHwYPDCA+fwLRpk/D09CQoqAuLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,680 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" _, last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('decoder', reuse = False):\n",
|
||||
" rnn_cells_dec = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)], state_is_tuple = False\n",
|
||||
" )\n",
|
||||
" drop_dec = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_dec, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop_dec, self.X, initial_state = last_state, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0813 21:47:16.666563 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69f3ff7908>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:47:16.753933 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69fd7aa860>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:47:16.834197 140095600830272 deprecation.py:323] From <ipython-input-10-f89ab136c7c7>:41: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.11it/s, acc=97.9, cost=0.00101] \n",
|
||||
"W0813 21:48:54.353741 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69fd79e9e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:48:54.437589 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69f2dedeb8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.05it/s, acc=98.3, cost=0.00069] \n",
|
||||
"W0813 21:50:34.225154 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69f35367f0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:50:34.305581 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f696417da20>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.06it/s, acc=97.7, cost=0.00117] \n",
|
||||
"W0813 21:52:13.825603 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69e80d60f0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:52:13.908980 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f695cdf5518>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:37<00:00, 3.08it/s, acc=98.4, cost=0.000614]\n",
|
||||
"W0813 21:53:52.767824 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f695d0ed1d0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:53:52.849310 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693eab10f0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.03it/s, acc=98.2, cost=0.000755]\n",
|
||||
"W0813 21:55:32.572073 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693ed38cf8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:55:32.654169 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693c7376a0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.07it/s, acc=98.3, cost=0.000681]\n",
|
||||
"W0813 21:57:12.073868 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693ce4e080>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 21:57:12.156364 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693a339e80>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.01it/s, acc=97.7, cost=0.00126] \n",
|
||||
"W0813 21:58:51.933507 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693ab0ffd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0813 21:58:52.153095 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693801aba8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.04it/s, acc=98.5, cost=0.000589]\n",
|
||||
"W0813 22:00:31.650501 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f69380c7f98>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:00:31.732362 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6935bf8ef0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.01it/s, acc=98.4, cost=0.000625]\n",
|
||||
"W0813 22:02:11.445839 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6936470550>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:02:11.528598 140095600830272 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f693387feb8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.08it/s, acc=96.8, cost=0.0027] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeVxVZf7A8c+97JtAiiKruD05aj81E5VynFJbXNpmKkvT0horRbPMRs1EzdFyx1yysSyz0sYV02wZx9wql8m0OriDC4IIAsLleu+5vz/OhUDBFbyI3/frdV/c+5znnPN9Do/gl+c5zzE5HA6EEEIIIYQQQtz4zK4OQAghhBBCCCFExZAETwghhBBCCCGqCUnwhBBCCCGEEKKakARPCCGEEEIIIaoJSfCEEEIIIYQQopqQBE8IIYQQQgghqglJ8IQQQgghhBCimnB3dQBCCCGEuDpKKRMwAvg7EAR8CTyvaVrOefVuATRA0zTtznKO5QVMBB4HfIBPgcGapp1zbq8HzAbaAYXAF8AQTdNszu3vAX8GGgHPapr2YYljzwV6lTidB2DVNC3gGpovhBCiDDKCJ4QQosIppW6qPyC6sL1PA72BOCA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JOkpuLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,736 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
"\n",
|
||||
" backward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.backward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.forward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" _, last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward,\n",
|
||||
" drop_backward,\n",
|
||||
" self.X,\n",
|
||||
" initial_state_fw = self.forward_hidden_layer,\n",
|
||||
" initial_state_bw = self.backward_hidden_layer,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('decoder', reuse = False):\n",
|
||||
" backward_rnn_cells_decoder = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells_decoder = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" drop_backward_decoder = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells_decoder, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward_decoder = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells_decoder, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward_decoder, drop_backward_decoder, self.X, \n",
|
||||
" initial_state_fw = last_state[0],\n",
|
||||
" initial_state_bw = last_state[1],\n",
|
||||
" dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" self.outputs = tf.concat(self.outputs, 2)\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0813 22:30:03.664880 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c6a29f9e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:30:03.666436 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c6a0dba58>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:30:03.827417 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c6a0db5f8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:30:03.828239 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c6a0dbe48>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0813 22:30:03.988492 140106178451264 deprecation.py:323] From <ipython-input-10-79385dfa86b9>:67: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [02:29<00:00, 2.03it/s, acc=96.4, cost=0.00318] \n",
|
||||
"W0813 22:32:35.430384 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c68a2de10>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:32:35.431268 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c689d96a0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:32:35.592414 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c689d91d0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:32:35.593283 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c680bc630>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:30<00:00, 2.00it/s, acc=98.1, cost=0.000912]\n",
|
||||
"W0813 22:35:08.073616 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c68a6df28>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:35:08.074523 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c40475208>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:35:08.237059 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bbbf9afd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:35:08.237945 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bbbf56fd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:31<00:00, 1.99it/s, acc=98.2, cost=0.000814]\n",
|
||||
"W0813 22:37:40.822348 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6c40403080>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:37:40.823194 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bba08cac8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:37:40.984025 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb9234278>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:37:40.984853 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb90d59e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:31<00:00, 1.98it/s, acc=98.2, cost=0.000732]\n",
|
||||
"W0813 22:40:14.461846 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb230a7f0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:40:14.462596 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb31e4588>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:40:14.624744 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb2361198>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:40:14.625597 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb223d5c0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:28<00:00, 2.04it/s, acc=98.6, cost=0.000437]\n",
|
||||
"W0813 22:42:44.319911 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6baf458dd8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:42:44.320685 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb0355400>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:42:44.481708 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6baf534d30>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:42:44.482524 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6baf3f2c88>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:30<00:00, 2.00it/s, acc=98.8, cost=0.000304]\n",
|
||||
"W0813 22:45:16.281273 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bb04d4240>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:45:16.282183 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bad522c18>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:45:16.443265 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bad522940>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:45:16.444107 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6bac5314e0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:31<00:00, 1.99it/s, acc=98.5, cost=0.000517]\n",
|
||||
"W0813 22:47:49.574930 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba974ef28>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:47:49.575763 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6baa69bc18>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:47:49.735839 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba9794a20>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:47:49.736666 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba7e914a8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:31<00:00, 1.99it/s, acc=96.9, cost=0.00272] \n",
|
||||
"W0813 22:50:22.981087 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba69149b0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:50:22.982089 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba7828080>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:50:23.141866 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba69434e0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:50:23.142687 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba68067b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:31<00:00, 1.99it/s, acc=98.2, cost=0.000705]\n",
|
||||
"W0813 22:52:55.940449 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba3aacbe0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:52:55.941309 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba49a4080>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:52:56.101360 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba21a14e0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0813 22:52:56.102182 140106178451264 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f6ba39be898>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:27<00:00, 2.03it/s, acc=98.6, cost=0.000453]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeXgUVdbA4V9vWTo7kADZIGylsgiIBIjgBqgIuM04IiI44IjKJoo6gAwBVFBWoyzioAiiwoxsAdQPR2VYRJYogk6FfUkCCZC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IBV7KyskhNTcFqtdKiRSuWLPmUAwf2c+DAPrKzs4mJaUxBQUHxYC5r1qzCbtcnGM/MzMRmsxEb25HBg4fg7+9PamoKfn5+2Gw2HA5HlZ//5ZAWPCGEEEIIIUS1FhISwtixE4iPH4PT6SQ4OIRx4yaWW3748JeZPfs9Bgzog8FgwGLxYtiwl6lbtx7PPfcis2e/x+rVK/Dy8ub++3vQvn0Hhg0byejRrxAQEEBsbCeCgoIASE8/wZQpk3A6nTidTjp06ETz5i0xGo107/4A/fs/QUBA4AWDrAAMGvQ0GRknyMnJ4ZFHehAb25HXX3+jyq4TgMHlcl2ykKIoU4HLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,721 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" lambda_coeff = 0.5\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" _, last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" self.z_mean = tf.layers.dense(last_state, size)\n",
|
||||
" self.z_log_sigma = tf.layers.dense(last_state, size)\n",
|
||||
" \n",
|
||||
" epsilon = tf.random_normal(tf.shape(self.z_log_sigma))\n",
|
||||
" self.z_vector = self.z_mean + tf.exp(self.z_log_sigma)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('decoder', reuse = False):\n",
|
||||
" rnn_cells_dec = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)], state_is_tuple = False\n",
|
||||
" )\n",
|
||||
" drop_dec = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_dec, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" x = tf.concat([tf.expand_dims(self.z_vector, axis=0), self.X], axis = 1)\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop_dec, self.X, initial_state = last_state, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.lambda_coeff = lambda_coeff\n",
|
||||
" \n",
|
||||
" self.kl_loss = -0.5 * tf.reduce_sum(1.0 + 2 * self.z_log_sigma - self.z_mean ** 2 - \n",
|
||||
" tf.exp(2 * self.z_log_sigma), 1)\n",
|
||||
" self.kl_loss = tf.scalar_mul(self.lambda_coeff, self.kl_loss)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits) + self.kl_loss)\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x = np.random.binomial(1, 0.5, batch_x.shape) * batch_x\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0816 15:26:45.502804 139658996016960 deprecation.py:323] From <ipython-input-6-d907d7a4dee6>:13: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0816 15:26:45.505823 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f04dbc873c8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:26:45.507445 139658996016960 deprecation.py:323] From <ipython-input-6-d907d7a4dee6>:17: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0816 15:26:45.829126 139658996016960 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0816 15:26:45.832581 139658996016960 deprecation.py:323] From <ipython-input-6-d907d7a4dee6>:28: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0816 15:26:46.024316 139658996016960 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 15:26:46.031064 139658996016960 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:961: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 15:26:46.507349 139658996016960 deprecation.py:323] From <ipython-input-6-d907d7a4dee6>:31: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"W0816 15:26:46.696353 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f04778236d8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:26:46.879564 139658996016960 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1205: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.80it/s, acc=97, cost=0.00235] \n",
|
||||
"W0816 15:28:35.363878 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f0455d7deb8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:28:35.471002 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f044b3bf198>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:46<00:00, 2.82it/s, acc=96.9, cost=0.00305]\n",
|
||||
"W0816 15:30:22.970038 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f044b349f60>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:30:23.075726 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f03f03e0e10>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.77it/s, acc=95.1, cost=0.00633]\n",
|
||||
"W0816 15:32:11.926008 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f03f043bfd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:32:12.031505 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f03a4bb2a58>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.86it/s, acc=95.9, cost=0.00422]\n",
|
||||
"W0816 15:34:00.252120 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f03f0194f60>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0816 15:34:00.478516 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f044c079320>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.75it/s, acc=96.3, cost=0.00351]\n",
|
||||
"W0816 15:35:49.588577 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f03965d9940>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:35:49.693055 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f0393f722e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.79it/s, acc=96.2, cost=0.00384]\n",
|
||||
"W0816 15:37:38.517486 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f0393f489e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:37:38.625684 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f039189c940>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.79it/s, acc=95.6, cost=0.00472]\n",
|
||||
"W0816 15:39:27.256033 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f0391929fd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:39:27.363451 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f038f1f4d68>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.78it/s, acc=96.1, cost=0.00394]\n",
|
||||
"W0816 15:41:15.619689 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f038f286940>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:41:15.724680 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f038cb1fda0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.82it/s, acc=97.3, cost=0.00223]\n",
|
||||
"W0816 15:43:04.145420 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f038d74f630>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0816 15:43:04.251741 139658996016960 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f0388c03d68>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:45<00:00, 2.82it/s, acc=96.6, cost=0.00292]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeXxM9/7H8dfMJJF9EWskiO2QhKJaWykatNqry7231xZLS6/aaZUfqoL2osQSimrVVlp6aV26aDW2qrZKq7YTeyKJSiORTMaYzPL7YyYRBAmSSeLzfDw8kjnnzDmfc/JF3vP9nu/R2Gw2hBBCCCGEEEKUfVpnFyCEEEIIIYQQ4v6QgCeEEEIIIYQQ5YQEPCGEEEIIIYQoJyTgCSGEEEIIIUQ5IQFPCCGEEEIIIcoJCXhCCCGEEEIIUU5IwBNCCCGEEEKIcsLF2QUIIYQQ4u4oiqIBJgD/BvyBL4FXVFXNdKxfAfQCTPne5qeqquUO+90OdAJcVVU1O5ZNA54DGgHTVVWdUoQ6KgCLgX8ABmCWqqox93LuQgghCiY9eEIIIe47RVEeqA8QnXi+fYEooC0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ffozYsQYJkx4HR8fH1q2bIOfnx8AFy/+ycyZ07FYLFgsFlqLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,687 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.GRUCell(size_layer)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" _, last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('decoder', reuse = False):\n",
|
||||
" rnn_cells_dec = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)], state_is_tuple = False\n",
|
||||
" )\n",
|
||||
" drop_dec = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_dec, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop_dec, self.X, initial_state = last_state, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0816 17:29:50.622041 140611410274112 deprecation.py:323] From <ipython-input-6-7a2cc302036d>:12: GRUCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.GRUCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0816 17:29:50.624857 140611410274112 deprecation.py:323] From <ipython-input-6-7a2cc302036d>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0816 17:29:50.941864 140611410274112 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0816 17:29:50.945153 140611410274112 deprecation.py:323] From <ipython-input-6-7a2cc302036d>:27: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0816 17:29:51.137009 140611410274112 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 17:29:51.144164 140611410274112 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:564: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 17:29:51.155159 140611410274112 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:574: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 17:29:51.387444 140611410274112 deprecation.py:323] From <ipython-input-6-7a2cc302036d>:41: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.15it/s, acc=97.7, cost=0.00125] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:37<00:00, 3.07it/s, acc=98.1, cost=0.000968]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:34<00:00, 3.14it/s, acc=97.3, cost=0.00201] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.12it/s, acc=97.8, cost=0.00113] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.15it/s, acc=97.6, cost=0.00147] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.11it/s, acc=98.2, cost=0.000856]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:35<00:00, 3.14it/s, acc=97.7, cost=0.00139] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.11it/s, acc=97.1, cost=0.002] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:36<00:00, 3.13it/s, acc=97.8, cost=0.00111] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:37<00:00, 3.06it/s, acc=97.4, cost=0.00152] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOy9ebQlyX3X+YklM++9b6t61XurV3V3dksjjDcxHKPBYNnH2AbPgQFjS5YwA3gY4HiGAwyLGcEAZrUZtgGNQbbkxrYEMxbG9hgfbOPBxkeYGZYxUqe61d1SS+ruqnr16lW9d+/NJSLmj4hc7n33Vb2qrq41vnWiIuIXkXlze5nxjd8SwjlHRERERERERERERERExK0PeaMPICIiIiIiIiIiIiIiIuLaIBK8iIiIiIiIiIiIiIiI2wSR4EVERERERERERERERNwmiAQvIiIiIiIiIiIiIiLiNkEkeBEREREREREREREREbcJIsGLiIiIiIiIiIiIiIi4TRAJXkRERERERERERERExG0CfaMPICIiIiIiIuLqkOe5AP408J3ACeCngD9QFMWF0J4Bfx/4b4Ap8NeKovi+S+zrLwDfAawD/x74Q0VR/OfQvh329W7AAf8C+IOD33oZuBcwYZf/piiKr1vxOz8L/GYgKYqieWNXICIiIiJiGVGDFxERERFxzZHn+R01gXgDz/d9wLc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wznHwcE+586d49y5nZDOLeS7u7tdvSznfPCDP8A73vFrbvShHxuR4EVERERERERERETcpphOp/zkT/44n//8KyilSZKEJNFonZAkCUqpIEvQOkFr1bUN21vZsN3nGq0X9yeEuC7n5pzj4sUL7OzssLu7TNLOsbNzbqW8qqqV+xNCcPLkSU6e3GZ7+xQPPfQQ99xzH/fcc891OZ9rhUjwIiIiIiIiIiIiIm4zFMVzfOQjH+JjH/tR9vbO35BjEEJ0SUq5UG8THJb5xCX7Wms5f36XpmlW/raUku3t7Y6sPfLIo3zZl315Vz916lRX3t7eZnt7m62tEyilrus1ejNwWYKX5/nfAH4H8CjwjqIofjXInwI+DJwCdoD3FUXx/Btpi4iIiIiIiIiIiIi4OsxmM/75P/84P/RDP8gnPvHLpGnKN33Tb+N97/u9fPmXfyVN02BMQ103NE1NXdc0TUPTNKFch3Lf3vYftrf9h+19f4NzLiSLcwzqRydYrFtrB3UO9RNCcvLkyQWCdvLkdkfctrZOIOWdGW7kOBq8jwN/C/jXS/J/APy9oiiezfP8vcAHgd/8BtsiIiIiIiIiIiIiIq4Azz//aT7ykQ/x0Y/+MOfPn+fxx9/KBz7wF/mWb/k27rrrrq5flmU38CgjrhcuS/CKovhFgDzPO1me5/cAXwZ8bRD9CPB38zy/GxBX01YUxZk3fDYRERERERERERERdwDKsuQnfuKf8ZGP/AC//Mu/RJIkfMM3/Fbe977v4Ku+6l13rPYq4up98B4CvlAUhQEoisLkef7FIBdX2RYJXkRERERERERERMQl8OKLL/CRj/wgH/3oP2ZnZ4dHHnmU7/7uP8fv/t3vveWCgUS8Obhlg6ycOrV+ow9hJe6+e+NGH0LETYL4LES0iM9CRIub6Vlwzi340QxTVVUr5cvJWtvta1V+XNlx+rf+OEO/nFXly7Vfqq/WmpMLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,726 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.GRUCell(size_layer)\n",
|
||||
"\n",
|
||||
" backward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.backward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.forward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" _, last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward,\n",
|
||||
" drop_backward,\n",
|
||||
" self.X,\n",
|
||||
" initial_state_fw = self.forward_hidden_layer,\n",
|
||||
" initial_state_bw = self.backward_hidden_layer,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('decoder', reuse = False):\n",
|
||||
" backward_rnn_cells_decoder = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells_decoder = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" drop_backward_decoder = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells_decoder, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward_decoder = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells_decoder, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward_decoder, drop_backward_decoder, self.X, \n",
|
||||
" initial_state_fw = last_state[0],\n",
|
||||
" initial_state_bw = last_state[1],\n",
|
||||
" dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" self.outputs = tf.concat(self.outputs, 2)\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0816 18:33:46.362064 140384958228288 deprecation.py:323] From <ipython-input-6-2500790da2db>:12: GRUCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.GRUCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0816 18:33:46.364130 140384958228288 deprecation.py:323] From <ipython-input-6-2500790da2db>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0816 18:33:46.687459 140384958228288 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0816 18:33:46.692470 140384958228288 deprecation.py:323] From <ipython-input-6-2500790da2db>:42: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API\n",
|
||||
"W0816 18:33:46.693083 140384958228288 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py:464: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0816 18:33:46.884588 140384958228288 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 18:33:46.891244 140384958228288 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:564: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 18:33:46.900250 140384958228288 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:574: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 18:33:47.374557 140384958228288 deprecation.py:323] From <ipython-input-6-2500790da2db>:67: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [02:28<00:00, 2.02it/s, acc=97.7, cost=0.00125] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:26<00:00, 2.05it/s, acc=98.3, cost=0.000708]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:29<00:00, 2.01it/s, acc=98.1, cost=0.000848]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:27<00:00, 2.03it/s, acc=98.5, cost=0.000662]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:30<00:00, 2.01it/s, acc=97.4, cost=0.0017] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:29<00:00, 2.01it/s, acc=97.7, cost=0.00127] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:30<00:00, 1.99it/s, acc=98.3, cost=0.000625]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:29<00:00, 2.01it/s, acc=98.2, cost=0.000883]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:29<00:00, 2.01it/s, acc=98.5, cost=0.000547]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:29<00:00, 2.00it/s, acc=96.9, cost=0.00229] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4IAAAFBCAYAAAAi6hFSAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeZwU1b3//1dVd88+MDiyjwgqFIomiEQ2JYtboonZjDeoRLzqvSZxiSZe/akx4pIrLrgQt3gvakQ0aNyCS7xJvkZxR4jErRCUfRuBgRnGmZ6uqt8fVdVdPdPDOjM9zLyfj0dPV51zqvrT1ad76lOnutrwPA8RERERERHpPsx8ByAiIiIiIiIdS4mgiIiIiIhIN6NEUEREREREpJtRIigiIiIiItLNKBEUERERERHpZpQIioiIiIiIdDNKBEVERERERLqZeL4DEBERkfZlWVZv4A7gJMAFnrdt+/Sg7gNg/0jzIuAF27a/k2M9BnAF8J9ABfA88B+2bW8N6gcCdwNHA/XA9bZt3xtZ/vfAV4GhwL/btv1gpG4K8L/AF5GH/LZt2y/vwVMXEZFWaERQRETywrKsbnUwMs/P90lgHTAI6APcElbYtj3Ctu0y27bLgHJgJfB4K+v5CTAZmAAMAIqBGZH6WcBnQF/8pPO3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1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,704 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" lambda_coeff = 0.5\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.GRUCell(size_layer)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" _, last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" self.z_mean = tf.layers.dense(last_state, size)\n",
|
||||
" self.z_log_sigma = tf.layers.dense(last_state, size)\n",
|
||||
" \n",
|
||||
" epsilon = tf.random_normal(tf.shape(self.z_log_sigma))\n",
|
||||
" self.z_vector = self.z_mean + tf.exp(self.z_log_sigma)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('decoder', reuse = False):\n",
|
||||
" rnn_cells_dec = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)], state_is_tuple = False\n",
|
||||
" )\n",
|
||||
" drop_dec = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_dec, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" x = tf.concat([tf.expand_dims(self.z_vector, axis=0), self.X], axis = 1)\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop_dec, self.X, initial_state = last_state, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.lambda_coeff = lambda_coeff\n",
|
||||
" \n",
|
||||
" self.kl_loss = -0.5 * tf.reduce_sum(1.0 + 2 * self.z_log_sigma - self.z_mean ** 2 - \n",
|
||||
" tf.exp(2 * self.z_log_sigma), 1)\n",
|
||||
" self.kl_loss = tf.scalar_mul(self.lambda_coeff, self.kl_loss)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits) + self.kl_loss)\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x = np.random.binomial(1, 0.5, batch_x.shape) * batch_x\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0816 23:54:04.861056 140552998012736 deprecation.py:323] From <ipython-input-6-f18f06dc1a5f>:13: GRUCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.GRUCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0816 23:54:04.862557 140552998012736 deprecation.py:323] From <ipython-input-6-f18f06dc1a5f>:17: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0816 23:54:05.179484 140552998012736 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0816 23:54:05.182720 140552998012736 deprecation.py:323] From <ipython-input-6-f18f06dc1a5f>:28: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0816 23:54:05.374030 140552998012736 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 23:54:05.380675 140552998012736 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:564: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 23:54:05.389776 140552998012736 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:574: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0816 23:54:05.536239 140552998012736 deprecation.py:323] From <ipython-input-6-f18f06dc1a5f>:31: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"W0816 23:54:05.986564 140552998012736 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_grad.py:1205: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.73it/s, acc=96, cost=0.00448] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:49<00:00, 2.74it/s, acc=95.6, cost=0.00512]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.76it/s, acc=96.2, cost=0.0037] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.75it/s, acc=95.5, cost=0.00715]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.78it/s, acc=96.6, cost=0.0041] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.75it/s, acc=97.3, cost=0.00204]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:47<00:00, 2.81it/s, acc=62, cost=7.74] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.80it/s, acc=95, cost=0.00699] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.76it/s, acc=96.8, cost=0.00279]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:48<00:00, 2.75it/s, acc=97.1, cost=0.00215]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/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IEHtl+/dg27MjRWLv8IMHC5XHTuFI8jQGeHtpWnn3sFwzCodPkwLMEUlFSRsSOdxR/MobioAJvNRmFhIem7cujYMR6v18vzz09j0KDBjBx5dgtdhYaRgCeEEEIIIYQ4xKi+x+5lay5V1V5KK9woikLH6GCsFgu6rh+yTXCw47BXGTz33Et06pRwyNLKykoUBdpHBB94Tk/XDbw+nYcmP8ek/3c3Q4edjdvjZdzVYymrqKRzQizvvbeI1NRfWb9+LbNnz2LOnPlNeconRUbRFEIIIYQQQrRKHo+PvOIqevbsze6sDPbszgbgiy+W0727isMRUufrRo06h/nz5+Hz+QAoKSkhNzcHh8NBnz79WLToYFllWVkpAXYrFRVOUpISiQ4PYt2ar/F43MTFhFDpLMPlcjFs2AjuuONuQkNDyc3NISQkBJfLhdfrbfoLcRykB08IIYQQQgjR6uiGQbHTTXerQo/keJ544mmmTn0Mn89HZGQUTz45rd7X3nvvA7zxxmtMnDgORVGw2wOYPPkB4uM78eST05gxYzrjx1+LxWJlzJiLuOmmiUyePIVHH32QsLALine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,717 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def layer_norm(inputs, epsilon=1e-8):\n",
|
||||
" mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True)\n",
|
||||
" normalized = (inputs - mean) / (tf.sqrt(variance + epsilon))\n",
|
||||
"\n",
|
||||
" params_shape = inputs.get_shape()[-1:]\n",
|
||||
" gamma = tf.get_variable('gamma', params_shape, tf.float32, tf.ones_initializer())\n",
|
||||
" beta = tf.get_variable('beta', params_shape, tf.float32, tf.zeros_initializer())\n",
|
||||
" \n",
|
||||
" outputs = gamma * normalized + beta\n",
|
||||
" return outputs\n",
|
||||
"\n",
|
||||
"def multihead_attn(queries, keys, q_masks, k_masks, future_binding, num_units, num_heads):\n",
|
||||
" \n",
|
||||
" T_q = tf.shape(queries)[1] \n",
|
||||
" T_k = tf.shape(keys)[1] \n",
|
||||
"\n",
|
||||
" Q = tf.layers.dense(queries, num_units, name='Q') \n",
|
||||
" K_V = tf.layers.dense(keys, 2*num_units, name='K_V') \n",
|
||||
" K, V = tf.split(K_V, 2, -1) \n",
|
||||
"\n",
|
||||
" Q_ = tf.concat(tf.split(Q, num_heads, axis=2), axis=0) \n",
|
||||
" K_ = tf.concat(tf.split(K, num_heads, axis=2), axis=0) \n",
|
||||
" V_ = tf.concat(tf.split(V, num_heads, axis=2), axis=0) \n",
|
||||
"\n",
|
||||
" align = tf.matmul(Q_, tf.transpose(K_, [0,2,1])) \n",
|
||||
" align = align / np.sqrt(K_.get_shape().as_list()[-1]) \n",
|
||||
"\n",
|
||||
" paddings = tf.fill(tf.shape(align), float('-inf')) \n",
|
||||
"\n",
|
||||
" key_masks = k_masks \n",
|
||||
" key_masks = tf.tile(key_masks, [num_heads, 1]) \n",
|
||||
" key_masks = tf.tile(tf.expand_dims(key_masks, 1), [1, T_q, 1]) \n",
|
||||
" align = tf.where(tf.equal(key_masks, 0), paddings, align) \n",
|
||||
"\n",
|
||||
" if future_binding:\n",
|
||||
" lower_tri = tf.ones([T_q, T_k]) \n",
|
||||
" lower_tri = tf.linalg.LinearOperatorLowerTriangular(lower_tri).to_dense() \n",
|
||||
" masks = tf.tile(tf.expand_dims(lower_tri,0), [tf.shape(align)[0], 1, 1]) \n",
|
||||
" align = tf.where(tf.equal(masks, 0), paddings, align) \n",
|
||||
" \n",
|
||||
" align = tf.nn.softmax(align) \n",
|
||||
" query_masks = tf.to_float(q_masks) \n",
|
||||
" query_masks = tf.tile(query_masks, [num_heads, 1]) \n",
|
||||
" query_masks = tf.tile(tf.expand_dims(query_masks, -1), [1, 1, T_k]) \n",
|
||||
" align *= query_masks\n",
|
||||
" \n",
|
||||
" outputs = tf.matmul(align, V_) \n",
|
||||
" outputs = tf.concat(tf.split(outputs, num_heads, axis=0), axis=2) \n",
|
||||
" outputs += queries \n",
|
||||
" outputs = layer_norm(outputs) \n",
|
||||
" return outputs\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def pointwise_feedforward(inputs, hidden_units, activation=None):\n",
|
||||
" outputs = tf.layers.dense(inputs, 4*hidden_units, activation=activation)\n",
|
||||
" outputs = tf.layers.dense(outputs, hidden_units, activation=None)\n",
|
||||
" outputs += inputs\n",
|
||||
" outputs = layer_norm(outputs)\n",
|
||||
" return outputs\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def learned_position_encoding(inputs, mask, embed_dim):\n",
|
||||
" T = tf.shape(inputs)[1]\n",
|
||||
" outputs = tf.range(tf.shape(inputs)[1]) # (T_q)\n",
|
||||
" outputs = tf.expand_dims(outputs, 0) # (1, T_q)\n",
|
||||
" outputs = tf.tile(outputs, [tf.shape(inputs)[0], 1]) # (N, T_q)\n",
|
||||
" outputs = embed_seq(outputs, T, embed_dim, zero_pad=False, scale=False)\n",
|
||||
" return tf.expand_dims(tf.to_float(mask), -1) * outputs\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def sinusoidal_position_encoding(inputs, mask, repr_dim):\n",
|
||||
" T = tf.shape(inputs)[1]\n",
|
||||
" pos = tf.reshape(tf.range(0.0, tf.to_float(T), dtype=tf.float32), [-1, 1])\n",
|
||||
" i = np.arange(0, repr_dim, 2, np.float32)\n",
|
||||
" denom = np.reshape(np.power(10000.0, i / repr_dim), [1, -1])\n",
|
||||
" enc = tf.expand_dims(tf.concat([tf.sin(pos / denom), tf.cos(pos / denom)], 1), 0)\n",
|
||||
" return tf.tile(enc, [tf.shape(inputs)[0], 1, 1]) * tf.expand_dims(tf.to_float(mask), -1)\n",
|
||||
"\n",
|
||||
"def label_smoothing(inputs, epsilon=0.1):\n",
|
||||
" C = inputs.get_shape().as_list()[-1]\n",
|
||||
" return ((1 - epsilon) * inputs) + (epsilon / C)\n",
|
||||
"\n",
|
||||
"class Attention:\n",
|
||||
" def __init__(self, size_layer, embedded_size, learning_rate, size, output_size,\n",
|
||||
" num_blocks = 2,\n",
|
||||
" num_heads = 8,\n",
|
||||
" min_freq = 50):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" \n",
|
||||
" encoder_embedded = tf.layers.dense(self.X, embedded_size)\n",
|
||||
" encoder_embedded = tf.nn.dropout(encoder_embedded, keep_prob = 0.8)\n",
|
||||
" x_mean = tf.reduce_mean(self.X, axis = 2)\n",
|
||||
" en_masks = tf.sign(x_mean)\n",
|
||||
" encoder_embedded += sinusoidal_position_encoding(self.X, en_masks, embedded_size)\n",
|
||||
" \n",
|
||||
" for i in range(num_blocks):\n",
|
||||
" with tf.variable_scope('encoder_self_attn_%d'%i,reuse=tf.AUTO_REUSE):\n",
|
||||
" encoder_embedded = multihead_attn(queries = encoder_embedded,\n",
|
||||
" keys = encoder_embedded,\n",
|
||||
" q_masks = en_masks,\n",
|
||||
" k_masks = en_masks,\n",
|
||||
" future_binding = False,\n",
|
||||
" num_units = size_layer,\n",
|
||||
" num_heads = num_heads)\n",
|
||||
"\n",
|
||||
" with tf.variable_scope('encoder_feedforward_%d'%i,reuse=tf.AUTO_REUSE):\n",
|
||||
" encoder_embedded = pointwise_feedforward(encoder_embedded,\n",
|
||||
" embedded_size,\n",
|
||||
" activation = tf.nn.relu)\n",
|
||||
" \n",
|
||||
" self.logits = tf.layers.dense(encoder_embedded[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.001"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Attention(size_layer, size_layer, learning_rate, df_log.shape[1], df_log.shape[1])\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" )\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0817 12:08:12.096583 140064997701440 deprecation.py:323] From <ipython-input-6-24d2a24c36ef>:91: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"W0817 12:08:12.104836 140064997701440 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0817 12:08:12.294501 140064997701440 deprecation.py:506] From <ipython-input-6-24d2a24c36ef>:92: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
|
||||
"W0817 12:08:12.305350 140064997701440 deprecation.py:323] From <ipython-input-6-24d2a24c36ef>:73: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use `tf.cast` instead.\n",
|
||||
"W0817 12:08:12.446460 140064997701440 deprecation.py:323] From <ipython-input-6-24d2a24c36ef>:33: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use tf.where in 2.0, which has the same broadcast rule as np.where\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:41<00:00, 2.97it/s, acc=96.7, cost=0.00409] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.99it/s, acc=97.3, cost=0.00184] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.98it/s, acc=96.7, cost=0.00351] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.98it/s, acc=97.9, cost=0.00112] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:41<00:00, 2.97it/s, acc=98, cost=0.00113] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.98it/s, acc=97.5, cost=0.00165] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:41<00:00, 2.96it/s, acc=95.8, cost=0.00513]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:41<00:00, 2.98it/s, acc=98, cost=0.000974] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.99it/s, acc=96.8, cost=0.00322] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy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o9FI7dp1yMrKYt++vcyd+3ZuXU9PTwCOHz/GlCkTSUtLw8nJifPnz3Hu3Fl8fHwxmUxER0+jbdv2dO7cpaSaLYQQQgghRJmQlpbG66+PZePG9bRq9SBr1nxGq1YP3PqDpVC5TfACW918lK00qVLF9botVmbOnE29er75tmZlZRW6jylTJjJs2CiCgrphsVgIDn6Y7OxsatSoyYcfriUx8Rd27/6ZxYvjWL58ZTG0QgghhBBCiLLFarXy2WfrmDjxNTIzM5kwIZKhQ0fg7Ozs6NDumiyyUgJatGjFoUNJHDmSAsCXX26iSROFq6tbgfUDA4NYuXJF7oIrFy5c4MSJ47i6utKy5QOsXXttWmXOFM3MzEzq1vUBYPPmjWRnZwOQnp6O0WgkIKATgwcPw93dnRMnjuPm5obRaMRkMhVXs4UQQgghhCi1Tp48wYsLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,718 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def encoder_block(inp, n_hidden, filter_size):\n",
|
||||
" inp = tf.expand_dims(inp, 2)\n",
|
||||
" inp = tf.pad(\n",
|
||||
" inp,\n",
|
||||
" [\n",
|
||||
" [0, 0],\n",
|
||||
" [(filter_size[0] - 1) // 2, (filter_size[0] - 1) // 2],\n",
|
||||
" [0, 0],\n",
|
||||
" [0, 0],\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
" conv = tf.layers.conv2d(\n",
|
||||
" inp, n_hidden, filter_size, padding = 'VALID', activation = None\n",
|
||||
" )\n",
|
||||
" conv = tf.squeeze(conv, 2)\n",
|
||||
" return conv\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def decoder_block(inp, n_hidden, filter_size):\n",
|
||||
" inp = tf.expand_dims(inp, 2)\n",
|
||||
" inp = tf.pad(inp, [[0, 0], [filter_size[0] - 1, 0], [0, 0], [0, 0]])\n",
|
||||
" conv = tf.layers.conv2d(\n",
|
||||
" inp, n_hidden, filter_size, padding = 'VALID', activation = None\n",
|
||||
" )\n",
|
||||
" conv = tf.squeeze(conv, 2)\n",
|
||||
" return conv\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def glu(x):\n",
|
||||
" return tf.multiply(\n",
|
||||
" x[:, :, : tf.shape(x)[2] // 2],\n",
|
||||
" tf.sigmoid(x[:, :, tf.shape(x)[2] // 2 :]),\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def layer(inp, conv_block, kernel_width, n_hidden, residual = None):\n",
|
||||
" z = conv_block(inp, n_hidden, (kernel_width, 1))\n",
|
||||
" return glu(z) + (residual if residual is not None else 0)\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" kernel_size = 3,\n",
|
||||
" n_attn_heads = 16,\n",
|
||||
" dropout = 0.9,\n",
|
||||
" ):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
"\n",
|
||||
" encoder_embedded = tf.layers.dense(self.X, size_layer)\n",
|
||||
"\n",
|
||||
" e = tf.identity(encoder_embedded)\n",
|
||||
" for i in range(num_layers):\n",
|
||||
" z = layer(\n",
|
||||
" encoder_embedded,\n",
|
||||
" encoder_block,\n",
|
||||
" kernel_size,\n",
|
||||
" size_layer * 2,\n",
|
||||
" encoder_embedded,\n",
|
||||
" )\n",
|
||||
" z = tf.nn.dropout(z, keep_prob = dropout)\n",
|
||||
" encoder_embedded = z\n",
|
||||
"\n",
|
||||
" encoder_output, output_memory = z, z + e\n",
|
||||
" g = tf.identity(encoder_embedded)\n",
|
||||
"\n",
|
||||
" for i in range(num_layers):\n",
|
||||
" attn_res = h = layer(\n",
|
||||
" encoder_embedded,\n",
|
||||
" decoder_block,\n",
|
||||
" kernel_size,\n",
|
||||
" size_layer * 2,\n",
|
||||
" residual = tf.zeros_like(encoder_embedded),\n",
|
||||
" )\n",
|
||||
" C = []\n",
|
||||
" for j in range(n_attn_heads):\n",
|
||||
" h_ = tf.layers.dense(h, size_layer // n_attn_heads)\n",
|
||||
" g_ = tf.layers.dense(g, size_layer // n_attn_heads)\n",
|
||||
" zu_ = tf.layers.dense(\n",
|
||||
" encoder_output, size_layer // n_attn_heads\n",
|
||||
" )\n",
|
||||
" ze_ = tf.layers.dense(output_memory, size_layer // n_attn_heads)\n",
|
||||
"\n",
|
||||
" d = tf.layers.dense(h_, size_layer // n_attn_heads) + g_\n",
|
||||
" dz = tf.matmul(d, tf.transpose(zu_, [0, 2, 1]))\n",
|
||||
" a = tf.nn.softmax(dz)\n",
|
||||
" c_ = tf.matmul(a, ze_)\n",
|
||||
" C.append(c_)\n",
|
||||
"\n",
|
||||
" c = tf.concat(C, 2)\n",
|
||||
" h = tf.layers.dense(attn_res + c, size_layer)\n",
|
||||
" h = tf.nn.dropout(h, keep_prob = dropout)\n",
|
||||
" encoder_embedded = h\n",
|
||||
"\n",
|
||||
" encoder_embedded = tf.sigmoid(encoder_embedded[-1])\n",
|
||||
" self.logits = tf.layers.dense(encoder_embedded, output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = test_size\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.7\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 1e-3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], \n",
|
||||
" dropout = dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {modelnn.X: batch_x, modelnn.Y: batch_y},\n",
|
||||
" ) \n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" )\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0818 16:16:28.504163 139649888855872 deprecation.py:323] From <ipython-input-6-6c0655f4345e>:55: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"W0818 16:16:28.507718 139649888855872 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0818 16:16:28.696973 139649888855872 deprecation.py:323] From <ipython-input-6-6c0655f4345e>:13: conv2d (from tensorflow.python.layers.convolutional) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use `tf.keras.layers.Conv2D` instead.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0818 16:16:28.910956 139649888855872 deprecation.py:506] From <ipython-input-6-6c0655f4345e>:66: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 7.09it/s, acc=96.6, cost=0.00251]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 7.08it/s, acc=96.9, cost=0.00232] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 6.99it/s, acc=94.1, cost=0.00764] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 6.98it/s, acc=96.6, cost=0.00273]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 7.02it/s, acc=97.7, cost=0.00113] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 7.06it/s, acc=97.7, cost=0.00117]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 6.98it/s, acc=96.4, cost=0.00286]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 6.97it/s, acc=94.7, cost=0.00573] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 6.94it/s, acc=93.9, cost=0.00807] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:43<00:00, 7.05it/s, acc=94.6, cost=0.006] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy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Jzs6iqKgQnU5HQUEBGzasIzc3B6PRi9zcHLKzs4iNjcdqtTJt2mTatm1Hx463Xv4TcB4kwRNCCCGEEEJU0qnl2VvZahNfX79TljiYOvUN4uLiKy3Nzc3B4XCwb99eHA4HJpM30dGx1KlTh9dem8jgwSPo3Pl27HY7XbrcgsViISwsnGXL1rBz569s3/4zCxcmsWTJ8st3cOdJBlkRQgghhBBC1GrNm7fkwIH9HDlyGIDPP99I48YKfn7+1Zbv1Kkzy5cvxWazYbfbOXz4ID/+uJUjRw7ToEEjvv/+Oxo2vI5mza7Hx8cHk8lEUVERMTGxAGzatAGLRXvAeG5uLqWlpbRv34FBgwYTEBBAaupx/P39KS0txWq1XpZzcK6kBU8IIYQQQghRq4WGhjJ+/KtMmvQSNpuNkJBQJkyYfNryw4aNYt68WfTu/Rh2ux2DwUjPnn1o374jr732OklJs3jhhQHo9Qa6du1Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,706 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def position_encoding(inputs):\n",
|
||||
" T = tf.shape(inputs)[1]\n",
|
||||
" repr_dim = inputs.get_shape()[-1].value\n",
|
||||
" pos = tf.reshape(tf.range(0.0, tf.to_float(T), dtype=tf.float32), [-1, 1])\n",
|
||||
" i = np.arange(0, repr_dim, 2, np.float32)\n",
|
||||
" denom = np.reshape(np.power(10000.0, i / repr_dim), [1, -1])\n",
|
||||
" enc = tf.expand_dims(tf.concat([tf.sin(pos / denom), tf.cos(pos / denom)], 1), 0)\n",
|
||||
" return tf.tile(enc, [tf.shape(inputs)[0], 1, 1])\n",
|
||||
"\n",
|
||||
"def layer_norm(inputs, epsilon=1e-8):\n",
|
||||
" mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True)\n",
|
||||
" normalized = (inputs - mean) / (tf.sqrt(variance + epsilon))\n",
|
||||
" params_shape = inputs.get_shape()[-1:]\n",
|
||||
" gamma = tf.get_variable('gamma', params_shape, tf.float32, tf.ones_initializer())\n",
|
||||
" beta = tf.get_variable('beta', params_shape, tf.float32, tf.zeros_initializer())\n",
|
||||
" return gamma * normalized + beta\n",
|
||||
"\n",
|
||||
"def cnn_block(x, dilation_rate, pad_sz, hidden_dim, kernel_size):\n",
|
||||
" x = layer_norm(x)\n",
|
||||
" pad = tf.zeros([tf.shape(x)[0], pad_sz, hidden_dim])\n",
|
||||
" x = tf.layers.conv1d(inputs = tf.concat([pad, x, pad], 1),\n",
|
||||
" filters = hidden_dim,\n",
|
||||
" kernel_size = kernel_size,\n",
|
||||
" dilation_rate = dilation_rate)\n",
|
||||
" x = x[:, :-pad_sz, :]\n",
|
||||
" x = tf.nn.relu(x)\n",
|
||||
" return x\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" kernel_size = 3,\n",
|
||||
" n_attn_heads = 16,\n",
|
||||
" dropout = 0.9,\n",
|
||||
" ):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
"\n",
|
||||
" encoder_embedded = tf.layers.dense(self.X, size_layer)\n",
|
||||
" encoder_embedded += position_encoding(encoder_embedded)\n",
|
||||
" \n",
|
||||
" e = tf.identity(encoder_embedded)\n",
|
||||
" for i in range(num_layers): \n",
|
||||
" dilation_rate = 2 ** i\n",
|
||||
" pad_sz = (kernel_size - 1) * dilation_rate \n",
|
||||
" with tf.variable_scope('block_%d'%i):\n",
|
||||
" encoder_embedded += cnn_block(encoder_embedded, dilation_rate, \n",
|
||||
" pad_sz, size_layer, kernel_size)\n",
|
||||
" \n",
|
||||
" encoder_output, output_memory = encoder_embedded, encoder_embedded + e\n",
|
||||
" g = tf.identity(encoder_embedded)\n",
|
||||
"\n",
|
||||
" for i in range(num_layers):\n",
|
||||
" dilation_rate = 2 ** i\n",
|
||||
" pad_sz = (kernel_size - 1) * dilation_rate\n",
|
||||
" with tf.variable_scope('decode_%d'%i):\n",
|
||||
" attn_res = h = cnn_block(encoder_embedded, dilation_rate, \n",
|
||||
" pad_sz, size_layer, kernel_size)\n",
|
||||
"\n",
|
||||
" C = []\n",
|
||||
" for j in range(n_attn_heads):\n",
|
||||
" h_ = tf.layers.dense(h, size_layer // n_attn_heads)\n",
|
||||
" g_ = tf.layers.dense(g, size_layer // n_attn_heads)\n",
|
||||
" zu_ = tf.layers.dense(\n",
|
||||
" encoder_output, size_layer // n_attn_heads\n",
|
||||
" )\n",
|
||||
" ze_ = tf.layers.dense(output_memory, size_layer // n_attn_heads)\n",
|
||||
"\n",
|
||||
" d = tf.layers.dense(h_, size_layer // n_attn_heads) + g_\n",
|
||||
" dz = tf.matmul(d, tf.transpose(zu_, [0, 2, 1]))\n",
|
||||
" a = tf.nn.softmax(dz)\n",
|
||||
" c_ = tf.matmul(a, ze_)\n",
|
||||
" C.append(c_)\n",
|
||||
"\n",
|
||||
" c = tf.concat(C, 2)\n",
|
||||
" h = tf.layers.dense(attn_res + c, size_layer)\n",
|
||||
" h = tf.nn.dropout(h, keep_prob = dropout)\n",
|
||||
" encoder_embedded += h\n",
|
||||
"\n",
|
||||
" encoder_embedded = tf.sigmoid(encoder_embedded[-1])\n",
|
||||
" self.logits = tf.layers.dense(encoder_embedded, output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = test_size\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 5e-4"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], \n",
|
||||
" dropout = dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {modelnn.X: batch_x, modelnn.Y: batch_y},\n",
|
||||
" ) \n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" )\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0829 00:04:33.873839 140104212150080 deprecation.py:323] From <ipython-input-6-1aeaade5f897>:44: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"W0829 00:04:33.883059 140104212150080 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0829 00:04:34.265801 140104212150080 deprecation.py:323] From <ipython-input-6-1aeaade5f897>:4: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use `tf.cast` instead.\n",
|
||||
"W0829 00:04:34.294613 140104212150080 deprecation.py:323] From <ipython-input-6-1aeaade5f897>:24: conv1d (from tensorflow.python.layers.convolutional) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use `tf.keras.layers.Conv1D` instead.\n",
|
||||
"W0829 00:04:36.600379 140104212150080 deprecation.py:506] From <ipython-input-6-1aeaade5f897>:82: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 20.69it/s, acc=93, cost=0.0106] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 20.99it/s, acc=97.6, cost=0.00116]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 20.94it/s, acc=95.2, cost=0.00553]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 20.97it/s, acc=95.4, cost=0.00442]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 21.88it/s, acc=95.6, cost=0.00393]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 21.01it/s, acc=95.3, cost=0.00454]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 21.05it/s, acc=96.7, cost=0.00229]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 21.01it/s, acc=97.1, cost=0.00178]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 20.80it/s, acc=95.3, cost=0.00492]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:14<00:00, 20.94it/s, acc=90.6, cost=0.0192] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeVhV1frA8S+HQUYBEVQExXFlDjklKubtllNO1a28oZJWejNTTNPsOuN01ZySnK5dyzTHbk6o6c9uZWpWzmm6UVJBUEEF5YhwPMPvj3NARFRU4CC+n+fxOeestfbe795nRbystdd2sFgsCCGEEEIIIYR49OnsHYAQQgghhBBCiMIhCZ4QQgghhBBClBKS4AkhhBBCCCFEKSEJnhBCCCGEEEKUEpLgCSGEEEIIIUQpIQmeEEIIIYQQQpQSkuAJIYQQQgghRCnhZO8AhBBCCPFglFIOwAjgHcAH2Az8Q9O0q7b6L4DugCHXZt6appnusK8JwJuAJ3AAeE/TtKO52rQBpgEKSAWGaJq2WilVHlgPPAE4AseAoZqm7bJt1xv4D3A91yE7a5r2w8NdASGEEHlJgieEEKLQKaWcNE0z2juO4mLH830DiADCsCZcXwHRQK9cbaZpmjaqAPt6DXgLaAWcASYCS4HGAEqpJ4Hltn3/H+CNNakE0Nu2PQFYgBeBjUqpgFzX5WdN01o92GkKIYQoKEnwhBDiMaOU+gjoCwQACcBITdPWKqXKABeAVpqmHbG19QfigaqapiUrpTpj/cU/BPgD6Kdp2mFb29PAfKCH9aPyAIbmdyxbe0eso0G9gHRgBtbkxFnTNKNSyhuYCXQEzMDnwNg7jD41Az4B6mAdJfov1tElg62+LjAbaALcAD7RNG2yLYbhwNu2GGOBl7COQp3KjsW2jx+AZZqmfWYbkeoL/Io1yZqvlPocWAQ8hTXJ2Yp1BCzNtn2wLcZnsN4isQIYApwH/qJ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PyA2ViM8PIKwsGdITb3MxIljiI7+N9WqVScmZh1RUaNYtMg6RfLUqT8ZNmwE9eo1AGDKlAk0bNiYjz4ajdlsJipqFJs2baBr15cZP340LVqEMWnSxwCkpaUBEBranLZt2+Pg4EB8/GkGDerP2rWbOXkylgMH9rJs2Rp0Oh1Xr14FYMiQ4fTpE8EXXywvkmskhBBCCCHEo+TixYv8618TWLbsC8qVK8eMGXPo3j0CR0dHe4d2X0ptgleSHD16hBo1alOtWnUAOnbsyowZU8nIuAZAUFBwTnIHsHPnDo4dO8rKlV8BkJmZSUBABTIyMjhy5DCzZs3Naevj4wNAYuJZxo0bSUpKCk5OTly+fIlLly4SGBiE0WhkypQJNG7clJYtnymu0xZCCCGEEKLEu3HjBosX/5uPP55CRsY1/vGPdxk69CO8vX3sHdoDKbUJXlj9u4+ylSRubu55SixMnjydypWDbinNyMi44z7GjRvJgAGDad36WcxmM23atMJgMODnV56lS1dz4MA+9u79lfnzo1m8eFkRnIUQQgghhBCPlu+//47Roz8iNlbjr399ngkTplC7trJ3WA9FFlkpBnXr1icuLpYzZ04DsGVLDLVqKdzdPfJtHxbWmmXLluQsuJKWlkZSUiLu7u7Uq9eA1atvTqvMnqKp1+upVMk6P3jTpg0YDAYAUlNTyczMJDSLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,705 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
"\n",
|
||||
" backward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.backward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.forward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward,\n",
|
||||
" drop_backward,\n",
|
||||
" self.X,\n",
|
||||
" initial_state_fw = self.forward_hidden_layer,\n",
|
||||
" initial_state_bw = self.backward_hidden_layer,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
" self.outputs = tf.concat(self.outputs, 2)\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0812 10:20:04.613218 140016646534976 deprecation.py:323] From <ipython-input-6-32a8ad1d5669>:12: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 10:20:04.617547 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f579b74fd68>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:20:04.620435 140016646534976 deprecation.py:323] From <ipython-input-6-32a8ad1d5669>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 10:20:04.623959 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f579b6efa20>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0812 10:20:04.949644 140016646534976 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0812 10:20:04.954938 140016646534976 deprecation.py:323] From <ipython-input-6-32a8ad1d5669>:42: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API\n",
|
||||
"W0812 10:20:04.955546 140016646534976 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py:464: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0812 10:20:05.149145 140016646534976 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 10:20:05.156026 140016646534976 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:961: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 10:20:05.712592 140016646534976 deprecation.py:323] From <ipython-input-6-32a8ad1d5669>:45: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.04it/s, acc=97.8, cost=0.00113] \n",
|
||||
"W0812 10:21:46.695034 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f58216a3208>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:21:46.695935 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f5790ef9e10>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 3.03it/s, acc=97.1, cost=0.00187] \n",
|
||||
"W0812 10:23:27.984155 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f57919d69e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:23:27.985092 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f57008a7b70>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.97it/s, acc=97.8, cost=0.00118] \n",
|
||||
"W0812 10:26:50.307250 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f5791f0d2e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:26:50.308161 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56dbd71160>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.97it/s, acc=97.1, cost=0.00237]\n",
|
||||
"W0812 10:28:31.638492 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56dbe750b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:28:31.639337 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d982db38>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:37<00:00, 3.11it/s, acc=97.5, cost=0.00143] \n",
|
||||
"W0812 10:30:09.934609 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d99130b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:30:09.935530 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d7b95320>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.01it/s, acc=97.4, cost=0.00163] \n",
|
||||
"W0812 10:31:50.447502 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d7384cf8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:31:50.448328 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d56a2748>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.05it/s, acc=96.6, cost=0.00322] \n",
|
||||
"W0812 10:33:30.276075 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d4e9bba8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:33:30.276944 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d2868da0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.07it/s, acc=97.7, cost=0.00133] \n",
|
||||
"W0812 10:35:09.746517 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d290ccf8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 10:35:09.747369 140016646534976 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f56d03129b0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.03it/s, acc=97.5, cost=0.00142] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeVxV1fr48c85DDKIgqggoojTstSuU6GilCaiNvxut+8tJ1LTblaKqZkmauKUQ6JJzl3NNC3tZpoTVGpkNjiVabZRE3AWERRExDP8/tgHRMUZOIDP+/U6Lzhrr733s/dZKA9r2Aar1YoQQgghhBBCiNLPaO8AhBBCCCGEEEIUDknwhBBCCCGEEKKMkARPCCGEEEIIIcoISfCEEEIIIYQQooyQBE8IIYQQQgghyghJ8IQQQgghhBCijJAETwghhBBCCCHKCEd7ByCEEEKIe6OUMgAjgVcBT2AD8B9N0y7Ytk8FugEVgTRgvqZpk25xvIHAEMAbSADe1DRtW75zTQb62ap/BIzQNM2qlKoMrAEaAA7AAeAtTdN+zHfswcBwwA34AnhN07TLhXEfhBBCXCU9eEIIIQqdUuqB+gOiHa/3JSAcCAb8AFcgJt/2/wINNE2rALQGeiil/lXQgZRSQegJ3P+hJ4T/BVYrpRxsVf4D/BP4B/AI8Ax6YgmQCbwMVAG8gCnA17n3RSkVBowAngQCgNp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KxWPD09GLYsJH4+9dg9+6dfPDB+yj1EAkJGq+88hpNmjQlJmYGhw8fJCcnh6ZNWzBw4GAcHBxISTnDzJnTOHbsKAAdOoQRHt6HuLhNrFq1ApPpCgBvvPEmLVo8hsViITp6Krt378DJyRk3N1fmzl1EdPQUMjMz6d27Oy4uLsybt+iaa3B0dOTRR4M4efJEsd8/IYQQQggh7OXEieO8884wNm5cx8MPN8LHp5q9Q7orZTbB+/GPk2zbe7JIjt3mkWoEN775Bx0RMZSEBI1u3cIJDm5LWto5JkwYQ0zMAgIDa7Nu3VdERY1i4UJ9iOSRI38zbNhIGjV6BIDJk8fTpEkzRowYjcViISpqFOvXr+XZZ59j3LjRtGoVzMSJ0wBIT08HICioJaGhYRgMBpKTExk06HVWr97AoUMJ7Nmzk2XLVmE0Grlw4QIAQ4YMp1+/cD7+eHmR3CMhhBBCCCFKE7PZzKJFC5g0aTwWi5nRo8fRv/8bODk52Tu0u1JmE7ySZP/+fdSpU5/AwNoAdOnyLNOnTyEr6yIA/v418pI7gG3b4jlwYD+fffYpANnZ2VSt6kNWVhb79u1lxozZeXU9PT0BOH78GGPHRpKSkoKjoyPnzqWSmnoWPz9/TCYTkyePp1mzFrRu3ba4LlsIIYQQQohS4Y8/fmfo0Ah++20P7dt3YMqUaAICatk7rHtSZhO84Ma37mUrSVxd3a4rsTJp0vtUr+5/TWlWVtZNjzF2bCQDBgwmJOQJLBYLHTq0IScnB2/vyixdupI9e3axc+evzJ0bw6JFy4rgKoQQQgghhChdMjMzmTp1EgsWzMHbuzILFizm//2/f5Xqx6/JIivFoGHDxhw+nEBSUiIAGzeuo149hZube4H1g4NDWLZsSd6CK+np6Zw4cRw3NzcaNXqElSuvDqvMHaKZmZlJtWp+AKxfv5acnBwA0tLSyM7OJiioFf37D6B8+fKcOHEcd3dLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,759 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('forward', reuse = False):\n",
|
||||
" rnn_cells_forward = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X_forward = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" drop_forward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_forward, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer_forward = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs_forward, self.last_state_forward = tf.nn.dynamic_rnn(\n",
|
||||
" drop_forward,\n",
|
||||
" self.X_forward,\n",
|
||||
" initial_state = self.hidden_layer_forward,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" with tf.variable_scope('backward', reuse = False):\n",
|
||||
" rnn_cells_backward = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X_backward = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_backward, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer_backward = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs_backward, self.last_state_backward = tf.nn.dynamic_rnn(\n",
|
||||
" drop_backward,\n",
|
||||
" self.X_backward,\n",
|
||||
" initial_state = self.hidden_layer_backward,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" self.outputs = self.outputs_backward - self.outputs_forward\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x_forward = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[k : index, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state_forward, last_state_backward, _, loss = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" modelnn.optimizer,\n",
|
||||
" modelnn.cost,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" batch_x_forward = np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp, :], axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[k : k + timestamp, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1, :] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" batch_x_forward = np.expand_dims(df_train.iloc[upper_b:, :], axis = 0)\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[upper_b:, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state_forward, modelnn.last_state_backward],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" o_f = np.flip(o, axis = 0)\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.X_backward: np.expand_dims(o_f, axis = 0),\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0812 16:41:29.569112 139847292135232 deprecation.py:323] From <ipython-input-6-2e28fdecec52>:12: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 16:41:29.570642 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f302d208da0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:41:29.571565 139847292135232 deprecation.py:323] From <ipython-input-6-2e28fdecec52>:17: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0812 16:41:29.886489 139847292135232 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0812 16:41:29.889781 139847292135232 deprecation.py:323] From <ipython-input-6-2e28fdecec52>:30: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0812 16:41:30.079713 139847292135232 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 16:41:30.086595 139847292135232 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:961: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 16:41:30.565006 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f302d1de6d8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:41:30.647609 139847292135232 deprecation.py:323] From <ipython-input-6-2e28fdecec52>:54: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.02it/s, acc=97.7, cost=0.00132] \n",
|
||||
"W0812 16:43:12.068012 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f3022b11fd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:43:12.148377 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f30229e3080>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 3.02it/s, acc=97.4, cost=0.00157]\n",
|
||||
"W0812 16:44:53.274921 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2fdc217b00>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:44:53.357845 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2fdc1bc0f0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.98it/s, acc=97.3, cost=0.00171]\n",
|
||||
"W0812 16:46:35.140946 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f847c0240>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:46:35.223572 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f847c00b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.00it/s, acc=96.5, cost=0.00334] \n",
|
||||
"W0812 16:48:14.756632 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f843747f0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:48:14.838256 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f722b19e8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:41<00:00, 2.98it/s, acc=97.9, cost=0.00113]\n",
|
||||
"W0812 16:49:56.968556 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f6bd75b70>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:49:57.051066 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f6bd755f8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 3.01it/s, acc=97.7, cost=0.00145]\n",
|
||||
"W0812 16:51:38.877053 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f6a0db3c8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:51:38.959546 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f6976aef0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:41<00:00, 2.98it/s, acc=97.3, cost=0.00172]\n",
|
||||
"W0812 16:53:21.123231 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f67bdbcc0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:53:21.205539 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f67258e10>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.06it/s, acc=97.8, cost=0.00117]\n",
|
||||
"W0812 16:55:00.356067 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f65677da0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:55:00.437367 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f65677898>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.05it/s, acc=97.7, cost=0.00127]\n",
|
||||
"W0812 16:56:40.365346 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f628e67b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0812 16:56:40.448274 139847292135232 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f2f628e6320>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.97it/s, acc=97.2, cost=0.00216]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzde3xMZ/7A8c9ccr83FSRBQjkUXdQKUtoSbLVrt7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uTgwQO0adOO6OjWAAwePIRFixIpLS0BIDKyRXlyB7Bz5w4OHTrIRx99AIDFYiEsrCmlpaUcOLCfxYvfLK8bHBwMQGbmKWbNmkZOTg5ms5nz53PJzT1HeHgkNpuN+fPn0K1bd3r37lNXhy2EEEIIIUSDcP58LtOmTeGf/1zL3Xd34oMP1nLPPV3cHdZNabQJXmzna/ey1Sc+Pr5XLHEwb95CIiIiL1taWlpa5TZmzZrGCy+Mp2/fB9A0jbi4+7BarYSG3sn7769l374f2LPnO5YtS2bFitW1cBRCCCGEEEI0PBs3bmDKlAnk5Z1n8uSXGTduIp6enu4O66bJJCt1oGPHzhw7lkp6ehoAn322kbZtFXx9/SqtHxvbl9WrV5VPuJKfn09WVia+vr506nQPa9deGlbpGqJZXFxM8+bhAGzatAGr1QpLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,675 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 hours, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.GRUCell(size_layer)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0811 22:46:29.978655 140681713489728 deprecation.py:323] From <ipython-input-6-1b755385b006>:12: GRUCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.GRUCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0811 22:46:29.981659 140681713489728 deprecation.py:323] From <ipython-input-6-1b755385b006>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0811 22:46:31.758260 140681713489728 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0811 22:46:31.762153 140681713489728 deprecation.py:323] From <ipython-input-6-1b755385b006>:27: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0811 22:46:32.109607 140681713489728 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0811 22:46:32.121295 140681713489728 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:564: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0811 22:46:32.136707 140681713489728 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:574: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0811 22:46:32.395149 140681713489728 deprecation.py:323] From <ipython-input-6-1b755385b006>:29: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [02:10<00:00, 2.45it/s, acc=97.1, cost=0.00211]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:09<00:00, 2.48it/s, acc=96.2, cost=0.00432]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:08<00:00, 1.88it/s, acc=96.7, cost=0.00239]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:05<00:00, 2.12it/s, acc=96.4, cost=0.00307]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:12<00:00, 1.95it/s, acc=96.9, cost=0.00227]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:15<00:00, 2.64it/s, acc=96.4, cost=0.00318]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:10<00:00, 2.59it/s, acc=96.9, cost=0.00256]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:11<00:00, 2.19it/s, acc=97.2, cost=0.00188]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:14<00:00, 2.48it/s, acc=96.9, cost=0.00226]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [02:06<00:00, 2.48it/s, acc=97.5, cost=0.00167]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd5wddb3/8dfpdespW7PZ1EmnJ1QRFEQQpIgKioKiXtEr6LVdFQUBpYngRURR7P5UmhSRLkhNSCEkW2aTbDbbezu7e+rM/P6Yc042yW7qJrvZfJ6Px3nMnJk5Z77n7DeT857vd75jMQwDIYQQQgghhBCHP+tkF0AIIYQQQgghxMSQgCeEEEIIIYQQ04QEPCGEEEIIIYSYJiTgCSGEEEIIIcQ0IQFPCCGEEEIIIaYJCXhCCCGEEEIIMU1IwBNCCCGEEEKIacI+2QUQQgghxP5RFMUCfAf4ApAPPA18XlXVwZ22KwRUQFVV9dTdvN9s4GfA6UAceFBV1W+m1/0JeB/gA9qB21VV/XV63YnATcBxgAa8DHxFVdW29PpvAJ8GZgLdwH2qqt4xAV+BEEKInUgLnhBCiAmnKMoRdQJxEj/vp4ArgFOAUsAD/N8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Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,704 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.GRUCell(size_layer)\n",
|
||||
"\n",
|
||||
" backward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.backward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.forward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward,\n",
|
||||
" drop_backward,\n",
|
||||
" self.X,\n",
|
||||
" initial_state_fw = self.forward_hidden_layer,\n",
|
||||
" initial_state_bw = self.backward_hidden_layer,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
" self.outputs = tf.concat(self.outputs, 2)\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0812 17:04:18.991346 140383403915072 deprecation.py:323] From <ipython-input-6-5c392a5d20ef>:12: GRUCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.GRUCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 17:04:18.995361 140383403915072 deprecation.py:323] From <ipython-input-6-5c392a5d20ef>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0812 17:04:19.316777 140383403915072 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0812 17:04:19.322190 140383403915072 deprecation.py:323] From <ipython-input-6-5c392a5d20ef>:42: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API\n",
|
||||
"W0812 17:04:19.322940 140383403915072 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py:464: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0812 17:04:19.515542 140383403915072 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 17:04:19.522486 140383403915072 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:564: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 17:04:19.531559 140383403915072 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:574: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 17:04:19.763414 140383403915072 deprecation.py:323] From <ipython-input-6-5c392a5d20ef>:45: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.98it/s, acc=97.1, cost=0.00199]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.02it/s, acc=76.2, cost=0.139] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 3.00it/s, acc=97.1, cost=0.00205]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.99it/s, acc=95.3, cost=0.00587]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.97it/s, acc=96.2, cost=0.00386]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.99it/s, acc=97.1, cost=0.00196]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.98it/s, acc=96.7, cost=0.0032] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.00it/s, acc=85.2, cost=0.0599] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.99it/s, acc=97.6, cost=0.00142]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.03it/s, acc=97.7, cost=0.00138]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAAC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zl6IYQQQghxMjJv/I7ASRFbeMcRk+QpikJk7m0kH/knnOUPEV3wmTxFKfoCSfDyxD+wl8yqJ3C3LgPNwJx6NcaU96HGik7rfGpBGbGr/47se6+SWfYA7Q//I5GLbsGw5qMoSjdH3/sEQYBfvx2loAw1XpLvcIQQQghxHslufQt32wrMWR9CK6s66jFaWRXG5CvIrs9NuDJgzDmOUvQVkuCdY37LvrBit3kpqAbG5Kswp15z2oldV4qiYI5fiD54IulXf0Pmtbtxt68iOv/TqInSboi+d/Lqt5N58w94ezeBoqANHIc+eg7GiJlHjG0UQgghhOhOfvJA2DWzYiTmlKuPe2xkxo24W5eTfuNe4jd9S5bDEqdFErxzJEzsnsLd/AaoGsakK8PELl7c7ddSCyuIXXsn2bdfJvPWg7Q/9A2iF9+KPubi86qa57c1knnrYdwtb6JEC4lcfCtBupXsluVkXrubzOv3og2ejDF6DvqwC1CMSL5DFkIIIUQfEgQBmdfvJXDTxBbefsKETTGiRC76GOkXf0b2nZcxJ11xjiIVfYkkeGeZ31qPs/opspveAFXBmHg55rRrzno3QUVRMSddjj5kMulXfk36lV+Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,742 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.GRUCell(size_layer)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('forward', reuse = False):\n",
|
||||
" rnn_cells_forward = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X_forward = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" drop_forward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_forward, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer_forward = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs_forward, self.last_state_forward = tf.nn.dynamic_rnn(\n",
|
||||
" drop_forward,\n",
|
||||
" self.X_forward,\n",
|
||||
" initial_state = self.hidden_layer_forward,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" with tf.variable_scope('backward', reuse = False):\n",
|
||||
" rnn_cells_backward = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X_backward = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_backward, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer_backward = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs_backward, self.last_state_backward = tf.nn.dynamic_rnn(\n",
|
||||
" drop_backward,\n",
|
||||
" self.X_backward,\n",
|
||||
" initial_state = self.hidden_layer_backward,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" self.outputs = self.outputs_backward - self.outputs_forward\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x_forward = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[k : index, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state_forward, last_state_backward, _, loss = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" modelnn.optimizer,\n",
|
||||
" modelnn.cost,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" batch_x_forward = np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp, :], axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[k : k + timestamp, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1, :] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" batch_x_forward = np.expand_dims(df_train.iloc[upper_b:, :], axis = 0)\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[upper_b:, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state_forward, modelnn.last_state_backward],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" o_f = np.flip(o, axis = 0)\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.X_backward: np.expand_dims(o_f, axis = 0),\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0812 17:35:02.847837 140485361571648 deprecation.py:323] From <ipython-input-6-0ad5fabb14ba>:12: GRUCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.GRUCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 17:35:02.849749 140485361571648 deprecation.py:323] From <ipython-input-6-0ad5fabb14ba>:17: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0812 17:35:03.166620 140485361571648 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0812 17:35:03.169869 140485361571648 deprecation.py:323] From <ipython-input-6-0ad5fabb14ba>:30: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0812 17:35:03.361743 140485361571648 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 17:35:03.368463 140485361571648 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:564: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 17:35:03.377521 140485361571648 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:574: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 17:35:03.612136 140485361571648 deprecation.py:323] From <ipython-input-6-0ad5fabb14ba>:54: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.04it/s, acc=97.5, cost=0.00174]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 3.01it/s, acc=96.7, cost=0.00259]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:39<00:00, 2.99it/s, acc=97.4, cost=0.00178]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.05it/s, acc=96.2, cost=0.00314]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 2.99it/s, acc=97.4, cost=0.00166]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.02it/s, acc=97.3, cost=0.00162]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 3.00it/s, acc=96.1, cost=0.00347]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:40<00:00, 3.01it/s, acc=96.1, cost=0.004] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.05it/s, acc=95.3, cost=0.00537]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:38<00:00, 3.05it/s, acc=97.1, cost=0.00213]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOy9d5xtWVnn/d355MpVN4e+Yd++oSONHWhs0AYR5sVBB2RG0ddXB+OoqIg6M8jrKyIqY8BBxTgvBhCVURHoFhqEDjQ0dHPvbTjdN6fK6eSd1po/1j6nTtWtujnf9f18dq2406l99tm//TzrWYaUEo1Go9FoNBqNRqPRXP+YV/sANBqNRqPRaDQajUZzadACT6PRaDQajUaj0WhuELTA02g0Go1Go9FoNJobBC3wNBqNRqPRaDQajeYGQQs8jUaj0Wg0Go1Go7lB0AJPo9FoNBqNRqPRaG4QtMDTaDQajUaj0Wg0mhsE+2ofgEaj0Wg0mgvD930D+EXgrUAv8C/Afy6Xy5W0/b3Am4EeYBb4w3K5/O4VtvVa4BeA3UAL+Gfgp8vlcjVtfyPwU8AdwNPlcvmhJev/EfDNwDbgB8rl8p93tf0B8D1d3R0gLJfLxYs4fY1Go9Esg7bgaTQajeaS4/v+TfUC8Sqe71uA7wUeANYAWeD3utr/BNhRLpdLwP3Af/J9/w0rbKsH+P/S7dwKrAV+o6t9Bvht4D0rrP8c8KPAV5Y2lMvlHy6Xy4X2Avw18LfndIYajUajOS9uqh9gjUaj0YDv++8AfggYBo4Dv1Qul//B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rM2rXrsO0bQxrdGGeh0Wg0Go1Go9FoAEhqNRrP7+d//81f8ssf+WuCJOaB1WswPA/R00sYRVSrFSYnJ2m1mgRBQKvVotVqEQQtoii6JMdhGAamaS5aDMPAMLrrjE6/dv3S9dQ6BtIwEKilFYbMTYySdB2rYVqURlYzvHY9277l29i8+Rb8W25h99atbL9lM729fZfkvK51tMDTaDQajUaj0WiuY6QQtI4cprFvL/V9e5l5oczv7HuOjx87ys41a3nfz/8SqysVql96Gqu3l8F//12U7rsfw1w+oH6SJB2xFwQBzaYSgUHQ6hKCAa1Wc0k+IAwDpJQIIZBSpHlVbi9SSqRcXFZ5VR8nCY0ooR5F1KOYRhjTiGJEuj0pBCXH5vZXvJpNGzex/ZZb2LNtK7s2bybnuVf407/20AJPo9FoNBqNRqO5zojn5qjv30tj/z7q+/ch6nUwDF7wPN75pSc5OT3NT/3kz/Bzb/9FHMcBoPeVDzPx4b9i/M/+mLnHPs3QG7+b3Hb/tG1blkU+nyefz1/286hGMaONoGsJmWqF5IEhwDNNVuVcVuU81uQ8VmU9RrIurqVne1sJLfA0Go1Go9FoNJprHBnHNA+8SH3fXhr79xIcPw6AVSpRuP0OHP9WPviZR/nt3/8d1q1bz8c+9iHuvfe+RdvIbtvGhl/8b1S/+BRTf/9RTrz31yjc/RIGv+uNuEPDl/X4EymZaoUdETfWCDjVCKjHSadPr2uzOuexu7/A6qzH6pxLn+dgXoHxfDcSZxV4vu//JvCdwCZgT7lc3pfWbwf+AhgApoG3lMvlFy+mTaPRaDQ3NzJJaHzj61S/9EVEs4mVz2Pm8otSlc916sxs9ooM5tdoNJorTTQ5SX2/crtsfP3ryKAFlkV2y1YG3/Bd5HbvwVu3nsNHDvNLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,672 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 hours, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.BasicRNNCell(size_layer)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0812 22:23:09.113305 140115879184192 deprecation.py:323] From <ipython-input-6-35b560f11ea1>:12: BasicRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.SimpleRNNCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0812 22:23:09.116059 140115879184192 deprecation.py:323] From <ipython-input-6-35b560f11ea1>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0812 22:23:09.445186 140115879184192 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0812 22:23:09.448605 140115879184192 deprecation.py:323] From <ipython-input-6-35b560f11ea1>:27: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0812 22:23:09.640925 140115879184192 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 22:23:09.647897 140115879184192 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:459: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0812 22:23:09.740828 140115879184192 deprecation.py:323] From <ipython-input-6-35b560f11ea1>:29: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [00:52<00:00, 5.77it/s, acc=76.9, cost=0.129] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:49<00:00, 6.03it/s, acc=78, cost=0.11] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:52<00:00, 5.77it/s, acc=73.7, cost=0.154] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:50<00:00, 6.11it/s, acc=82.7, cost=0.075] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:52<00:00, 5.65it/s, acc=76.3, cost=0.12] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:48<00:00, 6.15it/s, acc=77, cost=0.123] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:52<00:00, 5.65it/s, acc=80.2, cost=0.0896]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:50<00:00, 5.99it/s, acc=75.1, cost=0.15] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:52<00:00, 5.58it/s, acc=87.2, cost=0.0367]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [00:52<00:00, 5.69it/s, acc=76.7, cost=0.121] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3wU1fr48c9uNr0npBACJCQwQAICAqEIAoKKBctVFAXFq96vehWvBfVio1jAgih6EfVHUUAFlSIIYqFKhyAkwqRQQnrvbdvvj93EhARSSIXn/Xrlxe6cmTPPTIbNPnPOnKMxm80IIYQQQgghhGj/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jWv4ENIAkeEJcIrPZzP/b+BcFxeW8PGUgTg62XDsgkA0748kvLsfNya61QxRCCCGEaJDhfepuZWtLHB2dzlti5q233qNTp8BqS4uLiwFITU0hPz+f8vIytFotbm7urFixjKeeeoZrrx2DyWRi7NhrKC8vx9u7A199tZrIyMMcOnSARYsWsmTJihY6soaTQVaEuES/HDzHn/FZTBwdSld/VwBGXx2I0WTm4In0Vo5OCCGEEKL9CwvrQ3x8DGfPngFg8+aNdO+u4OTkXOv6w4ePZMWLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,701 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.BasicRNNCell(size_layer)\n",
|
||||
"\n",
|
||||
" backward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" forward_rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" backward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" forward_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" forward_rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.backward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.forward_hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, shape = (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.bidirectional_dynamic_rnn(\n",
|
||||
" forward_backward,\n",
|
||||
" drop_backward,\n",
|
||||
" self.X,\n",
|
||||
" initial_state_fw = self.forward_hidden_layer,\n",
|
||||
" initial_state_bw = self.backward_hidden_layer,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
" self.outputs = tf.concat(self.outputs, 2)\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.backward_hidden_layer: init_value_backward,\n",
|
||||
" modelnn.forward_hidden_layer: init_value_forward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state[0]\n",
|
||||
" init_value_backward = last_state[1]\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0813 00:50:36.840563 140096227489600 deprecation.py:323] From <ipython-input-6-c6fe3802893b>:12: BasicRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.SimpleRNNCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0813 00:50:36.842919 140096227489600 deprecation.py:323] From <ipython-input-6-c6fe3802893b>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0813 00:50:37.159364 140096227489600 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0813 00:50:37.164350 140096227489600 deprecation.py:323] From <ipython-input-6-c6fe3802893b>:42: bidirectional_dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.Bidirectional(keras.layers.RNN(cell))`, which is equivalent to this API\n",
|
||||
"W0813 00:50:37.165004 140096227489600 deprecation.py:323] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn.py:464: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0813 00:50:37.355312 140096227489600 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0813 00:50:37.362000 140096227489600 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:459: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0813 00:50:37.520977 140096227489600 deprecation.py:323] From <ipython-input-6-c6fe3802893b>:45: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:04<00:00, 4.68it/s, acc=71.9, cost=0.169]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:06<00:00, 4.45it/s, acc=77.7, cost=0.11] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:04<00:00, 4.67it/s, acc=68.9, cost=0.211] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:06<00:00, 4.45it/s, acc=78.9, cost=0.104] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:06<00:00, 4.53it/s, acc=70.2, cost=0.193]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:06<00:00, 4.57it/s, acc=70.6, cost=0.189]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:07<00:00, 4.43it/s, acc=66.1, cost=0.253]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:05<00:00, 4.53it/s, acc=80.6, cost=0.0892]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:07<00:00, 4.51it/s, acc=63.5, cost=0.287] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:03<00:00, 4.71it/s, acc=72.8, cost=0.167]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0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89iqbBuqWpGAwGihYls7++kx5J0zyOBHhCCCGEEEKIsLdtbwvz0mJIS4wGoEhxomlQWiO9eCNJgCeEEEIIIYQIaw2tHhpaPZy3LPXYssxkOynxUVJN8wQS4AkhhBBCCCHC2rbKFkxGA6sXJx9bdixNs64Lt0x6fowEeEIIIYQQQoiwFQhobKtqYXlOIg5bxHHripRkAppGWU3bDLUu/EiAJ4QQQgghhAhbVXUddHsGWLc09aR1c1PsOOMi2SVpmsdIgCeEEEIIIYQIW1srW7BZzeTnJp20bjhNc19tJ56+wRloXfiRAE8IIYQQQggRlvp8Q5RWu1i9OBmLefTQ5ViaZrVU0wQJ8IQQQgghhBBhqrTaxcBggHXLTk7PHJad6iApNlImPQ+SAE8IIYQQQggRlrZWtuCMiyQ3I/aU2xgMBoqUZKpqO/D2S5qmBHhCCCGEEEKIsNPR08/+uk7WLU3FYDCcdtuiRcn4AxrlUk1TAjwhhBBCCCFE+NledRQNjpvc/FTmpTlIjLFKNU0kwBNCCCGEEEKEGU3T2FrZQm5GLMnxtjNubzAYWKkks/dwB739Q9PQwvAlAZ4QQgghhBAirNQf9dDU5h1T792wVcNpmgfO7WIrEuAJIYQQQgghwsqWymbMJgOrFieP+TXLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,739 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Split train and test\n",
|
||||
"\n",
|
||||
"I will cut the dataset to train and test datasets,\n",
|
||||
"\n",
|
||||
"1. Train dataset derived from starting timestamp until last 30 days\n",
|
||||
"2. Test dataset derived from last 30 days until end of the dataset\n",
|
||||
"\n",
|
||||
"So we will let the model do forecasting based on last 30 days, and we will going to repeat the experiment for 10 times. You can increase it locally if you want, and tuning parameters will help you by a lot."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (222, 1), (30, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_size = 30\n",
|
||||
"simulation_size = 10\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.BasicRNNCell(size_layer)\n",
|
||||
" \n",
|
||||
" with tf.variable_scope('forward', reuse = False):\n",
|
||||
" rnn_cells_forward = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X_forward = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" drop_forward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_forward, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer_forward = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs_forward, self.last_state_forward = tf.nn.dynamic_rnn(\n",
|
||||
" drop_forward,\n",
|
||||
" self.X_forward,\n",
|
||||
" initial_state = self.hidden_layer_forward,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" with tf.variable_scope('backward', reuse = False):\n",
|
||||
" rnn_cells_backward = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X_backward = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" drop_backward = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells_backward, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer_backward = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs_backward, self.last_state_backward = tf.nn.dynamic_rnn(\n",
|
||||
" drop_backward,\n",
|
||||
" self.X_backward,\n",
|
||||
" initial_state = self.hidden_layer_backward,\n",
|
||||
" dtype = tf.float32,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" self.outputs = self.outputs_backward - self.outputs_forward\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"future_day = test_size\n",
|
||||
"learning_rate = 0.01"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x_forward = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[k : index, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state_forward, last_state_backward, _, loss = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" modelnn.optimizer,\n",
|
||||
" modelnn.cost,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value_forward = np.zeros((1, num_layers * size_layer))\n",
|
||||
" init_value_backward = np.zeros((1, num_layers * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" batch_x_forward = np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp, :], axis = 0\n",
|
||||
" )\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[k : k + timestamp, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1, :] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" batch_x_forward = np.expand_dims(df_train.iloc[upper_b:, :], axis = 0)\n",
|
||||
" batch_x_backward = np.expand_dims(\n",
|
||||
" np.flip(df_train.iloc[upper_b:, :].values, axis = 0), axis = 0\n",
|
||||
" )\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state_forward, modelnn.last_state_backward],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: batch_x_forward,\n",
|
||||
" modelnn.X_backward: batch_x_backward,\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" o_f = np.flip(o, axis = 0)\n",
|
||||
" out_logits, last_state_forward, last_state_backward = sess.run(\n",
|
||||
" [\n",
|
||||
" modelnn.logits,\n",
|
||||
" modelnn.last_state_forward,\n",
|
||||
" modelnn.last_state_backward,\n",
|
||||
" ],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X_forward: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.X_backward: np.expand_dims(o_f, axis = 0),\n",
|
||||
" modelnn.hidden_layer_forward: init_value_forward,\n",
|
||||
" modelnn.hidden_layer_backward: init_value_backward,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value_forward = last_state_forward\n",
|
||||
" init_value_backward = last_state_backward\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.3)\n",
|
||||
" \n",
|
||||
" return deep_future[-test_size:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0813 01:22:40.361487 140690772289344 deprecation.py:323] From <ipython-input-6-04b2b1d463f4>:12: BasicRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.SimpleRNNCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0813 01:22:40.364087 140690772289344 deprecation.py:323] From <ipython-input-6-04b2b1d463f4>:17: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0813 01:22:40.688215 140690772289344 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0813 01:22:40.691791 140690772289344 deprecation.py:323] From <ipython-input-6-04b2b1d463f4>:30: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0813 01:22:40.883351 140690772289344 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0813 01:22:40.890208 140690772289344 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:459: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0813 01:22:41.052329 140690772289344 deprecation.py:323] From <ipython-input-6-04b2b1d463f4>:54: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:08<00:00, 4.43it/s, acc=73.8, cost=0.155] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.32it/s, acc=75, cost=0.151] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:08<00:00, 4.41it/s, acc=75.2, cost=0.14] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:08<00:00, 4.36it/s, acc=71, cost=0.186] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.33it/s, acc=84, cost=0.0574] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:09<00:00, 4.34it/s, acc=72.3, cost=0.166] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:08<00:00, 4.44it/s, acc=80.4, cost=0.0918]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:07<00:00, 4.45it/s, acc=79.7, cost=0.101] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:05<00:00, 4.61it/s, acc=80.6, cost=0.088] \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:08<00:00, 4.40it/s, acc=70.8, cost=0.194] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdd3hcV5n48e9UaWbURxr1bum6yXGN7diJ4/QG/AiBJBAIsAuEuuwuuxsWdmHpLBCWLAnJUgMJSYAQCCGkOMXdsRPLtmzLV7Z6H2k00qiMpCn398eMbEmWbJVR9ft5Hj0zc8u558rXmvvec857dJqmIYQQQgghhBBi4dPPdQWEEEIIIYQQQkSGBHhCCCGEEEIIsUhIgCeEEEIIIYQQi4QEeEIIIYQQQgixSEiAJ4QQQgghhBCLhAR4QgghhBBCCLFISIAnhBBCCCGEEIuEca4rIIQQQoipURRFB/w78AkgAXgB+Liqqp7w+vcBnwdWAwdVVb36AmVdDbwG9A1b/GlVVR8btV0RUAb8QVXVe4Ytfz/wbSAZeAX4qKqqHcPW3wV8BcgBWoAPq6q6e0onLoQQYlzSgieEECLiFEW5pB4gzuH5fgj4ILAFyAAswP8OW98B/A/wnQmW16Sqasywn8fG2OYh4NDwBYqirAAeDdcllVCQ+PCw9dcD3wU+AsQCVwFVE6yTEEKISbikvoCFEEKAoij3Ax8DHEA98CVVVZ9VFCUKaAW2qqp6PLxtClAH5Kqq6lQU5TbgG0AecBK4T1XVY+Fta4CfAB8IfVRswBfGOlZ4ewPw38C9QDfwA0LBiUlVVb+iKPHAA8AtQBD4JfAVVVUDY5zT5cCPgGWAF3gG+CdVVQfD61cQCnTWAT7gR6qqfitch38D/i5cxwrg/wEGoHqoLuEy3gAeV1X1Z4qifDh8XgcJBVk/URTll8BPgcs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gO1M9ASHEzHA7G9CCQexpuQA4sosBGYcnFo8qTx85MdEkRJm4Ki2R8s5earu9c10tIYQQYlomFOCpqrpHVdX64csURXEAa4Enw4ueBNYqipJykeLWAf2qqu4Jf34EeN/EqyyEmA0dLTUAJKXnARCTkIw1NlHG4YlFwesP0NQ3QEGsBYAtqQnEGA281NAu3ZCFEEIsaNMZg5cNNKqqGgAIvzaFlw95QlGUY4qiPKwoSkJ4WQ7DWgJVVW0H9IqiJE2jLkKICHM112CNS8JiiwNAp9OFxuHVn0bTgnNbOSGmqbrbiwYUxFkBMBv0XJOZRE1PPxVdfXNbOSGEEGIaJjQGb4quVFW1XlGUKOB/gB8D90SqcLs9JlJFRVRKSuxcV0HMEwv9Wuh01pORWzjiPAqXl1Bz8iD6gIfk9OwL7C2GW+jXwmLU5OzErNexNi8Zoz70rPNmewz72zy82uLmiiWp6HW6iB9XrgUxnFwPYohcC5Mz6B/kraZjbMpei163uHJGRuJamE6AVw9kKopiUFU1oCiKAcgIL2eoS6eqqgOKojwMPBferw7IHSpEUZRkIKiqasdkDu5y9RAMzq9uNCkpsbS1dc91NcQ8sNCvBW9PF92dLgovu2rEeVgSQkHdqWOlKMaE8XYXwyz0a2GxOuHsIjfGgtvVO2L5NWmJPF3VwqtqE6vtcRE9plwLYji5HsQQuRYm783mt/l1+dPc1xugJHn5XFcnYiZzLej1unEbvKYc8oazXh4B7g4vuhsoVVWLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].iloc[-test_size:].values, r) for r in results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'].iloc[-test_size:].values, label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
# Copyright 2017 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""DNC access modules."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import collections
|
||||
import sonnet as snt
|
||||
import tensorflow as tf
|
||||
|
||||
import addressing
|
||||
import util
|
||||
|
||||
AccessState = collections.namedtuple('AccessState', (
|
||||
'memory', 'read_weights', 'write_weights', 'linkage', 'usage'))
|
||||
|
||||
|
||||
def _erase_and_write(memory, address, reset_weights, values):
|
||||
"""Module to erase and write in the external memory.
|
||||
|
||||
Erase operation:
|
||||
M_t'(i) = M_{t-1}(i) * (1 - w_t(i) * e_t)
|
||||
|
||||
Add operation:
|
||||
M_t(i) = M_t'(i) + w_t(i) * a_t
|
||||
|
||||
where e are the reset_weights, w the write weights and a the values.
|
||||
|
||||
Args:
|
||||
memory: 3-D tensor of shape `[batch_size, memory_size, word_size]`.
|
||||
address: 3-D tensor `[batch_size, num_writes, memory_size]`.
|
||||
reset_weights: 3-D tensor `[batch_size, num_writes, word_size]`.
|
||||
values: 3-D tensor `[batch_size, num_writes, word_size]`.
|
||||
|
||||
Returns:
|
||||
3-D tensor of shape `[batch_size, num_writes, word_size]`.
|
||||
"""
|
||||
with tf.name_scope('erase_memory', values=[memory, address, reset_weights]):
|
||||
expand_address = tf.expand_dims(address, 3)
|
||||
reset_weights = tf.expand_dims(reset_weights, 2)
|
||||
weighted_resets = expand_address * reset_weights
|
||||
reset_gate = tf.reduce_prod(1 - weighted_resets, [1])
|
||||
memory *= reset_gate
|
||||
|
||||
with tf.name_scope('additive_write', values=[memory, address, values]):
|
||||
add_matrix = tf.matmul(address, values, adjoint_a=True)
|
||||
memory += add_matrix
|
||||
|
||||
return memory
|
||||
|
||||
|
||||
class MemoryAccess(snt.RNNCore):
|
||||
"""Access module of the Differentiable Neural Computer.
|
||||
|
||||
This memory module supports multiple read and write heads. It makes use of:
|
||||
|
||||
* `addressing.TemporalLinkage` to track the temporal ordering of writes in
|
||||
memory for each write head.
|
||||
* `addressing.FreenessAllocator` for keeping track of memory usage, where
|
||||
usage increase when a memory location is written to, and decreases when
|
||||
memory is read from that the controller says can be freed.
|
||||
|
||||
Write-address selection is done by an interpolation between content-based
|
||||
lookup and using unused memory.
|
||||
|
||||
Read-address selection is done by an interpolation of content-based lookup
|
||||
and following the link graph in the forward or backwards read direction.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
memory_size=128,
|
||||
word_size=20,
|
||||
num_reads=1,
|
||||
num_writes=1,
|
||||
name='memory_access'):
|
||||
"""Creates a MemoryAccess module.
|
||||
|
||||
Args:
|
||||
memory_size: The number of memory slots (N in the DNC paper).
|
||||
word_size: The width of each memory slot (W in the DNC paper)
|
||||
num_reads: The number of read heads (R in the DNC paper).
|
||||
num_writes: The number of write heads (fixed at 1 in the paper).
|
||||
name: The name of the module.
|
||||
"""
|
||||
super(MemoryAccess, self).__init__(name=name)
|
||||
self._memory_size = memory_size
|
||||
self._word_size = word_size
|
||||
self._num_reads = num_reads
|
||||
self._num_writes = num_writes
|
||||
|
||||
self._write_content_weights_mod = addressing.CosineWeights(
|
||||
num_writes, word_size, name='write_content_weights')
|
||||
self._read_content_weights_mod = addressing.CosineWeights(
|
||||
num_reads, word_size, name='read_content_weights')
|
||||
|
||||
self._linkage = addressing.TemporalLinkage(memory_size, num_writes)
|
||||
self._freeness = addressing.Freeness(memory_size)
|
||||
|
||||
def _build(self, inputs, prev_state):
|
||||
"""Connects the MemoryAccess module into the graph.
|
||||
|
||||
Args:
|
||||
inputs: tensor of shape `[batch_size, input_size]`. This is used to
|
||||
control this access module.
|
||||
prev_state: Instance of `AccessState` containing the previous state.
|
||||
|
||||
Returns:
|
||||
A tuple `(output, next_state)`, where `output` is a tensor of shape
|
||||
`[batch_size, num_reads, word_size]`, and `next_state` is the new
|
||||
`AccessState` named tuple at the current time t.
|
||||
"""
|
||||
inputs = self._read_inputs(inputs)
|
||||
|
||||
# Update usage using inputs['free_gate'] and previous read & write weights.
|
||||
usage = self._freeness(
|
||||
write_weights=prev_state.write_weights,
|
||||
free_gate=inputs['free_gate'],
|
||||
read_weights=prev_state.read_weights,
|
||||
prev_usage=prev_state.usage)
|
||||
|
||||
# Write to memory.
|
||||
write_weights = self._write_weights(inputs, prev_state.memory, usage)
|
||||
memory = _erase_and_write(
|
||||
prev_state.memory,
|
||||
address=write_weights,
|
||||
reset_weights=inputs['erase_vectors'],
|
||||
values=inputs['write_vectors'])
|
||||
|
||||
linkage_state = self._linkage(write_weights, prev_state.linkage)
|
||||
|
||||
# Read from memory.
|
||||
read_weights = self._read_weights(
|
||||
inputs,
|
||||
memory=memory,
|
||||
prev_read_weights=prev_state.read_weights,
|
||||
link=linkage_state.link)
|
||||
read_words = tf.matmul(read_weights, memory)
|
||||
|
||||
return (read_words, AccessState(
|
||||
memory=memory,
|
||||
read_weights=read_weights,
|
||||
write_weights=write_weights,
|
||||
linkage=linkage_state,
|
||||
usage=usage))
|
||||
|
||||
def _read_inputs(self, inputs):
|
||||
"""Applies transformations to `inputs` to get control for this module."""
|
||||
|
||||
def _linear(first_dim, second_dim, name, activation=None):
|
||||
"""Returns a linear transformation of `inputs`, followed by a reshape."""
|
||||
linear = snt.Linear(first_dim * second_dim, name=name)(inputs)
|
||||
if activation is not None:
|
||||
linear = activation(linear, name=name + '_activation')
|
||||
return tf.reshape(linear, [-1, first_dim, second_dim])
|
||||
|
||||
# v_t^i - The vectors to write to memory, for each write head `i`.
|
||||
write_vectors = _linear(self._num_writes, self._word_size, 'write_vectors')
|
||||
|
||||
# e_t^i - Amount to erase the memory by before writing, for each write head.
|
||||
erase_vectors = _linear(self._num_writes, self._word_size, 'erase_vectors',
|
||||
tf.sigmoid)
|
||||
|
||||
# f_t^j - Amount that the memory at the locations read from at the previous
|
||||
# time step can be declared unused, for each read head `j`.
|
||||
free_gate = tf.sigmoid(
|
||||
snt.Linear(self._num_reads, name='free_gate')(inputs))
|
||||
|
||||
# g_t^{a, i} - Interpolation between writing to unallocated memory and
|
||||
# content-based lookup, for each write head `i`. Note: `a` is simply used to
|
||||
# identify this gate with allocation vs writing (as defined below).
|
||||
allocation_gate = tf.sigmoid(
|
||||
snt.Linear(self._num_writes, name='allocation_gate')(inputs))
|
||||
|
||||
# g_t^{w, i} - Overall gating of write amount for each write head.
|
||||
write_gate = tf.sigmoid(
|
||||
snt.Linear(self._num_writes, name='write_gate')(inputs))
|
||||
|
||||
# \pi_t^j - Mixing between "backwards" and "forwards" positions (for
|
||||
# each write head), and content-based lookup, for each read head.
|
||||
num_read_modes = 1 + 2 * self._num_writes
|
||||
read_mode = snt.BatchApply(tf.nn.softmax)(
|
||||
_linear(self._num_reads, num_read_modes, name='read_mode'))
|
||||
|
||||
# Parameters for the (read / write) "weights by content matching" modules.
|
||||
write_keys = _linear(self._num_writes, self._word_size, 'write_keys')
|
||||
write_strengths = snt.Linear(self._num_writes, name='write_strengths')(
|
||||
inputs)
|
||||
|
||||
read_keys = _linear(self._num_reads, self._word_size, 'read_keys')
|
||||
read_strengths = snt.Linear(self._num_reads, name='read_strengths')(inputs)
|
||||
|
||||
result = {
|
||||
'read_content_keys': read_keys,
|
||||
'read_content_strengths': read_strengths,
|
||||
'write_content_keys': write_keys,
|
||||
'write_content_strengths': write_strengths,
|
||||
'write_vectors': write_vectors,
|
||||
'erase_vectors': erase_vectors,
|
||||
'free_gate': free_gate,
|
||||
'allocation_gate': allocation_gate,
|
||||
'write_gate': write_gate,
|
||||
'read_mode': read_mode,
|
||||
}
|
||||
return result
|
||||
|
||||
def _write_weights(self, inputs, memory, usage):
|
||||
"""Calculates the memory locations to write to.
|
||||
|
||||
This uses a combination of content-based lookup and finding an unused
|
||||
location in memory, for each write head.
|
||||
|
||||
Args:
|
||||
inputs: Collection of inputs to the access module, including controls for
|
||||
how to chose memory writing, such as the content to look-up and the
|
||||
weighting between content-based and allocation-based addressing.
|
||||
memory: A tensor of shape `[batch_size, memory_size, word_size]`
|
||||
containing the current memory contents.
|
||||
usage: Current memory usage, which is a tensor of shape `[batch_size,
|
||||
memory_size]`, used for allocation-based addressing.
|
||||
|
||||
Returns:
|
||||
tensor of shape `[batch_size, num_writes, memory_size]` indicating where
|
||||
to write to (if anywhere) for each write head.
|
||||
"""
|
||||
with tf.name_scope('write_weights', values=[inputs, memory, usage]):
|
||||
# c_t^{w, i} - The content-based weights for each write head.
|
||||
write_content_weights = self._write_content_weights_mod(
|
||||
memory, inputs['write_content_keys'],
|
||||
inputs['write_content_strengths'])
|
||||
|
||||
# a_t^i - The allocation weights for each write head.
|
||||
write_allocation_weights = self._freeness.write_allocation_weights(
|
||||
usage=usage,
|
||||
write_gates=(inputs['allocation_gate'] * inputs['write_gate']),
|
||||
num_writes=self._num_writes)
|
||||
|
||||
# Expands gates over memory locations.
|
||||
allocation_gate = tf.expand_dims(inputs['allocation_gate'], -1)
|
||||
write_gate = tf.expand_dims(inputs['write_gate'], -1)
|
||||
|
||||
# w_t^{w, i} - The write weightings for each write head.
|
||||
return write_gate * (allocation_gate * write_allocation_weights +
|
||||
(1 - allocation_gate) * write_content_weights)
|
||||
|
||||
def _read_weights(self, inputs, memory, prev_read_weights, link):
|
||||
"""Calculates read weights for each read head.
|
||||
|
||||
The read weights are a combination of following the link graphs in the
|
||||
forward or backward directions from the previous read position, and doing
|
||||
content-based lookup. The interpolation between these different modes is
|
||||
done by `inputs['read_mode']`.
|
||||
|
||||
Args:
|
||||
inputs: Controls for this access module. This contains the content-based
|
||||
keys to lookup, and the weightings for the different read modes.
|
||||
memory: A tensor of shape `[batch_size, memory_size, word_size]`
|
||||
containing the current memory contents to do content-based lookup.
|
||||
prev_read_weights: A tensor of shape `[batch_size, num_reads,
|
||||
memory_size]` containing the previous read locations.
|
||||
link: A tensor of shape `[batch_size, num_writes, memory_size,
|
||||
memory_size]` containing the temporal write transition graphs.
|
||||
|
||||
Returns:
|
||||
A tensor of shape `[batch_size, num_reads, memory_size]` containing the
|
||||
read weights for each read head.
|
||||
"""
|
||||
with tf.name_scope(
|
||||
'read_weights', values=[inputs, memory, prev_read_weights, link]):
|
||||
# c_t^{r, i} - The content weightings for each read head.
|
||||
content_weights = self._read_content_weights_mod(
|
||||
memory, inputs['read_content_keys'], inputs['read_content_strengths'])
|
||||
|
||||
# Calculates f_t^i and b_t^i.
|
||||
forward_weights = self._linkage.directional_read_weights(
|
||||
link, prev_read_weights, forward=True)
|
||||
backward_weights = self._linkage.directional_read_weights(
|
||||
link, prev_read_weights, forward=False)
|
||||
|
||||
backward_mode = inputs['read_mode'][:, :, :self._num_writes]
|
||||
forward_mode = (
|
||||
inputs['read_mode'][:, :, self._num_writes:2 * self._num_writes])
|
||||
content_mode = inputs['read_mode'][:, :, 2 * self._num_writes]
|
||||
|
||||
read_weights = (
|
||||
tf.expand_dims(content_mode, 2) * content_weights + tf.reduce_sum(
|
||||
tf.expand_dims(forward_mode, 3) * forward_weights, 2) +
|
||||
tf.reduce_sum(tf.expand_dims(backward_mode, 3) * backward_weights, 2))
|
||||
|
||||
return read_weights
|
||||
|
||||
@property
|
||||
def state_size(self):
|
||||
"""Returns a tuple of the shape of the state tensors."""
|
||||
return AccessState(
|
||||
memory=tf.TensorShape([self._memory_size, self._word_size]),
|
||||
read_weights=tf.TensorShape([self._num_reads, self._memory_size]),
|
||||
write_weights=tf.TensorShape([self._num_writes, self._memory_size]),
|
||||
linkage=self._linkage.state_size,
|
||||
usage=self._freeness.state_size)
|
||||
|
||||
@property
|
||||
def output_size(self):
|
||||
"""Returns the output shape."""
|
||||
return tf.TensorShape([self._num_reads, self._word_size])
|
||||
@@ -0,0 +1,410 @@
|
||||
# Copyright 2017 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""DNC addressing modules."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import collections
|
||||
import sonnet as snt
|
||||
import tensorflow as tf
|
||||
|
||||
import util
|
||||
|
||||
# Ensure values are greater than epsilon to avoid numerical instability.
|
||||
_EPSILON = 1e-6
|
||||
|
||||
TemporalLinkageState = collections.namedtuple('TemporalLinkageState',
|
||||
('link', 'precedence_weights'))
|
||||
|
||||
|
||||
def _vector_norms(m):
|
||||
squared_norms = tf.reduce_sum(m * m, axis=2, keep_dims=True)
|
||||
return tf.sqrt(squared_norms + _EPSILON)
|
||||
|
||||
|
||||
def weighted_softmax(activations, strengths, strengths_op):
|
||||
"""Returns softmax over activations multiplied by positive strengths.
|
||||
|
||||
Args:
|
||||
activations: A tensor of shape `[batch_size, num_heads, memory_size]`, of
|
||||
activations to be transformed. Softmax is taken over the last dimension.
|
||||
strengths: A tensor of shape `[batch_size, num_heads]` containing strengths to
|
||||
multiply by the activations prior to the softmax.
|
||||
strengths_op: An operation to transform strengths before softmax.
|
||||
|
||||
Returns:
|
||||
A tensor of same shape as `activations` with weighted softmax applied.
|
||||
"""
|
||||
transformed_strengths = tf.expand_dims(strengths_op(strengths), -1)
|
||||
sharp_activations = activations * transformed_strengths
|
||||
softmax = snt.BatchApply(module_or_op=tf.nn.softmax)
|
||||
return softmax(sharp_activations)
|
||||
|
||||
|
||||
class CosineWeights(snt.AbstractModule):
|
||||
"""Cosine-weighted attention.
|
||||
|
||||
Calculates the cosine similarity between a query and each word in memory, then
|
||||
applies a weighted softmax to return a sharp distribution.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
num_heads,
|
||||
word_size,
|
||||
strength_op=tf.nn.softplus,
|
||||
name='cosine_weights'):
|
||||
"""Initializes the CosineWeights module.
|
||||
|
||||
Args:
|
||||
num_heads: number of memory heads.
|
||||
word_size: memory word size.
|
||||
strength_op: operation to apply to strengths (default is tf.nn.softplus).
|
||||
name: module name (default 'cosine_weights')
|
||||
"""
|
||||
super(CosineWeights, self).__init__(name=name)
|
||||
self._num_heads = num_heads
|
||||
self._word_size = word_size
|
||||
self._strength_op = strength_op
|
||||
|
||||
def _build(self, memory, keys, strengths):
|
||||
"""Connects the CosineWeights module into the graph.
|
||||
|
||||
Args:
|
||||
memory: A 3-D tensor of shape `[batch_size, memory_size, word_size]`.
|
||||
keys: A 3-D tensor of shape `[batch_size, num_heads, word_size]`.
|
||||
strengths: A 2-D tensor of shape `[batch_size, num_heads]`.
|
||||
|
||||
Returns:
|
||||
Weights tensor of shape `[batch_size, num_heads, memory_size]`.
|
||||
"""
|
||||
# Calculates the inner product between the query vector and words in memory.
|
||||
dot = tf.matmul(keys, memory, adjoint_b=True)
|
||||
|
||||
# Outer product to compute denominator (euclidean norm of query and memory).
|
||||
memory_norms = _vector_norms(memory)
|
||||
key_norms = _vector_norms(keys)
|
||||
norm = tf.matmul(key_norms, memory_norms, adjoint_b=True)
|
||||
|
||||
# Calculates cosine similarity between the query vector and words in memory.
|
||||
similarity = dot / (norm + _EPSILON)
|
||||
|
||||
return weighted_softmax(similarity, strengths, self._strength_op)
|
||||
|
||||
|
||||
class TemporalLinkage(snt.RNNCore):
|
||||
"""Keeps track of write order for forward and backward addressing.
|
||||
|
||||
This is a pseudo-RNNCore module, whose state is a pair `(link,
|
||||
precedence_weights)`, where `link` is a (collection of) graphs for (possibly
|
||||
multiple) write heads (represented by a tensor with values in the range
|
||||
[0, 1]), and `precedence_weights` records the "previous write locations" used
|
||||
to build the link graphs.
|
||||
|
||||
The function `directional_read_weights` computes addresses following the
|
||||
forward and backward directions in the link graphs.
|
||||
"""
|
||||
|
||||
def __init__(self, memory_size, num_writes, name='temporal_linkage'):
|
||||
"""Construct a TemporalLinkage module.
|
||||
|
||||
Args:
|
||||
memory_size: The number of memory slots.
|
||||
num_writes: The number of write heads.
|
||||
name: Name of the module.
|
||||
"""
|
||||
super(TemporalLinkage, self).__init__(name=name)
|
||||
self._memory_size = memory_size
|
||||
self._num_writes = num_writes
|
||||
|
||||
def _build(self, write_weights, prev_state):
|
||||
"""Calculate the updated linkage state given the write weights.
|
||||
|
||||
Args:
|
||||
write_weights: A tensor of shape `[batch_size, num_writes, memory_size]`
|
||||
containing the memory addresses of the different write heads.
|
||||
prev_state: `TemporalLinkageState` tuple containg a tensor `link` of
|
||||
shape `[batch_size, num_writes, memory_size, memory_size]`, and a
|
||||
tensor `precedence_weights` of shape `[batch_size, num_writes,
|
||||
memory_size]` containing the aggregated history of recent writes.
|
||||
|
||||
Returns:
|
||||
A `TemporalLinkageState` tuple `next_state`, which contains the updated
|
||||
link and precedence weights.
|
||||
"""
|
||||
link = self._link(prev_state.link, prev_state.precedence_weights,
|
||||
write_weights)
|
||||
precedence_weights = self._precedence_weights(prev_state.precedence_weights,
|
||||
write_weights)
|
||||
return TemporalLinkageState(
|
||||
link=link, precedence_weights=precedence_weights)
|
||||
|
||||
def directional_read_weights(self, link, prev_read_weights, forward):
|
||||
"""Calculates the forward or the backward read weights.
|
||||
|
||||
For each read head (at a given address), there are `num_writes` link graphs
|
||||
to follow. Thus this function computes a read address for each of the
|
||||
`num_reads * num_writes` pairs of read and write heads.
|
||||
|
||||
Args:
|
||||
link: tensor of shape `[batch_size, num_writes, memory_size,
|
||||
memory_size]` representing the link graphs L_t.
|
||||
prev_read_weights: tensor of shape `[batch_size, num_reads,
|
||||
memory_size]` containing the previous read weights w_{t-1}^r.
|
||||
forward: Boolean indicating whether to follow the "future" direction in
|
||||
the link graph (True) or the "past" direction (False).
|
||||
|
||||
Returns:
|
||||
tensor of shape `[batch_size, num_reads, num_writes, memory_size]`
|
||||
"""
|
||||
with tf.name_scope('directional_read_weights'):
|
||||
# We calculate the forward and backward directions for each pair of
|
||||
# read and write heads; hence we need to tile the read weights and do a
|
||||
# sort of "outer product" to get this.
|
||||
expanded_read_weights = tf.stack([prev_read_weights] * self._num_writes,
|
||||
1)
|
||||
result = tf.matmul(expanded_read_weights, link, adjoint_b=forward)
|
||||
# Swap dimensions 1, 2 so order is [batch, reads, writes, memory]:
|
||||
return tf.transpose(result, perm=[0, 2, 1, 3])
|
||||
|
||||
def _link(self, prev_link, prev_precedence_weights, write_weights):
|
||||
"""Calculates the new link graphs.
|
||||
|
||||
For each write head, the link is a directed graph (represented by a matrix
|
||||
with entries in range [0, 1]) whose vertices are the memory locations, and
|
||||
an edge indicates temporal ordering of writes.
|
||||
|
||||
Args:
|
||||
prev_link: A tensor of shape `[batch_size, num_writes, memory_size,
|
||||
memory_size]` representing the previous link graphs for each write
|
||||
head.
|
||||
prev_precedence_weights: A tensor of shape `[batch_size, num_writes,
|
||||
memory_size]` which is the previous "aggregated" write weights for
|
||||
each write head.
|
||||
write_weights: A tensor of shape `[batch_size, num_writes, memory_size]`
|
||||
containing the new locations in memory written to.
|
||||
|
||||
Returns:
|
||||
A tensor of shape `[batch_size, num_writes, memory_size, memory_size]`
|
||||
containing the new link graphs for each write head.
|
||||
"""
|
||||
with tf.name_scope('link'):
|
||||
batch_size = prev_link.get_shape()[0].value
|
||||
write_weights_i = tf.expand_dims(write_weights, 3)
|
||||
write_weights_j = tf.expand_dims(write_weights, 2)
|
||||
prev_precedence_weights_j = tf.expand_dims(prev_precedence_weights, 2)
|
||||
prev_link_scale = 1 - write_weights_i - write_weights_j
|
||||
new_link = write_weights_i * prev_precedence_weights_j
|
||||
link = prev_link_scale * prev_link + new_link
|
||||
# Return the link with the diagonal set to zero, to remove self-looping
|
||||
# edges.
|
||||
return tf.matrix_set_diag(
|
||||
link,
|
||||
tf.zeros(
|
||||
[batch_size, self._num_writes, self._memory_size],
|
||||
dtype=link.dtype))
|
||||
|
||||
def _precedence_weights(self, prev_precedence_weights, write_weights):
|
||||
"""Calculates the new precedence weights given the current write weights.
|
||||
|
||||
The precedence weights are the "aggregated write weights" for each write
|
||||
head, where write weights with sum close to zero will leave the precedence
|
||||
weights unchanged, but with sum close to one will replace the precedence
|
||||
weights.
|
||||
|
||||
Args:
|
||||
prev_precedence_weights: A tensor of shape `[batch_size, num_writes,
|
||||
memory_size]` containing the previous precedence weights.
|
||||
write_weights: A tensor of shape `[batch_size, num_writes, memory_size]`
|
||||
containing the new write weights.
|
||||
|
||||
Returns:
|
||||
A tensor of shape `[batch_size, num_writes, memory_size]` containing the
|
||||
new precedence weights.
|
||||
"""
|
||||
with tf.name_scope('precedence_weights'):
|
||||
write_sum = tf.reduce_sum(write_weights, 2, keep_dims=True)
|
||||
return (1 - write_sum) * prev_precedence_weights + write_weights
|
||||
|
||||
@property
|
||||
def state_size(self):
|
||||
"""Returns a `TemporalLinkageState` tuple of the state tensors' shapes."""
|
||||
return TemporalLinkageState(
|
||||
link=tf.TensorShape(
|
||||
[self._num_writes, self._memory_size, self._memory_size]),
|
||||
precedence_weights=tf.TensorShape([self._num_writes,
|
||||
self._memory_size]),)
|
||||
|
||||
|
||||
class Freeness(snt.RNNCore):
|
||||
"""Memory usage that is increased by writing and decreased by reading.
|
||||
|
||||
This module is a pseudo-RNNCore whose state is a tensor with values in
|
||||
the range [0, 1] indicating the usage of each of `memory_size` memory slots.
|
||||
|
||||
The usage is:
|
||||
|
||||
* Increased by writing, where usage is increased towards 1 at the write
|
||||
addresses.
|
||||
* Decreased by reading, where usage is decreased after reading from a
|
||||
location when free_gate is close to 1.
|
||||
|
||||
The function `write_allocation_weights` can be invoked to get free locations
|
||||
to write to for a number of write heads.
|
||||
"""
|
||||
|
||||
def __init__(self, memory_size, name='freeness'):
|
||||
"""Creates a Freeness module.
|
||||
|
||||
Args:
|
||||
memory_size: Number of memory slots.
|
||||
name: Name of the module.
|
||||
"""
|
||||
super(Freeness, self).__init__(name=name)
|
||||
self._memory_size = memory_size
|
||||
|
||||
def _build(self, write_weights, free_gate, read_weights, prev_usage):
|
||||
"""Calculates the new memory usage u_t.
|
||||
|
||||
Memory that was written to in the previous time step will have its usage
|
||||
increased; memory that was read from and the controller says can be "freed"
|
||||
will have its usage decreased.
|
||||
|
||||
Args:
|
||||
write_weights: tensor of shape `[batch_size, num_writes,
|
||||
memory_size]` giving write weights at previous time step.
|
||||
free_gate: tensor of shape `[batch_size, num_reads]` which indicates
|
||||
which read heads read memory that can now be freed.
|
||||
read_weights: tensor of shape `[batch_size, num_reads,
|
||||
memory_size]` giving read weights at previous time step.
|
||||
prev_usage: tensor of shape `[batch_size, memory_size]` giving
|
||||
usage u_{t - 1} at the previous time step, with entries in range
|
||||
[0, 1].
|
||||
|
||||
Returns:
|
||||
tensor of shape `[batch_size, memory_size]` representing updated memory
|
||||
usage.
|
||||
"""
|
||||
# Calculation of usage is not differentiable with respect to write weights.
|
||||
write_weights = tf.stop_gradient(write_weights)
|
||||
usage = self._usage_after_write(prev_usage, write_weights)
|
||||
usage = self._usage_after_read(usage, free_gate, read_weights)
|
||||
return usage
|
||||
|
||||
def write_allocation_weights(self, usage, write_gates, num_writes):
|
||||
"""Calculates freeness-based locations for writing to.
|
||||
|
||||
This finds unused memory by ranking the memory locations by usage, for each
|
||||
write head. (For more than one write head, we use a "simulated new usage"
|
||||
which takes into account the fact that the previous write head will increase
|
||||
the usage in that area of the memory.)
|
||||
|
||||
Args:
|
||||
usage: A tensor of shape `[batch_size, memory_size]` representing
|
||||
current memory usage.
|
||||
write_gates: A tensor of shape `[batch_size, num_writes]` with values in
|
||||
the range [0, 1] indicating how much each write head does writing
|
||||
based on the address returned here (and hence how much usage
|
||||
increases).
|
||||
num_writes: The number of write heads to calculate write weights for.
|
||||
|
||||
Returns:
|
||||
tensor of shape `[batch_size, num_writes, memory_size]` containing the
|
||||
freeness-based write locations. Note that this isn't scaled by
|
||||
`write_gate`; this scaling must be applied externally.
|
||||
"""
|
||||
with tf.name_scope('write_allocation_weights'):
|
||||
# expand gatings over memory locations
|
||||
write_gates = tf.expand_dims(write_gates, -1)
|
||||
|
||||
allocation_weights = []
|
||||
for i in range(num_writes):
|
||||
allocation_weights.append(self._allocation(usage))
|
||||
# update usage to take into account writing to this new allocation
|
||||
usage += ((1 - usage) * write_gates[:, i, :] * allocation_weights[i])
|
||||
|
||||
# Pack the allocation weights for the write heads into one tensor.
|
||||
return tf.stack(allocation_weights, axis=1)
|
||||
|
||||
def _usage_after_write(self, prev_usage, write_weights):
|
||||
"""Calcualtes the new usage after writing to memory.
|
||||
|
||||
Args:
|
||||
prev_usage: tensor of shape `[batch_size, memory_size]`.
|
||||
write_weights: tensor of shape `[batch_size, num_writes, memory_size]`.
|
||||
|
||||
Returns:
|
||||
New usage, a tensor of shape `[batch_size, memory_size]`.
|
||||
"""
|
||||
with tf.name_scope('usage_after_write'):
|
||||
# Calculate the aggregated effect of all write heads
|
||||
write_weights = 1 - tf.reduce_prod(1 - write_weights, [1])
|
||||
return prev_usage + (1 - prev_usage) * write_weights
|
||||
|
||||
def _usage_after_read(self, prev_usage, free_gate, read_weights):
|
||||
"""Calcualtes the new usage after reading and freeing from memory.
|
||||
|
||||
Args:
|
||||
prev_usage: tensor of shape `[batch_size, memory_size]`.
|
||||
free_gate: tensor of shape `[batch_size, num_reads]` with entries in the
|
||||
range [0, 1] indicating the amount that locations read from can be
|
||||
freed.
|
||||
read_weights: tensor of shape `[batch_size, num_reads, memory_size]`.
|
||||
|
||||
Returns:
|
||||
New usage, a tensor of shape `[batch_size, memory_size]`.
|
||||
"""
|
||||
with tf.name_scope('usage_after_read'):
|
||||
free_gate = tf.expand_dims(free_gate, -1)
|
||||
free_read_weights = free_gate * read_weights
|
||||
phi = tf.reduce_prod(1 - free_read_weights, [1], name='phi')
|
||||
return prev_usage * phi
|
||||
|
||||
def _allocation(self, usage):
|
||||
r"""Computes allocation by sorting `usage`.
|
||||
|
||||
This corresponds to the value a = a_t[\phi_t[j]] in the paper.
|
||||
|
||||
Args:
|
||||
usage: tensor of shape `[batch_size, memory_size]` indicating current
|
||||
memory usage. This is equal to u_t in the paper when we only have one
|
||||
write head, but for multiple write heads, one should update the usage
|
||||
while iterating through the write heads to take into account the
|
||||
allocation returned by this function.
|
||||
|
||||
Returns:
|
||||
Tensor of shape `[batch_size, memory_size]` corresponding to allocation.
|
||||
"""
|
||||
with tf.name_scope('allocation'):
|
||||
# Ensure values are not too small prior to cumprod.
|
||||
usage = _EPSILON + (1 - _EPSILON) * usage
|
||||
|
||||
nonusage = 1 - usage
|
||||
sorted_nonusage, indices = tf.nn.top_k(
|
||||
nonusage, k=self._memory_size, name='sort')
|
||||
sorted_usage = 1 - sorted_nonusage
|
||||
prod_sorted_usage = tf.cumprod(sorted_usage, axis=1, exclusive=True)
|
||||
sorted_allocation = sorted_nonusage * prod_sorted_usage
|
||||
inverse_indices = util.batch_invert_permutation(indices)
|
||||
|
||||
# This final line "unsorts" sorted_allocation, so that the indexing
|
||||
# corresponds to the original indexing of `usage`.
|
||||
return util.batch_gather(sorted_allocation, inverse_indices)
|
||||
|
||||
@property
|
||||
def state_size(self):
|
||||
"""Returns the shape of the state tensor."""
|
||||
return tf.TensorShape([self._memory_size])
|
||||
@@ -0,0 +1,41 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
import time
|
||||
|
||||
def reducedimension(input_, dimension = 2, learning_rate = 0.01, hidden_layer = 256, epoch = 20):
|
||||
|
||||
input_size = input_.shape[1]
|
||||
X = tf.placeholder("float", [None, input_size])
|
||||
|
||||
weights = {
|
||||
'encoder_h1': tf.Variable(tf.random_normal([input_size, hidden_layer])),
|
||||
'encoder_h2': tf.Variable(tf.random_normal([hidden_layer, dimension])),
|
||||
'decoder_h1': tf.Variable(tf.random_normal([dimension, hidden_layer])),
|
||||
'decoder_h2': tf.Variable(tf.random_normal([hidden_layer, input_size])),
|
||||
}
|
||||
|
||||
biases = {
|
||||
'encoder_b1': tf.Variable(tf.random_normal([hidden_layer])),
|
||||
'encoder_b2': tf.Variable(tf.random_normal([dimension])),
|
||||
'decoder_b1': tf.Variable(tf.random_normal([hidden_layer])),
|
||||
'decoder_b2': tf.Variable(tf.random_normal([input_size])),
|
||||
}
|
||||
|
||||
first_layer_encoder = tf.nn.sigmoid(tf.add(tf.matmul(X, weights['encoder_h1']), biases['encoder_b1']))
|
||||
second_layer_encoder = tf.nn.sigmoid(tf.add(tf.matmul(first_layer_encoder, weights['encoder_h2']), biases['encoder_b2']))
|
||||
first_layer_decoder = tf.nn.sigmoid(tf.add(tf.matmul(second_layer_encoder, weights['decoder_h1']), biases['decoder_b1']))
|
||||
second_layer_decoder = tf.nn.sigmoid(tf.add(tf.matmul(first_layer_decoder, weights['decoder_h2']), biases['decoder_b2']))
|
||||
cost = tf.reduce_mean(tf.pow(X - second_layer_decoder, 2))
|
||||
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)
|
||||
sess = tf.InteractiveSession()
|
||||
sess.run(tf.global_variables_initializer())
|
||||
|
||||
for i in range(epoch):
|
||||
last_time = time.time()
|
||||
_, loss = sess.run([optimizer, cost], feed_dict={X: input_})
|
||||
if (i + 1) % 10 == 0:
|
||||
print('epoch:', i + 1, 'loss:', loss, 'time:', time.time() - last_time)
|
||||
|
||||
vectors = sess.run(second_layer_encoder, feed_dict={X: input_})
|
||||
tf.reset_default_graph()
|
||||
return vectors
|
||||
@@ -0,0 +1,142 @@
|
||||
# Copyright 2017 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""DNC Cores.
|
||||
|
||||
These modules create a DNC core. They take input, pass parameters to the memory
|
||||
access module, and integrate the output of memory to form an output.
|
||||
"""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import collections
|
||||
import numpy as np
|
||||
import sonnet as snt
|
||||
import tensorflow as tf
|
||||
|
||||
import access
|
||||
|
||||
DNCState = collections.namedtuple('DNCState', ('access_output', 'access_state',
|
||||
'controller_state'))
|
||||
|
||||
|
||||
class DNC(snt.RNNCore):
|
||||
"""DNC core module.
|
||||
|
||||
Contains controller and memory access module.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
access_config,
|
||||
controller_config,
|
||||
output_size,
|
||||
clip_value=None,
|
||||
name='dnc'):
|
||||
"""Initializes the DNC core.
|
||||
|
||||
Args:
|
||||
access_config: dictionary of access module configurations.
|
||||
controller_config: dictionary of controller (LSTM) module configurations.
|
||||
output_size: output dimension size of core.
|
||||
clip_value: clips controller and core output values to between
|
||||
`[-clip_value, clip_value]` if specified.
|
||||
name: module name (default 'dnc').
|
||||
|
||||
Raises:
|
||||
TypeError: if direct_input_size is not None for any access module other
|
||||
than KeyValueMemory.
|
||||
"""
|
||||
super(DNC, self).__init__(name=name)
|
||||
|
||||
with self._enter_variable_scope():
|
||||
self._controller = snt.LSTM(**controller_config)
|
||||
self._access = access.MemoryAccess(**access_config)
|
||||
|
||||
self._access_output_size = np.prod(self._access.output_size.as_list())
|
||||
self._output_size = output_size
|
||||
self._clip_value = clip_value or 0
|
||||
|
||||
self._output_size = tf.TensorShape([output_size])
|
||||
self._state_size = DNCState(
|
||||
access_output=self._access_output_size,
|
||||
access_state=self._access.state_size,
|
||||
controller_state=self._controller.state_size)
|
||||
|
||||
def _clip_if_enabled(self, x):
|
||||
if self._clip_value > 0:
|
||||
return tf.clip_by_value(x, -self._clip_value, self._clip_value)
|
||||
else:
|
||||
return x
|
||||
|
||||
def _build(self, inputs, prev_state):
|
||||
"""Connects the DNC core into the graph.
|
||||
|
||||
Args:
|
||||
inputs: Tensor input.
|
||||
prev_state: A `DNCState` tuple containing the fields `access_output`,
|
||||
`access_state` and `controller_state`. `access_state` is a 3-D Tensor
|
||||
of shape `[batch_size, num_reads, word_size]` containing read words.
|
||||
`access_state` is a tuple of the access module's state, and
|
||||
`controller_state` is a tuple of controller module's state.
|
||||
|
||||
Returns:
|
||||
A tuple `(output, next_state)` where `output` is a tensor and `next_state`
|
||||
is a `DNCState` tuple containing the fields `access_output`,
|
||||
`access_state`, and `controller_state`.
|
||||
"""
|
||||
|
||||
prev_access_output = prev_state.access_output
|
||||
prev_access_state = prev_state.access_state
|
||||
prev_controller_state = prev_state.controller_state
|
||||
|
||||
batch_flatten = snt.BatchFlatten()
|
||||
controller_input = tf.concat(
|
||||
[batch_flatten(inputs), batch_flatten(prev_access_output)], 1)
|
||||
|
||||
controller_output, controller_state = self._controller(
|
||||
controller_input, prev_controller_state)
|
||||
|
||||
controller_output = self._clip_if_enabled(controller_output)
|
||||
controller_state = snt.nest.map(self._clip_if_enabled, controller_state)
|
||||
|
||||
access_output, access_state = self._access(controller_output,
|
||||
prev_access_state)
|
||||
|
||||
output = tf.concat([controller_output, batch_flatten(access_output)], 1)
|
||||
output = snt.Linear(
|
||||
output_size=self._output_size.as_list()[0],
|
||||
name='output_linear')(output)
|
||||
output = self._clip_if_enabled(output)
|
||||
|
||||
return output, DNCState(
|
||||
access_output=access_output,
|
||||
access_state=access_state,
|
||||
controller_state=controller_state)
|
||||
|
||||
def initial_state(self, batch_size, dtype=tf.float32):
|
||||
return DNCState(
|
||||
controller_state=self._controller.initial_state(batch_size, dtype),
|
||||
access_state=self._access.initial_state(batch_size, dtype),
|
||||
access_output=tf.zeros(
|
||||
[batch_size] + self._access.output_size.as_list(), dtype))
|
||||
|
||||
@property
|
||||
def state_size(self):
|
||||
return self._state_size
|
||||
|
||||
@property
|
||||
def output_size(self):
|
||||
return self._output_size
|
||||
@@ -0,0 +1,739 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"if not sys.warnoptions:\n",
|
||||
" warnings.simplefilter('ignore')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"from tqdm import tqdm\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2016-11-02</td>\n",
|
||||
" <td>778.200012</td>\n",
|
||||
" <td>781.650024</td>\n",
|
||||
" <td>763.450012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>768.700012</td>\n",
|
||||
" <td>1872400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2016-11-03</td>\n",
|
||||
" <td>767.250000</td>\n",
|
||||
" <td>769.950012</td>\n",
|
||||
" <td>759.030029</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>762.130005</td>\n",
|
||||
" <td>1943200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2016-11-04</td>\n",
|
||||
" <td>750.659973</td>\n",
|
||||
" <td>770.359985</td>\n",
|
||||
" <td>750.560974</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>762.020020</td>\n",
|
||||
" <td>2134800</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2016-11-07</td>\n",
|
||||
" <td>774.500000</td>\n",
|
||||
" <td>785.190002</td>\n",
|
||||
" <td>772.549988</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>782.520020</td>\n",
|
||||
" <td>1585100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2016-11-08</td>\n",
|
||||
" <td>783.400024</td>\n",
|
||||
" <td>795.632996</td>\n",
|
||||
" <td>780.190002</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>790.510010</td>\n",
|
||||
" <td>1350800</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2016-11-02 778.200012 781.650024 763.450012 768.700012 768.700012 \n",
|
||||
"1 2016-11-03 767.250000 769.950012 759.030029 762.130005 762.130005 \n",
|
||||
"2 2016-11-04 750.659973 770.359985 750.560974 762.020020 762.020020 \n",
|
||||
"3 2016-11-07 774.500000 785.190002 772.549988 782.520020 782.520020 \n",
|
||||
"4 2016-11-08 783.400024 795.632996 780.190002 790.510010 790.510010 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 1872400 \n",
|
||||
"1 1943200 \n",
|
||||
"2 2134800 \n",
|
||||
"3 1585100 \n",
|
||||
"4 1350800 "
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/GOOG-year.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.112708</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.090008</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.089628</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.160459</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.188066</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0\n",
|
||||
"0 0.112708\n",
|
||||
"1 0.090008\n",
|
||||
"2 0.089628\n",
|
||||
"3 0.160459\n",
|
||||
"4 0.188066"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 4:5].astype('float32')) # Close index\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Forecast\n",
|
||||
"\n",
|
||||
"This example is using model 1.lstm, if you want to use another model, need to tweak a little bit, but I believe it is not that hard.\n",
|
||||
"\n",
|
||||
"I want to forecast 30 days ahead! So just change `test_size` to forecast `t + N` ahead.\n",
|
||||
"\n",
|
||||
"Also, I want to simulate 10 times, 10 variances of forecasted patterns. Just change `simulation_size`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((252, 7), (252, 1))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"simulation_size = 10\n",
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 300\n",
|
||||
"dropout_rate = 0.8\n",
|
||||
"test_size = 30\n",
|
||||
"learning_rate = 0.01\n",
|
||||
"\n",
|
||||
"df_train = df_log\n",
|
||||
"df.shape, df_train.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" forget_bias = 0.1,\n",
|
||||
" ):\n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
"\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell(\n",
|
||||
" [lstm_cell(size_layer) for _ in range(num_layers)],\n",
|
||||
" state_is_tuple = False,\n",
|
||||
" )\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(\n",
|
||||
" rnn_cells, output_keep_prob = forget_bias\n",
|
||||
" )\n",
|
||||
" self.hidden_layer = tf.placeholder(\n",
|
||||
" tf.float32, (None, num_layers * 2 * size_layer)\n",
|
||||
" )\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(\n",
|
||||
" drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32\n",
|
||||
" )\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1], output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def forecast():\n",
|
||||
" tf.reset_default_graph()\n",
|
||||
" modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate\n",
|
||||
" )\n",
|
||||
" sess = tf.InteractiveSession()\n",
|
||||
" sess.run(tf.global_variables_initializer())\n",
|
||||
" date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"\n",
|
||||
" pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
" for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, last_state, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: batch_x,\n",
|
||||
" modelnn.Y: batch_y,\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" ) \n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))\n",
|
||||
" \n",
|
||||
" future_day = test_size\n",
|
||||
"\n",
|
||||
" output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
" output_predict[0] = df_train.iloc[0]\n",
|
||||
" upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"\n",
|
||||
" for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" ),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
" if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"\n",
|
||||
" init_value = last_state\n",
|
||||
" \n",
|
||||
" for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i]\n",
|
||||
" out_logits, last_state = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.last_state],\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(o, axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value,\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
" \n",
|
||||
" output_predict = minmax.inverse_transform(output_predict)\n",
|
||||
" deep_future = anchor(output_predict[:, 0], 0.4)\n",
|
||||
" \n",
|
||||
" return deep_future"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0818 12:00:52.795618 140214804277056 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:12: LSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
|
||||
"W0818 12:00:52.799092 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f8644897400>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n",
|
||||
"W0818 12:00:52.801252 140214804277056 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:16: MultiRNNCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"This class is equivalent as tf.keras.layers.StackedRNNCells, and will be replaced by that in Tensorflow 2.0.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 1\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"W0818 12:00:53.121960 140214804277056 lazy_loader.py:50] \n",
|
||||
"The TensorFlow contrib module will not be included in TensorFlow 2.0.\n",
|
||||
"For more information, please see:\n",
|
||||
" * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md\n",
|
||||
" * https://github.com/tensorflow/addons\n",
|
||||
" * https://github.com/tensorflow/io (for I/O related ops)\n",
|
||||
"If you depend on functionality not listed there, please file an issue.\n",
|
||||
"\n",
|
||||
"W0818 12:00:53.125179 140214804277056 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:27: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
|
||||
"W0818 12:00:53.314420 140214804277056 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0818 12:00:53.321002 140214804277056 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py:961: calling Zeros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0818 12:00:53.718872 140214804277056 deprecation.py:323] From <ipython-input-6-d01d21f09afe>:29: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"train loop: 100%|██████████| 300/300 [01:17<00:00, 3.90it/s, acc=95.9, cost=0.00437]\n",
|
||||
"W0818 12:02:12.766668 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f85be966eb8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 2\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:18<00:00, 3.81it/s, acc=96.2, cost=0.00386]\n",
|
||||
"W0818 12:03:31.524121 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f85b4c59dd8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 3\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:17<00:00, 3.86it/s, acc=95.9, cost=0.00421]\n",
|
||||
"W0818 12:04:49.292782 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f85ac67f5f8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 4\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:17<00:00, 3.85it/s, acc=95.1, cost=0.00617]\n",
|
||||
"W0818 12:06:07.690939 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f85209545f8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 5\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:18<00:00, 3.81it/s, acc=96.8, cost=0.00293]\n",
|
||||
"W0818 12:07:26.842436 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f85089d1128>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 6\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:17<00:00, 3.82it/s, acc=97.3, cost=0.00178]\n",
|
||||
"W0818 12:08:45.222193 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f85082c6160>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 7\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:16<00:00, 3.94it/s, acc=97.5, cost=0.00161]\n",
|
||||
"W0818 12:10:01.933482 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f84fc7de208>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 8\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:17<00:00, 3.81it/s, acc=97.5, cost=0.00156]\n",
|
||||
"W0818 12:11:20.348971 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f84fc7127b8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 9\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:18<00:00, 3.81it/s, acc=96.7, cost=0.00297]\n",
|
||||
"W0818 12:12:39.812369 140214804277056 rnn_cell_impl.py:893] <tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7f84f6ed44a8>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"simulation 10\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 300/300 [01:17<00:00, 3.98it/s, acc=97.5, cost=0.00179]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"results = []\n",
|
||||
"for i in range(simulation_size):\n",
|
||||
" print('simulation %d'%(i + 1))\n",
|
||||
" results.append(forecast())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30', '2017-12-01']"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"for i in range(test_size):\n",
|
||||
" date_ori.append(date_ori[-1] + timedelta(days = 1))\n",
|
||||
"date_ori = pd.Series(date_ori).dt.strftime(date_format = '%Y-%m-%d').tolist()\n",
|
||||
"date_ori[-5:]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Sanity check\n",
|
||||
"\n",
|
||||
"Some of our models might not have stable gradient, so forecasted trend might really hangwired. You can use many methods to filter out unstable models.\n",
|
||||
"\n",
|
||||
"This method is very simple,\n",
|
||||
"1. If one of element in forecasted trend lower than min(original trend).\n",
|
||||
"2. If one of element in forecasted trend bigger than max(original trend) * 2.\n",
|
||||
"\n",
|
||||
"If both are true, reject that trend."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"6"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accepted_results = []\n",
|
||||
"for r in results:\n",
|
||||
" if (np.array(r[-test_size:]) < np.min(df['Close'])).sum() == 0 and \\\n",
|
||||
" (np.array(r[-test_size:]) > np.max(df['Close']) * 2).sum() == 0:\n",
|
||||
" accepted_results.append(r)\n",
|
||||
"len(accepted_results)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3gAAAFBCAYAAAAlhA0CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzdeXhV1b3/8fcZEjIPhCHMg8AWGQREw6DUKkirLb22VkVBsWJ/WDQoSvUiUhD1ggoiUZHaoiiihV5RC2qpWuVSRUWiCOIOQ0KABBIykBxCcnLOPr8/9i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b7aawsICCgn20bt0Wn8/HzJkz6NevP4MGXXG2hi0iIiIi0mAVlVYAkBATHuJITk+jLfAG9zrxLNu5JDIy6qgjAZ588hnatGl7xNGysrIa+5g27RHuued+hgy5EsuyGDr0crxeL0lJzXj99aWkp3/DunVfMX9+GgsXLq6HUYiIiIiINB7FnkMFXsOawdMmK2dBjx692LYtgx07sgD44IMVdO1qEBUVfdzzBw8ewuLFi6o2XCkuLiYnZzdRUVH07NmbpUsPL6s8tETT4/HQqlVrAFaufA+v155SLioqory8nJSUgYwbdw8xMTHk5OwmOjqa8vJyfD5ffQ1bRERERKTBKvZUEB3hJjzMFepQTkujncE7lyQLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"accuracies = [calculate_accuracy(df['Close'].values, r[:-test_size]) for r in accepted_results]\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"for no, r in enumerate(accepted_results):\n",
|
||||
" plt.plot(r, label = 'forecast %d'%(no + 1))\n",
|
||||
"plt.plot(df['Close'], label = 'true trend', c = 'black')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('average accuracy: %.4f'%(np.mean(accuracies)))\n",
|
||||
"\n",
|
||||
"x_range_future = np.arange(len(results[0]))\n",
|
||||
"plt.xticks(x_range_future[::30], date_ori[::30])\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,746 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import pandas as pd\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta\n",
|
||||
"sns.set()\n",
|
||||
"tf.compat.v1.random.set_random_seed(1234)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>timestamp</th>\n",
|
||||
" <th>close</th>\n",
|
||||
" <th>positive</th>\n",
|
||||
" <th>negative</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2019-08-09T23:00:00</td>\n",
|
||||
" <td>11860.074544</td>\n",
|
||||
" <td>0.672896</td>\n",
|
||||
" <td>0.327104</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2019-08-09T23:20:00</td>\n",
|
||||
" <td>11872.025879</td>\n",
|
||||
" <td>0.595100</td>\n",
|
||||
" <td>0.404900</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2019-08-09T23:40:00</td>\n",
|
||||
" <td>11880.504557</td>\n",
|
||||
" <td>0.596702</td>\n",
|
||||
" <td>0.403298</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2019-08-10T00:00:00</td>\n",
|
||||
" <td>11918.873481</td>\n",
|
||||
" <td>0.577972</td>\n",
|
||||
" <td>0.422028</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2019-08-10T00:20:00</td>\n",
|
||||
" <td>11937.581272</td>\n",
|
||||
" <td>0.585342</td>\n",
|
||||
" <td>0.414658</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" timestamp close positive negative\n",
|
||||
"0 2019-08-09T23:00:00 11860.074544 0.672896 0.327104\n",
|
||||
"1 2019-08-09T23:20:00 11872.025879 0.595100 0.404900\n",
|
||||
"2 2019-08-09T23:40:00 11880.504557 0.596702 0.403298\n",
|
||||
"3 2019-08-10T00:00:00 11918.873481 0.577972 0.422028\n",
|
||||
"4 2019-08-10T00:20:00 11937.581272 0.585342 0.414658"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/BTC-sentiment.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## How we gather the data, provided by Bitcurate, bitcurate.com\n",
|
||||
"\n",
|
||||
"Because I don't have sentiment data related to stock market, so I will use crpytocurrency data, `BTC/USDT` from binance.\n",
|
||||
"\n",
|
||||
"1. close data came from CCXT, https://github.com/ccxt/ccxt, an open source cryptocurrency aggregator.\n",
|
||||
"2. We gather from streaming twitter, crawling hardcoded crpyocurrency telegram groups and Reddit. And we store in Elasticsearch as a single index. We trained 1/4 layers BERT MULTILANGUAGE (200MB-ish, originally 700MB-ish) released by Google on most-possible-found sentiment data on the internet, leveraging sentiment on multilanguages, eg, english, korea, japan. **Actually, it is very hard to found negative sentiment related to bitcoin / btc in large volume.**\n",
|
||||
"\n",
|
||||
"How we request using elasticsearch-dsl, https://elasticsearch-dsl.readthedocs.io,\n",
|
||||
"```python\n",
|
||||
"# from index name\n",
|
||||
"s = s.filter(\n",
|
||||
" 'query_string',\n",
|
||||
" default_field = 'text',\n",
|
||||
" query = 'bitcoin OR btc',\n",
|
||||
")\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"We only do text query only contain `bitcoin` or `btc`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Consensus introduction\n",
|
||||
"\n",
|
||||
"We have 2 questions here when saying about consensus, what happened,\n",
|
||||
"\n",
|
||||
"1. to future price if we assumed future sentiment is really positive, near to 1.0 . Eg, suddenly China want to adapt cryptocurrency and that can cause huge requested volumes.\n",
|
||||
"2. to future price if we assumed future sentiment is really negative, near to 1.0 . Eg, suddenly hackers broke binance or any exchanges, or any news that caused wreck by negative sentiment.\n",
|
||||
"\n",
|
||||
"**We can use deep-learning to simulate for us!**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count":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Ng6FhDPeMW1fu2dsxkoPUXvr1GWmri4tLOfpgJ9reZ/Gf9r4j5mJ01hsnrq2dDQzDYMuWzSW+vpX45Cc/xU9/+iO+970fsXChqZDZsuVF/H4/nZ2HWL/+TKfs6tUnsW/fa+zdu4cVK44nl8vywAP3ceGFpvXwzp07aGhooL19AQDR6E7C4Xrq6sKkUqVrT1VVufXWm+nv7+Md77hoOrvuMkdxheBjiB37h4inVdafWBps4ISlTfzL0iaeerWbm+7fwS8f2MFpSoR7ntzHmhUtfOBtVrQ8QwddnZBfqUgPm+Wp7hNsJAZI3fkVQm////AuO2XM+tRdG5FqGvEuKvUjEZk4Un0EyRtAbmxHj03cRHhCFAnw+Zfvx7vijKp9zz11G0aij5p3fnl621CEEAZajxmcTKSGJ+RPagx1Fq4fR+uldW4n//zdeBe+AU9k+RjtEOQ3/Q5kGU/7CXjaT5hxodno20vm/h8QeueX8S48cUbvNV0Iy+xKCAHCAI8fo38fUjiCd+kpgFSmCdb2v4jWue2oEoKFoYOulfnYavs2k330emov/y5yODJmHVp3FE9k+ZRSQgHkt/4FdaSL0OXVhWCtO4pt6ibSYwvBtguFMd1zxgRwNgYNDXXfC/hPOLfkvGNFkygLGlpeVyYOanba2zgR9MGDSL7guM/exWUm0Hp2k334Z9S8+6vIDfNLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1224x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from mpl_toolkits.axes_grid1 import host_subplot\n",
|
||||
"import mpl_toolkits.axisartist as AA\n",
|
||||
"\n",
|
||||
"close = df['close'].tolist()\n",
|
||||
"positive = df['positive'].tolist()\n",
|
||||
"negative = df['negative'].tolist()\n",
|
||||
"timestamp = df['timestamp'].tolist()\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (17, 5))\n",
|
||||
"host = host_subplot(111)\n",
|
||||
"plt.subplots_adjust(right = 0.75, top = 0.8)\n",
|
||||
"par1 = host.twinx()\n",
|
||||
"par2 = host.twinx()\n",
|
||||
"\n",
|
||||
"par2.spines['right'].set_position(('axes', 1.1))\n",
|
||||
"par2.spines['bottom'].set_position(('axes', 0.9))\n",
|
||||
"host.set_xlabel('timestamp')\n",
|
||||
"host.set_ylabel('BTC/USDT')\n",
|
||||
"par1.set_ylabel('positive')\n",
|
||||
"par2.set_ylabel('negative')\n",
|
||||
"\n",
|
||||
"host.plot(close, label = 'BTC/USDT')\n",
|
||||
"par1.plot(positive, label = 'positive')\n",
|
||||
"par2.plot(negative, label = 'negative')\n",
|
||||
"host.legend()\n",
|
||||
"plt.xticks(\n",
|
||||
" np.arange(len(timestamp))[::30], timestamp[::30], rotation = '45', ha = 'right'\n",
|
||||
" )\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>0</th>\n",
|
||||
" <th>1</th>\n",
|
||||
" <th>2</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.947020</td>\n",
|
||||
" <td>0.672896</td>\n",
|
||||
" <td>0.327104</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.955190</td>\n",
|
||||
" <td>0.595100</td>\n",
|
||||
" <td>0.404900</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.960986</td>\n",
|
||||
" <td>0.596702</td>\n",
|
||||
" <td>0.403298</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.987212</td>\n",
|
||||
" <td>0.577972</td>\n",
|
||||
" <td>0.422028</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>1.000000</td>\n",
|
||||
" <td>0.585342</td>\n",
|
||||
" <td>0.414658</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" 0 1 2\n",
|
||||
"0 0.947020 0.672896 0.327104\n",
|
||||
"1 0.955190 0.595100 0.404900\n",
|
||||
"2 0.960986 0.596702 0.403298\n",
|
||||
"3 0.987212 0.577972 0.422028\n",
|
||||
"4 1.000000 0.585342 0.414658"
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"minmax = MinMaxScaler().fit(df.iloc[:, 1:2].astype('float32'))\n",
|
||||
"df_log = minmax.transform(df.iloc[:, 1:2].astype('float32'))\n",
|
||||
"df_log = pd.DataFrame(df_log)\n",
|
||||
"df_log[1] = df['positive']\n",
|
||||
"df_log[2] = df['negative']\n",
|
||||
"df_log.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Model definition\n",
|
||||
"\n",
|
||||
"This example is using model 17.cnn-seq2seq, if you want to use another model, need to tweak a little bit, but I believe it is not that hard."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((339, 4), (309, 3), (30, 3))"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"size_layer = 128\n",
|
||||
"epoch = 200\n",
|
||||
"dropout_rate = 0.75\n",
|
||||
"test_size = 3 * 10 # timestamp every 20 minutes, and I want to test on last 12 hours\n",
|
||||
"learning_rate = 1e-3\n",
|
||||
"timestamp = test_size\n",
|
||||
"\n",
|
||||
"df_train = df_log.iloc[:-test_size]\n",
|
||||
"df_test = df_log.iloc[-test_size:]\n",
|
||||
"df.shape, df_train.shape, df_test.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def encoder_block(inp, n_hidden, filter_size):\n",
|
||||
" inp = tf.expand_dims(inp, 2)\n",
|
||||
" inp = tf.pad(\n",
|
||||
" inp,\n",
|
||||
" [\n",
|
||||
" [0, 0],\n",
|
||||
" [(filter_size[0] - 1) // 2, (filter_size[0] - 1) // 2],\n",
|
||||
" [0, 0],\n",
|
||||
" [0, 0],\n",
|
||||
" ],\n",
|
||||
" )\n",
|
||||
" conv = tf.layers.conv2d(\n",
|
||||
" inp, n_hidden, filter_size, padding = 'VALID', activation = None\n",
|
||||
" )\n",
|
||||
" conv = tf.squeeze(conv, 2)\n",
|
||||
" return conv\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def decoder_block(inp, n_hidden, filter_size):\n",
|
||||
" inp = tf.expand_dims(inp, 2)\n",
|
||||
" inp = tf.pad(inp, [[0, 0], [filter_size[0] - 1, 0], [0, 0], [0, 0]])\n",
|
||||
" conv = tf.layers.conv2d(\n",
|
||||
" inp, n_hidden, filter_size, padding = 'VALID', activation = None\n",
|
||||
" )\n",
|
||||
" conv = tf.squeeze(conv, 2)\n",
|
||||
" return conv\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def glu(x):\n",
|
||||
" return tf.multiply(\n",
|
||||
" x[:, :, : tf.shape(x)[2] // 2],\n",
|
||||
" tf.sigmoid(x[:, :, tf.shape(x)[2] // 2 :]),\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def layer(inp, conv_block, kernel_width, n_hidden, residual = None):\n",
|
||||
" z = conv_block(inp, n_hidden, (kernel_width, 1))\n",
|
||||
" return glu(z) + (residual if residual is not None else 0)\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(\n",
|
||||
" self,\n",
|
||||
" learning_rate,\n",
|
||||
" num_layers,\n",
|
||||
" size,\n",
|
||||
" size_layer,\n",
|
||||
" output_size,\n",
|
||||
" kernel_size = 3,\n",
|
||||
" n_attn_heads = 16,\n",
|
||||
" dropout = 0.9,\n",
|
||||
" ):\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, output_size))\n",
|
||||
"\n",
|
||||
" encoder_embedded = tf.layers.dense(self.X, size_layer)\n",
|
||||
"\n",
|
||||
" e = tf.identity(encoder_embedded)\n",
|
||||
" for i in range(num_layers):\n",
|
||||
" z = layer(\n",
|
||||
" encoder_embedded,\n",
|
||||
" encoder_block,\n",
|
||||
" kernel_size,\n",
|
||||
" size_layer * 2,\n",
|
||||
" encoder_embedded,\n",
|
||||
" )\n",
|
||||
" z = tf.nn.dropout(z, keep_prob = dropout)\n",
|
||||
" encoder_embedded = z\n",
|
||||
"\n",
|
||||
" encoder_output, output_memory = z, z + e\n",
|
||||
" g = tf.identity(encoder_embedded)\n",
|
||||
"\n",
|
||||
" for i in range(num_layers):\n",
|
||||
" attn_res = h = layer(\n",
|
||||
" encoder_embedded,\n",
|
||||
" decoder_block,\n",
|
||||
" kernel_size,\n",
|
||||
" size_layer * 2,\n",
|
||||
" residual = tf.zeros_like(encoder_embedded),\n",
|
||||
" )\n",
|
||||
" C = []\n",
|
||||
" for j in range(n_attn_heads):\n",
|
||||
" h_ = tf.layers.dense(h, size_layer // n_attn_heads)\n",
|
||||
" g_ = tf.layers.dense(g, size_layer // n_attn_heads)\n",
|
||||
" zu_ = tf.layers.dense(\n",
|
||||
" encoder_output, size_layer // n_attn_heads\n",
|
||||
" )\n",
|
||||
" ze_ = tf.layers.dense(output_memory, size_layer // n_attn_heads)\n",
|
||||
"\n",
|
||||
" d = tf.layers.dense(h_, size_layer // n_attn_heads) + g_\n",
|
||||
" dz = tf.matmul(d, tf.transpose(zu_, [0, 2, 1]))\n",
|
||||
" a = tf.nn.softmax(dz)\n",
|
||||
" c_ = tf.matmul(a, ze_)\n",
|
||||
" C.append(c_)\n",
|
||||
"\n",
|
||||
" c = tf.concat(C, 2)\n",
|
||||
" h = tf.layers.dense(attn_res + c, size_layer)\n",
|
||||
" h = tf.nn.dropout(h, keep_prob = dropout)\n",
|
||||
" encoder_embedded = h\n",
|
||||
"\n",
|
||||
" encoder_embedded = tf.sigmoid(encoder_embedded[-1])\n",
|
||||
" self.logits = tf.layers.dense(encoder_embedded, output_size)\n",
|
||||
" self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(\n",
|
||||
" self.cost\n",
|
||||
" )\n",
|
||||
" \n",
|
||||
"def calculate_accuracy(real, predict):\n",
|
||||
" real = np.array(real) + 1\n",
|
||||
" predict = np.array(predict) + 1\n",
|
||||
" percentage = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))\n",
|
||||
" return percentage * 100\n",
|
||||
"\n",
|
||||
"def anchor(signal, weight):\n",
|
||||
" buffer = []\n",
|
||||
" last = signal[0]\n",
|
||||
" for i in signal:\n",
|
||||
" smoothed_val = last * weight + (1 - weight) * i\n",
|
||||
" buffer.append(smoothed_val)\n",
|
||||
" last = smoothed_val\n",
|
||||
" return buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING: Logging before flag parsing goes to stderr.\n",
|
||||
"W0818 16:34:24.824237 140007582447424 deprecation.py:323] From <ipython-input-6-6c0655f4345e>:55: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use keras.layers.dense instead.\n",
|
||||
"W0818 16:34:24.831443 140007582447424 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Call initializer instance with the dtype argument instead of passing it to the constructor\n",
|
||||
"W0818 16:34:25.094202 140007582447424 deprecation.py:323] From <ipython-input-6-6c0655f4345e>:13: conv2d (from tensorflow.python.layers.convolutional) is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Use `tf.keras.layers.Conv2D` instead.\n",
|
||||
"W0818 16:34:25.236837 140007582447424 deprecation.py:506] From <ipython-input-6-6c0655f4345e>:66: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
|
||||
"Instructions for updating:\n",
|
||||
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"tf.reset_default_graph()\n",
|
||||
"modelnn = Model(\n",
|
||||
" learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], \n",
|
||||
" dropout = dropout_rate\n",
|
||||
")\n",
|
||||
"sess = tf.InteractiveSession()\n",
|
||||
"sess.run(tf.global_variables_initializer())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"train loop: 100%|██████████| 200/200 [00:40<00:00, 5.17it/s, acc=98, cost=0.000637] \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from tqdm import tqdm\n",
|
||||
"\n",
|
||||
"pbar = tqdm(range(epoch), desc = 'train loop')\n",
|
||||
"for i in pbar:\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss, total_acc = [], []\n",
|
||||
" for k in range(0, df_train.shape[0] - 1, timestamp):\n",
|
||||
" index = min(k + timestamp, df_train.shape[0] - 1)\n",
|
||||
" batch_x = np.expand_dims(\n",
|
||||
" df_train.iloc[k : index, :].values, axis = 0\n",
|
||||
" )\n",
|
||||
" batch_y = df_train.iloc[k + 1 : index + 1, :].values\n",
|
||||
" logits, _, loss = sess.run(\n",
|
||||
" [modelnn.logits, modelnn.optimizer, modelnn.cost],\n",
|
||||
" feed_dict = {modelnn.X: batch_x, modelnn.Y: batch_y},\n",
|
||||
" ) \n",
|
||||
" total_loss.append(loss)\n",
|
||||
" total_acc.append(calculate_accuracy(batch_y[:, 0], logits[:, 0]))\n",
|
||||
" pbar.set_postfix(cost = np.mean(total_loss), acc = np.mean(total_acc))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"future_day = test_size\n",
|
||||
"\n",
|
||||
"output_predict = np.zeros((df_train.shape[0] + future_day, df_train.shape[1]))\n",
|
||||
"output_predict[0] = df_train.iloc[0]\n",
|
||||
"upper_b = (df_train.shape[0] // timestamp) * timestamp\n",
|
||||
"\n",
|
||||
"for k in range(0, (df_train.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(\n",
|
||||
" df_train.iloc[k : k + timestamp], axis = 0\n",
|
||||
" )\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[k + 1 : k + timestamp + 1] = out_logits\n",
|
||||
"\n",
|
||||
"if upper_b != df_train.shape[0]:\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: np.expand_dims(df_train.iloc[upper_b:], axis = 0)\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[upper_b + 1 : df_train.shape[0] + 1] = out_logits\n",
|
||||
" future_day -= 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"output_predict_negative = output_predict.copy()\n",
|
||||
"output_predict_positive = output_predict.copy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for i in range(future_day):\n",
|
||||
" o = output_predict[-future_day - timestamp + i:-future_day + i].copy()\n",
|
||||
" o = np.expand_dims(o, axis = 0)\n",
|
||||
" \n",
|
||||
" o_negative = output_predict_negative[-future_day - timestamp + i:-future_day + i].copy()\n",
|
||||
" o_negative = np.expand_dims(o_negative, axis = 0)\n",
|
||||
" o_negative[:, :, 1] = 0.0\n",
|
||||
" o_negative[:, :, 2] = 1.0\n",
|
||||
" \n",
|
||||
" o_positive = output_predict_positive[-future_day - timestamp + i:-future_day + i].copy()\n",
|
||||
" o_positive = np.expand_dims(o_positive, axis = 0)\n",
|
||||
" o_positive[:, :, 1] = 1.0\n",
|
||||
" o_positive[:, :, 2] = 0.0\n",
|
||||
" \n",
|
||||
" # original without any consensus\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: o\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict[-future_day + i] = out_logits[-1]\n",
|
||||
" \n",
|
||||
" # negative consensus\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: o_negative\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict_negative[-future_day + i] = out_logits[-1]\n",
|
||||
" \n",
|
||||
" # positive consensus\n",
|
||||
" out_logits = sess.run(\n",
|
||||
" modelnn.logits,\n",
|
||||
" feed_dict = {\n",
|
||||
" modelnn.X: o_positive\n",
|
||||
" },\n",
|
||||
" )\n",
|
||||
" output_predict_positive[-future_day + i] = out_logits[-1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"output_predict_original = minmax.inverse_transform(output_predict[:,:1])\n",
|
||||
"output_predict_negative = minmax.inverse_transform(output_predict_negative[:,:1])\n",
|
||||
"output_predict_positive = minmax.inverse_transform(output_predict_positive[:,:1])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"deep_future = anchor(output_predict_original[:, 0], 0.7)\n",
|
||||
"deep_future_negative = anchor(output_predict_negative[:, 0], 0.7)\n",
|
||||
"deep_future_positive = anchor(output_predict_positive[:, 0], 0.7)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"((339, 4), 339)"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df.shape, len(deep_future_negative)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_train = minmax.inverse_transform(df_train)\n",
|
||||
"df_test = minmax.inverse_transform(df_test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4kAAAGFCAYAAABQc7YHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nOzde1zO5//A8dd93xU6IinlVA43E0vlkDYbY8yMmY0xTRszxwxj5pTmMOdThFFO5fybUznv8HWYIZmzm+QwDBWhbpXuu98f5U46CKnk/Xw89lh9rutzfd6f933n0bvr+ly3IjU1FSGEEEIIIYQQAkBZ2AEIIYQQQgghhCg6pEgUQgghhBBCCGEgRaIQQgghhBBCCAMpEoUQQgghhBBCGEiRKIQQQgghhBDCQIpEIYQQQgghhBAGRvk1kFqtngZ0BKoCdTUazUm1Wm0NrACqAcnAeeBbjUYTnX5OY2AhUAq4BHTTaDS3XqRNCCGEEEIIIcTzy7ciEdgIzAb2PnYsFZii0Wj+BFCr1VOBSUAPtVqtBIIBb41Gs0+tVo9Kb/v6edvyGGcJoAHwH6B7oTsWQgghhBBCiFePCqgAHAaSnmzMtyJRo9HsA1Cr1Y8fuw38+Vi3v4E+6V+7AYmPzgMWkDYr+PULtOVFAzIXskIIIYQQQgjxOnob2PfkwQJ7JjF9BrAPsDn9UG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vDOTWv++OQQk/320fRGWbaan+RB+o5hiYmJfPONN3PmzMDV1Z2ff55K+fK29Or1Fffu3WVjyGQ8tzZhmN/Lez5SCCGEEEKI14kUiUXIL7/8wpdfdqF+rUp08a7B7iqxdL5dlxp1G+V5DBMTE9zcGnDg+FXWjL/M4Y8OsbjMaEJ/uITHe59hblmaL76ZRL+7b7PTPpqpUz7NMkawJoDyCQqmDdvLJL+9DPlhTaYlrgBeDYZyryR0iuzLTdOHuEWbM8k8jN6j6nL80O5sY1u1xpdkI/i0yUAC+/5N22v2jH7QgWC/SByqpn1gbHvHrtwyS2XR3GHcu3eXzz//hNDQTYwb9zPLlq2ExBPYtoulWoVkRg3qxvc3J6BTwlqbs5yK+N8zZFsIIYQQQgiRHdndtIgYO7w9hxV/c90qiWuWepR6ePeGNbP7/IGtQ9VnGmvy5AnMnDmV8+evYGFhmW0fvU5HV9/q7LGNJcj+Z1p37Aek7WLaXtODPjGN8RuT/bOBkPZ84Mcj7LlXIpmZrVdRp/47jJjYjGW2JwGodseIVjp3ensHYFe5OgAf+lbgjnES+36MNhSdqamp+PvP4u7dOHr16ktpKwsazbanWrwFt/dVRqM5g7//Ajp27ET8vTg851flppkevRKMdVBOq6Di9vIc73CT967ZsnRy8XxeUXbly0zykUFykZnkI4PkIjPJRwbZ3TSz4rKDZX6QXGRWXPIhu5u+wu4k/0eSiZ7692357s677H33N1aPv/jMBSJA48ZN0Ov1HD58MMc+SpWKWd/+hsN9FT9EjuTyueMABP0xllIPodeXc3K9xqFDBzkQlMCpgIccOh6JcYkSTPX7i5311zLkbgtskkoRUO5vPH51pfeousyf9Q2HbRN4X++eqUD08xvN+PG++PvPpEGDugzw6Uvja5XYZ3eXMiZ3CAlZR8eOnQBYtLAv/1nomWU2kB8TPsL1lhmjK/gyac7/0eK8Ldsr3WSLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"timestamp = df['timestamp'].tolist()\n",
|
||||
"pad_test = np.pad(df_test[:,0], (df_train.shape[0], 0), 'constant', constant_values=np.nan)\n",
|
||||
"\n",
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"plt.plot(pad_test, label = 'test trend', c = 'blue')\n",
|
||||
"plt.plot(df_train[:,0], label = 'train trend', c = 'black')\n",
|
||||
"plt.plot(deep_future, label = 'forecast without consensus')\n",
|
||||
"plt.plot(deep_future_negative, label = 'forecast with negative consensus', c = 'red')\n",
|
||||
"plt.plot(deep_future_positive, label = 'forecast with positive consensus', c = 'green')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.xticks(\n",
|
||||
" np.arange(len(timestamp))[::30], timestamp[::30], rotation = '45', ha = 'right'\n",
|
||||
")\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## What we can observe\n",
|
||||
"\n",
|
||||
"1. The model learn, if positive and negative sentiments increasing, both will increase the price. That is why, using positive consensus or negative consensus caused price going up.\n",
|
||||
"2. Volatility of price is higher if negative sentiment is higher, still positive volatility.\n",
|
||||
"3. Momentum of price is higher if negative sentiment is higher, still positive momentum."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
# Copyright 2017 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""DNC util ops and modules."""
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
|
||||
def batch_invert_permutation(permutations):
|
||||
"""Returns batched `tf.invert_permutation` for every row in `permutations`."""
|
||||
with tf.name_scope('batch_invert_permutation', values=[permutations]):
|
||||
unpacked = tf.unstack(permutations)
|
||||
inverses = [tf.invert_permutation(permutation) for permutation in unpacked]
|
||||
return tf.stack(inverses)
|
||||
|
||||
|
||||
def batch_gather(values, indices):
|
||||
"""Returns batched `tf.gather` for every row in the input."""
|
||||
with tf.name_scope('batch_gather', values=[values, indices]):
|
||||
unpacked = zip(tf.unstack(values), tf.unstack(indices))
|
||||
result = [tf.gather(value, index) for value, index in unpacked]
|
||||
return tf.stack(result)
|
||||
|
||||
|
||||
def one_hot(length, index):
|
||||
"""Return an nd array of given `length` filled with 0s and a 1 at `index`."""
|
||||
result = np.zeros(length)
|
||||
result[index] = 1
|
||||
return result
|
||||
@@ -0,0 +1,708 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import time\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"import random\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"seaborn==0.9.0\n",
|
||||
"pandas==0.23.4\n",
|
||||
"numpy==1.14.5\n",
|
||||
"matplotlib==3.0.2\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pkg_resources\n",
|
||||
"import types\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_imports():\n",
|
||||
" for name, val in globals().items():\n",
|
||||
" if isinstance(val, types.ModuleType):\n",
|
||||
" name = val.__name__.split('.')[0]\n",
|
||||
" elif isinstance(val, type):\n",
|
||||
" name = val.__module__.split('.')[0]\n",
|
||||
" poorly_named_packages = {'PIL': 'Pillow', 'sklearn': 'scikit-learn'}\n",
|
||||
" if name in poorly_named_packages.keys():\n",
|
||||
" name = poorly_named_packages[name]\n",
|
||||
" yield name\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"imports = list(set(get_imports()))\n",
|
||||
"requirements = []\n",
|
||||
"for m in pkg_resources.working_set:\n",
|
||||
" if m.project_name in imports and m.project_name != 'pip':\n",
|
||||
" requirements.append((m.project_name, m.version))\n",
|
||||
"\n",
|
||||
"for r in requirements:\n",
|
||||
" print('{}=={}'.format(*r))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_state(data, t, n):\n",
|
||||
" d = t - n + 1\n",
|
||||
" block = data[d : t + 1] if d >= 0 else -d * [data[0]] + data[0 : t + 1]\n",
|
||||
" res = []\n",
|
||||
" for i in range(n - 1):\n",
|
||||
" res.append(block[i + 1] - block[i])\n",
|
||||
" return np.array([res])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"TSLA Time Period: **Mar 23, 2018 - Mar 23, 2019**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2018-03-23</td>\n",
|
||||
" <td>311.250000</td>\n",
|
||||
" <td>311.250000</td>\n",
|
||||
" <td>300.450012</td>\n",
|
||||
" <td>301.540009</td>\n",
|
||||
" <td>301.540009</td>\n",
|
||||
" <td>6654900</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2018-03-26</td>\n",
|
||||
" <td>307.339996</td>\n",
|
||||
" <td>307.589996</td>\n",
|
||||
" <td>291.359985</td>\n",
|
||||
" <td>304.179993</td>\n",
|
||||
" <td>304.179993</td>\n",
|
||||
" <td>8375200</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2018-03-27</td>\n",
|
||||
" <td>304.000000</td>\n",
|
||||
" <td>304.269989</td>\n",
|
||||
" <td>277.179993</td>\n",
|
||||
" <td>279.179993</td>\n",
|
||||
" <td>279.179993</td>\n",
|
||||
" <td>13872000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2018-03-28</td>\n",
|
||||
" <td>264.579987</td>\n",
|
||||
" <td>268.679993</td>\n",
|
||||
" <td>252.100006</td>\n",
|
||||
" <td>257.779999</td>\n",
|
||||
" <td>257.779999</td>\n",
|
||||
" <td>21001400</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2018-03-29</td>\n",
|
||||
" <td>256.489990</td>\n",
|
||||
" <td>270.959991</td>\n",
|
||||
" <td>248.210007</td>\n",
|
||||
" <td>266.130005</td>\n",
|
||||
" <td>266.130005</td>\n",
|
||||
" <td>15170700</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2018-03-23 311.250000 311.250000 300.450012 301.540009 301.540009 \n",
|
||||
"1 2018-03-26 307.339996 307.589996 291.359985 304.179993 304.179993 \n",
|
||||
"2 2018-03-27 304.000000 304.269989 277.179993 279.179993 279.179993 \n",
|
||||
"3 2018-03-28 264.579987 268.679993 252.100006 257.779999 257.779999 \n",
|
||||
"4 2018-03-29 256.489990 270.959991 248.210007 266.130005 266.130005 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 6654900 \n",
|
||||
"1 8375200 \n",
|
||||
"2 13872000 \n",
|
||||
"3 21001400 \n",
|
||||
"4 15170700 "
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('../dataset/TSLA.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"close = df.Close.values.tolist()\n",
|
||||
"window_size = 30\n",
|
||||
"skip = 1\n",
|
||||
"l = len(close) - 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Deep_Evolution_Strategy:\n",
|
||||
"\n",
|
||||
" inputs = None\n",
|
||||
"\n",
|
||||
" def __init__(\n",
|
||||
" self, weights, reward_function, population_size, sigma, learning_rate\n",
|
||||
" ):\n",
|
||||
" self.weights = weights\n",
|
||||
" self.reward_function = reward_function\n",
|
||||
" self.population_size = population_size\n",
|
||||
" self.sigma = sigma\n",
|
||||
" self.learning_rate = learning_rate\n",
|
||||
"\n",
|
||||
" def _get_weight_from_population(self, weights, population):\n",
|
||||
" weights_population = []\n",
|
||||
" for index, i in enumerate(population):\n",
|
||||
" jittered = self.sigma * i\n",
|
||||
" weights_population.append(weights[index] + jittered)\n",
|
||||
" return weights_population\n",
|
||||
"\n",
|
||||
" def get_weights(self):\n",
|
||||
" return self.weights\n",
|
||||
"\n",
|
||||
" def train(self, epoch = 100, print_every = 1):\n",
|
||||
" lasttime = time.time()\n",
|
||||
" for i in range(epoch):\n",
|
||||
" population = []\n",
|
||||
" rewards = np.zeros(self.population_size)\n",
|
||||
" for k in range(self.population_size):\n",
|
||||
" x = []\n",
|
||||
" for w in self.weights:\n",
|
||||
" x.append(np.random.randn(*w.shape))\n",
|
||||
" population.append(x)\n",
|
||||
" for k in range(self.population_size):\n",
|
||||
" weights_population = self._get_weight_from_population(\n",
|
||||
" self.weights, population[k]\n",
|
||||
" )\n",
|
||||
" rewards[k] = self.reward_function(weights_population)\n",
|
||||
" rewards = (rewards - np.mean(rewards)) / np.std(rewards)\n",
|
||||
" for index, w in enumerate(self.weights):\n",
|
||||
" A = np.array([p[index] for p in population])\n",
|
||||
" self.weights[index] = (\n",
|
||||
" w\n",
|
||||
" + self.learning_rate\n",
|
||||
" / (self.population_size * self.sigma)\n",
|
||||
" * np.dot(A.T, rewards).T\n",
|
||||
" )\n",
|
||||
" if (i + 1) % print_every == 0:\n",
|
||||
" print(\n",
|
||||
" 'iter %d. reward: %f'\n",
|
||||
" % (i + 1, self.reward_function(self.weights))\n",
|
||||
" )\n",
|
||||
" print('time taken to train:', time.time() - lasttime, 'seconds')\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Model:\n",
|
||||
" def __init__(self, input_size, layer_size, output_size):\n",
|
||||
" self.weights = [\n",
|
||||
" np.random.randn(input_size, layer_size),\n",
|
||||
" np.random.randn(layer_size, output_size),\n",
|
||||
" np.random.randn(layer_size, 1),\n",
|
||||
" np.random.randn(1, layer_size),\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" def predict(self, inputs):\n",
|
||||
" feed = np.dot(inputs, self.weights[0]) + self.weights[-1]\n",
|
||||
" decision = np.dot(feed, self.weights[1])\n",
|
||||
" buy = np.dot(feed, self.weights[2])\n",
|
||||
" return decision, buy\n",
|
||||
"\n",
|
||||
" def get_weights(self):\n",
|
||||
" return self.weights\n",
|
||||
"\n",
|
||||
" def set_weights(self, weights):\n",
|
||||
" self.weights = weights"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Agent:\n",
|
||||
"\n",
|
||||
" POPULATION_SIZE = 15\n",
|
||||
" SIGMA = 0.1\n",
|
||||
" LEARNING_RATE = 0.03\n",
|
||||
"\n",
|
||||
" def __init__(self, model, money, max_buy, max_sell):\n",
|
||||
" self.model = model\n",
|
||||
" self.initial_money = money\n",
|
||||
" self.max_buy = max_buy\n",
|
||||
" self.max_sell = max_sell\n",
|
||||
" self.es = Deep_Evolution_Strategy(\n",
|
||||
" self.model.get_weights(),\n",
|
||||
" self.get_reward,\n",
|
||||
" self.POPULATION_SIZE,\n",
|
||||
" self.SIGMA,\n",
|
||||
" self.LEARNING_RATE,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" def act(self, sequence):\n",
|
||||
" decision, buy = self.model.predict(np.array(sequence))\n",
|
||||
" return np.argmax(decision[0]), int(buy[0])\n",
|
||||
"\n",
|
||||
" def get_reward(self, weights):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" starting_money = initial_money\n",
|
||||
" self.model.weights = weights\n",
|
||||
" state = get_state(close, 0, window_size + 1)\n",
|
||||
" inventory = []\n",
|
||||
" quantity = 0\n",
|
||||
" for t in range(0, l, skip):\n",
|
||||
" action, buy = self.act(state)\n",
|
||||
" next_state = get_state(close, t + 1, window_size + 1)\n",
|
||||
" if action == 1 and initial_money >= close[t]:\n",
|
||||
" if buy < 0:\n",
|
||||
" buy = 1\n",
|
||||
" if buy > self.max_buy:\n",
|
||||
" buy_units = self.max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = buy\n",
|
||||
" total_buy = buy_units * close[t]\n",
|
||||
" initial_money -= total_buy\n",
|
||||
" inventory.append(total_buy)\n",
|
||||
" quantity += buy_units\n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" if quantity > self.max_sell:\n",
|
||||
" sell_units = self.max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = quantity\n",
|
||||
" quantity -= sell_units\n",
|
||||
" total_sell = sell_units * close[t]\n",
|
||||
" initial_money += total_sell\n",
|
||||
"\n",
|
||||
" state = next_state\n",
|
||||
" return ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
"\n",
|
||||
" def fit(self, iterations, checkpoint):\n",
|
||||
" self.es.train(iterations, print_every = checkpoint)\n",
|
||||
"\n",
|
||||
" def buy(self):\n",
|
||||
" initial_money = self.initial_money\n",
|
||||
" state = get_state(close, 0, window_size + 1)\n",
|
||||
" starting_money = initial_money\n",
|
||||
" states_sell = []\n",
|
||||
" states_buy = []\n",
|
||||
" inventory = []\n",
|
||||
" quantity = 0\n",
|
||||
" for t in range(0, l, skip):\n",
|
||||
" action, buy = self.act(state)\n",
|
||||
" next_state = get_state(close, t + 1, window_size + 1)\n",
|
||||
" if action == 1 and initial_money >= close[t]:\n",
|
||||
" if buy < 0:\n",
|
||||
" buy = 1\n",
|
||||
" if buy > self.max_buy:\n",
|
||||
" buy_units = self.max_buy\n",
|
||||
" else:\n",
|
||||
" buy_units = buy\n",
|
||||
" total_buy = buy_units * close[t]\n",
|
||||
" initial_money -= total_buy\n",
|
||||
" inventory.append(total_buy)\n",
|
||||
" quantity += buy_units\n",
|
||||
" states_buy.append(t)\n",
|
||||
" print(\n",
|
||||
" 'day %d: buy %d units at price %f, total balance %f'\n",
|
||||
" % (t, buy_units, total_buy, initial_money)\n",
|
||||
" )\n",
|
||||
" elif action == 2 and len(inventory) > 0:\n",
|
||||
" bought_price = inventory.pop(0)\n",
|
||||
" if quantity > self.max_sell:\n",
|
||||
" sell_units = self.max_sell\n",
|
||||
" else:\n",
|
||||
" sell_units = quantity\n",
|
||||
" if sell_units < 1:\n",
|
||||
" continue\n",
|
||||
" quantity -= sell_units\n",
|
||||
" total_sell = sell_units * close[t]\n",
|
||||
" initial_money += total_sell\n",
|
||||
" states_sell.append(t)\n",
|
||||
" try:\n",
|
||||
" invest = ((total_sell - bought_price) / bought_price) * 100\n",
|
||||
" except:\n",
|
||||
" invest = 0\n",
|
||||
" print(\n",
|
||||
" 'day %d, sell %d units at price %f, investment %f %%, total balance %f,'\n",
|
||||
" % (t, sell_units, total_sell, invest, initial_money)\n",
|
||||
" )\n",
|
||||
" state = next_state\n",
|
||||
"\n",
|
||||
" invest = ((initial_money - starting_money) / starting_money) * 100\n",
|
||||
" print(\n",
|
||||
" '\\ntotal gained %f, total investment %f %%'\n",
|
||||
" % (initial_money - starting_money, invest)\n",
|
||||
" )\n",
|
||||
" plt.figure(figsize = (20, 10))\n",
|
||||
" plt.plot(close, label = 'true close', c = 'g')\n",
|
||||
" plt.plot(\n",
|
||||
" close, 'X', label = 'predict buy', markevery = states_buy, c = 'b'\n",
|
||||
" )\n",
|
||||
" plt.plot(\n",
|
||||
" close, 'o', label = 'predict sell', markevery = states_sell, c = 'r'\n",
|
||||
" )\n",
|
||||
" plt.legend()\n",
|
||||
" plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"iter 10. reward: 22.809600\n",
|
||||
"iter 20. reward: 51.687003\n",
|
||||
"iter 30. reward: 61.576206\n",
|
||||
"iter 40. reward: 69.384603\n",
|
||||
"iter 50. reward: 74.372100\n",
|
||||
"iter 60. reward: 86.872802\n",
|
||||
"iter 70. reward: 95.984703\n",
|
||||
"iter 80. reward: 86.611603\n",
|
||||
"iter 90. reward: 91.603299\n",
|
||||
"iter 100. reward: 97.332000\n",
|
||||
"iter 110. reward: 97.179203\n",
|
||||
"iter 120. reward: 99.749703\n",
|
||||
"iter 130. reward: 100.879403\n",
|
||||
"iter 140. reward: 87.869305\n",
|
||||
"iter 150. reward: 95.844503\n",
|
||||
"iter 160. reward: 103.064303\n",
|
||||
"iter 170. reward: 108.591401\n",
|
||||
"iter 180. reward: 113.703303\n",
|
||||
"iter 190. reward: 109.320401\n",
|
||||
"iter 200. reward: 114.320704\n",
|
||||
"iter 210. reward: 118.800302\n",
|
||||
"iter 220. reward: 120.808302\n",
|
||||
"iter 230. reward: 116.255301\n",
|
||||
"iter 240. reward: 118.316202\n",
|
||||
"iter 250. reward: 118.671802\n",
|
||||
"iter 260. reward: 118.965402\n",
|
||||
"iter 270. reward: 118.079800\n",
|
||||
"iter 280. reward: 115.773998\n",
|
||||
"iter 290. reward: 109.795800\n",
|
||||
"iter 300. reward: 116.520801\n",
|
||||
"iter 310. reward: 119.137195\n",
|
||||
"iter 320. reward: 118.383199\n",
|
||||
"iter 330. reward: 114.609903\n",
|
||||
"iter 340. reward: 125.628802\n",
|
||||
"iter 350. reward: 121.527300\n",
|
||||
"iter 360. reward: 121.432399\n",
|
||||
"iter 370. reward: 118.581801\n",
|
||||
"iter 380. reward: 119.989300\n",
|
||||
"iter 390. reward: 120.004502\n",
|
||||
"iter 400. reward: 124.851201\n",
|
||||
"iter 410. reward: 122.869297\n",
|
||||
"iter 420. reward: 123.599999\n",
|
||||
"iter 430. reward: 126.341600\n",
|
||||
"iter 440. reward: 127.074699\n",
|
||||
"iter 450. reward: 128.540102\n",
|
||||
"iter 460. reward: 126.781902\n",
|
||||
"iter 470. reward: 128.691898\n",
|
||||
"iter 480. reward: 127.336802\n",
|
||||
"iter 490. reward: 127.829601\n",
|
||||
"iter 500. reward: 129.304401\n",
|
||||
"time taken to train: 36.91986346244812 seconds\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"model = Model(window_size, 500, 3)\n",
|
||||
"agent = Agent(model, 10000, 5, 5)\n",
|
||||
"agent.fit(500, 10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"day 2: buy 5 units at price 1395.899965, total balance 8604.100035\n",
|
||||
"day 3: buy 5 units at price 1288.899995, total balance 7315.200040\n",
|
||||
"day 4: buy 5 units at price 1330.650025, total balance 5984.550015\n",
|
||||
"day 5: buy 5 units at price 1262.399980, total balance 4722.150035\n",
|
||||
"day 6: buy 5 units at price 1337.649995, total balance 3384.500040\n",
|
||||
"day 8, sell 5 units at price 1528.600005, investment 9.506415 %, total balance 4913.100045,\n",
|
||||
"day 11, sell 5 units at price 1523.500060, investment 18.201572 %, total balance 6436.600105,\n",
|
||||
"day 12, sell 5 units at price 1504.649965, investment 13.076311 %, total balance 7941.250070,\n",
|
||||
"day 13: buy 5 units at price 1470.399935, total balance 6470.850135\n",
|
||||
"day 14, sell 5 units at price 1501.699980, investment 18.955957 %, total balance 7972.550115,\n",
|
||||
"day 15: buy 5 units at price 1456.049955, total balance 6516.500160\n",
|
||||
"day 16: buy 5 units at price 1438.450010, total balance 5078.050150\n",
|
||||
"day 18, sell 5 units at price 1500.399935, investment 12.166855 %, total balance 6578.450085,\n",
|
||||
"day 19: buy 5 units at price 1451.199950, total balance 5127.250135\n",
|
||||
"day 22: buy 5 units at price 1403.450010, total balance 3723.800125\n",
|
||||
"day 24, sell 5 units at price 1470.399935, investment 0.000000 %, total balance 5194.200060,\n",
|
||||
"day 25: buy 5 units at price 1469.499970, total balance 3724.700090\n",
|
||||
"day 26, sell 5 units at price 1499.600065, investment 2.990976 %, total balance 5224.300155,\n",
|
||||
"day 27, sell 5 units at price 1505.749970, investment 4.678644 %, total balance 6730.050125,\n",
|
||||
"day 28: buy 1 units at price 284.450012, total balance 6445.600113\n",
|
||||
"day 29: buy 1 units at price 294.089996, total balance 6151.510117\n",
|
||||
"day 30, sell 5 units at price 1513.849945, investment 4.317117 %, total balance 7665.360062,\n",
|
||||
"day 31: buy 5 units at price 1509.850005, total balance 6155.510057\n",
|
||||
"day 32, sell 5 units at price 1534.250030, investment 9.319892 %, total balance 7689.760087,\n",
|
||||
"day 34, sell 5 units at price 1505.299990, investment 2.436204 %, total balance 9195.060077,\n",
|
||||
"day 35: buy 5 units at price 1459.850005, total balance 7735.210072\n",
|
||||
"day 36: buy 5 units at price 1420.899965, total balance 6314.310107\n",
|
||||
"day 37, sell 5 units at price 1432.400055, investment 403.568288 %, total balance 7746.710162,\n",
|
||||
"day 38: buy 5 units at price 1422.700045, total balance 6324.010117\n",
|
||||
"day 39: buy 5 units at price 1384.100035, total balance 4939.910082\n",
|
||||
"day 41: buy 5 units at price 1375.050050, total balance 3564.860032\n",
|
||||
"day 42: buy 5 units at price 1395.350035, total balance 2169.509997\n",
|
||||
"day 44: buy 5 units at price 1394.250030, total balance 775.259967\n",
|
||||
"day 45: buy 5 units at price 1418.800050, total balance -643.540083\n",
|
||||
"day 49, sell 5 units at price 1483.699950, investment 404.505413 %, total balance 840.159867,\n",
|
||||
"day 50: buy 5 units at price 1455.650025, total balance -615.490158\n",
|
||||
"day 51, sell 5 units at price 1597.500000, investment 5.805212 %, total balance 982.009842,\n",
|
||||
"day 53: buy 5 units at price 1588.300020, total balance -606.290178\n",
|
||||
"day 54, sell 5 units at price 1660.500030, investment 13.744564 %, total balance 1054.209852,\n",
|
||||
"day 55, sell 5 units at price 1713.849945, investment 20.617214 %, total balance 2768.059797,\n",
|
||||
"day 56, sell 5 units at price 1723.899995, investment 21.171009 %, total balance 4491.959792,\n",
|
||||
"day 57, sell 5 units at price 1788.600005, investment 29.224764 %, total balance 6280.559797,\n",
|
||||
"day 58, sell 5 units at price 1790.850065, investment 30.238900 %, total balance 8071.409862,\n",
|
||||
"day 59, sell 5 units at price 1854.149935, investment 32.880631 %, total balance 9925.559797,\n",
|
||||
"day 61, sell 5 units at price 1811.100005, investment 29.897792 %, total balance 11736.659802,\n",
|
||||
"day 62, sell 5 units at price 1737.550050, investment 22.466168 %, total balance 13474.209852,\n",
|
||||
"day 63: buy 1 units at price 333.630005, total balance 13140.579847\n",
|
||||
"day 64, sell 3 units at price 999.030030, investment -31.368803 %, total balance 14139.609877,\n",
|
||||
"day 69: buy 1 units at price 335.070007, total balance 13804.539870\n",
|
||||
"day 70: buy 1 units at price 310.859985, total balance 13493.679885\n",
|
||||
"day 72: buy 5 units at price 1544.499970, total balance 11949.179915\n",
|
||||
"day 73, sell 5 units at price 1592.550050, investment 375.288751 %, total balance 13541.729965,\n",
|
||||
"day 75: buy 1 units at price 318.959991, total balance 13222.769974\n",
|
||||
"day 76: buy 5 units at price 1583.549955, total balance 11639.220019\n",
|
||||
"day 78: buy 5 units at price 1550.500030, total balance 10088.719989\n",
|
||||
"day 79: buy 1 units at price 322.690002, total balance 9766.029987\n",
|
||||
"day 82: buy 5 units at price 1567.899935, total balance 8198.130052\n",
|
||||
"day 83: buy 1 units at price 303.200012, total balance 7894.930040\n",
|
||||
"day 84: buy 5 units at price 1487.149965, total balance 6407.780075\n",
|
||||
"day 85: buy 1 units at price 308.739990, total balance 6099.040085\n",
|
||||
"day 86: buy 5 units at price 1533.249970, total balance 4565.790115\n",
|
||||
"day 87: buy 1 units at price 297.179993, total balance 4268.610122\n",
|
||||
"day 88: buy 5 units at price 1450.850065, total balance 2817.760057\n",
|
||||
"day 89: buy 5 units at price 1490.700075, total balance 1327.059982\n",
|
||||
"day 91, sell 5 units at price 1747.700045, investment 462.214543 %, total balance 3074.760027,\n",
|
||||
"day 92, sell 5 units at price 1740.850065, investment 12.712858 %, total balance 4815.610092,\n",
|
||||
"day 93: buy 1 units at price 341.989990, total balance 4473.620102\n",
|
||||
"day 94, sell 5 units at price 1897.850035, investment 495.011941 %, total balance 6371.470137,\n",
|
||||
"day 95, sell 5 units at price 1851.699980, investment 16.933474 %, total balance 8223.170117,\n",
|
||||
"day 96: buy 1 units at price 352.450012, total balance 7870.720105\n",
|
||||
"day 97, sell 5 units at price 1777.449950, investment 14.637208 %, total balance 9648.170055,\n",
|
||||
"day 98, sell 5 units at price 1782.050020, investment 452.248291 %, total balance 11430.220075,\n",
|
||||
"day 99, sell 5 units at price 1738.200075, investment 10.861671 %, total balance 13168.420150,\n",
|
||||
"day 101, sell 5 units at price 1677.250060, investment 453.182716 %, total balance 14845.670210,\n",
|
||||
"day 102: buy 5 units at price 1527.500000, total balance 13318.170210\n",
|
||||
"day 103: buy 1 units at price 308.440002, total balance 13009.730208\n",
|
||||
"day 104, sell 5 units at price 1609.499970, investment 8.227146 %, total balance 14619.230178,\n",
|
||||
"day 105, sell 5 units at price 1608.200075, investment 420.891406 %, total balance 16227.430253,\n",
|
||||
"day 118: buy 5 units at price 1397.200010, total balance 14830.230243\n",
|
||||
"day 119: buy 5 units at price 1452.700045, total balance 13377.530198\n",
|
||||
"day 121: buy 5 units at price 1476.000060, total balance 11901.530138\n",
|
||||
"day 122: buy 5 units at price 1474.199980, total balance 10427.330158\n",
|
||||
"day 124, sell 5 units at price 1495.099945, investment 7.006866 %, total balance 11922.430103,\n",
|
||||
"day 125, sell 5 units at price 1491.649935, investment 2.681207 %, total balance 13414.080038,\n",
|
||||
"day 126: buy 1 units at price 299.100006, total balance 13114.980032\n",
|
||||
"day 127, sell 5 units at price 1498.399965, investment 1.517609 %, total balance 14613.379997,\n",
|
||||
"day 128: buy 5 units at price 1504.949950, total balance 13108.430047\n",
|
||||
"day 129, sell 5 units at price 1547.899935, investment 4.999319 %, total balance 14656.329982,\n",
|
||||
"day 131: buy 5 units at price 1323.849945, total balance 13332.480037\n",
|
||||
"day 132, sell 5 units at price 1553.500060, investment 419.391517 %, total balance 14885.980097,\n",
|
||||
"day 135: buy 1 units at price 281.829987, total balance 14604.150110\n",
|
||||
"day 136: buy 5 units at price 1309.750060, total balance 13294.400050\n",
|
||||
"day 137: buy 5 units at price 1252.799990, total balance 12041.600060\n",
|
||||
"day 139: buy 5 units at price 1284.400025, total balance 10757.200035\n",
|
||||
"day 140: buy 5 units at price 1261.149980, total balance 9496.050055\n",
|
||||
"day 142: buy 5 units at price 1297.949980, total balance 8198.100075\n",
|
||||
"day 143: buy 1 units at price 276.589996, total balance 7921.510079\n",
|
||||
"day 145: buy 5 units at price 1319.550020, total balance 6601.960059\n",
|
||||
"day 146: buy 1 units at price 260.000000, total balance 6341.960059\n",
|
||||
"day 147: buy 5 units at price 1304.750060, total balance 5037.209999\n",
|
||||
"day 148, sell 5 units at price 1470.700075, investment -2.275815 %, total balance 6507.910074,\n",
|
||||
"day 150: buy 5 units at price 1574.299925, total balance 4933.610149\n",
|
||||
"day 151, sell 5 units at price 1654.499970, investment 24.976398 %, total balance 6588.110119,\n",
|
||||
"day 153: buy 5 units at price 1649.499970, total balance 4938.610149\n",
|
||||
"day 155, sell 5 units at price 1721.399995, investment 510.793767 %, total balance 6660.010144,\n",
|
||||
"day 156: buy 1 units at price 346.410004, total balance 6313.600140\n",
|
||||
"day 157, sell 5 units at price 1706.999970, investment 30.330207 %, total balance 8020.600110,\n",
|
||||
"day 158, sell 5 units at price 1705.299990, investment 36.119094 %, total balance 9725.900100,\n",
|
||||
"day 159: buy 1 units at price 348.160004, total balance 9377.740096\n",
|
||||
"day 160, sell 5 units at price 1756.999970, investment 36.795386 %, total balance 11134.740066,\n",
|
||||
"day 162: buy 1 units at price 331.279999, total balance 10803.460067\n",
|
||||
"day 164, sell 5 units at price 1720.000000, investment 36.383462 %, total balance 12523.460067,\n",
|
||||
"day 166, sell 5 units at price 1771.549990, investment 36.488310 %, total balance 14295.010057,\n",
|
||||
"day 167, sell 5 units at price 1767.350005, investment 538.978282 %, total balance 16062.360062,\n",
|
||||
"day 168: buy 1 units at price 347.489990, total balance 15714.870072\n",
|
||||
"day 169: buy 1 units at price 338.190002, total balance 15376.680070\n",
|
||||
"day 170: buy 1 units at price 325.829987, total balance 15050.850083\n",
|
||||
"day 171, sell 5 units at price 1730.000000, investment 31.105299 %, total balance 16780.850083,\n",
|
||||
"day 173, sell 5 units at price 1739.349975, investment 568.980760 %, total balance 18520.200058,\n",
|
||||
"day 175: buy 5 units at price 1752.400055, total balance 16767.800003\n",
|
||||
"day 178, sell 5 units at price 1815.299990, investment 39.130094 %, total balance 18583.099993,\n",
|
||||
"day 179, sell 5 units at price 1789.850005, investment 13.691805 %, total balance 20372.949998,\n",
|
||||
"day 189: buy 1 units at price 319.769989, total balance 20053.180009\n",
|
||||
"day 190: buy 5 units at price 1476.950075, total balance 18576.229934\n",
|
||||
"day 191, sell 5 units at price 1630.449980, investment 409.882114 %, total balance 20206.679914,\n",
|
||||
"day 195: buy 1 units at price 310.119995, total balance 19896.559919\n",
|
||||
"day 196: buy 5 units at price 1501.799925, total balance 18394.759994\n",
|
||||
"day 197, sell 5 units at price 1588.450010, investment 7.549337 %, total balance 19983.210004,\n",
|
||||
"day 198, sell 2 units at price 669.919982, investment 116.019603 %, total balance 20653.129986,\n",
|
||||
"day 202: buy 1 units at price 347.260010, total balance 20305.869976\n",
|
||||
"day 205, sell 1 units at price 346.049988, investment -0.348448 %, total balance 20651.919964,\n",
|
||||
"day 207: buy 1 units at price 302.260010, total balance 20349.659954\n",
|
||||
"day 208, sell 1 units at price 298.920013, investment -1.105008 %, total balance 20648.579967,\n",
|
||||
"day 209: buy 5 units at price 1437.949980, total balance 19210.629987\n",
|
||||
"day 210: buy 5 units at price 1457.550050, total balance 17753.079937\n",
|
||||
"day 212: buy 5 units at price 1481.900025, total balance 16271.179912\n",
|
||||
"day 213: buy 1 units at price 297.459991, total balance 15973.719921\n",
|
||||
"day 215: buy 1 units at price 307.019989, total balance 15666.699932\n",
|
||||
"day 216, sell 5 units at price 1561.049955, investment 8.560797 %, total balance 17227.749887,\n",
|
||||
"day 217, sell 5 units at price 1564.450075, investment 7.334227 %, total balance 18792.199962,\n",
|
||||
"day 218, sell 5 units at price 1606.750030, investment 8.424995 %, total balance 20398.949992,\n",
|
||||
"day 219, sell 2 units at price 634.440002, investment 113.285827 %, total balance 21033.389994,\n",
|
||||
"day 220: buy 1 units at price 307.510010, total balance 20725.879984\n",
|
||||
"day 221: buy 5 units at price 1528.999940, total balance 19196.880044\n",
|
||||
"day 222: buy 1 units at price 312.839996, total balance 18884.040048\n",
|
||||
"day 224: buy 1 units at price 308.170013, total balance 18575.870035\n",
|
||||
"day 225, sell 5 units at price 1518.849945, investment 394.707185 %, total balance 20094.719980,\n",
|
||||
"day 229: buy 5 units at price 1456.150055, total balance 18638.569925\n",
|
||||
"day 230, sell 5 units at price 1473.549955, investment 379.187638 %, total balance 20112.119880,\n",
|
||||
"day 232: buy 1 units at price 297.859985, total balance 19814.259895\n",
|
||||
"day 233, sell 4 units at price 1258.959960, investment -17.661216 %, total balance 21073.219855,\n",
|
||||
"day 245: buy 5 units at price 1377.149965, total balance 19696.069890\n",
|
||||
"day 246: buy 1 units at price 269.489990, total balance 19426.579900\n",
|
||||
"day 247, sell 5 units at price 1337.350005, investment -2.890024 %, total balance 20763.929905,\n",
|
||||
"day 248, sell 1 units at price 273.600006, investment 1.525109 %, total balance 21037.529911,\n",
|
||||
"\n",
|
||||
"total gained 11037.529911, total investment 110.375299 %\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAABI4AAAJHCAYAAAD2XB3fAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xm8XHV9//HXmTmzz92X7AsgDCJLQJYQUayVgi1WTKpFNBgQFFupC1Z/rbXWpdW6AoqVyhKD0LokLBasYmvVEEKNSxQahiWQhJub3H2Zfc6c8/tjltybO/feucnczL2Z9/Px4EHumTPnfHMzZ5b3fL6fr+E4DiIiIiIiIiIiIodz1XoAIiIiIiIiIiIyNyk4EhERERERERGRshQciYiIiIiIiIhIWQqORERERERERESkLAVHIiIiIiIiIiJSloIjEREREREREREpS8GRiIiIiIiIiIiUpeBIRERERERERETKUnAkIiIiIiIiIiJlKTgSEREREREREZGyFByJiIiIiIiIiEhZZq0HMAkfcB7QDeRqPBYRERERERERkeOBG1gE/BJIV3KHuRocnQf8otaDEBERERERERE5Dr0a2FrJjnM1OOoGGByMY9tOrcdyVNrawvT3x2o9DJE5T9eKSGV0rYhURteKSGV0rYhU5ni5Vlwug5aWEBRyl0rM1eAoB2DbzrwPjoDj4u8gcizoWhGpjK4VkcroWhGpjK4VkcocZ9dKxW2B1BxbRERERERERETKUnAkIiIiIiIiIiJlzdWpai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diOw649A1Pca2Yuv/xNACxfvpJTTonw1FO/L932hjdcXvrz1q0/Z8eO/+Xaa9/Bhg1XsWXLdzlwYD8Av/nNjtJxmptbuPji15U912OP/YIrrlhHMBgCoKmpueJxnnnmWSxfvhKAN77xCn71qx3T3mft2rfwyCM/wLIsfvzjRzj//NWlUGw2qOJIRESkCizbKv15IK3gSERERGQyxalqkK80sh2HXM5hx9O9rL/01Fk/fyAQLP3ZcRze+c5rSwHRXOF2u3GcQ8FaJpMp/XnBgoWceurL2br1Z2zZ8j0+8pGPzepYVHEkIiJSBWODo8HUcA1HIiIiIjK3ffjKVVy8ajENAQ/XX34arzlzEQ0BDzdccXrVzvHwww8BsG/fXp59NsorXnFG2f0uuug13H//9xkZGQHyAc2zzz4DwDnnnMcjj/wAgOHhIX7+85+WPcarXvVqHnhgM4lEvLQvQCgUJh6PTTnO3/9+J/v27S2N+ZWvPBeApUuXEY0+jW3bJBJxtm37xbj7rVv359x665cxTZPTTz9z6l/GUVLFkYiISBWMC47SgzUciYiIiMjc1hT2sf7SSKmn0bmndla90iiXy3HNNVeRSqX467/+20mncl122Z8wPDzEjTe+GwDbtnnzm9/CySefwoYN1/HZz36Sq65aR1tbO6tWnT3pMXp7e3j3u6/BNE0CgQC33fZNTjrpZSxfvoL169/KihUry/YuOuOMs7jttpt56aV9pebYABdf/Dr+678e5e1v/zMWLFhIJPLycfc7++xX4vV6efOb33I0v6aKGI7jzPpJjsBK4IX+/hi2PSfHV7GOjgZ6e0drPQyROU/Xisx3PYk+Prk9/2ZLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1440x720 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"agent.buy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,807 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"from datetime import datetime\n",
|
||||
"from datetime import timedelta"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style>\n",
|
||||
" .dataframe thead tr:only-child th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: left;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\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>Cami Dresses</th>\n",
|
||||
" <th>Shirts</th>\n",
|
||||
" <th>Tote Bags</th>\n",
|
||||
" <th>Sneakers</th>\n",
|
||||
" <th>Crop Tops</th>\n",
|
||||
" <th>Polos</th>\n",
|
||||
" <th>Cross Body Bags</th>\n",
|
||||
" <th>Casual Jackets</th>\n",
|
||||
" <th>Swimwear Bottoms</th>\n",
|
||||
" <th>...</th>\n",
|
||||
" <th>Heels</th>\n",
|
||||
" <th>T-Shirts</th>\n",
|
||||
" <th>Activewear Tops & T-Shirts</th>\n",
|
||||
" <th>Watches & Timepieces</th>\n",
|
||||
" <th>Wallets & Card Holders</th>\n",
|
||||
" <th>Bodycon Dresses</th>\n",
|
||||
" <th>Beauty Tools & Accessories</th>\n",
|
||||
" <th>Skinny Jeans</th>\n",
|
||||
" <th>Beauty Eyes</th>\n",
|
||||
" <th>Beauty Face</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2017-08-04</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2017-08-07</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2017-08-10</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2017-08-13</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2017-08-16</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>1.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>5 rows × 46 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" date Cami Dresses Shirts Tote Bags Sneakers Crop Tops Polos \\\n",
|
||||
"0 2017-08-04 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"1 2017-08-07 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"2 2017-08-10 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"3 2017-08-13 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"4 2017-08-16 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" Cross Body Bags Casual Jackets Swimwear Bottoms ... Heels \\\n",
|
||||
"0 0.0 0.0 0.0 ... 1.0 \n",
|
||||
"1 0.0 0.0 0.0 ... 1.0 \n",
|
||||
"2 0.0 0.0 0.0 ... 1.0 \n",
|
||||
"3 0.0 0.0 0.0 ... 1.0 \n",
|
||||
"4 0.0 0.0 0.0 ... 1.0 \n",
|
||||
"\n",
|
||||
" T-Shirts Activewear Tops & T-Shirts Watches & Timepieces \\\n",
|
||||
"0 0.0 0.0 0.0 \n",
|
||||
"1 0.0 0.0 0.0 \n",
|
||||
"2 0.0 0.0 0.0 \n",
|
||||
"3 0.0 0.0 0.0 \n",
|
||||
"4 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" Wallets & Card Holders Bodycon Dresses Beauty Tools & Accessories \\\n",
|
||||
"0 0.0 0.0 0.0 \n",
|
||||
"1 0.0 0.0 0.0 \n",
|
||||
"2 0.0 0.0 0.0 \n",
|
||||
"3 0.0 0.0 0.0 \n",
|
||||
"4 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" Skinny Jeans Beauty Eyes Beauty Face \n",
|
||||
"0 0.0 0.0 0.0 \n",
|
||||
"1 0.0 0.0 0.0 \n",
|
||||
"2 0.0 0.0 0.0 \n",
|
||||
"3 0.0 0.0 0.0 \n",
|
||||
"4 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
"[5 rows x 46 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('fashion.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"date_ori = pd.to_datetime(df.iloc[:, 0]).tolist()\n",
|
||||
"df = df.iloc[:,1:]\n",
|
||||
"df_copy = df.copy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"num_layers = 1\n",
|
||||
"learning_rate = 0.01\n",
|
||||
"size_layer = 128\n",
|
||||
"timestamp = 5\n",
|
||||
"epoch = 500\n",
|
||||
"dropout_rate = 0.7\n",
|
||||
"future_weeks = 30"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Model:\n",
|
||||
" def __init__(self, learning_rate, num_layers, \n",
|
||||
" size, size_layer, forget_bias = 0.8):\n",
|
||||
" \n",
|
||||
" def lstm_cell(size_layer):\n",
|
||||
" return tf.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)\n",
|
||||
" rnn_cells = tf.nn.rnn_cell.MultiRNNCell([lstm_cell(size_layer) for _ in range(num_layers)], \n",
|
||||
" state_is_tuple = False)\n",
|
||||
" self.X = tf.placeholder(tf.float32, (None, None, size))\n",
|
||||
" self.Y = tf.placeholder(tf.float32, (None, size))\n",
|
||||
" drop = tf.contrib.rnn.DropoutWrapper(rnn_cells, output_keep_prob = forget_bias)\n",
|
||||
" self.hidden_layer = tf.placeholder(tf.float32, \n",
|
||||
" (None, num_layers * 2 * size_layer))\n",
|
||||
" self.outputs, self.last_state = tf.nn.dynamic_rnn(drop, self.X, \n",
|
||||
" initial_state = self.hidden_layer, \n",
|
||||
" dtype = tf.float32)\n",
|
||||
" self.logits = tf.layers.dense(self.outputs[-1],size,\n",
|
||||
" kernel_initializer=tf.glorot_uniform_initializer())\n",
|
||||
" self.cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.Y,logits=self.logits))\n",
|
||||
" self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x7feb0aebbfd0>: Using a concatenated state is slower and will soon be deprecated. Use state_is_tuple=True.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"tf.reset_default_graph()\n",
|
||||
"modelnn = Model(learning_rate, num_layers, df.shape[1], size_layer, dropout_rate)\n",
|
||||
"sess = tf.InteractiveSession()\n",
|
||||
"sess.run(tf.global_variables_initializer())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"epoch: 100 avg loss: 0.032256167317772734\n",
|
||||
"epoch: 200 avg loss: 0.01611048075348412\n",
|
||||
"epoch: 300 avg loss: 0.010450065255883663\n",
|
||||
"epoch: 400 avg loss: 0.010217295865004417\n",
|
||||
"epoch: 500 avg loss: 0.009890825056635518\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for i in range(epoch):\n",
|
||||
" init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
" total_loss = 0\n",
|
||||
" for k in range(0, (df.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" batch_x = np.expand_dims(df.iloc[k: k + timestamp].values, axis = 0)\n",
|
||||
" batch_y = df.iloc[k + 1: k + timestamp + 1].values\n",
|
||||
" last_state, _, loss = sess.run([modelnn.last_state, \n",
|
||||
" modelnn.optimizer, \n",
|
||||
" modelnn.cost], feed_dict={modelnn.X: batch_x, \n",
|
||||
" modelnn.Y: batch_y, \n",
|
||||
" modelnn.hidden_layer: init_value})\n",
|
||||
" init_value = last_state\n",
|
||||
" total_loss += loss\n",
|
||||
" total_loss /= (df.shape[0] // timestamp)\n",
|
||||
" if (i + 1) % 100 == 0:\n",
|
||||
" print('epoch:', i + 1, 'avg loss:', total_loss)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"output_predict = np.zeros((df.shape[0] + future_weeks, df.shape[1]))\n",
|
||||
"output_predict[0, :] = df.iloc[0] \n",
|
||||
"upper_b = (df.shape[0] // timestamp) * timestamp\n",
|
||||
"init_value = np.zeros((1, num_layers * 2 * size_layer))\n",
|
||||
"for k in range(0, (df.shape[0] // timestamp) * timestamp, timestamp):\n",
|
||||
" out_logits, last_state = sess.run([tf.nn.sigmoid(modelnn.logits), modelnn.last_state], \n",
|
||||
" feed_dict = {modelnn.X:np.expand_dims(df.iloc[k: k + timestamp], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value})\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[k + 1: k + timestamp + 1] = out_logits\n",
|
||||
" \n",
|
||||
"out_logits, last_state = sess.run([tf.nn.sigmoid(modelnn.logits), modelnn.last_state], \n",
|
||||
" feed_dict = {modelnn.X:np.expand_dims(df.iloc[upper_b:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value})\n",
|
||||
"init_value = last_state\n",
|
||||
"output_predict[upper_b + 1: df.shape[0] + 1] = out_logits\n",
|
||||
"df.loc[df.shape[0]] = out_logits[-1]\n",
|
||||
"date_ori.append(date_ori[-1]+timedelta(days=3))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for i in range(future_weeks - 1):\n",
|
||||
" out_logits, last_state = sess.run([tf.nn.sigmoid(modelnn.logits), modelnn.last_state], feed_dict = \n",
|
||||
" {modelnn.X:np.expand_dims(df.iloc[-timestamp:], axis = 0),\n",
|
||||
" modelnn.hidden_layer: init_value})\n",
|
||||
" init_value = last_state\n",
|
||||
" output_predict[df.shape[0], :] = out_logits[-1, :]\n",
|
||||
" df.loc[df.shape[0]] = out_logits[-1, :]\n",
|
||||
" date_ori.append(date_ori[-1]+timedelta(days=3))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"date_ori=pd.Series(date_ori).dt.strftime(date_format='%Y-%m-%d').tolist()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([35, 21, 34, 10, 3, 33])"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"index = (-np.round(df.values).sum(axis=0)).argsort()[4:10]\n",
|
||||
"index"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA3AAAANOCAYAAABQmu4KAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XmcXfP9x/HXXWZNJsmQQRYk2vRbJCJFglJVDYkSlBYpFT+0P61dqViTELtWqK0kRO1avzZtU8RWSi2xpsHXkpIVE7LPdrffH+ecm5vJnZk7M+fee2bm/Xw88sjcs37v5567fM53C6VSKURERERERCT4wsUugIiIiIiIiORGCZyIiIiIiEgXoQRORERERESki1ACJyIiIiIi0kUogRMREREREekiosUuQHO1tesCOSxmdXUlq1bVFbsY3Y7i6j/FND8UV/8ppvmhuPpPMc0PxdV/imn3UVNTFW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vglUA//OWFZtjClz/+4PfBt4t/m+IiJSPGXuKJTxpAYxERERCbo2EzhrbRw4DXgCeA94xFq70BgzzRiTOSXAMcBD1trMJpA7AvONMW8Dz+L0gVMLine truncated
|
||||
"text/plain": [
|
||||
"<matplotlib.figure.Figure at 0x7feaa86c4550>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15,15))\n",
|
||||
"for no, i in enumerate(index):\n",
|
||||
" plt.subplot(6,1,no+1)\n",
|
||||
" label = list(df)[i]\n",
|
||||
" plt.plot(np.around(df.iloc[:,i]),label='predicted ' + label,alpha=0.7)\n",
|
||||
" plt.plot(np.around(df_copy.iloc[:,i]),label='real ' + label,alpha=0.7)\n",
|
||||
" plt.legend()\n",
|
||||
" x_range_future = np.arange(df.shape[0])\n",
|
||||
" plt.xticks(x_range_future[::20], date_ori[::20])\n",
|
||||
"plt.show()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def df_shift(df,lag=0,rejected_columns = []):\n",
|
||||
" df = df.copy()\n",
|
||||
" if not lag:\n",
|
||||
" return df\n",
|
||||
" cols ={}\n",
|
||||
" for i in range(1,lag+1):\n",
|
||||
" for x in list(df.columns):\n",
|
||||
" if x not in rejected_columns:\n",
|
||||
" if not x in cols:\n",
|
||||
" cols[x] = ['{}_{}'.format(x, i)]\n",
|
||||
" else:\n",
|
||||
" cols[x].append('{}_{}'.format(x, i))\n",
|
||||
" for k,v in cols.items():\n",
|
||||
" columns = v\n",
|
||||
" dfn = pd.DataFrame(data=None, columns=columns, index=df.index) \n",
|
||||
" i = 1\n",
|
||||
" for c in columns:\n",
|
||||
" dfn[c] = df[k].shift(periods=i)\n",
|
||||
" i+=1\n",
|
||||
" df = pd.concat([df, dfn], axis=1, join_axes=[df.index])\n",
|
||||
" return df"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(179, 135)"
|
||||
]
|
||||
},
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df_new = df_shift(df, 2)\n",
|
||||
"df_new.shape"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style>\n",
|
||||
" .dataframe thead tr:only-child th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: left;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>Cami Dresses</th>\n",
|
||||
" <th>Shirts</th>\n",
|
||||
" <th>Tote Bags</th>\n",
|
||||
" <th>Sneakers</th>\n",
|
||||
" <th>Crop Tops</th>\n",
|
||||
" <th>Polos</th>\n",
|
||||
" <th>Cross Body Bags</th>\n",
|
||||
" <th>Casual Jackets</th>\n",
|
||||
" <th>Swimwear Bottoms</th>\n",
|
||||
" <th>Scarves</th>\n",
|
||||
" <th>...</th>\n",
|
||||
" <th>Scarves_1</th>\n",
|
||||
" <th>Scarves_2</th>\n",
|
||||
" <th>Beauty Eyes_1</th>\n",
|
||||
" <th>Beauty Eyes_2</th>\n",
|
||||
" <th>Swimwear Tops_1</th>\n",
|
||||
" <th>Swimwear Tops_2</th>\n",
|
||||
" <th>Bracelets_1</th>\n",
|
||||
" <th>Bracelets_2</th>\n",
|
||||
" <th>Wallets & Card Holders_1</th>\n",
|
||||
" <th>Wallets & Card Holders_2</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>NaN</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" <td>0.0</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>5 rows × 135 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Cami Dresses Shirts Tote Bags Sneakers Crop Tops Polos \\\n",
|
||||
"0 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"1 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"2 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"3 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"4 0.0 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" Cross Body Bags Casual Jackets Swimwear Bottoms Scarves \\\n",
|
||||
"0 0.0 0.0 0.0 0.0 \n",
|
||||
"1 0.0 0.0 0.0 0.0 \n",
|
||||
"2 0.0 0.0 0.0 0.0 \n",
|
||||
"3 0.0 0.0 0.0 0.0 \n",
|
||||
"4 0.0 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" ... Scarves_1 Scarves_2 Beauty Eyes_1 \\\n",
|
||||
"0 ... NaN NaN NaN \n",
|
||||
"1 ... 0.0 NaN 0.0 \n",
|
||||
"2 ... 0.0 0.0 0.0 \n",
|
||||
"3 ... 0.0 0.0 0.0 \n",
|
||||
"4 ... 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" Beauty Eyes_2 Swimwear Tops_1 Swimwear Tops_2 Bracelets_1 Bracelets_2 \\\n",
|
||||
"0 NaN NaN NaN NaN NaN \n",
|
||||
"1 NaN 0.0 NaN 0.0 NaN \n",
|
||||
"2 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"3 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"4 0.0 0.0 0.0 0.0 0.0 \n",
|
||||
"\n",
|
||||
" Wallets & Card Holders_1 Wallets & Card Holders_2 \n",
|
||||
"0 NaN NaN \n",
|
||||
"1 0.0 NaN \n",
|
||||
"2 0.0 0.0 \n",
|
||||
"3 0.0 0.0 \n",
|
||||
"4 0.0 0.0 \n",
|
||||
"\n",
|
||||
"[5 rows x 135 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df_new.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_new = df_new.dropna()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxYAAALfCAYAAAD8LlRvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xm8XdP9//FXEhKZiXlopZQPGgRBqSGGojW05tYYbVE1fPEztVVStNUaSw01BtU2NWuqhiKtommQmL0pQlNTYkxkEJHfH2vdc499z73n3tzk3nOP9/PxOA/ns4e1197Zj2Ovu9Znr27z5s3DzMzMzMysPbp3dgXMzMzMzKzrc8PCzMzMzMzazQ0LMzMzMzNrNzcszMzMzMys3dywMDMzMzOzdnPDwszMzMzM2m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cszMzMzMzayTkWblgsdBHxY9KkdXNJSdaHShq3gMoeTnot7U4LojwzMzMzs/nlHIuFKCI2Ic0jMVzS7DwDds+cLL4gyh/OfDYsImIRSR/Px2F9w5iZmVlHqsl8gqK+mx3TYc9IH/7zvJq8Ju6xWLiWB6ZKmg2ltzuRJ9q7BtgZWBTYM09s1xe4kPRWqUWBkZJuy5PuXQf0zeUeIemh8gPleSwuI01090Yz5YwAdgP6AT0i4lukGb4HkO6FwyQ90NIJzf7gndL3XgMG8fsJk0vxPuut1GR9Z48L9Vhlx7UWQ+ePVXZc3zH4nnJcf79b1jV07+wK1Lm7gc9FxPMRcXGeCK/BVEnrA5cAx+VlPwbuk7QLine truncated
|
||||
"text/plain": [
|
||||
"<matplotlib.figure.Figure at 0x7fea8ff87cc0>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"colormap = plt.cm.RdBu\n",
|
||||
"plt.figure(figsize=(15,10))\n",
|
||||
"plt.title('2 days correlation', y=1.05, size=16)\n",
|
||||
"\n",
|
||||
"mask = np.zeros_like(df_new.corr())\n",
|
||||
"mask[np.triu_indices_from(mask)] = True\n",
|
||||
"\n",
|
||||
"sns.heatmap(df_new.corr(), mask=mask, linewidths=0.1,vmax=1.0, \n",
|
||||
" square=True, cmap=colormap, linecolor='white', annot=False)\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
date,Cami Dresses,Shirts,Tote Bags,Sneakers,Crop Tops,Polos,Cross Body Bags,Casual Jackets,Swimwear,Scarves,Coats,Wedges,Sandals & Flip Flops,Necklaces,Activewear Shoes,Blazers,Flats,Sweatshirts,Coats,Scarves,Earrings,Blouses,Swing & Trapeze Dresses,Boots,Beauty Skin Care,Pumps,Backpacks,Casual Jackets,Swimwear Tops,Scarves & Hijabs,Ethnicwear,Bracelets,Smart Jackets,Ethnicwear Dresses,Swimwear,Heels,T-Shirts,Activewear Tops & T-Shirts,Watches & Timepieces,Wallets & Card Holders,Bodycon Dresses,Beauty Tools & Accessories,Skinny Jeans,Beauty Eyes,Beauty Face
|
||||
2017-08-04,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
||||
2017-08-07,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
||||
2017-08-10,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,1.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0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
||||
2018-10-19,0.0,1.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
||||
2018-10-22,0.0,1.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
|
||||
|
|
After Width: | Height: | Size: 59 KiB |
@@ -0,0 +1,389 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import matplotlib.dates as mdates\n",
|
||||
"import matplotlib.ticker as mticker\n",
|
||||
"import matplotlib\n",
|
||||
"from mpl_finance import candlestick_ohlc\n",
|
||||
"from datetime import datetime\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Adj Close</th>\n",
|
||||
" <th>Volume</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>2018-05-23</td>\n",
|
||||
" <td>277.760010</td>\n",
|
||||
" <td>279.910004</td>\n",
|
||||
" <td>274.000000</td>\n",
|
||||
" <td>279.070007</td>\n",
|
||||
" <td>279.070007</td>\n",
|
||||
" <td>5953100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>2018-05-24</td>\n",
|
||||
" <td>278.399994</td>\n",
|
||||
" <td>281.109985</td>\n",
|
||||
" <td>274.890015</td>\n",
|
||||
" <td>277.850006</td>\n",
|
||||
" <td>277.850006</td>\n",
|
||||
" <td>4176700</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>2018-05-25</td>\n",
|
||||
" <td>277.630005</td>\n",
|
||||
" <td>279.640015</td>\n",
|
||||
" <td>275.609985</td>\n",
|
||||
" <td>278.850006</td>\n",
|
||||
" <td>278.850006</td>\n",
|
||||
" <td>3875100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>2018-05-29</td>\n",
|
||||
" <td>278.510010</td>\n",
|
||||
" <td>286.500000</td>\n",
|
||||
" <td>276.149994</td>\n",
|
||||
" <td>283.760010</td>\n",
|
||||
" <td>283.760010</td>\n",
|
||||
" <td>5666600</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>2018-05-30</td>\n",
|
||||
" <td>283.290009</td>\n",
|
||||
" <td>295.010010</td>\n",
|
||||
" <td>281.600006</td>\n",
|
||||
" <td>291.720001</td>\n",
|
||||
" <td>291.720001</td>\n",
|
||||
" <td>7489700</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Date Open High Low Close Adj Close \\\n",
|
||||
"0 2018-05-23 277.760010 279.910004 274.000000 279.070007 279.070007 \n",
|
||||
"1 2018-05-24 278.399994 281.109985 274.890015 277.850006 277.850006 \n",
|
||||
"2 2018-05-25 277.630005 279.640015 275.609985 278.850006 278.850006 \n",
|
||||
"3 2018-05-29 278.510010 286.500000 276.149994 283.760010 283.760010 \n",
|
||||
"4 2018-05-30 283.290009 295.010010 281.600006 291.720001 291.720001 \n",
|
||||
"\n",
|
||||
" Volume \n",
|
||||
"0 5953100 \n",
|
||||
"1 4176700 \n",
|
||||
"2 3875100 \n",
|
||||
"3 5666600 \n",
|
||||
"4 7489700 "
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_csv('TSLA.csv')\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"date = [datetime.strptime(d, '%Y-%m-%d') for d in df['Date']]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"candlesticks = list(zip(mdates.date2num(date),df['Open'],\n",
|
||||
" df['High'],df['Low'],df['Close'],df['Volume']))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7oAAAGGCAYAAABVI/2SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3X2cXGV99/HPZGHZ7ENCHgYQMQ2mcFmFAkUFFWqgMZRgmgZtFaqIGgTFIqVokVuB0BZT4E41kN4GUkGxWMUS1kCsNIIUimhNQyR6c8EdXJewgpsEkn3IErKZ+4+ZMzk7mYdzZs6Z8zDf9+uVV3Znzpy55szsmfO7Hn6/TC6XQ0RERERERCQtJkXdABEREREREZEgKdAVERERERGRVFGgKyIiIiIiIqmiQFdERERERERSRYGuiIiIiIiIpIoCXREREREREUmVg6JugIiIiIiIiFRnjLkZeB8wGzjeWru5xvbvBP7JddNhwIvW2j8IrZExokBXREREREQk/u4DvgI86mVja+3jwInO78aY+4DHwmla/CjQFRERERERiTlr7WMAxpgJtxtjTgGWAVMKN11jrX2gZJvDgPnAxeG3NB60RldERERERCSBjDGHAl8FzrfWngy8F1hVuN3tAuBBa+1LzW5jVDSiKyIiIiIikkzvBI4Gvu8a6c0Bvwv8zLXdR4HPN7dp0VKgKyIiIiIikkwZ4OfW2j+stIEx5lRgOrCuaa2KAU1dFhERERERSabHgWOMMWc4Nxhj3maMybi2+Rhwl7V2b9NbF6FMLpeLug0iIiIiIiJShTFmBXAucASwDdhurX2LMeZtwE3ANKAdeA5YaK3dZ4yZDLwInGKtfTqipkdCga6IiIiIiIikiqYui4iIiIiISKq0YjKqHuBDwNPAaxG3RUREREREpNkOBt4EfBMYirgtoWjFQPdDwD9F3QgREREREZEY+D9RNyAMrRjoPg2wa9duxsf3HXDntGldvPzySNMb1Yp0rJtLx7u5dLybR8e6uXS8m0vHu7l0vJtHx7q5So93W9skpkyZDIXYKI1aMdB9DWB8fB979x4Y6AIVb5fg6Vg3l453c+l4N4+OdXPpeDeXjndz6Xg3j451c1U43qldyqlkVCIiIiIiIpIqCnRFREREREQkVRToioiIiIiISKoo0BUREREREZFUUaArIiIiIiIiqaJAV0RERERERFJFga6IiIiIiIikigJdERERERERSRUFuiIiIiIiIpIqCnRFEmLT4OaomyAiIiIikggKdEUSYuvQQNRNEBERERFJBAW6IiIiIiIikioKdEVERERERCRVDoq6ASIirWxGVxuTOjvZNzrK9pHxqJsjIiIikgoa0RURidCkzk7IZPL/i4iIiEggFOiKJIyyL4uIiIiIVKdAVyRhlH1ZRERERKQ6BboiIiIiIiKSKgp0RUREREREJFWUdVkkAa567HqG9gzz6MATnH7kqVE3R0RSZnjjBrpPOjnqZoiIiARGI7oiCTC0Z3jC/yIiQRrr74+6CSIiIoFSoCsiIiINGd64IeomiIiITKBAV0REROrilDvTiLCIiMSN1uiKiIiIL07eAICe9m6u5PiIWyQiIjKRRnRFRETEF3e+AOUOEBGROFKgKyIiInVbcu8gO9b2suWKy6JuioiISJEC3ZRz1k+JiIiEoWssB8D4rl0Rt0RERGQ/Bbopt3VoIOomiIiIiIiINJUCXREREalJJYRERCRJFOiKiIhITU4Joe1P/CTiloiIiNSmQDfFrnrsetb1reeqx66PuikiIpISI7/qi7oJIiIiNSnQTTGn5IPzv6adpYM6L0Sk2bZccdmEzMo97d0Rt0hERKQ6BbotxJl2JiIi4igNYstxMiqP79rF8//6HS66dxsrz7yRBbPnNauZIiIivhwUdQNExJ8Fs+dxzhvnR90MEUmYTYObOSF73AG3u4NYr1RKSERE4k4juiIiIi2gtNyc6qyLiEiaKdAVSZijeo4s/qx11yLxFue/UT911tumTMn/kMlM+N19PhIREYkTBbotLM4XYFKZe+qh1l2LxFsc/kYbPddvGtzMnOUrmL5wEcfefgdv+OCfM2f5CiB/PnK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eN/x86xXXymyuNLn6ORtqfVT7c+yduPOpHOl8q/t5B/39zc2znvaa3Hu/8wWv2YN5PfY633pn5+jt2Ol7aXvX1oz/AB+9n+xE+YceopgbTjp1uf9LytH+6g3c8+52XfUfdzen2e0u+B8V27Wv5z7j5fVzt3u7X6MWs2He/m0bFurlY73rEIdK21S6BYk+kmYAFwurX2eWPMIcCXgVuBD/nc9ZeBO903XHzxxW8F7klieaGRkVfLts1reys93u++RkZeZeek8qMR1R5fel+cj3XY3Jlae9q7Of3IUysei5GRVyf87t7OeU9Hq7y34zMOr/h4CY/KCzXPr17dwtGHzPG8/WjJ35Sjp737gPdg22bLvjlv9rzvau/hL1/YwjEet61XMz5DjZ67W/1z7j5fVzt3O1r5uzIKOt7No2PdXJXKC6VZLAJdR0lNpucLt71qjPkn4HuFzZz6TAAYY2YC+8qM5lIYAS6tA3VUOK1vLT3t3QdkGBXvSjO1rutbH9q0SU0zl7Tre2UrRx9eO9DdNLiZb9l7GdozTAbIQXG6b7mass562h1rew+YcuvurGq2JK3rdCfOSzMv70mS3jcRkTSYFOWTG2O6jTFvcP3u1GQaM8ZMLdyWAT4IOHO+NgCTjTGnFX6/hHzNppbi1Eytt3Zqo5l6l51Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x1080 with 2 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15, 15))\n",
|
||||
"ax = fig.add_subplot(1,1,1)\n",
|
||||
"ax.set_ylabel('Quote ($)', size=20)\n",
|
||||
"\n",
|
||||
"dates = [x[0] for x in candlesticks]\n",
|
||||
"dates = np.asarray(dates)\n",
|
||||
"volume = [x[5] for x in candlesticks]\n",
|
||||
"volume = np.asarray(volume)\n",
|
||||
"\n",
|
||||
"candlestick_ohlc(ax, candlesticks, width=1,\n",
|
||||
" colorup='g', colordown='r')\n",
|
||||
"pad = 0.25\n",
|
||||
"yl = ax.get_ylim()\n",
|
||||
"ax.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])\n",
|
||||
"ax2 = ax.twinx()\n",
|
||||
"\n",
|
||||
"ax2.set_position(matplotlib.transforms.Bbox([[0.125,0],[0.9,0.32]]))\n",
|
||||
"\n",
|
||||
"pos = df['Open'] - df['Close']<0\n",
|
||||
"neg = df['Open'] - df['Close']>0\n",
|
||||
"ax2.bar(dates[pos],volume[pos],color='green',width=1,align='center')\n",
|
||||
"ax2.bar(dates[neg],volume[neg],color='red',width=1,align='center')\n",
|
||||
"\n",
|
||||
"ax2.set_xlim(min(dates),max(dates))\n",
|
||||
"yticks = ax2.get_yticks()\n",
|
||||
"ax2.set_yticks(yticks[::3])\n",
|
||||
"\n",
|
||||
"ax2.yaxis.set_label_position(\"right\")\n",
|
||||
"ax2.set_ylabel('Volume', size=20)\n",
|
||||
"\n",
|
||||
"ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))\n",
|
||||
"ax.xaxis.set_major_locator(mticker.MaxNLocator(10))\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def removal(signal, repeat):\n",
|
||||
" copy_signal = np.copy(signal)\n",
|
||||
" for j in range(repeat):\n",
|
||||
" for i in range(3, len(signal)):\n",
|
||||
" copy_signal[i - 1] = (copy_signal[i - 2] + copy_signal[i]) / 2\n",
|
||||
" return copy_signal\n",
|
||||
"\n",
|
||||
"def get(original_signal, removed_signal):\n",
|
||||
" buffer = []\n",
|
||||
" for i in range(len(removed_signal)):\n",
|
||||
" buffer.append(original_signal[i] - removed_signal[i])\n",
|
||||
" return np.array(buffer)\n",
|
||||
"\n",
|
||||
"signal = np.copy(df.Open.values)\n",
|
||||
"removed_signal = removal(signal, 30)\n",
|
||||
"noise_open = get(signal, removed_signal)\n",
|
||||
"\n",
|
||||
"signal = np.copy(df.High.values)\n",
|
||||
"removed_signal = removal(signal, 30)\n",
|
||||
"noise_high = get(signal, removed_signal)\n",
|
||||
"\n",
|
||||
"signal = np.copy(df.Low.values)\n",
|
||||
"removed_signal = removal(signal, 30)\n",
|
||||
"noise_low = get(signal, removed_signal)\n",
|
||||
"\n",
|
||||
"signal = np.copy(df.Close.values)\n",
|
||||
"removed_signal = removal(signal, 30)\n",
|
||||
"noise_close = get(signal, removed_signal)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA5EAAAE3CAYAAAAzCMVlAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3XucVXW9//HXnhlmhrmJDoM3RATlm7cQL6WFWUqakHkwf3kyNTQouzykrDzW4aASFmmRYp6fBhYaWVZHokTLjC5imQrICbUv/lAkRGRm5DIX5rZnfn/svcY9w+w9a1/XZb+fj8c8ZmZf1vrs9d177fX5XiN9fX2IiIiIiIiIuFHidQAiIiIiIiISHEoiRURERERExDUlkSIiIiIiIuKakkgRERERERFxTUmkiIiIiIiIuKYkUkRERERERFxTEikiIiIiIiKuKYkUERERERER15REioiIiIiIiGtKIkVERERERMQ1JZEiIiIiIiLiWpnXAXioAjgDeAOIehyLiIiIiIhIoZUChwPPAp1un1TMSeQZwJNeByEiIiIiIuKxs4G1bh9czEnkGwC7d7fR29vndSyeqq+vobm51eswJAmVj7+pfPxN5eNvKh9/U/n4m8rH34JSPiUlEQ4+uBriuZFbxZxERgF6e/uKPokEdAx8TuXjbyoff1P5+JvKx99UPv6m8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w6I8Y47bmfy5Clcc82nef317cyadTnvfvdZab/Wjo4O7r33R7zxxg6uuuoyLrzwIsrLy7n55nncdNNCjj56PO3tbXzqU1dy0knv5Oijx2dySAtCSaSIiIiISJEY0VVBX5UX3VlhZLTmgNteeGETEydO4phjJgAwffpH+O53v017exvnnHMud90Va6EDeP759cybt4D9+/ezYcM69uzZ07+d9vY2tm7d2p9EfuhDM/rvO+WUKfz3fy+ho6ODU089nVNPPR2A5557hrlzv0IkEqG6uoZp087nueeeOSCJXL9+HV/84lcBOPLIsZx++hkZvf5p084H4PDDj6C2to7Gxl309vby2muvctNNX+9/XHd3N1u3vqokUkREREREvFe39yD2jvJiXGSEMZ2Hp/WMyspKpk49h9///rcATJ16DiNHjqS9vY1IJMKyZQ9QVjZ0OlNVNbL/7/e//zxOOumdPPPM06xYsZzVq3/N/PnfyPylZKi8/O1uxCUlJUSjPUCEgw4axfLlDxY8nmxoYh0RERERkSJR0TmSwzrHApEC7jXCYZ1jh1wr8sQTT2bLls289tpWAB577BGOO85QVVUNwIUXXsSjjz7Co48+wvTpsXGCVVXVTJ48hRUrlvdv5803d9Lc3DTk3rdv/xeHHFLP9OkXcfXVc3jxxdgYyNNPfxerV6+ir6+P9vY2/vCHxznjjHcf8PzTTjud1at/DcCOHa/z3HPPZnwkBhs37mgqKyv57W9X99/22mtbaWsbevyoXwSqJdIYcxNwM3CytXaTMeZM4F5gJLAVuMJau8u7CEVERERE/G1M55FU99Syq+IN9pe20hPpzst+yvpGMDJaw5jOw4dMIAEOPvhg5s1bwC23/CfRaJRRow4e0Eo4efIptLe39f/tmD//GyxZspirropNTlNVVc3Line truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x360 with 1 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"noise_candlesticks = list(zip(mdates.date2num(date),noise_open,\n",
|
||||
" noise_high,noise_low,noise_close))\n",
|
||||
"\n",
|
||||
"fig = plt.figure(figsize = (15, 5))\n",
|
||||
"ax = fig.add_subplot(1,1,1)\n",
|
||||
"ax.set_ylabel('Quote ($)', size=20)\n",
|
||||
"\n",
|
||||
"candlestick_ohlc(ax, noise_candlesticks, width=1,\n",
|
||||
" colorup='g', colordown='r')\n",
|
||||
"ax.plot(dates, [np.percentile(noise_close, 95)] * len(noise_candlesticks), color = (1.0, 0.792156862745098, 0.8, 0.7),\n",
|
||||
" linewidth=10.0, label = 'overbought line')\n",
|
||||
"\n",
|
||||
"ax.plot(dates, [np.percentile(noise_close, 10)] * len(noise_candlesticks), \n",
|
||||
" color = (0.6627450980392157, 1.0, 0.6392156862745098, 0.7),\n",
|
||||
" linewidth=10.0, label = 'oversold line')\n",
|
||||
"\n",
|
||||
"ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))\n",
|
||||
"ax.xaxis.set_major_locator(mticker.MaxNLocator(10))\n",
|
||||
"\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA7wAAAK5CAYAAACc+SIwAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3X+cXGV99//3ZHHZ7GY3kGQEEWlwC1etUqEooIbeSCOWYJoGba1UETUIrTRQbqUptw0k7W1T8JHqKq2BVFBaqKKEbSRWGkHupBq9zR0iacsF3+C6xBXcJJDsD5bAZr5/zJzN2cn8OOfMOXN+zOv5eOSR3ZkzZ645M3vmfK7rc32uXKFQEAAAAAAAWTMj7gYAAAAAABAF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b3P5OZ+Ml/1NOA5MHKy4Dz+puLXasHN4l2Z63NaPAxMHp/5vxmeokXP3yy+8wOdcmjpn1zp3O1r5uzIOHO/m4ng3T7V1eLMsUQGvo2wR42dKt71kjPl7Sf9a2sxZ0FiSZIyZJ+lwhdFdlUaEyxdOPjma1reWLKQAxqmZ6ZROwRogy3605zGdemxvzW3qzZestpZ1kDXLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 1080x864 with 3 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fig = plt.figure(figsize = (15, 12))\n",
|
||||
"ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)\n",
|
||||
"\n",
|
||||
"ax1.set_ylabel('Quote ($)', size=20)\n",
|
||||
"\n",
|
||||
"dates = [x[0] for x in candlesticks]\n",
|
||||
"dates = np.asarray(dates)\n",
|
||||
"volume = [x[5] for x in candlesticks]\n",
|
||||
"volume = np.asarray(volume)\n",
|
||||
"\n",
|
||||
"candlestick_ohlc(ax1, candlesticks, width=1,\n",
|
||||
" colorup='g', colordown='r')\n",
|
||||
"pad = 0.25\n",
|
||||
"yl = ax1.get_ylim()\n",
|
||||
"ax1.set_ylim(yl[0]-(yl[1]-yl[0])*pad,yl[1])\n",
|
||||
"ax2 = ax1.twinx()\n",
|
||||
"\n",
|
||||
"pos = df['Open'] - df['Close']<0\n",
|
||||
"neg = df['Open'] - df['Close']>0\n",
|
||||
"ax2.bar(dates[pos],volume[pos],color='green',width=1,align='center')\n",
|
||||
"ax2.bar(dates[neg],volume[neg],color='red',width=1,align='center')\n",
|
||||
"\n",
|
||||
"ax2.set_xlim(min(dates),max(dates))\n",
|
||||
"yticks = ax2.get_yticks()\n",
|
||||
"ax2.set_yticks(yticks[::3])\n",
|
||||
"\n",
|
||||
"ax2.yaxis.set_label_position(\"right\")\n",
|
||||
"ax2.set_ylabel('Volume', size=20)\n",
|
||||
"\n",
|
||||
"ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))\n",
|
||||
"ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))\n",
|
||||
"\n",
|
||||
"ax2 = plt.subplot2grid((3, 1), (2, 0))\n",
|
||||
"\n",
|
||||
"ax2.set_ylabel('Quote ($)', size=20)\n",
|
||||
"\n",
|
||||
"candlestick_ohlc(ax2, noise_candlesticks, width=1,\n",
|
||||
" colorup='g', colordown='r')\n",
|
||||
"ax2.plot(dates, [np.percentile(noise_close, 95)] * len(noise_candlesticks), color = (1.0, 0.792156862745098, 0.8, 1.0),\n",
|
||||
" linewidth=5.0, label = 'overbought line')\n",
|
||||
"\n",
|
||||
"ax2.plot(dates, [np.percentile(noise_close, 10)] * len(noise_candlesticks), \n",
|
||||
" color = (0.6627450980392157, 1.0, 0.6392156862745098, 1.0),\n",
|
||||
" linewidth=5.0, label = 'oversold line')\n",
|
||||
"\n",
|
||||
"ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))\n",
|
||||
"ax2.xaxis.set_major_locator(mticker.MaxNLocator(10))\n",
|
||||
"\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
|
After Width: | Height: | Size: 43 KiB |
@@ -0,0 +1,295 @@
|
||||
,Polarity,Sensitivity,Tweet_vol,Open,High,Low,Volume_BTC,Volume_Dollar,Close_Price
|
||||
2018-07-11 20:00:00,0.10265670748614598,0.21614845947129513,4354.0,6342.97,6354.19,6291.0,986.73,6231532.37,6350.0
|
||||
2018-07-11 21:00:00,0.09800395297609978,0.21861239516350178,4432.0,6352.99,6370.0,6345.76,126.46,804221.55,6356.48
|
||||
2018-07-11 22:00:00,0.0966881579841847,0.23134226427071872,3980.0,6350.85,6378.47,6345.0,259.1,1646353.87,6361.93
|
||||
2018-07-11 23:00:00,0.10399680386738332,0.21773906718462835,3830.0,6362.36,6381.25,6356.74,81.54,519278.69,6368.78
|
||||
2018-07-12 00:00:00,0.09438312650721571,0.19525553005949395,3998.0,6369.49,6381.25,6361.83,124.55,793560.22,6380.0
|
||||
2018-07-12 01:00:00,0.10083589375817453,0.22307632548845868,3713.0,6379.13,6380.0,6347.72,141.5,900280.85,6365.43
|
||||
2018-07-12 02:00:00,0.1119643429977597,0.19504306787217293,3843.0,6365.24,6371.8,6324.48,141.3,896772.74,6327.94
|
||||
2018-07-12 03:00:00,0.1058881862567765,0.20993930342403333,3831.0,6328.4,6348.45,6302.18,162.37,1026431.72,6326.98
|
||||
2018-07-12 04:00:00,0.10811700734669419,0.20800324193424663,3743.0,6327.49,6343.22,6327.49,58.62,371327.65,6339.5
|
||||
2018-07-12 05:00:00,0.10666706633974722,0.21723050559221563,3480.0,6342.22,6344.57,6328.0,74.75,473450.28,6333.05
|
||||
2018-07-12 06:00:00,0.09819201661175406,0.20621768833627063,3954.0,6333.05,6335.55,6305.0,130.84,827194.5,6329.57
|
||||
2018-07-12 07:00:00,0.08077478187342219,0.19081446008983832,4561.0,6329.9,6334.74,6177.45,1211.64,7536289.75,6217.63
|
||||
2018-07-12 08:00:00,0.08877735580061671,0.18471850163515388,5621.0,6217.03,6223.14,6131.35,758.15,4677685.75,6180.5
|
||||
2018-07-12 09:00:00,0.0975813393389163,0.20191214692260084,5046.0,6180.46,6200.82,6085.59,1090.25,6719480.12,6200.82
|
||||
2018-07-12 10:00:00,0.09191287676658966,0.19896478587190353,5511.0,6200.23,6205.0,6175.01,289.87,1794667.77,6185.82
|
||||
2018-07-12 11:00:00,0.08382532077920497,0.2173181646652594,5277.0,6185.4,6187.99,6157.03,212.3,1310453.07,6175.18
|
||||
2018-07-12 12:00:00,0.07710081294119067,0.195526425954516,5493.0,6174.5,6182.13,6144.35,261.02,1608275.15,6162.41
|
||||
2018-07-12 13:00:00,0.07714852137139569,0.20891735896754152,5455.0,6167.17,6191.99,6155.0,246.37,1522744.15,6180.31
|
||||
2018-07-12 14:00:00,0.09711192039767944,0.23622240576351197,5811.0,6180.3,6183.99,6170.99,258.89,1599120.65,6182.99
|
||||
2018-07-12 15:00:00,0.09680845597780344,0.21027440482122414,5676.0,6182.98,6199.57,6173.5,275.0,1700530.69,6180.97
|
||||
2018-07-12 16:00:00,0.08795242249876721,0.2015228691350621,5983.0,6178.59,6182.55,6159.01,166.21,1025666.81,6169.51
|
||||
2018-07-12 17:00:00,0.09484858580146976,0.21709007457257168,5699.0,6168.44,6192.89,6166.78,342.08,2115989.77,6181.23
|
||||
2018-07-12 18:00:00,0.10102371245440864,0.21088171207458128,5273.0,6184.08,6189.0,6167.64,168.14,1039237.28,6179.12
|
||||
2018-07-12 19:00:00,0.08611796809952094,0.21457562723378348,4950.0,6179.12,6185.0,6165.22,103.57,639670.03,6173.99
|
||||
2018-07-12 20:00:00,0.07871816379987927,0.20741734995274108,4874.0,6171.06,6180.17,6150.43,148.79,916929.71,6174.76
|
||||
2018-07-12 21:00:00,0.08719564027112056,0.21046084704329646,4289.0,6179.38,6187.42,6166.91,120.99,747520.42,6173.05
|
||||
2018-07-12 22:00:00,0.09749038298261753,0.20786096842163354,4290.0,6170.03,6176.66,6153.52,81.22,500729.79,6171.68
|
||||
2018-07-12 23:00:00,0.09420668493105258,0.20923114266523765,4404.0,6171.94,6173.61,6110.08,309.59,1901284.59,6149.11
|
||||
2018-07-13 00:00:00,0.10956030065574283,0.20687239073099853,4339.0,6149.11,6267.31,6072.0,1039.01,6412278.5,6243.88
|
||||
2018-07-13 01:00:00,0.1059331692447039,0.2131436722864254,4112.0,6243.53,6284.05,6214.94,466.27,2910899.5,6236.88
|
||||
2018-07-13 02:00:00,0.10359979844473512,0.20187772547668398,4080.0,6236.88,6249.82,6231.94,114.45,714175.12,6232.71
|
||||
2018-07-13 03:00:00,0.12033375545093336,0.21227923614457422,4205.0,6232.88,6254.67,6228.76,187.3,1168928.22,6242.9
|
||||
2018-07-13 04:00:00,0.09555287236451522,0.21991078312830262,4395.0,6242.89,6252.93,6236.55,139.55,871611.63,6249.87
|
||||
2018-07-13 05:00:00,0.10343047308156315,0.2078529458176204,4179.0,6248.2,6264.75,6236.01,219.95,1375165.61,6245.68
|
||||
2018-07-13 06:00:00,0.10165723968487417,0.20809832469539916,3760.0,6245.68,6256.75,6229.16,242.49,1513210.93,6244.39
|
||||
2018-07-13 07:00:00,0.1022300476629104,0.21493596730628992,3875.0,6243.18,6247.98,6212.21,184.38,1148580.47,6230.01
|
||||
2018-07-13 08:00:00,0.10611204722476755,0.21574199226407334,5072.0,6230.4,6253.9,6229.38,236.18,1474017.93,6249.19
|
||||
2018-07-13 09:00:00,0.09147283872454691,0.18770163229568818,4500.0,6247.92,6250.0,6229.1,128.9,804393.43,6233.96
|
||||
2018-07-13 10:00:00,0.09960890509981776,0.1912204222641126,4818.0,6232.96,6245.47,6230.0,154.3,962281.91,6234.85
|
||||
2018-07-13 11:00:00,0.09279532026486634,0.19179629084253394,4671.0,6234.85,6245.87,6226.18,94.38,588479.19,6234.22
|
||||
2018-07-13 12:00:00,0.08698310262415916,0.19715054617599925,5181.0,6232.0,6248.99,6228.08,62.82,391997.33,6241.75
|
||||
2018-07-13 13:00:00,0.09692751891658306,0.2049111160954308,5298.0,6240.4,6286.36,6238.0,423.94,2655456.13,6269.06
|
||||
2018-07-13 14:00:00,0.09758484220495918,0.2026232776304885,5200.0,6272.4,6275.68,6228.67,394.0,2460513.08,6244.01
|
||||
2018-07-13 15:00:00,0.10318213207626459,0.21142511129952427,5116.0,6237.15,6264.35,6224.15,255.99,1599106.41,6245.99
|
||||
2018-07-13 16:00:00,0.09606416499703098,0.21215766204215622,5240.0,6245.99,6259.88,6237.0,195.93,1224745.98,6259.88
|
||||
2018-07-13 17:00:00,0.09716123209848196,0.21828153018266383,5558.0,6259.88,6268.05,6240.81,211.09,1319500.19,6254.99
|
||||
2018-07-13 18:00:00,0.08435959209609722,0.21622885818149568,5611.0,6254.99,6267.99,6247.11,97.84,612655.54,6264.94
|
||||
2018-07-13 19:00:00,0.08853718581752733,0.2203785027686753,5462.0,6264.91,6337.25,6226.99,571.75,3589882.19,6237.5
|
||||
2018-07-13 20:00:00,0.0923034543430851,0.21997207545969977,5030.0,6237.5,6245.78,6166.44,521.21,3228298.36,6174.99
|
||||
2018-07-13 21:00:00,0.10018122677823708,0.2161121468814689,4852.0,6177.35,6227.63,6121.01,612.28,3779623.53,6186.39
|
||||
2018-07-13 22:00:00,0.10095012149820652,0.23085818907913794,4449.0,6185.5,6229.0,6180.01,125.81,780174.87,6215.85
|
||||
2018-07-13 23:00:00,0.1134672424714027,0.22556652665784138,4208.0,6211.37,6225.39,6190.26,123.72,767545.63,6219.58
|
||||
2018-07-14 00:00:00,0.05169492007778924,0.21856501239382975,4371.0,6219.99,6248.94,6196.0,136.74,850825.64,6215.59
|
||||
2018-07-14 01:00:00,0.09950923554101708,0.2206044590073266,3907.0,6208.78,6273.97,6208.78,219.37,1370473.06,6237.99
|
||||
2018-07-14 02:00:00,0.11187410939405736,0.2202844564046976,3231.0,6237.98,6277.72,6214.11,171.24,1069456.99,6234.38
|
||||
2018-07-14 03:00:00,0.10049279048164668,0.20310504263708248,3558.0,6231.23,6240.0,6219.1,56.15,349888.49,6225.99
|
||||
2018-07-14 04:00:00,0.11271226782328998,0.22446433324987566,3130.0,6226.86,6227.67,6193.5,94.03,583969.64,6206.99
|
||||
2018-07-14 05:00:00,0.09652272423735678,0.2239328919413113,3415.0,6203.33,6224.8,6200.03,66.12,411019.51,6216.99
|
||||
2018-07-14 06:00:00,0.09735811595016991,0.20637940889185244,3114.0,6218.51,6233.38,6210.57,78.19,486424.82,6220.53
|
||||
2018-07-14 07:00:00,0.103780244266209,0.19285173493268673,3499.0,6220.53,6230.1,6201.97,75.07,466915.22,6212.81
|
||||
2018-07-14 08:00:00,0.0945108516295059,0.20254888141149976,4442.0,6212.92,6225.0,6204.91,95.79,595704.78,6220.06
|
||||
2018-07-14 09:00:00,0.09481203456127495,0.20144380409280152,3641.0,6221.8,6225.0,6203.79,98.38,611258.48,6216.2
|
||||
2018-07-14 10:00:00,0.0785761518393219,0.21936172359370504,4093.0,6216.2,6219.54,6180.0,130.22,807423.98,6195.32
|
||||
2018-07-14 11:00:00,0.08584598156862443,0.19659144803432665,4381.0,6203.99,6223.48,6195.73,39.45,244914.76,6220.48
|
||||
2018-07-14 12:00:00,0.08813036199981576,0.19326561881496315,4271.0,6220.48,6239.45,6215.79,42.5,264821.93,6233.0
|
||||
2018-07-14 13:00:00,0.08948837953046178,0.17648722495220495,4890.0,6231.13,6242.21,6224.63,82.37,513436.44,6236.12
|
||||
2018-07-14 14:00:00,0.09100328219923795,0.19926593911037158,4768.0,6235.76,6240.92,6222.8,32.42,201957.74,6237.0
|
||||
2018-07-14 15:00:00,0.08687249609815068,0.1916351459166314,4637.0,6231.95,6244.43,6226.23,48.81,304512.55,6238.2
|
||||
2018-07-14 16:00:00,0.08541838397706562,0.21048857295933318,4399.0,6238.2,6240.0,6216.18,47.57,296455.69,6239.99
|
||||
2018-07-14 17:00:00,0.08328018219976774,0.229995432773997,4293.0,6239.55,6270.69,6239.55,133.93,838019.93,6262.0
|
||||
2018-07-14 18:00:00,0.07986841649398302,0.23248332235550545,3861.0,6262.99,6270.59,6248.74,259.41,1624071.38,6255.49
|
||||
2018-07-14 19:00:00,0.08645546626420936,0.21603397917127862,3862.0,6259.54,6298.0,6229.48,68.14,426747.86,6237.68
|
||||
2018-07-14 20:00:00,0.08030492195122882,0.2035296529301922,3989.0,6239.99,6269.53,6200.01,146.93,916476.48,6267.99
|
||||
2018-07-14 21:00:00,0.09313462281643667,0.21950797348744847,4056.0,6269.53,6317.84,6205.47,425.57,2664428.61,6270.31
|
||||
2018-07-14 22:00:00,0.10330089707235544,0.22135970695783896,3466.0,6270.3,6286.37,6257.13,54.81,343757.75,6265.61
|
||||
2018-07-14 23:00:00,0.1016246044466634,0.21187473667744477,3453.0,6265.07,6277.9,6238.48,63.91,399501.47,6246.57
|
||||
2018-07-15 00:00:00,0.08645926265533192,0.19637008784261328,3344.0,6250.99,6261.06,6238.56,54.37,339748.71,6243.98
|
||||
2018-07-15 01:00:00,0.07440800340954949,0.17433025080936304,3890.0,6245.99,6255.86,6227.76,63.46,396208.02,6243.42
|
||||
2018-07-15 02:00:00,0.09153586908711045,0.19087176337574915,3136.0,6243.42,6247.97,6227.94,36.89,230131.13,6243.93
|
||||
2018-07-15 03:00:00,0.09914894113188619,0.22633646719980086,3121.0,6238.55,6250.64,6232.06,38.61,240983.43,6249.61
|
||||
2018-07-15 04:00:00,0.09269128033174348,0.20158160932478605,3397.0,6249.61,6252.85,6235.12,175.88,1097749.25,6248.6
|
||||
2018-07-15 05:00:00,0.0969583905604344,0.19882088768184264,3391.0,6248.6,6277.28,6248.6,77.26,484027.1,6266.27
|
||||
2018-07-15 06:00:00,0.12450486511852168,0.22112820700711266,3328.0,6266.16,6280.41,6258.72,46.05,288836.56,6275.95
|
||||
2018-07-15 07:00:00,0.0988669935841507,0.19227725225395115,3751.0,6278.82,6283.57,6266.26,86.17,540629.42,6274.99
|
||||
2018-07-15 08:00:00,0.09638586963250599,0.23420282158809222,4613.0,6270.26,6279.5,6256.5,22.0,137960.07,6277.0
|
||||
2018-07-15 09:00:00,0.11079858545480217,0.22384213168365805,3865.0,6277.0,6349.47,6267.31,299.95,1891954.57,6311.47
|
||||
2018-07-15 10:00:00,0.12746877528258913,0.22103256652160957,4084.0,6305.67,6330.99,6296.72,199.63,1260251.48,6302.99
|
||||
2018-07-15 11:00:00,0.11613103897994907,0.20725430234489406,4156.0,6303.85,6384.47,6303.85,508.32,3225731.02,6336.0
|
||||
2018-07-15 12:00:00,0.12037205312234465,0.2153608557262819,4178.0,6332.99,6359.81,6325.96,357.15,2268610.84,6353.01
|
||||
2018-07-15 13:00:00,0.12241130247880065,0.23832342374193266,4683.0,6353.01,6360.89,6324.21,91.99,583242.7,6329.26
|
||||
2018-07-15 14:00:00,0.11634313134396271,0.22410089987047968,4303.0,6329.26,6359.06,6322.03,111.84,709071.63,6354.96
|
||||
2018-07-15 15:00:00,0.11613708011053471,0.21907138095607467,4397.0,6356.86,6362.94,6327.14,140.14,889382.77,6347.99
|
||||
2018-07-15 16:00:00,0.10994101696467036,0.2279774612159839,3801.0,6347.98,6376.98,6342.62,249.59,1586484.37,6372.36
|
||||
2018-07-15 17:00:00,0.10674266070473037,0.22293243238284427,4492.0,6374.58,6397.21,6362.7,106.36,678296.77,6374.74
|
||||
2018-07-15 18:00:00,0.10997613960865044,0.23374366728242194,3964.0,6378.88,6393.51,6354.12,294.5,1879818.44,6383.0
|
||||
2018-07-15 19:00:00,0.09045161714398472,0.226387896197765,3445.0,6383.0,6383.0,6349.47,65.63,417658.22,6349.72
|
||||
2018-07-15 20:00:00,0.08711509708244272,0.20652699734090638,3505.0,6354.97,6372.94,6349.47,72.23,459477.97,6362.99
|
||||
2018-07-15 21:00:00,0.09560266939710718,0.2363889192541693,3590.0,6362.99,6372.69,6355.29,83.2,529393.61,6372.69
|
||||
2018-07-15 22:00:00,0.08745570188051328,0.2233889519208471,3198.0,6372.87,6397.21,6369.12,157.75,1006828.31,6379.95
|
||||
2018-07-15 23:00:00,0.10067384092591493,0.22686999793487145,3187.0,6379.95,6388.82,6350.11,177.91,1133835.24,6361.63
|
||||
2018-07-16 00:00:00,0.10101162074136538,0.22640124740108136,2998.0,6361.68,6373.07,6336.01,93.76,595245.86,6349.3
|
||||
2018-07-16 01:00:00,0.10680389406136824,0.21421138664372752,3695.0,6353.25,6377.82,6340.53,117.74,748249.33,6350.52
|
||||
2018-07-16 02:00:00,0.10851873206700743,0.19241046825043137,3771.0,6349.46,6354.99,6339.58,112.25,712492.32,6345.61
|
||||
2018-07-16 03:00:00,0.13063382784526958,0.2287674128269762,3299.0,6345.49,6363.53,6333.63,223.51,1417294.48,6351.87
|
||||
2018-07-16 04:00:00,0.11510180723062069,0.22669744921558144,3171.0,6351.68,6370.0,6346.49,198.15,1260752.84,6352.93
|
||||
2018-07-16 05:00:00,0.12421010338306845,0.22375148377130483,3683.0,6352.99,6357.17,6338.86,97.74,620504.75,6344.03
|
||||
2018-07-16 06:00:00,0.10025139746450867,0.19986633661542283,3831.0,6344.03,6367.99,6342.76,158.7,1009238.67,6362.99
|
||||
2018-07-16 07:00:00,0.12006243489878883,0.22294543258347688,3948.0,6362.99,6380.01,6355.07,119.64,761752.33,6363.13
|
||||
2018-07-16 08:00:00,0.10381535148494465,0.20600678278535753,4602.0,6362.98,6365.67,6348.41,157.83,1002695.44,6359.74
|
||||
2018-07-16 09:00:00,0.08866044469405737,0.18684176442941855,4378.0,6359.74,6396.08,6340.0,372.24,2369598.53,6368.02
|
||||
2018-07-16 10:00:00,0.09820637577528228,0.20079615873035667,5585.0,6367.99,6530.0,6360.0,1147.69,7418674.01,6522.15
|
||||
2018-07-16 11:00:00,0.09356232328934051,0.19267955935679773,5841.0,6516.03,6553.0,6496.46,610.8,3983335.56,6542.77
|
||||
2018-07-16 12:00:00,0.09658528762607374,0.20456811655826818,6359.0,6542.99,6667.0,6527.19,912.03,6022860.97,6625.99
|
||||
2018-07-16 13:00:00,0.0920606179084515,0.2004977756567735,7133.0,6627.99,6633.18,6587.3,436.89,2886535.04,6599.25
|
||||
2018-07-16 14:00:00,0.08435536040922681,0.1919805585897622,7280.0,6598.72,6619.97,6576.8,398.27,2627631.55,6588.18
|
||||
2018-07-16 15:00:00,0.08570224612525415,0.2124886445303301,6690.0,6581.87,6631.4,6580.0,369.45,2443755.81,6621.57
|
||||
2018-07-16 16:00:00,0.08231677287304599,0.21623361865548088,6271.0,6627.63,6647.85,6608.26,320.53,2124754.87,6623.44
|
||||
2018-07-16 17:00:00,0.10487584012803053,0.2243072925380704,5898.0,6628.12,6652.26,6610.63,697.8,4633743.83,6617.04
|
||||
2018-07-16 18:00:00,0.09189594855473134,0.22327380578556308,5539.0,6619.98,6724.78,6613.72,933.84,6232959.77,6665.62
|
||||
2018-07-16 19:00:00,0.10079133823349068,0.22437945772881338,4967.0,6665.8,6700.0,6659.51,436.1,2913361.09,6660.56
|
||||
2018-07-16 20:00:00,0.09804340086611071,0.20411718528510037,4937.0,6661.02,6689.94,6635.0,297.5,1981897.93,6675.01
|
||||
2018-07-16 21:00:00,0.09274331640552064,0.2182669786906523,4692.0,6674.96,6674.96,6651.01,245.83,1638392.23,6661.11
|
||||
2018-07-16 22:00:00,0.09794827588098044,0.23045003961517432,4290.0,6656.59,6668.32,6641.35,151.51,1008524.63,6661.66
|
||||
2018-07-16 23:00:00,0.08960596210615838,0.2111357903786806,3996.0,6659.17,6697.23,6651.91,188.37,1257005.96,6671.97
|
||||
2018-07-17 00:00:00,0.08441498966342238,0.20378772478045137,4159.0,6668.87,6755.0,6662.04,699.65,4700740.52,6721.04
|
||||
2018-07-17 01:00:00,0.07757985257908741,0.18945709572627056,4146.0,6721.21,6741.59,6700.36,375.73,2526123.13,6740.87
|
||||
2018-07-17 02:00:00,0.07779411857484492,0.1815114455443646,4063.0,6741.59,6749.11,6696.0,247.99,1666318.6,6721.0
|
||||
2018-07-17 03:00:00,0.08803316649020868,0.19529453582262724,3542.0,6720.99,6734.87,6712.29,194.25,1307163.64,6734.4
|
||||
2018-07-17 04:00:00,0.08060551768147316,0.18854092053740948,3596.0,6734.4,6749.7,6724.5,247.25,1665494.07,6738.86
|
||||
2018-07-17 05:00:00,0.07906925575205595,0.19812163257553825,3943.0,6738.86,6755.14,6702.91,328.33,2207847.78,6707.47
|
||||
2018-07-17 06:00:00,0.08166557090545296,0.1988207290633254,3782.0,6706.12,6718.3,6679.58,306.45,2053621.55,6688.11
|
||||
2018-07-17 07:00:00,0.08926668742545174,0.19717730891360574,3740.0,6689.3,6696.63,6657.95,387.29,2587491.88,6670.93
|
||||
2018-07-17 08:00:00,0.08669627364776862,0.20270935828254527,4895.0,6671.21,6702.19,6668.64,194.46,1300997.96,6698.76
|
||||
2018-07-17 09:00:00,0.08404939264271777,0.19747495622359704,4598.0,6699.66,6734.78,6694.19,341.46,2294438.1,6715.07
|
||||
2018-07-17 10:00:00,0.07728310090551331,0.21391281443005386,4858.0,6719.97,6719.97,6693.17,173.23,1161670.06,6702.3
|
||||
2018-07-17 11:00:00,0.0880541898029586,0.21086802375649066,5356.0,6697.33,6720.0,6696.01,155.38,1042120.84,6718.26
|
||||
2018-07-17 12:00:00,0.09866325851638928,0.228294372473011,5903.0,6719.21,6724.34,6675.63,201.22,1348614.56,6683.14
|
||||
2018-07-17 13:00:00,0.0994711918334477,0.21600808100382365,5666.0,6682.19,6708.01,6674.6,341.78,2287346.9,6699.3
|
||||
2018-07-17 14:00:00,0.1038629340479667,0.2376332340151667,5770.0,6699.76,6710.0,6682.88,207.8,1391864.94,6697.53
|
||||
2018-07-17 15:00:00,0.10365502473897802,0.2271468083106771,5911.0,6696.0,6764.47,6690.25,477.88,3212613.38,6718.0
|
||||
2018-07-17 16:00:00,0.09877498471380865,0.21739572998063109,6300.0,6718.0,6789.0,6711.68,407.12,2747512.48,6779.23
|
||||
2018-07-17 17:00:00,0.09924709835707932,0.21905868387840996,5622.0,6778.18,6778.18,6733.51,294.3,1986746.72,6751.72
|
||||
2018-07-17 18:00:00,0.09450994365782422,0.22158155212170066,7995.0,6748.56,7264.47,6745.83,2640.49,18620560.8,7183.99
|
||||
2018-07-17 19:00:00,0.08938425271174931,0.23788686788771235,10452.0,7186.99,7468.31,7171.33,2600.32,19126407.89,7346.91
|
||||
2018-07-17 20:00:00,0.08826820193223688,0.22801567852606316,7354.0,7348.42,7375.65,7295.13,874.26,6408774.66,7295.13
|
||||
2018-07-17 21:00:00,0.08757832718459499,0.2214965284757346,6001.0,7295.13,7332.21,7295.13,582.84,4263944.05,7317.11
|
||||
2018-07-17 22:00:00,0.10081845661400027,0.22948639301796517,5717.0,7317.11,7388.38,7312.89,378.16,2781076.34,7360.6
|
||||
2018-07-17 23:00:00,0.08739853380203005,0.20683654015580452,5405.0,7360.6,7379.0,7269.84,474.87,3475255.97,7310.56
|
||||
2018-07-18 00:00:00,0.10458653605004535,0.22565920099699327,4989.0,7315.57,7338.55,7296.6,270.47,1979623.49,7310.71
|
||||
2018-07-18 01:00:00,0.10426692766856487,0.2150542494067622,4636.0,7310.71,7369.52,7310.71,245.16,1799395.28,7341.22
|
||||
2018-07-18 02:00:00,0.09539051572444865,0.19786297896745184,4829.0,7342.1,7352.41,7315.01,125.74,922275.1,7336.99
|
||||
2018-07-18 03:00:00,0.10018737266442283,0.22056466916427314,4799.0,7335.94,7449.68,7328.49,511.71,3775483.69,7401.0
|
||||
2018-07-18 04:00:00,0.10003658691113283,0.21010508601828456,5090.0,7401.0,7546.68,7401.0,1576.15,11798048.6,7458.54
|
||||
2018-07-18 05:00:00,0.09632101300041417,0.19897012116782847,4972.0,7459.92,7471.41,7400.0,351.11,2608164.78,7433.0
|
||||
2018-07-18 06:00:00,0.10810567198577267,0.20782090857300461,4653.0,7433.0,7481.07,7416.27,281.91,2097039.92,7459.23
|
||||
2018-07-18 07:00:00,0.09837631003006138,0.19710796290408308,5090.0,7459.23,7477.57,7449.25,304.67,2272795.48,7464.86
|
||||
2018-07-18 08:00:00,0.10286441641920244,0.20909074678772308,6332.0,7464.36,7466.0,7403.8,665.62,4944348.99,7433.46
|
||||
2018-07-18 09:00:00,0.10236345987876981,0.1995544952702377,5765.0,7436.58,7437.98,7317.83,773.15,5691054.24,7362.08
|
||||
2018-07-18 10:00:00,0.09893850443227278,0.2046419638769042,5694.0,7362.98,7395.63,7361.96,338.75,2499982.03,7395.63
|
||||
2018-07-18 11:00:00,0.08721378948642718,0.20327885161459489,5534.0,7392.63,7424.42,7362.19,269.83,1996662.83,7397.61
|
||||
2018-07-18 12:00:00,0.08452868963858916,0.19673796033717042,6844.0,7397.61,7434.45,7380.97,386.63,2866370.21,7410.65
|
||||
2018-07-18 13:00:00,0.08691865918191342,0.20876507790721302,7171.0,7415.62,7468.31,7407.48,462.37,3441667.99,7430.0
|
||||
2018-07-18 14:00:00,0.09715237581185075,0.2151440570451033,7209.0,7433.99,7461.62,7385.98,383.98,2851271.78,7449.43
|
||||
2018-07-18 15:00:00,0.09719707503910022,0.2140918216301937,7222.0,7445.35,7525.17,7425.76,630.39,4706264.24,7446.3
|
||||
2018-07-18 16:00:00,0.09856703142075066,0.22027114066808534,6733.0,7442.35,7442.35,7378.0,817.06,6055122.46,7422.88
|
||||
2018-07-18 17:00:00,0.10700218237766784,0.23218631464650394,6493.0,7417.81,7451.99,7410.89,221.67,1647469.3,7431.18
|
||||
2018-07-18 18:00:00,0.11935964400029529,0.24128665869962426,5765.0,7433.67,7513.38,7428.63,1103.91,8247570.88,7500.86
|
||||
2018-07-18 19:00:00,0.11004891527390581,0.23781049893042644,6211.0,7498.5,7599.98,7376.0,1490.02,11172723.69,7428.05
|
||||
2018-07-18 20:00:00,0.10739199869227331,0.24353434180133784,5597.0,7422.02,7442.4,7339.12,649.21,4794432.56,7400.0
|
||||
2018-07-18 21:00:00,0.10121197130126297,0.24342249598605217,5381.0,7393.77,7393.77,7239.15,949.03,6922391.06,7336.18
|
||||
2018-07-18 22:00:00,0.10244741599158645,0.23728994500190434,4597.0,7337.99,7357.26,7299.92,177.31,1300009.44,7332.6
|
||||
2018-07-18 23:00:00,0.09582295967638106,0.22565433501245782,4333.0,7325.66,7388.99,7278.88,324.78,2380734.14,7359.68
|
||||
2018-07-19 00:00:00,0.07359266204853114,0.21975802525110122,4225.0,7362.54,7394.09,7340.71,307.69,2268343.66,7374.88
|
||||
2018-07-19 01:00:00,0.09088181849508321,0.20757276547809825,4603.0,7384.92,7384.92,7305.0,201.54,1479387.55,7340.77
|
||||
2018-07-19 02:00:00,0.10544487159119742,0.21616249279499702,4281.0,7342.6,7342.6,7278.84,286.2,2091067.8,7298.95
|
||||
2018-07-19 03:00:00,0.12028376135888774,0.22015166158516353,4346.0,7301.74,7342.45,7280.92,322.28,2354462.88,7323.19
|
||||
2018-07-19 04:00:00,0.10575343210474486,0.20353374171222816,4618.0,7320.75,7357.26,7316.87,131.57,965522.38,7316.87
|
||||
2018-07-19 05:00:00,0.11944988528427804,0.22145330825652093,4482.0,7319.58,7347.16,7305.67,203.41,1490708.7,7315.98
|
||||
2018-07-19 06:00:00,0.11394853026434082,0.21496222114861888,4422.0,7312.37,7336.99,7300.33,185.65,1359245.16,7329.47
|
||||
2018-07-19 07:00:00,0.11660140494214971,0.22604099746407488,4810.0,7329.47,7362.09,7326.23,262.75,1929530.25,7332.58
|
||||
2018-07-19 08:00:00,0.10234580298997183,0.2079983409586107,6110.0,7331.58,7345.39,7293.59,202.74,1484532.8,7310.83
|
||||
2018-07-19 09:00:00,0.10890424448438812,0.2110123359473201,5441.0,7310.83,7338.41,7298.38,358.44,2622125.54,7324.03
|
||||
2018-07-19 10:00:00,0.10259191610439748,0.19155543801644864,5681.0,7323.44,7420.0,7321.97,532.98,3927556.9,7408.79
|
||||
2018-07-19 11:00:00,0.09756634607806124,0.2002868882588213,5861.0,7407.76,7420.71,7370.46,218.13,1611513.72,7374.87
|
||||
2018-07-19 12:00:00,0.10470509818970977,0.22154672720611843,6197.0,7374.83,7434.34,7359.94,624.64,4622003.24,7416.99
|
||||
2018-07-19 13:00:00,0.10366334418573576,0.21574768659840218,6151.0,7417.6,7452.0,7406.78,334.26,2482521.47,7423.27
|
||||
2018-07-19 14:00:00,0.10688106583353042,0.22254927883140213,6584.0,7423.27,7513.0,7414.05,703.35,5248927.21,7486.65
|
||||
2018-07-19 15:00:00,0.11044259314994757,0.2338962169074704,6786.0,7484.21,7484.21,7431.1,485.06,3616235.46,7448.6
|
||||
2018-07-19 16:00:00,0.11669284158808829,0.2348234412544244,6886.0,7454.44,7483.29,7363.63,735.65,5462758.12,7391.83
|
||||
2018-07-19 17:00:00,0.09872221884419828,0.21150099988977572,6205.0,7391.83,7419.99,7348.27,609.23,4499487.53,7371.86
|
||||
2018-07-19 18:00:00,0.10839835323482973,0.23050014608126324,5922.0,7363.07,7431.1,7358.62,353.88,2620114.63,7422.04
|
||||
2018-07-19 19:00:00,0.1210115284698595,0.2446549891303321,5516.0,7421.89,7450.0,7406.46,248.57,1847374.31,7420.81
|
||||
2018-07-19 20:00:00,0.12769561242543415,0.2620480951938342,5346.0,7424.43,7488.0,7415.0,335.15,2499285.31,7448.08
|
||||
2018-07-19 21:00:00,0.11627265435113132,0.24421462507996972,4842.0,7448.08,7481.02,7414.66,341.19,2541848.79,7416.59
|
||||
2018-07-19 22:00:00,0.12117015923875779,0.23701036943748027,4596.0,7421.22,7460.2,7420.22,45.09,335446.22,7436.93
|
||||
2018-07-19 23:00:00,0.0787649784566368,0.23165228792384185,4187.0,7436.93,7570.9,7433.99,793.46,5947075.84,7479.61
|
||||
2018-07-20 00:00:00,0.08728006645427411,0.2209028353939531,4039.0,7485.18,7510.43,7431.1,288.01,2150833.25,7471.42
|
||||
2018-07-20 01:00:00,0.11516674961173322,0.2221229195530492,4113.0,7474.35,7475.99,7367.09,395.84,2932585.49,7409.27
|
||||
2018-07-20 02:00:00,0.10425012582819412,0.2272496181537681,3795.0,7409.62,7456.04,7389.34,193.28,1435525.58,7454.99
|
||||
2018-07-20 03:00:00,0.07460754622046298,0.21440884171861302,4032.0,7455.67,7471.23,7440.71,165.44,1233432.4,7453.99
|
||||
2018-07-20 04:00:00,0.1037929786100678,0.20109405904353525,3820.0,7455.15,7467.35,7428.37,188.45,1404045.57,7464.99
|
||||
2018-07-20 05:00:00,0.10570982080896332,0.22670832913753183,4181.0,7464.99,7466.48,7433.89,133.83,996276.67,7446.46
|
||||
2018-07-20 06:00:00,0.10053325863541185,0.21788108532109776,3782.0,7446.46,7454.99,7398.94,279.81,2077634.57,7409.78
|
||||
2018-07-20 07:00:00,0.08414827709682643,0.21499163800879564,4313.0,7409.78,7429.36,7338.91,630.88,4659133.12,7368.86
|
||||
2018-07-20 08:00:00,0.09010914398328636,0.20536192724458174,5755.0,7366.46,7446.8,7340.56,659.84,4872673.97,7439.8
|
||||
2018-07-20 09:00:00,0.09879370523308237,0.22031407763039842,5104.0,7438.04,7503.81,7433.81,548.73,4094985.82,7462.19
|
||||
2018-07-20 10:00:00,0.08814294560410647,0.18488960368603197,5180.0,7462.76,7479.99,7425.0,331.32,2468681.38,7441.72
|
||||
2018-07-20 11:00:00,0.10208339855904663,0.21178035276313925,5272.0,7439.52,7471.0,7437.96,234.2,1747024.94,7464.71
|
||||
2018-07-20 12:00:00,0.09804571430916943,0.20979500104083415,5459.0,7465.73,7514.47,7458.15,366.74,2744311.79,7494.0
|
||||
2018-07-20 13:00:00,0.09371463122741983,0.2187961052503923,5598.0,7494.0,7506.96,7437.47,245.64,1833699.6,7465.04
|
||||
2018-07-20 14:00:00,0.1171893448208811,0.2260779619183398,6369.0,7465.33,7478.73,7440.09,135.4,1010243.63,7466.15
|
||||
2018-07-20 15:00:00,0.10416287280578328,0.22643467088380403,6399.0,7466.15,7497.58,7431.1,318.68,2380183.59,7475.51
|
||||
2018-07-20 16:00:00,0.10553985034667926,0.21745263970717474,6361.0,7475.51,7517.84,7447.21,290.96,2174703.55,7471.67
|
||||
2018-07-20 17:00:00,0.09850890569577489,0.21561035761872477,6394.0,7471.46,7696.88,7463.39,1510.0,11439228.05,7463.39
|
||||
2018-07-20 18:00:00,0.10134484567020713,0.23067609815253698,6217.0,7461.0,7492.15,7290.01,1046.06,7723792.29,7330.39
|
||||
2018-07-20 19:00:00,0.09659720598948149,0.226286999012059,5433.0,7332.45,7388.0,7265.0,479.71,3516136.44,7357.98
|
||||
2018-07-20 20:00:00,0.10229267973913325,0.2380914675633593,4785.0,7354.54,7380.99,7290.0,289.33,2122913.02,7344.97
|
||||
2018-07-20 21:00:00,0.10900678497248634,0.23506402045291955,4398.0,7344.99,7375.0,7321.77,147.05,1080813.42,7349.99
|
||||
2018-07-20 22:00:00,0.10832154132262885,0.24604585086842823,3684.0,7350.28,7370.77,7284.48,130.47,957538.97,7300.22
|
||||
2018-07-20 23:00:00,0.09528614152701649,0.23572697984581573,3691.0,7300.22,7372.2,7276.43,221.74,1624157.14,7359.41
|
||||
2018-07-21 00:00:00,0.0933235286912038,0.20864850497750845,3541.0,7359.41,7359.41,7305.51,435.78,3191418.87,7330.84
|
||||
2018-07-21 01:00:00,0.11307970936560656,0.22957918763657537,3973.0,7330.84,7330.84,7212.0,870.94,6330338.54,7237.33
|
||||
2018-07-21 02:00:00,0.09658274372458275,0.2004882477405005,4107.0,7237.52,7272.82,7222.04,148.68,1077986.74,7260.82
|
||||
2018-07-21 03:00:00,0.1128373221892612,0.2257256708766154,3546.0,7260.81,7301.55,7255.36,232.07,1691227.04,7276.99
|
||||
2018-07-21 04:00:00,0.10440665521966554,0.20534172494766,3429.0,7276.99,7292.98,7256.7,99.61,724964.94,7276.7
|
||||
2018-07-21 05:00:00,0.1217976329133393,0.22545816021356463,3608.0,7276.7,7301.71,7252.01,66.48,483680.98,7301.65
|
||||
2018-07-21 06:00:00,0.10741531073319543,0.20666120511628086,3556.0,7301.74,7334.74,7286.9,150.87,1102500.41,7322.46
|
||||
2018-07-21 07:00:00,0.10464420305147851,0.2204195054748561,3672.0,7328.24,7352.42,7305.07,202.44,1483369.93,7343.78
|
||||
2018-07-21 08:00:00,0.10354493260245629,0.22569615022402867,4264.0,7343.27,7344.99,7311.6,199.19,1459975.45,7322.97
|
||||
2018-07-21 09:00:00,0.1098208735910409,0.21986457019400601,3827.0,7321.09,7328.12,7297.74,90.87,664644.84,7312.98
|
||||
2018-07-21 10:00:00,0.11901263145477894,0.21877047037671676,4266.0,7312.99,7339.49,7307.06,70.72,518060.6,7323.96
|
||||
2018-07-21 11:00:00,0.10593457957705563,0.21222636737527462,3937.0,7320.01,7333.74,7281.42,358.36,2614648.61,7286.96
|
||||
2018-07-21 12:00:00,0.09928269927455888,0.19453921211553568,4589.0,7286.95,7321.05,7281.55,126.68,923983.24,7313.47
|
||||
2018-07-21 13:00:00,0.11923258790066131,0.21638568264272276,4439.0,7307.09,7357.25,7293.04,156.19,1145572.08,7340.29
|
||||
2018-07-21 14:00:00,0.10098612445382547,0.2016477467970073,4529.0,7340.99,7370.0,7340.0,250.54,1843444.5,7356.01
|
||||
2018-07-21 15:00:00,0.10157389389702721,0.202950967384158,4902.0,7352.0,7380.66,7345.26,182.97,1347541.91,7350.68
|
||||
2018-07-21 16:00:00,0.09641253371676677,0.21278828120634385,4833.0,7350.68,7413.2,7336.59,196.62,1453185.74,7390.67
|
||||
2018-07-21 17:00:00,0.0995958974354282,0.20680878531929076,4700.0,7389.98,7429.79,7382.99,156.83,1161130.54,7399.97
|
||||
2018-07-21 18:00:00,0.10905814218071068,0.22951690316149903,4210.0,7397.18,7428.78,7371.93,170.1,1258317.25,7410.33
|
||||
2018-07-21 19:00:00,0.07858763234272367,0.2569517234589236,4097.0,7405.12,7443.33,7405.11,87.93,653121.75,7432.99
|
||||
2018-07-21 20:00:00,0.1113174433622039,0.2267321518306913,3831.0,7433.99,7449.66,7393.01,80.07,594254.35,7424.99
|
||||
2018-07-21 21:00:00,0.11224272657066275,0.23438750080921497,3591.0,7418.61,7458.0,7409.56,69.65,517298.07,7414.2
|
||||
2018-07-21 22:00:00,0.09651080685798608,0.21117207476044,3859.0,7414.2,7430.16,7395.22,26.6,197235.09,7412.56
|
||||
2018-07-21 23:00:00,0.10581036678100333,0.20383254887140026,4054.0,7412.56,7425.0,7396.83,29.27,216812.48,7415.57
|
||||
2018-07-22 00:00:00,0.10194283543860032,0.21383450044430183,4367.0,7424.99,7425.0,7400.8,26.13,193732.87,7409.92
|
||||
2018-07-22 01:00:00,0.1040755127249144,0.21786207321381515,3273.0,7398.16,7449.67,7336.15,268.23,1981138.35,7446.98
|
||||
2018-07-22 02:00:00,0.1152619306152038,0.2088211541143237,3301.0,7446.45,7478.0,7422.96,110.05,820165.37,7472.39
|
||||
2018-07-22 03:00:00,0.1194643276315172,0.2178897738159717,3235.0,7476.54,7486.04,7418.44,182.77,1363437.32,7433.53
|
||||
2018-07-22 04:00:00,0.12050623290831733,0.22002535380158078,3241.0,7425.0,7430.16,7398.44,140.51,1041527.4,7408.48
|
||||
2018-07-22 05:00:00,0.1350877194600542,0.23946630493812396,3096.0,7415.96,7415.96,7358.61,249.65,1843379.84,7394.89
|
||||
2018-07-22 06:00:00,0.1262987140367526,0.245820422036782,3177.0,7389.1,7405.99,7365.0,117.91,870528.63,7404.89
|
||||
2018-07-22 07:00:00,0.11411391234579984,0.2309179823335156,3383.0,7398.32,7405.55,7365.02,36.81,271870.33,7385.51
|
||||
2018-07-22 08:00:00,0.11418540423608992,0.2216572789009823,3891.0,7393.17,7404.43,7380.5,90.13,666107.38,7388.59
|
||||
2018-07-22 09:00:00,0.12278375136149591,0.2023395371945722,3863.0,7395.69,7462.42,7388.63,175.52,1304602.31,7442.52
|
||||
2018-07-22 10:00:00,0.11977810676752554,0.21598897881516124,4253.0,7440.53,7455.01,7428.55,73.56,547357.24,7442.6
|
||||
2018-07-22 11:00:00,0.10547037658357916,0.2006770907117357,4474.0,7434.3,7460.0,7426.63,102.93,766518.15,7447.7
|
||||
2018-07-22 12:00:00,0.12202057407182905,0.20553985614888104,5258.0,7456.45,7468.31,7442.69,63.61,474263.13,7450.98
|
||||
2018-07-22 13:00:00,0.09292394764555745,0.20145485862793946,4573.0,7450.88,7457.59,7427.11,84.88,631847.95,7429.06
|
||||
2018-07-22 14:00:00,0.08671733818551153,0.18255175859268646,4850.0,7427.3,7486.98,7427.3,145.01,1081758.53,7459.55
|
||||
2018-07-22 15:00:00,0.08965528482891023,0.183903537238312,4655.0,7459.56,7515.0,7449.75,288.01,2155953.39,7481.92
|
||||
2018-07-22 16:00:00,0.10648621710232598,0.20488575993511304,5139.0,7481.92,7551.94,7481.92,687.13,5175198.38,7526.63
|
||||
2018-07-22 17:00:00,0.10659364491955034,0.206219169508721,4769.0,7526.64,7544.93,7479.0,179.9,1352034.77,7517.17
|
||||
2018-07-22 18:00:00,0.08911612480432447,0.20697637765328827,4332.0,7517.26,7523.37,7481.54,66.57,499181.8,7497.72
|
||||
2018-07-22 19:00:00,0.08788718672766556,0.1950247629306966,4146.0,7493.03,7533.41,7493.03,75.79,569715.5,7508.51
|
||||
2018-07-22 20:00:00,0.09081366743628874,0.20768423288149673,3764.0,7508.57,7535.0,7501.19,237.26,1784881.96,7520.01
|
||||
2018-07-22 21:00:00,0.10586384162224395,0.21782724942968124,4331.0,7523.21,7546.99,7515.97,230.22,1734329.47,7520.0
|
||||
2018-07-22 22:00:00,0.11126556695736717,0.2349190790601346,4033.0,7522.36,7581.03,7338.91,968.69,7187218.03,7365.92
|
||||
2018-07-22 23:00:00,0.10922222548953939,0.2087997272094735,3853.0,7375.99,7423.2,7340.23,308.61,2278158.96,7406.09
|
||||
2018-07-23 00:00:00,0.10475577050207723,0.1975975491182442,3461.0,7402.32,7413.97,7376.32,224.23,1658642.03,7396.6
|
||||
2018-07-23 01:00:00,0.11149394600059791,0.212647853168363,3535.0,7393.5,7474.01,7369.86,609.63,4524224.24,7465.43
|
||||
2018-07-23 02:00:00,0.11047602335988127,0.22075716985714774,3452.0,7465.68,7520.0,7448.82,217.97,1631448.96,7518.64
|
||||
2018-07-23 03:00:00,0.09511737513819504,0.20342365895426537,3986.0,7518.64,7673.0,7511.86,1669.28,12700066.1,7637.22
|
||||
2018-07-23 04:00:00,0.09786054067564111,0.1928907161786437,4245.0,7637.22,7648.99,7606.21,210.54,1605740.75,7629.49
|
||||
2018-07-23 05:00:00,0.11115644692094986,0.19892366004931056,4420.0,7626.27,7707.68,7623.62,691.15,5300988.05,7679.95
|
||||
2018-07-23 06:00:00,0.0930190659985257,0.1914312574946213,3764.0,7675.71,7678.69,7628.58,221.08,1691646.71,7649.09
|
||||
2018-07-23 07:00:00,0.09844149580000178,0.2127421874307118,4030.0,7649.99,7703.83,7644.1,456.85,3505971.91,7680.0
|
||||
2018-07-23 08:00:00,0.1032116792203447,0.20561868482535076,5550.0,7679.99,7786.92,7647.99,1135.38,8779071.84,7749.51
|
||||
2018-07-23 09:00:00,0.08239309692567295,0.20248244219901074,5773.0,7749.51,7761.93,7678.7,494.54,3814472.79,7710.6
|
||||
2018-07-23 10:00:00,0.0935212329017346,0.18504442677479818,5171.0,7711.42,7721.77,7666.59,385.52,2967772.35,7711.08
|
||||
2018-07-23 11:00:00,0.10625135285351818,0.1971017527934391,5196.0,7716.81,7717.2,7670.0,269.5,2072539.69,7692.32
|
||||
2018-07-23 12:00:00,0.113248706472941,0.227611147137981,5627.0,7692.46,7711.99,7644.14,373.01,2865394.65,7683.28
|
||||
2018-07-23 13:00:00,0.09681735481146139,0.20133591857355834,5724.0,7683.28,7716.81,7660.0,223.22,1715839.95,7699.99
|
||||
2018-07-23 14:00:00,0.08262967845476092,0.21096312091455532,6166.0,7699.99,7728.86,7695.74,438.37,3380900.33,7716.09
|
||||
2018-07-23 15:00:00,0.09774247097918552,0.2200634400549309,5847.0,7716.09,7721.77,7680.51,442.38,3409098.97,7694.43
|
||||
2018-07-23 16:00:00,0.09576028411968329,0.21172021045644548,5821.0,7694.56,7740.0,7687.49,828.8,6400465.69,7716.45
|
||||
2018-07-23 17:00:00,0.09950865597332739,0.23053129702339756,5665.0,7719.99,7740.0,7701.99,592.3,4577037.35,7733.59
|
||||
2018-07-23 18:00:00,0.0932363588627437,0.2090972509522472,5399.0,7733.59,7749.0,7707.39,324.45,2505429.39,7728.37
|
||||
2018-07-23 19:00:00,0.09243442142842846,0.2301853582351032,5606.0,7727.4,7800.0,7638.03,1396.32,10800309.53,7735.53
|
||||
2018-07-23 20:00:00,0.08816379064295315,0.22017791032853326,5420.0,7736.08,7800.0,7724.5,514.3,3993228.42,7744.19
|
||||
2018-07-23 21:00:00,0.10728226544791052,0.23563579471332594,5164.0,7746.99,7763.59,7690.16,237.63,1836633.86,7706.0
|
||||
2018-07-23 22:00:00,0.09449302248273747,0.271796113269155,4646.0,7699.13,7759.99,7690.5,63.31,489000.25,7750.09
|
||||
2018-07-23 23:00:00,0.0742455916391433,0.2316396917419365,4455.0,7754.57,7777.0,7715.45,280.46,2173424.81,7722.32
|
||||
2018-07-24 00:00:00,0.08086966898267806,0.2193670741288557,3862.0,7722.95,7730.61,7690.17,496.48,3830571.66,7719.62
|
||||
2018-07-24 01:00:00,0.09071721460745699,0.2126255809666524,4620.0,7712.46,7727.7,7691.14,163.99,1264085.79,7723.22
|
||||
|
@@ -0,0 +1,342 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"sns.set()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['../dataset/AMD.csv',\n",
|
||||
" '../dataset/FB.csv',\n",
|
||||
" '../dataset/FSV.csv',\n",
|
||||
" '../dataset/INFY.csv',\n",
|
||||
" '../dataset/KNX.csv',\n",
|
||||
" '../dataset/MONDY.csv',\n",
|
||||
" '../dataset/MTDR.csv',\n",
|
||||
" '../dataset/SINA.csv',\n",
|
||||
" '../dataset/TMUS.csv',\n",
|
||||
" '../dataset/TSLA.csv',\n",
|
||||
" '../dataset/TWTR.csv']"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"directory = '../dataset/'\n",
|
||||
"ori_name = ['AMD.csv', 'FB.csv', 'FSV.csv', 'INFY.csv', 'KNX.csv',\n",
|
||||
" 'MONDY.csv', 'MTDR.csv', 'SINA.csv', 'TMUS.csv', 'TSLA.csv', 'TWTR.csv']\n",
|
||||
"stocks = [directory + s for s in ori_name]\n",
|
||||
"stocks"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dfs = [pd.read_csv(s)[['Date', 'Close']] for s in stocks]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<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>Close_x</th>\n",
|
||||
" <th>Close_y</th>\n",
|
||||
" <th>Close_x</th>\n",
|
||||
" <th>Close_y</th>\n",
|
||||
" <th>Close_x</th>\n",
|
||||
" <th>Close_y</th>\n",
|
||||
" <th>Close_x</th>\n",
|
||||
" <th>Close_y</th>\n",
|
||||
" <th>Close_x</th>\n",
|
||||
" <th>Close_y</th>\n",
|
||||
" <th>Close</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>16.270000</td>\n",
|
||||
" <td>207.320007</td>\n",
|
||||
" <td>78.820000</td>\n",
|
||||
" <td>9.710</td>\n",
|
||||
" <td>37.910000</td>\n",
|
||||
" <td>56.889999</td>\n",
|
||||
" <td>31.809999</td>\n",
|
||||
" <td>84.070000</td>\n",
|
||||
" <td>61.680000</td>\n",
|
||||
" <td>318.869995</td>\n",
|
||||
" <td>44.490002</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>16.580000</td>\n",
|
||||
" <td>207.229996</td>\n",
|
||||
" <td>78.250000</td>\n",
|
||||
" <td>9.800</td>\n",
|
||||
" <td>36.360001</td>\n",
|
||||
" <td>56.639999</td>\n",
|
||||
" <td>31.670000</td>\n",
|
||||
" <td>83.949997</td>\n",
|
||||
" <td>61.630001</td>\n",
|
||||
" <td>310.100006</td>\n",
|
||||
" <td>44.259998</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>16.870001</td>\n",
|
||||
" <td>209.990005</td>\n",
|
||||
" <td>77.940002</td>\n",
|
||||
" <td>9.950</td>\n",
|
||||
" <td>36.279999</td>\n",
|
||||
" <td>57.730000</td>\n",
|
||||
" <td>32.020000</td>\n",
|
||||
" <td>84.870003</td>\n",
|
||||
" <td>61.209999</td>\n",
|
||||
" <td>322.690002</td>\n",
|
||||
" <td>44.709999</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>16.850000</td>\n",
|
||||
" <td>209.360001</td>\n",
|
||||
" <td>77.940002</td>\n",
|
||||
" <td>9.840</td>\n",
|
||||
" <td>37.500000</td>\n",
|
||||
" <td>57.810001</td>\n",
|
||||
" <td>31.740000</td>\n",
|
||||
" <td>83.989998</td>\n",
|
||||
" <td>60.520000</td>\n",
|
||||
" <td>323.850006</td>\n",
|
||||
" <td>43.340000</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>16.709999</td>\n",
|
||||
" <td>208.089996</td>\n",
|
||||
" <td>78.055000</td>\n",
|
||||
" <td>9.855</td>\n",
|
||||
" <td>37.990002</td>\n",
|
||||
" <td>52.380001</td>\n",
|
||||
" <td>32.330002</td>\n",
|
||||
" <td>82.940002</td>\n",
|
||||
" <td>59.410000</td>\n",
|
||||
" <td>320.230011</td>\n",
|
||||
" <td>43.439999</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Close_x Close_y Close_x Close_y Close_x Close_y Close_x \\\n",
|
||||
"0 16.270000 207.320007 78.820000 9.710 37.910000 56.889999 31.809999 \n",
|
||||
"1 16.580000 207.229996 78.250000 9.800 36.360001 56.639999 31.670000 \n",
|
||||
"2 16.870001 209.990005 77.940002 9.950 36.279999 57.730000 32.020000 \n",
|
||||
"3 16.850000 209.360001 77.940002 9.840 37.500000 57.810001 31.740000 \n",
|
||||
"4 16.709999 208.089996 78.055000 9.855 37.990002 52.380001 32.330002 \n",
|
||||
"\n",
|
||||
" Close_y Close_x Close_y Close \n",
|
||||
"0 84.070000 61.680000 318.869995 44.490002 \n",
|
||||
"1 83.949997 61.630001 310.100006 44.259998 \n",
|
||||
"2 84.870003 61.209999 322.690002 44.709999 \n",
|
||||
"3 83.989998 60.520000 323.850006 43.340000 \n",
|
||||
"4 82.940002 59.410000 320.230011 43.439999 "
|
||||
]
|
||||
},
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from functools import reduce\n",
|
||||
"data = reduce(lambda left,right: pd.merge(left,right,on='Date'), dfs).iloc[:, 1:]\n",
|
||||
"data.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"returns = data.pct_change()\n",
|
||||
"mean_daily_returns = returns.mean()\n",
|
||||
"volatilities = returns.std()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Close_x 0.995185\n",
|
||||
"Close_y -0.247949\n",
|
||||
"Close_x 0.119677\n",
|
||||
"Close_y 0.190845\n",
|
||||
"Close_x -0.175416\n",
|
||||
"Close_y -0.170502\n",
|
||||
"Close_x -0.626256\n",
|
||||
"Close_y -0.450914\n",
|
||||
"Close_x 0.252493\n",
|
||||
"Close_y -0.069273\n",
|
||||
"Close -0.273753\n",
|
||||
"dtype: float64"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"mean_daily_returns * 252"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Close_x 12.196632\n",
|
||||
"Close_y 6.637175\n",
|
||||
"Close_x 3.677834\n",
|
||||
"Close_y 3.572859\n",
|
||||
"Close_x 7.104904\n",
|
||||
"Close_y 7.909165\n",
|
||||
"Close_x 8.121732\n",
|
||||
"Close_y 6.948244\n",
|
||||
"Close_x 3.863498\n",
|
||||
"Close_y 10.213733\n",
|
||||
"Close 8.873234\n",
|
||||
"dtype: float64"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"volatilities * 252"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"combine = pd.DataFrame({'returns': mean_daily_returns * 252,\n",
|
||||
" 'volatility': volatilities * 252})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfoAAAHwCAYAAABOjq0vAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzs3Xl8VPW9//HXmT2zZLKSECDsHBBQXBBRcV/xurS9be3uvba1tba9vb9u11Zre6/23t7e7ntrW3tra3fr7aZiaxV3FEFUhkUghCRkm8w+Z872+2OSkECAJCSZJZ/n48GDYXLmzGdCMu/5fs93UWzbRgghhBDlyVHoAoQQQggxeSTohRBCiDImQS+EEEKUMQl6IYQQooxJ0AshhBBlTIJeCCGEKGMS9EIIIUQZk6AXQgghypgEvRBCCFHGJOiFEEKIMiZBL4QQQpQxV6ELmGpdXYmiXNy/utpPNJoudBljIjVPDal5akjNU+NoNdfXh5QClDMtSIu+SLhczkKXMGZS89SQmqeG1Dw1SrHmUidBL4QQQpSxadd1L8RUMCzQdGNCzmX3pklrE3Ou8fC6XbikSSBEyZKgF2ISaLrBc68enJBzhYI+EsnshJxrPFYva8DllbcKIUqVfE4XQgghypgEvRBCCFHGJOiFEEKIMiZBL4QQQpQxCXohhBCijEnQCyGEEGVMgl4IIYQoYxL0QgghRBmToBdCCCHKmAS9EEIIUcYk6IUQQogyJkEvhBBClDEJeiGEEKKMSdALIYQQZUyCXgghhChjssm0EEXKtm1yhoWS0UlndQBcLgdupwNFUQpcnRCiVEjQC1FAtm0TS+WIxjV6ExrxVI5kRieV1cnp1oiPUQCvx0mwwk2wwk1V0ENNpY+aSh9+n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IRQFIXasI/asI8VC2oxLZsZ1RXsa08QaYny2JY2NjzfigLMa6pkYVMlS5urWTKnSlr8omxI0E8zumGxKdLJ3144wK4DMbweJ5euns2lZ8yhptJX6PKEmFROh8KCpjAr59fyD2fPwzAt9rTH2d7Sx572BI+92MaGTfngnz0jiNpcxbLmahZL8IsSJkE/TXT3Zfj7ljYe29JGIq3TUF3B9Rcv5tyVjfh98gYmpieX08Hi2VUsnl1FfX2ItvYYe9rjRFqibG/p4+9Dgn9WfaD/2DBL5lTJB2NRMiToy1g6q7Mp0sVT2zqI7O9DUWDVojouOm02y+ZVy77gQhzG7XKwZE4VS+ZUcfU5+R6wgeDf0RrjyZc7+NvmAwDUVnoHg3/x7Cqa6gPyOyWKkgR9mdF0k5f39PL0yx28uKsHw7RoqPHzunXzOXvFTGrD0goRYrSGBj+AaVm0dqbY2drHjtYYr+6L8vQrBwHwe10smh1m4awwC2ZWMm9miID0lokiIEFfBnrjWbbs7mHLrm5e3RdFNywq/W4uOLWJtcsbZb9vISaI0+FgbmOIuY0hLjljDrZt0xXLsnN/Hztb+9jZGmPr7p7B4xtq/CyYGWL+zErmN1XSPCMouzmKKSdBX4Li6Rw798fYsb+PSEuUls4kAPVVPs5f1cSqRXWozVU4HbL5hxCTSVEUZlRVMKOqgnNWzgTyl8z2dCTY0xZnT3ucV/ZFeerlfKvf6VCYWesf3KRndn2A2fVBqkNe+TAuJo0EfZHTciatXfn1vfd2JNjZ2kd7T359ebfLwcKmSt54wUJOXlRLine truncated
|
||||
"text/plain": [
|
||||
"<Figure size 504x504 with 3 Axes>"
|
||||
]
|
||||
},
|
||||
"metadata": {
|
||||
"needs_background": "light"
|
||||
},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"g = sns.jointplot(\"volatility\", \"returns\", data=combine, kind=\"reg\",height=7)\n",
|
||||
"\n",
|
||||
"for i in range(combine.shape[0]):\n",
|
||||
" plt.annotate(ori_name[i].replace('.csv',''), (combine.iloc[i, 1], combine.iloc[i, 0]))\n",
|
||||
" \n",
|
||||
"plt.text(0, -1.5, 'SELL', fontsize=25)\n",
|
||||
"plt.text(0, 1.0, 'BUY', fontsize=25)\n",
|
||||
" \n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.8"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 34 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 33 KiB |