├── .gitignore ├── LICENSE ├── README.md ├── code ├── IBM_Transformer+TimeEmbedding.ipynb ├── learning-pytorch-lstm-deep-learning-with-m5-data.ipynb └── transformers-for-stocks.ipynb ├── data └── ak │ ├── stock_zh_sh000300.pkl │ └── stock_zh_sh000905.pkl ├── doc ├── TODO.md ├── assert │ └── Transformer结构.jpg ├── level2.md ├── 乱七八糟.md ├── 参考网址.md ├── 学习笔记.md ├── 课题研究内容.pdf └── 选题+任务书.md ├── nb.ipynb ├── paper ├── caj │ ├── 基于CNN-Transformer的黄金期货价格预测_蒋超宇.pdf │ ├── 基于LSTM与Transformer模型的股价预测_辛洲扬.pdf │ ├── 基于Trans-T2V的量化选股策略研究_李瞿成.pdf │ ├── 基于Transformer的选股因子挖掘_曹昀炀.pdf │ ├── 基于技术面数据的自监督学习股市涨跌预测方法.pdf │ └── 基于神经网络的多变量时间序列预测_陈超.pdf ├── 其他 │ ├── Hierarchical Multi-Scale Gaussian Transformer for Stock Movement Prediction.pdf │ ├── Level-2证券行情数据分析系统的设计与实现_程磊.pdf │ ├── Predicting Stock Closing Prices in Emerging Markets with Transformer.pdf │ ├── Stock_Price_Prediction_Based_on_Temporal_Fusion_Transformer.pdf │ ├── Transformer-based attention network for stock movement prediction.pdf │ ├── 基于BERT-SA-LSTM的股指期货价格预测研究_唐韬.pdf │ └── 基于Informer模型的期权交易策略设计_刘一凡.pdf ├── 因子 │ └── 基于XGBoost的高频交易选股研究_王怡宁.pdf ├── 实验 │ └── TRANSFORMER-BASED DEEP LEARNING MODEL FOR STOCK PRICE PREDICTION.pdf ├── 文档 │ ├── Level_2_Account_form.pdf │ └── 上海证券交易所交易规则(2020年第二次修订).pdf ├── 模型 │ ├── -1LSTM:Long Short-Term Memory Neural Network for Financial Time Series.pdf │ ├── 0提出Transformer的论文:Attention Is All You Need.pdf │ └── Time2Vec:Learning a Vector Representation of Time.pdf └── 研报 │ ├── 2021-03-11_东方证券_金融工程_因子选股系列之七十四:神经网络日频alpha模型初步实践.pdf │ ├── 2022-05-31_海通证券_金融工程_选股因子系列研究(七十九):用注意力机制优化深度学习高频因子.pdf │ ├── 2022-06-12_东方证券_金融工程_因子选股系列之八十三:多模型学习量价时序特征.pdf │ ├── 选股因子系列研究(十一)——Level2 行情.pdf │ └── 选股因子系列研究(七十六)——基于深度学习的高频因子挖掘.pdf └── src ├── lstm.py ├── main.py ├── transformer.ipynb ├── transformer.py └── util.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | 132 | ### 133 | .DS_Store 134 | .idea 135 | .vscode 136 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # transformer-quant 2 | 3 | 基于Transformer架构的量化金融预测研究 4 | -------------------------------------------------------------------------------- /data/ak/stock_zh_sh000300.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/data/ak/stock_zh_sh000300.pkl -------------------------------------------------------------------------------- /data/ak/stock_zh_sh000905.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/data/ak/stock_zh_sh000905.pkl -------------------------------------------------------------------------------- /doc/TODO.md: -------------------------------------------------------------------------------- 1 | ## Done 2 | 3 | 根据 https://towardsdatascience.com/stock-predictions-with-state-of-the-art-transformer-and-time-embeddings-3a4485237de6 博文搭建一个简单的预测模型 4 | 5 | ## TODO 6 | 7 | 什么是level2? 8 | 9 | 10 | 创建一个腾讯文档,保存看过的论文集 11 | 12 | 每周五早上总结,下午开会 13 | 14 | transformer历史,有关它以及之前的模型的历史,我记得这是有很多教程的 15 | transformer搭建 16 | transformer简单应用起来 17 | 看transformer变种论文 18 | 19 | 20 | 量化,选因子等过程是什么 21 | 22 | 23 | ## 问题 24 | 25 | ### 解决的问题是什么?输入输出是什么? 26 | 27 | 使用Transformer预测下一段时间的值? 28 | decoder传的输入只是下一段时间的值还是滑窗的值? 29 | 是否只使用encoder? 30 | 31 | 32 | 33 | 预测的是涨幅?然后排序 34 | 使用Transformer选股? 35 | 有几篇论文都是量化选股 36 | 有一篇单步、多步预测μ和σ,然后采样 37 | 有一篇直接预测接下来的值,但是感觉这样效果不好,预测曲线相当于实际曲线右移(与今天股价最相关的是昨天的股价) 38 | 39 | 40 | 预测的时间不一定是同级别的吧,秒级数据可能在分钟级别甚至日级别的预测中才有作用? 41 | 什么是选因子?选因子好像也涉及到量化选股方面的事情? 42 | 43 | ### 如何解决问题 44 | 45 | Informer? 46 | -------------------------------------------------------------------------------- /doc/assert/Transformer结构.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/doc/assert/Transformer结构.jpg -------------------------------------------------------------------------------- /doc/level2.md: -------------------------------------------------------------------------------- 1 | level2行情数据 2 | 3 | 十档行情,买卖队列,逐笔成交 4 | 5 | - level1,6秒一次;level2,3秒一次 6 | - level1,5档委买委卖;level2,10档 7 | - 逐笔成交明细,实际“价格优先、时间优先” 的每笔撮合结果 8 | - 撤单数据 -------------------------------------------------------------------------------- /doc/乱七八糟.md: -------------------------------------------------------------------------------- 1 | 用VSCode写python不好的体验: 2 | - 悬浮文档没啥用,的公式没有渲染 3 | - 方法的参数有时候没有提示,比如torch.nn.LSTM 4 | 5 | 好的体验: 6 | - 轻量,快速 7 | - 默认的主题就很好看了 8 | 9 | 10 | 11 | 双均线策略: 12 | https://uqer.datayes.com/v3/community/share/554051bbf9f06c1c3d687fac 13 | 14 | 行业优选下的股息率选股策略: 15 | https://uqer.datayes.com/v3/community/share/591e61b11bfe1200537f1f33 16 | 17 | -------------------------------------------------------------------------------- /doc/参考网址.md: -------------------------------------------------------------------------------- 1 | 2 | ## 论文 3 | 4 | ReadPaper网站收藏的Transformer文件夹下的论文 5 | Transformer: Attention is all you need 6 | Transformer-XL 7 | Longformer 8 | 9 | Informer? 10 | 11 | MTGNN 和 TPA-LSTM ? 12 | 13 | 知网上收藏的硕博论文等 14 | 15 | 16 | 谷歌学术搜的: 17 | TRANSFORMER-BASED DEEP LEARNING MODEL FOR STOCK PRICE PREDICTION: A CASE STUDY ON BANGLADESH STOCK MARKET 18 | 19 | 20 | ## 博客 21 | 22 | 【时序】时间/时空序列分类/预测/异常检测顶会论文资源汇总 23 | https://juejin.cn/post/7123200971051630606#heading-2 24 | 25 | Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解 26 | https://blog.csdn.net/zhaohongfei_358/article/details/126019181 27 | 28 | 29 | 知乎 毕设 收藏夹收藏的文章 30 | 31 | 【时间序列】Transformer for TimeSeries时序预测算法详解 32 | https://zhuanlan.zhihu.com/p/391337035 33 | 34 | Transformers in Time Series: A Survey 35 | https://zhuanlan.zhihu.com/p/473235278 36 | 37 | Transformer在量化投资的应用 38 | https://zhuanlan.zhihu.com/p/393873813 39 | 40 | 41 | 42 | Google上搜出来的: 43 | 44 | https://towardsdatascience.com/stock-predictions-with-state-of-the-art-transformer-and-time-embeddings-3a4485237de6 45 | https://github.com/JanSchm/CapMarket/blob/master/bot_experiments/IBM_Transformer%2BTimeEmbedding.ipynb 46 | 47 | https://neuravest.net/how-transformers-with-attention-networks-boost-time-series-forecasting/ 48 | 49 | https://medium.com/a-paper-a-day-will-have-you-screaming-hurray/day-9-transformer-xl-attentive-language-models-beyond-a-fixed-length-context-d045abf6db8 50 | 51 | 52 | 53 | https://towardsdatascience.com/attention-for-time-series-classification-and-forecasting-261723e0006d 54 | 55 | 56 | https://babbemark.medium.com/bert-is-the-word-predicting-stock-prices-with-language-models-8d5205b8537c 57 | 58 | 59 | 60 | 61 | ## github 62 | 63 | 高赞项目: 64 | https://github.com/huseinzol05/Stock-Prediction-Models 65 | 66 | 67 | ## 视频 68 | 69 | Informer: 70 | https://www.bilibili.com/video/BV1PG411E77H/ 71 | 72 | B站 毕设 收藏的视频 73 | 74 | 75 | ## 文档 76 | 77 | pytorch transformer文档: 78 | https://pytorch.org/docs/stable/generated/torch.nn.Transformer.html?highlight=transformer#torch.nn.Transformer 79 | 80 | 上海证券交易所交易规则(2020年第二次修订) 81 | http://www.sse.com.cn/lawandrules/sselawsrules/stock/trading/main/c/c_20210409_5369803.shtml 82 | 83 | 84 | ## kaggle 85 | 86 | https://www.kaggle.com/code/arashnic/simple-lstm-regression 87 | 88 | 89 | https://www.kaggle.com/code/fredblair/transformers-for-stocks 90 | 91 | 讨论: 92 | https://www.kaggle.com/competitions/ubiquant-market-prediction/discussion/301739 93 | 94 | 一个数据集: 95 | https://www.kaggle.com/datasets/arashnic/time-series-forecasting-with-yahoo-stock-price 96 | 97 | ## 研报 98 | 99 | 选股因子系列研究(十一):Level2行情选股因子初探 100 | https://robo.datayes.com/v2/details/report/1771890?tab=original 101 | 非高频因子(单子大小成交占比),量化选股 102 | 103 | 华泰金工人工智能系列 104 | https://mp.weixin.qq.com/s/YXRThrwdodryQlo1gAFflw 105 | 106 | 海通选股因子系列 107 | https://www.htsec.com/ChannelHome/2016102405/index.shtml 108 | 109 | -------------------------------------------------------------------------------- /doc/学习笔记.md: -------------------------------------------------------------------------------- 1 | seq2seq模型(encoder+attention+decoder) 2 | - CNN 3 | - 平移不变性,可并行计算 4 | - 滑动窗口,具有局部关联性建模,依靠多层堆积来进行长程建模。 5 | - 对相对位置敏感,对绝对位置不敏感。 6 | - RNN 7 | - 依次有序递归建模,对顺序敏感,对相对位置敏感,对绝对位置敏感。 8 | - 串行计算耗时,计算复杂度与序列长度成线性关系,单步计算复杂度不变。 9 | - 长程建模能力弱。 10 | - Transformer 11 | - 无局部假设。可并行计算,对相对位置不敏感。 12 | - 无有序假设。需要位置编码来反映位置变化对于特征的影响。对绝对位置不敏感。 13 | - 任意两字符都可以建模。擅长长短程建模。自注意力机制需要序列程度的平方级别复杂度。 14 | 15 | 16 | Transformer 17 | ![Transformer结构](assert/Transformer结构.jpg) 18 | 19 | - encoder 20 | - input word embedding:one-hot进入一个无bias的ffn(feed-forward network)得到一个稠密的连续向量 21 | - position encoding: 22 | - 通过sin/cos来固定表征,此时每个位置都是确定的;可推广到更长的句子;pe(pos+k)可由pe(pos)表示 23 | - 通过残差连接来使得位置信息进入深层 24 | - multi-head self-attention 25 | - 由多组Q,K,V组成,每组单独计算一个attenion向量,并进入一个不带bias的ffn得到最终向量 26 | - 使得建模能力更强,表征更丰富 27 | - feed-forward network 28 | - 只考虑每个单独位置进行建模。不同位置参数共享。类似于1*1 point wise convolution 29 | - decoder 30 | - output word embedding 31 | - masked multi-head self-attention 32 | - multi-head cross-attention 33 | - feed-forward network 34 | - sotfmax 35 | 36 | -------------------------------------------------------------------------------- /doc/课题研究内容.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/doc/课题研究内容.pdf -------------------------------------------------------------------------------- /doc/选题+任务书.md: -------------------------------------------------------------------------------- 1 | ## 选题 2 | 3 | 课题申报人:王嘉(08703) 4 | 课题所属专业:人工智能, 5 | 课题选择情况:盲选 王运泽(519030910361) 6 | 课题名称:基于Transformer架构的量化金融预测研究 7 | 课题性质:论文 8 | 题目来源:横向 9 | 项目编号: 10 | 其他: 11 | 课题简介:深度学习领域的Transformer模型最初兴起于NLP领域,但是由于其独特的网络结构和机制,在很多其他领域中也展现出了优越的学习性能。而量化金融领域是当前交叉学科中的一个重要研究方向,也吸引了越来越多的从事人工智能以及量化交易等隶属于不同领域的研究人员开展交叉研究。本课题就是基于这样的背景,开展如下两方面的研究:其一是熟悉Transformer架构及其各种变种,以掌握这一类模型的构建与训练技巧,其二是将其应用于期货市场,在特定时间尺度上(例如分钟及以下级别),建立高频的多因子模型,并对特定期货走势进行预测,以验证其在量化金融领域里的作用。 12 | 课题要求(包括所具备的条件):对量化金融有兴趣,对Transformer模型有了解,可熟练使用python等编程; 13 | 课题工作量要求:本课题需要有较大的编程工作量。首先可从合作单位获取金融市场量价数据,在此基础上,1. 建立端到端Transformer预测模型,研究并实现相关构架,完成在高频数据下的未来截面预测。2. 其次,建立量价的多因子模型,并研究因子模型参数的学习。3. 研究各种期货行情的分类模型,以便于在不同市场行情下动态选择使用的模型。 14 | 其他指导教师:无其他指导教师 15 | 课题附件:无附件 16 | 17 | ## 任务书 18 | 19 | 完成transformer以及其多种变体的代码实现,例如Transformer-XL, Longformer等等, 考察这些Transformer变种是否可以用于预测任务,并完成端到端的训练过程,实现其在金融曲线,比如期货价格曲线上的预测。同时,通过Transformer的encoder-decoder结构,发掘可用于收益率预测的高频因子,并对这些高频因子进行排序并拟合。计算各高频因子的因子暴露。计算预测收益率与真实收益率之间的相关性。 20 | 21 | 1 2022.11.20-2022.12.31 查阅资料,方案确定; 22 | 2 2023.1.1-2023.2.28 实现基于Transformer的量价预测模型; 23 | 3 2023.3.1-2023.4.15 实现Transformer的多种变种,并比较预测性能; 24 | 4 2023.4.16-2023.5.31 研究并建立高频多因子模型,并计算各因子暴露; 25 | 5 2023.6.1-2023.6.16 论文撰写并答辩; 26 | -------------------------------------------------------------------------------- /nb.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "pycharm": { 8 | "name": "#%%\n" 9 | } 10 | }, 11 | "outputs": [ 12 | { 13 | "name": "stdout", 14 | "output_type": "stream", 15 | "text": [ 16 | "1.14.0.dev20221206\n", 17 | "False\n", 18 | "[]\n" 19 | ] 20 | } 21 | ], 22 | "source": [ 23 | "import torch\n", 24 | "\n", 25 | "print(torch.__version__)\n", 26 | "print(torch.cuda.is_available())\n", 27 | "print([torch.cuda.device(i) for i in range(torch.cuda.device_count())])" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "metadata": { 34 | "pycharm": { 35 | "name": "#%%\n" 36 | } 37 | }, 38 | "outputs": [ 39 | { 40 | "name": "stdout", 41 | "output_type": "stream", 42 | "text": [ 43 | "True\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "import torch\n", 49 | "\n", 50 | "print(torch.backends.mps.is_available())\n", 51 | "\n", 52 | "device = 'mps'" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": null, 58 | "metadata": { 59 | "pycharm": { 60 | "name": "#%%\n" 61 | } 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "from torch.nn import LSTM\n", 66 | "\n", 67 | "lstm = LSTM()" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 1, 73 | "metadata": { 74 | "collapsed": false, 75 | "pycharm": { 76 | "name": "#%%\n" 77 | } 78 | }, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/html": [ 83 | "\n", 84 | "\n", 91 | "\n", 92 | "
\n", 93 | "\n", 94 | "\n" 237 | ], 238 | "text/plain": [ 239 | "" 240 | ] 241 | }, 242 | "execution_count": 1, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "from pyecharts.charts import Bar\n", 249 | "# 使用 options 配置项,在 pyecharts 中,一切皆 Options。\n", 250 | "from pyecharts import options as opts\n", 251 | "# 使用 snapshot-selenium 渲染图片\n", 252 | "from pyecharts.render import make_snapshot\n", 253 | "# 内置主题类型可查看 pyecharts.globals.ThemeType\n", 254 | "from pyecharts.globals import ThemeType\n", 255 | "\n", 256 | "\n", 257 | "# pyecharts 所有方法均支持链式调用。\n", 258 | "# 内置主题\n", 259 | "bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT, bg_color='white')) \\\n", 260 | " .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"]) \\\n", 261 | " .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90]) \\\n", 262 | " .set_global_opts(title_opts=opts.TitleOpts(title=\"主标题\", subtitle=\"副标题\"))\n", 263 | "# 渲染图片\n", 264 | "# make_snapshot(snapshot, bar.render(), \"bar.png\")\n", 265 | "# 在 notebook 上渲染\n", 266 | "bar.render_notebook()" 267 | ] 268 | } 269 | ], 270 | "metadata": { 271 | "kernelspec": { 272 | "display_name": "Python 3.9.12 ('base')", 273 | "language": "python", 274 | "name": "python3" 275 | }, 276 | "language_info": { 277 | "codemirror_mode": { 278 | "name": "ipython", 279 | "version": 3 280 | }, 281 | "file_extension": ".py", 282 | "mimetype": "text/x-python", 283 | "name": "python", 284 | "nbconvert_exporter": "python", 285 | "pygments_lexer": "ipython3", 286 | "version": "3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]" 287 | }, 288 | "orig_nbformat": 4, 289 | "vscode": { 290 | "interpreter": { 291 | "hash": "665eb9f1f9e7e2a56a2add6318b645d1acf33870b9e0c088173f0d8baf236e9d" 292 | } 293 | } 294 | }, 295 | "nbformat": 4, 296 | "nbformat_minor": 2 297 | } 298 | -------------------------------------------------------------------------------- /paper/caj/基于CNN-Transformer的黄金期货价格预测_蒋超宇.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/caj/基于CNN-Transformer的黄金期货价格预测_蒋超宇.pdf -------------------------------------------------------------------------------- /paper/caj/基于LSTM与Transformer模型的股价预测_辛洲扬.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/caj/基于LSTM与Transformer模型的股价预测_辛洲扬.pdf -------------------------------------------------------------------------------- /paper/caj/基于Trans-T2V的量化选股策略研究_李瞿成.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/caj/基于Trans-T2V的量化选股策略研究_李瞿成.pdf -------------------------------------------------------------------------------- /paper/caj/基于Transformer的选股因子挖掘_曹昀炀.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/caj/基于Transformer的选股因子挖掘_曹昀炀.pdf -------------------------------------------------------------------------------- /paper/caj/基于技术面数据的自监督学习股市涨跌预测方法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/caj/基于技术面数据的自监督学习股市涨跌预测方法.pdf -------------------------------------------------------------------------------- /paper/caj/基于神经网络的多变量时间序列预测_陈超.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/caj/基于神经网络的多变量时间序列预测_陈超.pdf -------------------------------------------------------------------------------- /paper/其他/Hierarchical Multi-Scale Gaussian Transformer for Stock Movement Prediction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/Hierarchical Multi-Scale Gaussian Transformer for Stock Movement Prediction.pdf -------------------------------------------------------------------------------- /paper/其他/Level-2证券行情数据分析系统的设计与实现_程磊.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/Level-2证券行情数据分析系统的设计与实现_程磊.pdf -------------------------------------------------------------------------------- /paper/其他/Predicting Stock Closing Prices in Emerging Markets with Transformer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/Predicting Stock Closing Prices in Emerging Markets with Transformer.pdf -------------------------------------------------------------------------------- /paper/其他/Stock_Price_Prediction_Based_on_Temporal_Fusion_Transformer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/Stock_Price_Prediction_Based_on_Temporal_Fusion_Transformer.pdf -------------------------------------------------------------------------------- /paper/其他/Transformer-based attention network for stock movement prediction.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/Transformer-based attention network for stock movement prediction.pdf -------------------------------------------------------------------------------- /paper/其他/基于BERT-SA-LSTM的股指期货价格预测研究_唐韬.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/基于BERT-SA-LSTM的股指期货价格预测研究_唐韬.pdf -------------------------------------------------------------------------------- /paper/其他/基于Informer模型的期权交易策略设计_刘一凡.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/其他/基于Informer模型的期权交易策略设计_刘一凡.pdf -------------------------------------------------------------------------------- /paper/因子/基于XGBoost的高频交易选股研究_王怡宁.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/因子/基于XGBoost的高频交易选股研究_王怡宁.pdf -------------------------------------------------------------------------------- /paper/实验/TRANSFORMER-BASED DEEP LEARNING MODEL FOR STOCK PRICE PREDICTION.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/实验/TRANSFORMER-BASED DEEP LEARNING MODEL FOR STOCK PRICE PREDICTION.pdf -------------------------------------------------------------------------------- /paper/文档/Level_2_Account_form.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/文档/Level_2_Account_form.pdf -------------------------------------------------------------------------------- /paper/文档/上海证券交易所交易规则(2020年第二次修订).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/文档/上海证券交易所交易规则(2020年第二次修订).pdf -------------------------------------------------------------------------------- /paper/模型/-1LSTM:Long Short-Term Memory Neural Network for Financial Time Series.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/模型/-1LSTM:Long Short-Term Memory Neural Network for Financial Time Series.pdf -------------------------------------------------------------------------------- /paper/模型/0提出Transformer的论文:Attention Is All You Need.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/模型/0提出Transformer的论文:Attention Is All You Need.pdf -------------------------------------------------------------------------------- /paper/模型/Time2Vec:Learning a Vector Representation of Time.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/模型/Time2Vec:Learning a Vector Representation of Time.pdf -------------------------------------------------------------------------------- /paper/研报/2021-03-11_东方证券_金融工程_因子选股系列之七十四:神经网络日频alpha模型初步实践.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/研报/2021-03-11_东方证券_金融工程_因子选股系列之七十四:神经网络日频alpha模型初步实践.pdf -------------------------------------------------------------------------------- /paper/研报/2022-05-31_海通证券_金融工程_选股因子系列研究(七十九):用注意力机制优化深度学习高频因子.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/研报/2022-05-31_海通证券_金融工程_选股因子系列研究(七十九):用注意力机制优化深度学习高频因子.pdf -------------------------------------------------------------------------------- /paper/研报/2022-06-12_东方证券_金融工程_因子选股系列之八十三:多模型学习量价时序特征.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/研报/2022-06-12_东方证券_金融工程_因子选股系列之八十三:多模型学习量价时序特征.pdf -------------------------------------------------------------------------------- /paper/研报/选股因子系列研究(十一)——Level2 行情.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/研报/选股因子系列研究(十一)——Level2 行情.pdf -------------------------------------------------------------------------------- /paper/研报/选股因子系列研究(七十六)——基于深度学习的高频因子挖掘.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/paper/研报/选股因子系列研究(七十六)——基于深度学习的高频因子挖掘.pdf -------------------------------------------------------------------------------- /src/lstm.py: -------------------------------------------------------------------------------- 1 | import time 2 | from typing import Tuple 3 | 4 | import matplotlib.pyplot as plt 5 | import numpy as np 6 | import torch 7 | from torch import Tensor 8 | from torch import nn 9 | from torch.nn import LSTM, Linear 10 | from torch.optim import Adam 11 | 12 | from util import load_stock_data 13 | 14 | device = 'cpu' 15 | 16 | if torch.cuda.is_available(): 17 | device = 'cuda' 18 | elif torch.backends.mps.is_available(): 19 | device = 'mps' 20 | 21 | # TRAIN_CODE = '100' 22 | TRAIN_CODE = 'sh000300' 23 | EVAL_CODE = 'sh000300' 24 | EVAL = False 25 | # EVAL = True 26 | WINDOW_SIZE = 30 27 | PRED_DAY = 1 28 | EPOCH = 3 29 | MODEL_PATH = f'lstm_{TRAIN_CODE}_{EPOCH}.h5' 30 | BATCH_SIZE = 32 31 | 32 | 33 | class LSTMModel(nn.Module): 34 | def __init__(self) -> None: 35 | super().__init__() 36 | self.num_layers = 1 37 | self.lstm1 = LSTM(input_size=WINDOW_SIZE, hidden_size=128, 38 | num_layers=self.num_layers, batch_first=True) 39 | self.lstm2 = LSTM(input_size=128, hidden_size=64, 40 | num_layers=self.num_layers, batch_first=True) 41 | self.fc1 = Linear(64, 25) 42 | self.fc2 = Linear(25, 1) 43 | # self.model = Sequential( 44 | # LSTM(input_size=WINDOW_SIZE, hidden_size=128), 45 | # LSTM(input_size=128, hidden_size=64), 46 | # Linear(64, 25), 47 | # Linear(25, 1), 48 | # ) 49 | 50 | def forward(self, x) -> Tensor: 51 | x, _ = self.lstm1(x) 52 | x, _ = self.lstm2(x) 53 | x = self.fc1(x) 54 | x = self.fc2(x) 55 | return x 56 | 57 | 58 | def get_data_label_min_max(data: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: 59 | x = [] 60 | y = [] 61 | x_min = [] 62 | x_max = [] 63 | for i in range(len(data) - WINDOW_SIZE - PRED_DAY + 1): 64 | prev_n_x = data[i:i + WINDOW_SIZE] 65 | prev_n_max = max(prev_n_x) 66 | prev_n_min = min(prev_n_x) 67 | x_max.append([prev_n_max]) 68 | x_min.append([prev_n_min]) 69 | after_pred_day_price = data[i + WINDOW_SIZE + PRED_DAY - 1] 70 | prev_n_x = (prev_n_x - prev_n_min) / (prev_n_max - prev_n_min) * 2 - 1 71 | after_pred_day_price = (after_pred_day_price - prev_n_min) / (prev_n_max - prev_n_min) * 2 - 1 72 | y.append([after_pred_day_price]) 73 | x.append(prev_n_x) 74 | return np.array(x), np.array(y), np.array(x_min), np.array(x_max) 75 | 76 | 77 | if __name__ == '__main__': 78 | 79 | begin_time = time.time() 80 | 81 | # all_data = load_data(TRAIN_CODE) 82 | all_data = load_stock_data(TRAIN_CODE)['close'] 83 | # all_data = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]) 84 | x, y, x_min, x_max = get_data_label_min_max(all_data) 85 | 86 | if not EVAL: 87 | train_data_len = int(len(x) * 0.8) 88 | train_x = x[:train_data_len] 89 | train_y = y[:train_data_len] 90 | test_x = x[train_data_len:] 91 | test_y = y[train_data_len:] 92 | model = LSTMModel().to(device) 93 | criterion = nn.MSELoss().to(device) 94 | optimizer = Adam(model.parameters(), lr=1e-3) 95 | 96 | # Train the model 97 | model.train() 98 | for i in range(len(train_x)): 99 | x = torch.Tensor(train_x[i:i + 1]).to(device) 100 | y = torch.Tensor(train_y[i:i + 1]).to(device) 101 | 102 | output = model(x) 103 | optimizer.zero_grad() 104 | 105 | loss = criterion(output, y) 106 | loss.backward() 107 | print(i, loss) 108 | 109 | optimizer.step() 110 | 111 | # Eval 112 | model.eval() 113 | x = torch.Tensor(test_x).to(device) 114 | y = torch.Tensor(test_y).to(device) 115 | 116 | pred: Tensor = model(x) 117 | loss = criterion(pred, y) 118 | print(loss) 119 | 120 | # pred = pred * (x_max - x_min) + x_min 121 | # y = y * (x_max - x_min) + x_min 122 | line1, = plt.plot(pred.cpu().detach().numpy().reshape((-1))) 123 | line1.set_label('pred') 124 | line2, = plt.plot(y.cpu().detach().numpy().reshape((-1))) 125 | line2.set_label('real') 126 | print(device, time.time() - begin_time) 127 | plt.legend() 128 | plt.show() 129 | -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nullgit/transformer-quant/4107e7e681c8c8180a5bb765a2da55f20798fcc6/src/main.py -------------------------------------------------------------------------------- /src/transformer.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from typing import Tuple 4 | 5 | import akshare as ak 6 | import matplotlib.pyplot as plt 7 | import numpy as np 8 | import torch 9 | from torch import Tensor 10 | from torch import nn 11 | from torch.nn import LSTM, Linear, Transformer, TransformerEncoder, TransformerEncoderLayer 12 | from torch.optim import Adam 13 | 14 | def load_data(code: str) -> np.ndarray: 15 | # data = np.arange(100) 16 | data_path = f'data/ak/stock_zh_{code}.npy' 17 | if not os.path.exists(data_path): 18 | data = ak.stock_zh_index_daily(symbol=code)['close'].to_numpy() 19 | np.save(data_path, data) 20 | else: 21 | data = np.load(data_path) 22 | return data 23 | 24 | 25 | device = 'cpu' 26 | 27 | if torch.cuda.is_available(): 28 | device = 'cuda' 29 | elif torch.backends.mps.is_available(): 30 | device = 'mps' 31 | 32 | # TRAIN_CODE = '100' 33 | TRAIN_CODE = 'sh000300' 34 | EVAL_CODE = 'sh000300' 35 | EVAL = False 36 | # EVAL = True 37 | WINDOW_SIZE = 30 38 | PRED_DAY = 1 39 | EPOCH = 3 40 | MODEL_PATH = f'lstm_{TRAIN_CODE}_{EPOCH}.h5' 41 | BATCH_SIZE = 32 42 | 43 | 44 | class TransformerModel(nn.Module): 45 | def __init__(self) -> None: 46 | super(TransformerModel, self).__init__() 47 | # self.transformer = Transformer(d_model=1, nhead=1, batch_first=True) 48 | encoder_layer = TransformerEncoderLayer(d_model=1, nhead=1, batch_first=True) 49 | self.encoder = TransformerEncoder(encoder_layer, 2) 50 | self.linear = Linear(in_features=WINDOW_SIZE, out_features=1) 51 | 52 | def forward(self, x, y) -> Tensor: 53 | # x = self.transformer(x, y) 54 | x = self.encoder(x) 55 | x = x.reshape((-1, WINDOW_SIZE)) 56 | x = self.linear(x) 57 | return x 58 | 59 | 60 | 61 | def get_data_label_min_max(data: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: 62 | x = [] 63 | y = [] 64 | x_min = [] 65 | x_max = [] 66 | for i in range(len(data) - WINDOW_SIZE - PRED_DAY + 1): 67 | prev_n_x = data[i:i + WINDOW_SIZE] 68 | prev_n_max = max(prev_n_x) 69 | prev_n_min = min(prev_n_x) 70 | x_max.append([prev_n_max]) 71 | x_min.append([prev_n_min]) 72 | after_pred_day_price = data[i + WINDOW_SIZE + PRED_DAY - 1] 73 | prev_n_x = (prev_n_x - prev_n_min) / (prev_n_max - prev_n_min) * 2 - 1 74 | after_pred_day_price = (after_pred_day_price - prev_n_min) / (prev_n_max - prev_n_min) * 2 - 1 75 | y.append([after_pred_day_price]) 76 | x.append(prev_n_x) 77 | return np.array(x), np.array(y), np.array(x_min), np.array(x_max) 78 | 79 | 80 | if __name__ == '__main__': 81 | 82 | begin_time = time.time() 83 | 84 | # all_data = load_data(TRAIN_CODE) 85 | all_data = load_data(TRAIN_CODE) 86 | # all_data = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]) 87 | x, y, x_min, x_max = get_data_label_min_max(all_data) 88 | 89 | if not EVAL: 90 | train_data_len = int(len(x) * 0.8) 91 | train_x = x[:train_data_len] 92 | train_y = y[:train_data_len] 93 | test_x = x[train_data_len:] 94 | test_y = y[train_data_len:] 95 | model = TransformerModel().to(device) 96 | criterion = nn.MSELoss().to(device) 97 | optimizer = Adam(model.parameters(), lr=1e-2) 98 | 99 | # Train the model 100 | model.train() 101 | for i in range(len(train_x)): 102 | x = torch.Tensor(train_x[i:i + 1]).reshape((1, -1, 1)).to(device) 103 | y = torch.Tensor(train_y[i:i + 1]).reshape((1, -1)).to(device) 104 | 105 | output = model(x, y) 106 | optimizer.zero_grad() 107 | 108 | loss = criterion(output, y) 109 | loss.backward() 110 | print(i, loss) 111 | 112 | optimizer.step() 113 | 114 | # Eval 115 | model.eval() 116 | x = torch.Tensor(test_x).reshape((-1, WINDOW_SIZE, 1)).to(device) 117 | y = torch.Tensor(test_y).reshape((-1, 1, 1)).to(device) 118 | 119 | pred: Tensor = model(x, y) 120 | loss = criterion(pred, y) 121 | print(loss) 122 | 123 | # pred = pred * (x_max - x_min) + x_min 124 | # y = y * (x_max - x_min) + x_min 125 | line1, = plt.plot(pred.cpu().detach().numpy().reshape((-1))) 126 | line1.set_label('pred') 127 | line2, = plt.plot(y.cpu().detach().numpy().reshape((-1))) 128 | line2.set_label('real') 129 | print(device, time.time() - begin_time) 130 | plt.legend() 131 | plt.show() 132 | -------------------------------------------------------------------------------- /src/util.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import akshare as ak 4 | import numpy as np 5 | import pandas as pd 6 | import pickle 7 | import torch 8 | 9 | 10 | def load_stock_data(code: str) -> pd.DataFrame: 11 | # data = np.arange(100) 12 | data_path = f'data/ak/stock_zh_{code}.pkl' 13 | if not os.path.exists(data_path): 14 | data = ak.stock_zh_index_daily(symbol=code) 15 | with open(data_path, 'wb') as fp: 16 | pickle.dump(data, fp) 17 | else: 18 | with open(data_path, 'rb') as fp: 19 | data = pickle.load(fp) 20 | return data 21 | 22 | def get_device() -> str: 23 | device = 'cpu' 24 | if torch.cuda.is_available(): 25 | device = 'cuda' 26 | elif torch.backends.mps.is_available(): 27 | device = 'mps' 28 | return device 29 | --------------------------------------------------------------------------------