资源简介

使用强化学习完成股票预测。强化学习是机器学习的另一个分支,在决策的时候采取合适的行动 (Action) 使最后的奖励最大化。与监督学习预测未来的数值不同,强化学习根据输入的状态(如当日开盘价、收盘价等),输出系列动作(例如:买进、持有、卖出),使得最后的收益最大化,实现自动交易。

资源截图

代码片段和文件信息

import baostock as bs
import pandas as pd
import os


OUTPUT = ‘./stockdata‘


def mkdir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)


class Downloader(object):
    def __init__(self
                 output_dir
                 date_start=‘1990-01-01‘
                 date_end=‘2020-03-23‘):
        self._bs = bs
        bs.login()
        self.date_start = date_start
        # self.date_end = datetime.datetime.now().strftime(“%Y-%m-%d“)
        self.date_end = date_end
        self.output_dir = output_dir
        self.fields = “datecodeopenhighlowclosevolumeamount“ \
                      “adjustflagturntradestatuspctChgpeTTM“ \
                      “pbMRQpsTTMpcfNcfTTMisST“

    def exit(self):
        bs.logout()

    def get_codes_by_date(self date):
        print(date)
        stock_rs = bs.query_all_stock(date)
        stock_df = stock_rs.get_data()
        print(stock_df)
        return stock_df

    def run(self):
        stock_df = self.get_codes_by_date(self.date_end)
        for index row in stock_df.iterrows():
            print(f‘processing {row[“code“]} {row[“code_name“]}‘)
            df_code = bs.query_history_k_data_plus(row[“code“] self.fields
                                                   start_date=self.date_start
                                                   end_date=self.date_end).get_data()
            df_code.to_csv(f‘{self.output_dir}/{row[“code“]}.{row[“code_name“]}.csv‘ index=False)
        self.exit()


if __name__ == ‘__main__‘:
    # 获取全部股票的日K线数据
    mkdir(‘./stockdata/train‘)
    downloader = Downloader(‘./stockdata/train‘ date_start=‘1990-01-01‘ date_end=‘2019-11-29‘)
    downloader.run()

    mkdir(‘./stockdata/test‘)
    downloader = Downloader(‘./stockdata/test‘ date_start=‘2019-12-01‘ date_end=‘2019-12-31‘)
    downloader.run()


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-03-28 03:22  RL-Stock-master\
     文件        2208  2020-03-28 03:22  RL-Stock-master\.gitignore
     文件        1072  2020-03-28 03:22  RL-Stock-master\LICENSE
     文件        7019  2020-03-28 03:22  RL-Stock-master\README.md
     目录           0  2020-03-28 03:22  RL-Stock-master\font\
     文件       21368  2020-03-28 03:22  RL-Stock-master\font\simhei.ttf
     文件     5177387  2020-03-28 03:22  RL-Stock-master\font\wqy-microhei.ttc
     文件        1893  2020-03-28 03:22  RL-Stock-master\get_stock_data.py
     目录           0  2020-03-28 03:22  RL-Stock-master\img\
     文件      136091  2020-03-28 03:22  RL-Stock-master\img\2020-03-25-17-05-58.png
     文件      135361  2020-03-28 03:22  RL-Stock-master\img\2020-03-25-18-19-03.png
     文件       60714  2020-03-28 03:22  RL-Stock-master\img\2020-03-25-18-55-13.png
     文件     1175044  2020-03-28 03:22  RL-Stock-master\img\2020-03-25-18-58-52.png
     文件      104218  2020-03-28 03:22  RL-Stock-master\img\2020-03-25-19-38-49.png
     文件      108666  2020-03-28 03:22  RL-Stock-master\img\2020-03-27-10-22-58.png
     文件       97777  2020-03-28 03:22  RL-Stock-master\img\2020-03-27-10-45-59.png
     文件      118213  2020-03-28 03:22  RL-Stock-master\img\2020-03-28-10-34-20.png
     文件       10391  2020-03-28 03:22  RL-Stock-master\img\2020-03-28-10-34-46.png
     文件        5836  2020-03-28 03:22  RL-Stock-master\img\hist.png
     文件       14315  2020-03-28 03:22  RL-Stock-master\img\pie.png
     文件       32485  2020-03-28 03:22  RL-Stock-master\img\sh.600036.png
     文件        2533  2020-03-28 03:22  RL-Stock-master\main.py
     文件         841  2020-03-28 03:22  RL-Stock-master\requirements.txt
     目录           0  2020-03-28 03:22  RL-Stock-master\rlenv\
     文件        5547  2020-03-28 03:22  RL-Stock-master\rlenv\StockTradingEnv0.py
     文件           0  2020-03-28 03:22  RL-Stock-master\rlenv\__init__.py
     文件       36845  2020-03-28 03:22  RL-Stock-master\vis.ipynb

评论

共有 条评论