5. 本地数据库

很简单的用本地Sqlite查找股票数据。

DataSource类,返回的是Dataframe物件。这个Dataframe物件,在之后的业务,如计算股票指标,还需要特别处理。

import os
import sqlite3 as sqlite3
import numpy as np
import pandas as pd # 数据源
class DataSource:
def __init__(self):
self.db = None # 数据库
self.cursor = None # 指针
self.stocks = {} # 股票池
self.indexs = {} # 指数池
self.name = 'unit_test.db' # 数据源名称 def connect(self):
self.db = sqlite3.connect(os.path.abspath(self.name))
self.cursor = self.db.cursor() def get_stocks(self, ucodes):
# 股票池
try:
self.stocks = {}
self.connect()
self.db.row_factory = lambda cursor, row: row[0]
for ucode in ucodes:
sql = """SELECT t.code, t.lot, t.nmll, t.stime, t.high, t.low, t.open, t.close, t.volume
FROM (SELECT n.code, n.lot, n.nmll, c.stime, c.high, c.low, c.open, c.close, c.volume
FROM s_{} AS c INNER JOIN name AS n
ON c.code=n.code ORDER BY c.stime DESC LIMIT 365*20) AS t
/*INNER JOIN financial AS f
ON t.code=f.code AND substr(t.stime,1,4)=f.year*/
ORDER BY t.stime""".format(ucode)
self.cursor.execute(sql)
columns = ['code', 'lot', 'nmll', 'sdate', 'high', 'low', 'open', 'last', 'vol']
self.stocks[ucode] = pd.DataFrame(self.cursor.fetchall(), columns=columns)
self.db.commit()
self.cursor.close()
self.db.close()
return self.stocks
except sqlite3.Error as e:
print(e) def get_indexs(self, indexs):
try:
# 指数池
self.indexs = {}
self.connect()
self.db.row_factory = lambda cursor, row: row[0]
for index in indexs:
sql = """SELECT t.code, t.lot, t.nmll, t.stime, t.high, t.low, t.open, t.close, t.volume
FROM (SELECT n.code, n.lot, n.nmll, c.stime, c.high, c.low, c.open, c.close, c.volume
FROM s_{} AS c INNER JOIN name AS n
ON c.code=n.code ORDER BY c.stime DESC LIMIT 365*20) AS t
/*INNER JOIN financial AS f
ON t.code=f.code AND substr(t.stime,1,4)=f.year*/
ORDER BY t.stime""".format(index.upper())
self.cursor.execute(sql)
columns = ['code', 'lot', 'nmll', 'sdate', 'high', 'low', 'open', 'last', 'vol']
self.indexs[index] = pd.DataFrame(self.cursor.fetchall(), columns=columns)
self.db.commit()
self.cursor.close()
self.db.close()
return self.indexs
except sqlite3.Error as e:
print(e) data_source = DataSource()
df1 = data_source.get_stocks([''])
df2 = data_source.get_indexs(['hsi'])

python+Sqlite+Dataframe打造金融股票数据结构的更多相关文章

  1. 用Pandas Dataframe来架构起金融股票数据的内部形态

    2. 金融股票数据的另一个形态,怎样在业务内部流动,同时怎样避免错误 前一篇讲解了股票的原始状态,那麽在业务过程中,数据会变成怎样的形态,来完成众多奇奇怪怪的业务呢,以下将会解答. 首先,任何股票都有 ...

  2. 第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中

    第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详 ...

  3. 三十七 Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中

    Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详解 基本概念 如 ...

  4. Python分布式爬虫打造搜索引擎完整版-基于Scrapy、Redis、elasticsearch和django打造一个完整的搜索引擎网站

    Python分布式爬虫打造搜索引擎 基于Scrapy.Redis.elasticsearch和django打造一个完整的搜索引擎网站 https://github.com/mtianyan/Artic ...

  5. 简学Python第二章__巧学数据结构文件操作

    #cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...

  6. [Spark][Python][RDD][DataFrame]从 RDD 构造 DataFrame 例子

    [Spark][Python][RDD][DataFrame]从 RDD 构造 DataFrame 例子 from pyspark.sql.types import * schema = Struct ...

  7. [Spark][python]以DataFrame方式打开Json文件的例子

    [Spark][python]以DataFrame方式打开Json文件的例子: [training@localhost ~]$ cat people.json{"name":&qu ...

  8. 第三百七十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapyd部署scrapy项目

    第三百七十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapyd部署scrapy项目 scrapyd模块是专门用于部署scrapy项目的,可以部署和管理scrapy项目 下载地址:h ...

  9. 第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门搜索

    第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门 我的搜素简单实现原理我们可以用js来实现,首先用js获取到 ...

随机推荐

  1. Windows 10中使用VirtualBox

    新版Windows 10或者安装了新的更新以后,VirtualBox虚拟机就不能用了. 原因是WIndows10里面有个叫Virtualization-base security的安全机制打开了. 关 ...

  2. BZOJ 2744

    #include<iostream> #include<cstdio> #include<cstring> #include<vector> #incl ...

  3. 七十、SAP中内表批量指定位置插入

    一.代码如下 二.调试一下 三.被插入的数据 四.效果如下

  4. ActiveMQ消息队列和SignalR之日志实时监控及警报小实例

    主要技术: log4net-生成日志. ActiveMQ-生成日志的时候发送消息,并实时监控日志. SignalR-将ActiveMQ监控的日志实时显示到浏览器上,而不用刷新浏览器. 小实例介绍: 左 ...

  5. 自定义spark UDAF

    官网链接 样例代码: import java.util.ArrayList; import java.util.List; import org.apache.spark.sql.Dataset; i ...

  6. Pip,pywin32,whl文件下载网址,mayavi安装包,PyQt5安装,PyMuPDF安装等注意事项

    (1)pip安装的包不一定是用户想要的位置,此时可以用 -t 选项来指定位置. 例如目标位置是/usr/local/lib/python2.7/site-packages/ ,要安装xlrd 这个包 ...

  7. Python 正则表达式(RegEx)

    版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...

  8. Spring 框架介绍

    Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Control – I ...

  9. VM安装CentOS7步骤

    VM15下载,在360软件管家就可以下载 CentOS7下载地址:http://mirror.bit.edu.cn/centos/7.6.1810/isos/x86_64/CentOS-7-x86_6 ...

  10. POJ 1068:Parencodings

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22849   Accepted: 13394 De ...