python+Sqlite+Dataframe打造金融股票数据结构
2024-08-28 05:21:51
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'])
最新文章
- Azure ARM (12) ARM模式下,在负载均衡器上设置多个公网IP地址
- Android Fragment 你应该知道的一切
- JS对于数据常见操作
- H5实现俄罗斯方块(三)
- Linux Nginx 安装配置
- [BS-00] const限定常量或者变量(初次赋值后),其值不允许被改变
- 官网下载Spring dist
- 【转载】Restful 简介
- Java项目中基于Hibernate分页总结
- atoi函数和atof函数
- Java 实现FTP上传和下载
- ElasticSearch搜索(一)
- golang项目:goa和micro
- 第二十四节,TensorFlow下slim库函数的使用以及使用VGG网络进行预训练、迁移学习(附代码)
- linux Ctrl+z和Ctrl+c的区别
- ios-toolchain-based-on-clang-for-linux
- 【JMeter】1.9上考试jmeter测试调试
- 【codeforces】【比赛题解】#868 CF Round #438 (Div.1+Div.2)
- phpmyadmin新加用户登陆不了,测试解决方案。
- json data 解析demo
热门文章
- MongoDB 常用查询语法
- 【LeetCode】反转字符串
- SpringBoot 上传文件突然报错 Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1428942566812653608
- h5页面列表滚动加载数据
- HDU 5455:Fang Fang 查cff个数
- POJ 1573:Robot Motion
- location - 修改url后 - 重新加载
- JDBC批处理方法
- 关于spring cloud “Finchley.RC2”版本在spring cloud config中的ArrayIndexOutOfBoundsException
- Element.scrollIntoView() 和 document.elementFromPoint ()