ITPub博客

首页 > IT职业 > IT生活 > Python爬虫回测股票的方法

Python爬虫回测股票的方法

IT生活 作者:glovey 时间:2021-09-11 10:01:53 0 删除 编辑

1.生成上证与深证所有股票的代码:

#上证代码
shanghaicode = []
for i in range(600000, 604000, 1):
    shanghaicode.append(str(i))
    
#深证代码
shenzhencode = []
for i in range(1000000, 1005000, 1):
    i = str(i)[1:]  
    shenzhencode.append(i)

2.定义一个爬取函数,broker创建的实例:

def getalldata(code):
        if os.path.exists(datapath + code + '.csv'):
            print(code + 'already existed!')
            return
        metadata = broker.get_stock_pro(code)
        if len(metadata) == 0:
            return
        metadata.to_csv('C:/Users/abc/Desktop/' + code + '.csv',index = False)
        print(code + 'finished!')

3.导入多线程需要的模块

from concurrent.futures.thread import ThreadPoolExecutor #多线程

4.遍历所有代码开始爬取,max_workers可适当调整

    executor = ThreadPoolExecutor(max_workers=3)
    for datatemp in executor.map(getalldata, shenzhencode):
        pass  
    
    executor = ThreadPoolExecutor(max_workers=3)
    for datatemp in executor.map(getalldata, shanghaicode):
        pass

以上就是Python爬虫回测股票的方法,相信大家根据上面的步骤,已经学会获取股票数据的方法了。学会后就赶快展开数据搜集吧。

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2572/viewspace-2831098/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-16

  • 博文量
    86
  • 访问量
    770