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. 十九、JavaScript之数组

    一.代码如下 二.执行效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" cont ...

  2. HDU_4939 stupid tower defense 2014多校7 多变量型DP

    意思是有个塔防游戏,有三种塔,红塔在怪物经过的时候每秒会产生攻击力大小的伤害,绿塔对怪物经过以及经过之后每秒产生攻击力大小的伤害,还有种蓝塔,对怪物进行减速,即怪物从此之后经过一个单位都会减慢c秒 最 ...

  3. springmvc无法访问JS,CSS等文件

    配置好web.xml中的dispatchservlet后,js,css,都不能正常显示 web.xml配置文件 <!-- 核心控制器 --> <servlet> <ser ...

  4. scp、wget

    scp使用方法 -1  强制scp命令使用协议ssh1 -2  强制scp命令使用协议ssh2 -4  强制scp命令只使用IPv4寻址 -6  强制scp命令只使用IPv6寻址 -B  使用批处理模 ...

  5. Pillow库的学习和使用

    1.encoder jpeg not available sudo apt-get install libjpeg-dev pip install -I pillow

  6. java课程课后作业190502之单词统计续集

    第1步:输出单个文件中的前 N 个最常出现的英语单词. 功能1:输出文件中所有不重复的单词,按照出现次数由多到少排列,出现次数同样多的,以字典序排列. 功能2: 指定文件目录,对目录下每一个文件执行统 ...

  7. [Mathematics][Fundamentals of Complex Analysis][Small Trick] The Trick on drawing the picture of sin(z), for z in Complex Plane

    Exercises 3.2 21. (a). For $\omega = sinz$, what is the image of the semi-infinite strip $S_1 = \{x+ ...

  8. python的常用序列

    list1.list(obj)函数 obj可以为:元组(1,2,3),可迭代对象,字符串等转换换成数组类型2. 列表元素的添加 (1)list+[添加的元素] (2)list.append(添加元素) ...

  9. one_day_one_linuxCmd---crontab 命令

    <坚持每天学习一个 linux 命令,今天我们来学习 tar 命令> 摘要:crond 是 linux 下用来周期性的执行某种任务或等待处理事件的一个守护进程,周期执行的任务一般由 cro ...

  10. vSphere Replication5.5安装

    vSphere Replication5.5概述 VMware vSphere Replication简称VR是 VMwarevCenter Server 的扩展,提供基于管理程序的虚拟机复制和恢复功 ...