科技美学

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  90 随笔 :: 0 文章 :: 1 评论 :: 69469 阅读

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(['00700'])
df2 = data_source.get_indexs(['hsi'])
复制代码

 

posted on   chankuang  阅读(1923)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
· 全程使用 AI 从 0 到 1 写了个小工具
点击右上角即可分享
微信分享提示